oscurantismo commited on
Commit
7a5a54d
·
verified ·
1 Parent(s): 182440d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -89,27 +89,33 @@ def enhance_contrast(image):
89
  enhanced_image.save(enhanced_path)
90
  return enhanced_path
91
 
92
- # Function to provide additional suggestions with streaming
93
  def provide_suggestions_streaming(object_identified):
94
  if not object_identified:
95
- yield "Sorry, I couldn't find an object in your artwork. Try a different image."
96
  return
97
 
98
- stream = openai.ChatCompletion.create(
99
- model="gpt-4",
100
- messages=[
101
- {"role": "system", "content": "You are an expert digital art advisor."},
102
- {"role": "user", "content": f"Suggest ways to improve a digital artwork featuring a {object_identified}."}
103
- ],
104
- stream=True
105
- )
 
 
106
 
107
- response = ""
108
- for chunk in stream:
109
- if chunk.choices[0].delta.get("content"):
110
- token = chunk.choices[0].delta["content"]
111
- response += token
112
- yield response
 
 
 
 
 
113
 
114
  # Main image processing function
115
  def process_image(image):
 
89
  enhanced_image.save(enhanced_path)
90
  return enhanced_path
91
 
 
92
  def provide_suggestions_streaming(object_identified):
93
  if not object_identified:
94
+ yield "⚠️ Sorry, I couldn't identify an object in your artwork. Try uploading a different image."
95
  return
96
 
97
+ try:
98
+ # Use the OpenAI client for suggestions
99
+ response = client.chat.completions.create(
100
+ model="gpt-4o-mini",
101
+ messages=[
102
+ {"role": "system", "content": "You are an expert digital art advisor."},
103
+ {"role": "user", "content": f"Suggest ways to improve a digital artwork featuring a {object_identified}."},
104
+ ],
105
+ stream=True # Enable streaming
106
+ )
107
 
108
+ response_text = ""
109
+ for chunk in response:
110
+ # Extract the content safely
111
+ delta = chunk.choices[0].delta # Get the delta object
112
+ token = getattr(delta, "content", None) # Safely access the "content" field
113
+ if token: # Only process non-None tokens
114
+ response_text += token
115
+ yield response_text
116
+
117
+ except Exception as e:
118
+ yield f"❌ An error occurred while providing suggestions: {str(e)}"
119
 
120
  # Main image processing function
121
  def process_image(image):