Spaces:
Running
Running
Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -18,11 +18,12 @@ def process_message(message, history, image):
|
|
| 18 |
# Prepare messages for the API
|
| 19 |
messages = []
|
| 20 |
|
| 21 |
-
# Add chat history -
|
| 22 |
-
for
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
# Add current message and image
|
| 28 |
if message or image:
|
|
@@ -39,6 +40,9 @@ def process_message(message, history, image):
|
|
| 39 |
|
| 40 |
messages.append(current_message)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
# Get response from the model
|
| 43 |
response = ""
|
| 44 |
try:
|
|
@@ -48,17 +52,20 @@ def process_message(message, history, image):
|
|
| 48 |
stream=True,
|
| 49 |
)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
for chunk in stream:
|
| 52 |
if chunk.choices[0].delta.content:
|
| 53 |
response += chunk.choices[0].delta.content
|
| 54 |
-
# Update
|
| 55 |
-
new_history =
|
| 56 |
-
yield new_history
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
error_msg = f"Error: {str(e)}"
|
| 60 |
-
|
| 61 |
-
yield
|
| 62 |
|
| 63 |
# Create Gradio interface
|
| 64 |
with gr.Blocks() as demo:
|
|
@@ -72,7 +79,6 @@ with gr.Blocks() as demo:
|
|
| 72 |
chatbot = gr.Chatbot(
|
| 73 |
label="Conversation",
|
| 74 |
height=400,
|
| 75 |
-
type="messages", # Set to messages format
|
| 76 |
avatar_images=(
|
| 77 |
"https://cdn-icons-png.flaticon.com/512/147/147144.png",
|
| 78 |
"https://cdn-icons-png.flaticon.com/512/4712/4712025.png"
|
|
@@ -90,7 +96,7 @@ with gr.Blocks() as demo:
|
|
| 90 |
img = gr.Image(
|
| 91 |
label="Upload image",
|
| 92 |
type="filepath",
|
| 93 |
-
height=
|
| 94 |
)
|
| 95 |
|
| 96 |
with gr.Row():
|
|
@@ -98,18 +104,29 @@ with gr.Blocks() as demo:
|
|
| 98 |
clear_btn = gr.ClearButton(components=[msg, img, chatbot], value="Clear Chat")
|
| 99 |
|
| 100 |
# Set up the chat interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
msg.submit(
|
| 102 |
-
fn=
|
| 103 |
inputs=[msg, chatbot, img],
|
| 104 |
-
outputs=[chatbot
|
| 105 |
api_visibility="public"
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
submit_btn.click(
|
| 109 |
-
fn=
|
| 110 |
inputs=[msg, chatbot, img],
|
| 111 |
-
outputs=[chatbot
|
| 112 |
api_visibility="public"
|
|
|
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
|
| 115 |
# Launch the app
|
|
|
|
| 18 |
# Prepare messages for the API
|
| 19 |
messages = []
|
| 20 |
|
| 21 |
+
# Add chat history - Gradio 6 uses list of dicts format
|
| 22 |
+
for msg_dict in history:
|
| 23 |
+
if msg_dict["role"] == "user":
|
| 24 |
+
messages.append({"role": "user", "content": msg_dict["content"]})
|
| 25 |
+
elif msg_dict["role"] == "assistant":
|
| 26 |
+
messages.append({"role": "assistant", "content": msg_dict["content"]})
|
| 27 |
|
| 28 |
# Add current message and image
|
| 29 |
if message or image:
|
|
|
|
| 40 |
|
| 41 |
messages.append(current_message)
|
| 42 |
|
| 43 |
+
# Add user message to history immediately
|
| 44 |
+
new_history = history + [{"role": "user", "content": message if message else ""}]
|
| 45 |
+
|
| 46 |
# Get response from the model
|
| 47 |
response = ""
|
| 48 |
try:
|
|
|
|
| 52 |
stream=True,
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# Add assistant message to history
|
| 56 |
+
new_history.append({"role": "assistant", "content": ""})
|
| 57 |
+
|
| 58 |
for chunk in stream:
|
| 59 |
if chunk.choices[0].delta.content:
|
| 60 |
response += chunk.choices[0].delta.content
|
| 61 |
+
# Update the assistant's message
|
| 62 |
+
new_history[-1]["content"] = response
|
| 63 |
+
yield new_history
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
error_msg = f"Error: {str(e)}"
|
| 67 |
+
new_history[-1]["content"] = error_msg
|
| 68 |
+
yield new_history
|
| 69 |
|
| 70 |
# Create Gradio interface
|
| 71 |
with gr.Blocks() as demo:
|
|
|
|
| 79 |
chatbot = gr.Chatbot(
|
| 80 |
label="Conversation",
|
| 81 |
height=400,
|
|
|
|
| 82 |
avatar_images=(
|
| 83 |
"https://cdn-icons-png.flaticon.com/512/147/147144.png",
|
| 84 |
"https://cdn-icons-png.flaticon.com/512/4712/4712025.png"
|
|
|
|
| 96 |
img = gr.Image(
|
| 97 |
label="Upload image",
|
| 98 |
type="filepath",
|
| 99 |
+
height=150
|
| 100 |
)
|
| 101 |
|
| 102 |
with gr.Row():
|
|
|
|
| 104 |
clear_btn = gr.ClearButton(components=[msg, img, chatbot], value="Clear Chat")
|
| 105 |
|
| 106 |
# Set up the chat interface
|
| 107 |
+
def submit_message(message, history, image):
|
| 108 |
+
# Clear the textbox and image after submission
|
| 109 |
+
for update in process_message(message, history, image):
|
| 110 |
+
yield update
|
| 111 |
+
|
| 112 |
msg.submit(
|
| 113 |
+
fn=submit_message,
|
| 114 |
inputs=[msg, chatbot, img],
|
| 115 |
+
outputs=[chatbot],
|
| 116 |
api_visibility="public"
|
| 117 |
+
).then(
|
| 118 |
+
lambda: ("", None),
|
| 119 |
+
outputs=[msg, img]
|
| 120 |
)
|
| 121 |
|
| 122 |
submit_btn.click(
|
| 123 |
+
fn=submit_message,
|
| 124 |
inputs=[msg, chatbot, img],
|
| 125 |
+
outputs=[chatbot],
|
| 126 |
api_visibility="public"
|
| 127 |
+
).then(
|
| 128 |
+
lambda: ("", None),
|
| 129 |
+
outputs=[msg, img]
|
| 130 |
)
|
| 131 |
|
| 132 |
# Launch the app
|