Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -144,8 +144,7 @@ def generate_response(user_input, top_p, temperature, chat_counter, chatbot, his
|
|
| 144 |
messages = [
|
| 145 |
{"role": "system", "content": "You are a helpful AI assistant. Provide clear, friendly, and informative responses to user questions. Analyze any provided images, videos, or web content to inform your response."}
|
| 146 |
]
|
| 147 |
-
|
| 148 |
-
messages.append(msg)
|
| 149 |
|
| 150 |
# Prepare multimodal content
|
| 151 |
multimodal_content = []
|
|
@@ -176,34 +175,37 @@ def generate_response(user_input, top_p, temperature, chat_counter, chatbot, his
|
|
| 176 |
if content_type == 'webpage':
|
| 177 |
web_content = extract_webpage_content(url)
|
| 178 |
multimodal_content.append({"type": "text", "text": f"[Content from URL ({url})]:\n{web_content}"})
|
| 179 |
-
elif content_type
|
| 180 |
-
|
| 181 |
else:
|
| 182 |
multimodal_content.append({"type": "text", "text": f"[URL content of type '{content_type}' is not supported for direct analysis. Please describe the content if you can access it.]"})
|
| 183 |
|
|
|
|
| 184 |
messages.append({"role": "user", "content": multimodal_content})
|
| 185 |
|
| 186 |
-
# Generate response
|
| 187 |
response_text = generate_chatgpt_response(messages, temperature)
|
| 188 |
|
| 189 |
-
# Translate
|
| 190 |
if lang == "bg":
|
| 191 |
response_text = en_to_bg(response_text)[0]["translation_text"]
|
| 192 |
|
| 193 |
-
# Update history
|
| 194 |
history.append({"role": "user", "content": user_input})
|
| 195 |
history.append({"role": "assistant", "content": response_text})
|
| 196 |
|
|
|
|
| 197 |
chatbot_messages = []
|
| 198 |
for i in range(0, len(history), 2):
|
| 199 |
-
|
| 200 |
user_msg = history[i]["content"]
|
| 201 |
bot_msg = history[i + 1]["content"]
|
| 202 |
chatbot_messages.append([user_msg, bot_msg])
|
| 203 |
-
|
|
|
|
| 204 |
|
| 205 |
chat_counter += 1
|
| 206 |
-
return
|
| 207 |
|
| 208 |
# --- Gradio UI ---
|
| 209 |
|
|
@@ -214,7 +216,6 @@ def generate_response(user_input, top_p, temperature, chat_counter, chatbot, his
|
|
| 214 |
# demo.queue(max_size=10).launch(server_name="0.0.0.0", server_port=7860)
|
| 215 |
|
| 216 |
|
| 217 |
-
|
| 218 |
def reset_textbox():
|
| 219 |
return gr.update(value="", interactive=True)
|
| 220 |
|
|
|
|
| 144 |
messages = [
|
| 145 |
{"role": "system", "content": "You are a helpful AI assistant. Provide clear, friendly, and informative responses to user questions. Analyze any provided images, videos, or web content to inform your response."}
|
| 146 |
]
|
| 147 |
+
messages += history # OpenAI-style history
|
|
|
|
| 148 |
|
| 149 |
# Prepare multimodal content
|
| 150 |
multimodal_content = []
|
|
|
|
| 175 |
if content_type == 'webpage':
|
| 176 |
web_content = extract_webpage_content(url)
|
| 177 |
multimodal_content.append({"type": "text", "text": f"[Content from URL ({url})]:\n{web_content}"})
|
| 178 |
+
elif content_type.startswith("image"):
|
| 179 |
+
multimodal_content.append({"type": "image_url", "image_url": {"url": url}})
|
| 180 |
else:
|
| 181 |
multimodal_content.append({"type": "text", "text": f"[URL content of type '{content_type}' is not supported for direct analysis. Please describe the content if you can access it.]"})
|
| 182 |
|
| 183 |
+
# Add the new user message to the conversation
|
| 184 |
messages.append({"role": "user", "content": multimodal_content})
|
| 185 |
|
| 186 |
+
# Generate response from OpenAI
|
| 187 |
response_text = generate_chatgpt_response(messages, temperature)
|
| 188 |
|
| 189 |
+
# Translate back if needed
|
| 190 |
if lang == "bg":
|
| 191 |
response_text = en_to_bg(response_text)[0]["translation_text"]
|
| 192 |
|
| 193 |
+
# Update OpenAI-style history
|
| 194 |
history.append({"role": "user", "content": user_input})
|
| 195 |
history.append({"role": "assistant", "content": response_text})
|
| 196 |
|
| 197 |
+
# ✅ Convert OpenAI-style history to Chatbot-style list of [user, bot] pairs
|
| 198 |
chatbot_messages = []
|
| 199 |
for i in range(0, len(history), 2):
|
| 200 |
+
try:
|
| 201 |
user_msg = history[i]["content"]
|
| 202 |
bot_msg = history[i + 1]["content"]
|
| 203 |
chatbot_messages.append([user_msg, bot_msg])
|
| 204 |
+
except IndexError:
|
| 205 |
+
pass # incomplete pair; skip
|
| 206 |
|
| 207 |
chat_counter += 1
|
| 208 |
+
return chatbot_messages, history, chat_counter, "✅ Success", gr.update(value="", interactive=True), gr.update(interactive=True)
|
| 209 |
|
| 210 |
# --- Gradio UI ---
|
| 211 |
|
|
|
|
| 216 |
# demo.queue(max_size=10).launch(server_name="0.0.0.0", server_port=7860)
|
| 217 |
|
| 218 |
|
|
|
|
| 219 |
def reset_textbox():
|
| 220 |
return gr.update(value="", interactive=True)
|
| 221 |
|