Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,8 +75,7 @@ def chat_fn(user_message, chat_history, image, state):
|
|
| 75 |
# Prepare messages for API
|
| 76 |
messages = []
|
| 77 |
for turn in chat_history:
|
| 78 |
-
messages.append({"role": "
|
| 79 |
-
messages.append({"role": "assistant", "content": turn[1]})
|
| 80 |
# Add current user message
|
| 81 |
if image is not None:
|
| 82 |
# Image attached: send as multimodal input
|
|
@@ -86,13 +85,15 @@ def chat_fn(user_message, chat_history, image, state):
|
|
| 86 |
{"type": "input_image", "image_url": f"data:image/jpeg;base64,{base64_img}"}
|
| 87 |
]
|
| 88 |
messages.append({"role": "user", "content": content})
|
|
|
|
| 89 |
else:
|
| 90 |
messages.append({"role": "user", "content": user_message})
|
|
|
|
| 91 |
# Call OpenAI Responses API
|
| 92 |
try:
|
| 93 |
response = openai_responses_api(api_key, messages)
|
| 94 |
output_text = response.get("output_text", "")
|
| 95 |
-
chat_history.append(
|
| 96 |
state["previous_response_id"] = response.get("id")
|
| 97 |
return chat_history, state, gr.update(visible=False), ""
|
| 98 |
except Exception as e:
|
|
@@ -124,9 +125,7 @@ def image_analysis_fn(image, state):
|
|
| 124 |
def export_chat(chat_history):
|
| 125 |
"""Export chat history as JSON."""
|
| 126 |
import json
|
| 127 |
-
chat_json =
|
| 128 |
-
for user, bot in chat_history:
|
| 129 |
-
chat_json.append({"user": user, "bot": bot})
|
| 130 |
return gr.File.update(value=("chat_history.json", json.dumps(chat_json, indent=2)), visible=True)
|
| 131 |
|
| 132 |
def show_iframe(space_url):
|
|
@@ -149,8 +148,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".footer {text-align: center; font-si
|
|
| 149 |
api_key_box = gr.Textbox(label="Enter your OpenAI API Key", type="password", placeholder="sk-...", show_label=True)
|
| 150 |
api_key_btn = gr.Button("Submit")
|
| 151 |
api_key_error = gr.Markdown("", visible=False)
|
| 152 |
-
def _hide_api_key_row():
|
| 153 |
-
return gr.update(visible=False)
|
| 154 |
api_key_box.submit(set_api_key, [api_key_box, state], [api_key_row, api_key_error, state])
|
| 155 |
api_key_btn.click(set_api_key, [api_key_box, state], [api_key_row, api_key_error, state])
|
| 156 |
|
|
@@ -158,7 +155,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".footer {text-align: center; font-si
|
|
| 158 |
with gr.Tabs():
|
| 159 |
# --- Chat Tab ---
|
| 160 |
with gr.Tab("💬 Chatbot"):
|
| 161 |
-
chatbot = gr.Chatbot(label="Chatbot", height=400)
|
| 162 |
with gr.Row():
|
| 163 |
user_input = gr.Textbox(label="Your message", placeholder="Type your message here...", scale=4)
|
| 164 |
image_input = gr.Image(type="filepath", label="Attach image (optional)", scale=1)
|
|
@@ -182,9 +179,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".footer {text-align: center; font-si
|
|
| 182 |
image_error = gr.Markdown("", visible=False)
|
| 183 |
analyze_btn.click(image_analysis_fn, [image_upload, state], [image_result, api_key_error, image_error])
|
| 184 |
|
| 185 |
-
# --- Theme Toggle ---
|
| 186 |
-
gr.ThemeSwitcher()
|
| 187 |
-
|
| 188 |
# --- Footer ---
|
| 189 |
gr.Markdown(f"<div class='footer'>{FOOTER}</div>")
|
| 190 |
|
|
|
|
| 75 |
# Prepare messages for API
|
| 76 |
messages = []
|
| 77 |
for turn in chat_history:
|
| 78 |
+
messages.append({"role": turn["role"], "content": turn["content"]})
|
|
|
|
| 79 |
# Add current user message
|
| 80 |
if image is not None:
|
| 81 |
# Image attached: send as multimodal input
|
|
|
|
| 85 |
{"type": "input_image", "image_url": f"data:image/jpeg;base64,{base64_img}"}
|
| 86 |
]
|
| 87 |
messages.append({"role": "user", "content": content})
|
| 88 |
+
chat_history.append({"role": "user", "content": f"{user_message}\n[Image attached]"})
|
| 89 |
else:
|
| 90 |
messages.append({"role": "user", "content": user_message})
|
| 91 |
+
chat_history.append({"role": "user", "content": user_message})
|
| 92 |
# Call OpenAI Responses API
|
| 93 |
try:
|
| 94 |
response = openai_responses_api(api_key, messages)
|
| 95 |
output_text = response.get("output_text", "")
|
| 96 |
+
chat_history.append({"role": "assistant", "content": output_text})
|
| 97 |
state["previous_response_id"] = response.get("id")
|
| 98 |
return chat_history, state, gr.update(visible=False), ""
|
| 99 |
except Exception as e:
|
|
|
|
| 125 |
def export_chat(chat_history):
|
| 126 |
"""Export chat history as JSON."""
|
| 127 |
import json
|
| 128 |
+
chat_json = chat_history
|
|
|
|
|
|
|
| 129 |
return gr.File.update(value=("chat_history.json", json.dumps(chat_json, indent=2)), visible=True)
|
| 130 |
|
| 131 |
def show_iframe(space_url):
|
|
|
|
| 148 |
api_key_box = gr.Textbox(label="Enter your OpenAI API Key", type="password", placeholder="sk-...", show_label=True)
|
| 149 |
api_key_btn = gr.Button("Submit")
|
| 150 |
api_key_error = gr.Markdown("", visible=False)
|
|
|
|
|
|
|
| 151 |
api_key_box.submit(set_api_key, [api_key_box, state], [api_key_row, api_key_error, state])
|
| 152 |
api_key_btn.click(set_api_key, [api_key_box, state], [api_key_row, api_key_error, state])
|
| 153 |
|
|
|
|
| 155 |
with gr.Tabs():
|
| 156 |
# --- Chat Tab ---
|
| 157 |
with gr.Tab("💬 Chatbot"):
|
| 158 |
+
chatbot = gr.Chatbot(label="Chatbot", height=400, type="messages")
|
| 159 |
with gr.Row():
|
| 160 |
user_input = gr.Textbox(label="Your message", placeholder="Type your message here...", scale=4)
|
| 161 |
image_input = gr.Image(type="filepath", label="Attach image (optional)", scale=1)
|
|
|
|
| 179 |
image_error = gr.Markdown("", visible=False)
|
| 180 |
analyze_btn.click(image_analysis_fn, [image_upload, state], [image_result, api_key_error, image_error])
|
| 181 |
|
|
|
|
|
|
|
|
|
|
| 182 |
# --- Footer ---
|
| 183 |
gr.Markdown(f"<div class='footer'>{FOOTER}</div>")
|
| 184 |
|