Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import json
|
|
@@ -11,6 +12,7 @@ api_key = None
|
|
| 11 |
client = None
|
| 12 |
chat_history = []
|
| 13 |
current_response_id = None
|
|
|
|
| 14 |
|
| 15 |
def validate_api_key(key):
|
| 16 |
"""Validate OpenAI API key by making a simple request"""
|
|
@@ -41,19 +43,22 @@ def encode_image(image_path):
|
|
| 41 |
with open(image_path, 'rb') as image_file:
|
| 42 |
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 43 |
|
| 44 |
-
def chat(message,
|
| 45 |
"""Process chat messages with optional image"""
|
| 46 |
-
global client, current_response_id,
|
| 47 |
|
| 48 |
if not client:
|
| 49 |
-
return
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
# Save the uploaded image to a temporary file
|
| 54 |
temp_file = NamedTemporaryFile(delete=False, suffix=".png")
|
| 55 |
image_path = temp_file.name
|
| 56 |
-
|
| 57 |
|
| 58 |
# Encode the image
|
| 59 |
base64_image = encode_image(image_path)
|
|
@@ -83,6 +88,8 @@ def chat(message, history, image=None):
|
|
| 83 |
|
| 84 |
# Clean up the temporary file
|
| 85 |
os.unlink(image_path)
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
# Text-only message
|
| 88 |
response = client.responses.create(
|
|
@@ -93,14 +100,14 @@ def chat(message, history, image=None):
|
|
| 93 |
|
| 94 |
current_response_id = response.id
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
chat_history.append({"role": "user", "content": message})
|
| 98 |
chat_history.append({"role": "assistant", "content": response.output_text})
|
| 99 |
|
| 100 |
-
|
| 101 |
-
return history + [("You", message), ("Bot", response.output_text)]
|
| 102 |
except Exception as e:
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
|
| 105 |
def export_chat():
|
| 106 |
"""Export chat history to JSON file"""
|
|
@@ -119,9 +126,10 @@ def export_chat():
|
|
| 119 |
|
| 120 |
def clear_chat():
|
| 121 |
"""Clear chat history and reset response ID"""
|
| 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):
|
|
@@ -140,9 +148,11 @@ def generate_iframe_code(request: gr.Request):
|
|
| 140 |
|
| 141 |
return iframe_code
|
| 142 |
|
| 143 |
-
def
|
| 144 |
-
"""
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
|
| 147 |
def create_ui():
|
| 148 |
"""Create the Gradio UI"""
|
|
@@ -190,7 +200,7 @@ def create_ui():
|
|
| 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="
|
| 194 |
|
| 195 |
with gr.Row():
|
| 196 |
with gr.Column(scale=4):
|
|
@@ -224,21 +234,16 @@ def create_ui():
|
|
| 224 |
)
|
| 225 |
|
| 226 |
# Handle image uploads
|
| 227 |
-
image_upload = None
|
| 228 |
image_btn.upload(
|
| 229 |
-
|
| 230 |
-
inputs=[image_btn, msg],
|
| 231 |
-
outputs=[msg]
|
| 232 |
-
).then(
|
| 233 |
-
lambda x: x,
|
| 234 |
inputs=[image_btn],
|
| 235 |
-
outputs=[
|
| 236 |
)
|
| 237 |
|
| 238 |
# Handle chat submission
|
| 239 |
msg.submit(
|
| 240 |
chat,
|
| 241 |
-
inputs=[msg, chatbot
|
| 242 |
outputs=[chatbot]
|
| 243 |
).then(
|
| 244 |
lambda: gr.update(value=""),
|
|
@@ -273,4 +278,4 @@ def create_ui():
|
|
| 273 |
|
| 274 |
if __name__ == "__main__":
|
| 275 |
demo = create_ui()
|
| 276 |
-
demo.launch()
|
|
|
|
| 1 |
+
# Create updated app.py file with fixes
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import json
|
|
|
|
| 12 |
client = None
|
| 13 |
chat_history = []
|
| 14 |
current_response_id = None
|
| 15 |
+
current_image = None
|
| 16 |
|
| 17 |
def validate_api_key(key):
|
| 18 |
"""Validate OpenAI API key by making a simple request"""
|
|
|
|
| 43 |
with open(image_path, 'rb') as image_file:
|
| 44 |
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 45 |
|
| 46 |
+
def chat(message, chat_history):
|
| 47 |
"""Process chat messages with optional image"""
|
| 48 |
+
global client, current_response_id, current_image
|
| 49 |
|
| 50 |
if not client:
|
| 51 |
+
return chat_history + [{"role": "assistant", "content": "Please set your OpenAI API key first."}]
|
| 52 |
|
| 53 |
try:
|
| 54 |
+
# Add user message to history
|
| 55 |
+
chat_history.append({"role": "user", "content": message})
|
| 56 |
+
|
| 57 |
+
if current_image is not None:
|
| 58 |
# Save the uploaded image to a temporary file
|
| 59 |
temp_file = NamedTemporaryFile(delete=False, suffix=".png")
|
| 60 |
image_path = temp_file.name
|
| 61 |
+
current_image.save(image_path)
|
| 62 |
|
| 63 |
# Encode the image
|
| 64 |
base64_image = encode_image(image_path)
|
|
|
|
| 88 |
|
| 89 |
# Clean up the temporary file
|
| 90 |
os.unlink(image_path)
|
| 91 |
+
# Reset the image
|
| 92 |
+
current_image = None
|
| 93 |
else:
|
| 94 |
# Text-only message
|
| 95 |
response = client.responses.create(
|
|
|
|
| 100 |
|
| 101 |
current_response_id = response.id
|
| 102 |
|
| 103 |
+
# Add assistant response to history
|
|
|
|
| 104 |
chat_history.append({"role": "assistant", "content": response.output_text})
|
| 105 |
|
| 106 |
+
return chat_history
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
+
error_message = f"Error: {str(e)}"
|
| 109 |
+
chat_history.append({"role": "assistant", "content": error_message})
|
| 110 |
+
return chat_history
|
| 111 |
|
| 112 |
def export_chat():
|
| 113 |
"""Export chat history to JSON file"""
|
|
|
|
| 126 |
|
| 127 |
def clear_chat():
|
| 128 |
"""Clear chat history and reset response ID"""
|
| 129 |
+
global chat_history, current_response_id, current_image
|
| 130 |
chat_history = []
|
| 131 |
current_response_id = None
|
| 132 |
+
current_image = None
|
| 133 |
return []
|
| 134 |
|
| 135 |
def generate_iframe_code(request: gr.Request):
|
|
|
|
| 148 |
|
| 149 |
return iframe_code
|
| 150 |
|
| 151 |
+
def save_image(img):
|
| 152 |
+
"""Save uploaded image to global variable"""
|
| 153 |
+
global current_image
|
| 154 |
+
current_image = img
|
| 155 |
+
return "[Image attached]"
|
| 156 |
|
| 157 |
def create_ui():
|
| 158 |
"""Create the Gradio UI"""
|
|
|
|
| 200 |
with gr.Group(visible=False) as chat_interface:
|
| 201 |
with gr.Tabs() as tabs:
|
| 202 |
with gr.TabItem("Chat", elem_classes="tab-content"):
|
| 203 |
+
chatbot = gr.Chatbot(height=400, type="messages")
|
| 204 |
|
| 205 |
with gr.Row():
|
| 206 |
with gr.Column(scale=4):
|
|
|
|
| 234 |
)
|
| 235 |
|
| 236 |
# Handle image uploads
|
|
|
|
| 237 |
image_btn.upload(
|
| 238 |
+
save_image,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
inputs=[image_btn],
|
| 240 |
+
outputs=[msg]
|
| 241 |
)
|
| 242 |
|
| 243 |
# Handle chat submission
|
| 244 |
msg.submit(
|
| 245 |
chat,
|
| 246 |
+
inputs=[msg, chatbot],
|
| 247 |
outputs=[chatbot]
|
| 248 |
).then(
|
| 249 |
lambda: gr.update(value=""),
|
|
|
|
| 278 |
|
| 279 |
if __name__ == "__main__":
|
| 280 |
demo = create_ui()
|
| 281 |
+
demo.launch()
|