Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -46,7 +46,7 @@ def chat(message, history, image=None):
|
|
46 |
global client, current_response_id, chat_history
|
47 |
|
48 |
if not client:
|
49 |
-
return "Please set your OpenAI API key first."
|
50 |
|
51 |
try:
|
52 |
if image is not None:
|
@@ -97,9 +97,10 @@ def chat(message, history, image=None):
|
|
97 |
chat_history.append({"role": "user", "content": message})
|
98 |
chat_history.append({"role": "assistant", "content": response.output_text})
|
99 |
|
100 |
-
|
|
|
101 |
except Exception as e:
|
102 |
-
return f"Error: {str(e)}"
|
103 |
|
104 |
def export_chat():
|
105 |
"""Export chat history to JSON file"""
|
@@ -121,7 +122,7 @@ def clear_chat():
|
|
121 |
global chat_history, current_response_id
|
122 |
chat_history = []
|
123 |
current_response_id = None
|
124 |
-
return
|
125 |
|
126 |
def generate_iframe_code(request: gr.Request):
|
127 |
"""Generate iframe code for embedding the app"""
|
@@ -139,6 +140,10 @@ def generate_iframe_code(request: gr.Request):
|
|
139 |
|
140 |
return iframe_code
|
141 |
|
|
|
|
|
|
|
|
|
142 |
def create_ui():
|
143 |
"""Create the Gradio UI"""
|
144 |
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
@@ -185,7 +190,8 @@ def create_ui():
|
|
185 |
with gr.Group(visible=False) as chat_interface:
|
186 |
with gr.Tabs() as tabs:
|
187 |
with gr.TabItem("Chat", elem_classes="tab-content"):
|
188 |
-
chatbot = gr.Chatbot(height=400)
|
|
|
189 |
with gr.Row():
|
190 |
with gr.Column(scale=4):
|
191 |
msg = gr.Textbox(
|
@@ -194,8 +200,7 @@ def create_ui():
|
|
194 |
container=False
|
195 |
)
|
196 |
with gr.Column(scale=1):
|
197 |
-
|
198 |
-
image_btn = gr.UploadButton("π", file_types=["image"])
|
199 |
|
200 |
with gr.Row():
|
201 |
clear_btn = gr.Button("Clear Chat")
|
@@ -218,16 +223,27 @@ def create_ui():
|
|
218 |
outputs=[api_key_group, chat_interface, api_key_message]
|
219 |
)
|
220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
msg.submit(
|
222 |
chat,
|
223 |
-
inputs=[msg, chatbot,
|
224 |
outputs=[chatbot]
|
225 |
-
)
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
inputs=[image_btn],
|
230 |
-
outputs=[msg]
|
231 |
)
|
232 |
|
233 |
clear_btn.click(
|
@@ -241,9 +257,10 @@ def create_ui():
|
|
241 |
)
|
242 |
|
243 |
theme_btn.click(
|
244 |
-
lambda: gr.Theme.toggle_theme(),
|
245 |
None,
|
246 |
-
None
|
|
|
|
|
247 |
)
|
248 |
|
249 |
generate_iframe_btn.click(
|
|
|
46 |
global client, current_response_id, chat_history
|
47 |
|
48 |
if not client:
|
49 |
+
return history + [("You", "Please set your OpenAI API key first.")]
|
50 |
|
51 |
try:
|
52 |
if image is not None:
|
|
|
97 |
chat_history.append({"role": "user", "content": message})
|
98 |
chat_history.append({"role": "assistant", "content": response.output_text})
|
99 |
|
100 |
+
# Return updated history
|
101 |
+
return history + [("You", message), ("Bot", response.output_text)]
|
102 |
except Exception as e:
|
103 |
+
return history + [("You", message), ("Bot", f"Error: {str(e)}")]
|
104 |
|
105 |
def export_chat():
|
106 |
"""Export chat history to JSON file"""
|
|
|
122 |
global chat_history, current_response_id
|
123 |
chat_history = []
|
124 |
current_response_id = None
|
125 |
+
return []
|
126 |
|
127 |
def generate_iframe_code(request: gr.Request):
|
128 |
"""Generate iframe code for embedding the app"""
|
|
|
140 |
|
141 |
return iframe_code
|
142 |
|
143 |
+
def process_image_upload(img, text):
|
144 |
+
"""Process image upload and update message text"""
|
145 |
+
return gr.update(value=f"{text if text else ''}[Image attached]")
|
146 |
+
|
147 |
def create_ui():
|
148 |
"""Create the Gradio UI"""
|
149 |
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
|
|
190 |
with gr.Group(visible=False) as chat_interface:
|
191 |
with gr.Tabs() as tabs:
|
192 |
with gr.TabItem("Chat", elem_classes="tab-content"):
|
193 |
+
chatbot = gr.Chatbot(height=400, type="tuples")
|
194 |
+
|
195 |
with gr.Row():
|
196 |
with gr.Column(scale=4):
|
197 |
msg = gr.Textbox(
|
|
|
200 |
container=False
|
201 |
)
|
202 |
with gr.Column(scale=1):
|
203 |
+
image_btn = gr.UploadButton("π", file_types=["image"])
|
|
|
204 |
|
205 |
with gr.Row():
|
206 |
clear_btn = gr.Button("Clear Chat")
|
|
|
223 |
outputs=[api_key_group, chat_interface, api_key_message]
|
224 |
)
|
225 |
|
226 |
+
# Handle image uploads
|
227 |
+
image_upload = None
|
228 |
+
image_btn.upload(
|
229 |
+
process_image_upload,
|
230 |
+
inputs=[image_btn, msg],
|
231 |
+
outputs=[msg]
|
232 |
+
).then(
|
233 |
+
lambda x: x,
|
234 |
+
inputs=[image_btn],
|
235 |
+
outputs=[image_upload]
|
236 |
+
)
|
237 |
+
|
238 |
+
# Handle chat submission
|
239 |
msg.submit(
|
240 |
chat,
|
241 |
+
inputs=[msg, chatbot, image_btn],
|
242 |
outputs=[chatbot]
|
243 |
+
).then(
|
244 |
+
lambda: gr.update(value=""),
|
245 |
+
None,
|
246 |
+
[msg]
|
|
|
|
|
247 |
)
|
248 |
|
249 |
clear_btn.click(
|
|
|
257 |
)
|
258 |
|
259 |
theme_btn.click(
|
|
|
260 |
None,
|
261 |
+
None,
|
262 |
+
None,
|
263 |
+
_js="() => {document.body.classList.toggle('dark');}"
|
264 |
)
|
265 |
|
266 |
generate_iframe_btn.click(
|