Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -86,14 +86,20 @@ def generate_image(prompt, image=None, output_filename="generated_image"):
|
|
86 |
|
87 |
# Function to handle chat interaction
|
88 |
def chat_handler(prompt, user_image, chat_history, output_filename="generated_image"):
|
89 |
-
# Add the user prompt
|
90 |
-
user_message_content = []
|
91 |
if prompt:
|
92 |
-
|
93 |
if user_image is not None:
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# If no input, return early
|
99 |
if not prompt and not user_image:
|
@@ -115,13 +121,12 @@ def chat_handler(prompt, user_image, chat_history, output_filename="generated_im
|
|
115 |
thumbnail.save(buffered, format="PNG")
|
116 |
thumbnail_base64 = base64.b64encode(buffered.getvalue()).decode()
|
117 |
thumbnail_data_uri = f"data:image/png;base64,{thumbnail_base64}"
|
118 |
-
print("Thumbnail Data URI:", thumbnail_data_uri) # Print to console
|
119 |
assistant_message_content = gr.HTML(f'<img src="{thumbnail_data_uri}" alt="Generated Image Thumbnail">') # Use gr.HTML
|
120 |
else:
|
121 |
assistant_message_content = status # If no image, send text status
|
122 |
|
123 |
|
124 |
-
# Update chat history
|
125 |
chat_history.append({"role": "assistant", "content": assistant_message_content})
|
126 |
|
127 |
return chat_history, user_image, img, ""
|
|
|
86 |
|
87 |
# Function to handle chat interaction
|
88 |
def chat_handler(prompt, user_image, chat_history, output_filename="generated_image"):
|
89 |
+
# Add the user prompt to the chat history - ONLY TEXT PROMPT for user message
|
|
|
90 |
if prompt:
|
91 |
+
chat_history.append({"role": "user", "content": prompt})
|
92 |
if user_image is not None:
|
93 |
+
# If there's a user image, add a separate message for the *thumbnail* to chat history
|
94 |
+
user_thumbnail_size = (100, 100)
|
95 |
+
user_thumbnail = user_image.copy()
|
96 |
+
user_thumbnail.thumbnail(user_thumbnail_size)
|
97 |
+
buffered = io.BytesIO()
|
98 |
+
user_thumbnail.save(buffered, format="PNG")
|
99 |
+
user_thumbnail_base64 = base64.b64encode(buffered.getvalue()).decode()
|
100 |
+
user_thumbnail_data_uri = f"data:image/png;base64,{user_thumbnail_base64}"
|
101 |
+
chat_history.append({"role": "user", "content": gr.HTML(f'<img src="{user_thumbnail_data_uri}" alt="Uploaded Image Thumbnail">')})
|
102 |
+
|
103 |
|
104 |
# If no input, return early
|
105 |
if not prompt and not user_image:
|
|
|
121 |
thumbnail.save(buffered, format="PNG")
|
122 |
thumbnail_base64 = base64.b64encode(buffered.getvalue()).decode()
|
123 |
thumbnail_data_uri = f"data:image/png;base64,{thumbnail_base64}"
|
|
|
124 |
assistant_message_content = gr.HTML(f'<img src="{thumbnail_data_uri}" alt="Generated Image Thumbnail">') # Use gr.HTML
|
125 |
else:
|
126 |
assistant_message_content = status # If no image, send text status
|
127 |
|
128 |
|
129 |
+
# Update chat history - Assistant message is now EITHER gr.HTML or text
|
130 |
chat_history.append({"role": "assistant", "content": assistant_message_content})
|
131 |
|
132 |
return chat_history, user_image, img, ""
|