Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,26 +22,28 @@ custom_css = """
|
|
| 22 |
@spaces.GPU
|
| 23 |
def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
|
| 24 |
"""
|
| 25 |
-
Handles streaming responses using the
|
|
|
|
| 26 |
"""
|
| 27 |
if not message.strip():
|
| 28 |
yield history
|
| 29 |
return
|
| 30 |
|
| 31 |
-
# 1. Initialize message list with system prompt for
|
| 32 |
api_messages = [{"role": "system", "content": system_prompt}]
|
| 33 |
|
| 34 |
-
# 2. Append existing conversation history
|
| 35 |
-
for
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# 3. Append the newest user message to the API list
|
| 40 |
api_messages.append({"role": "user", "content": message})
|
| 41 |
|
| 42 |
-
# 4. Update the Gradio UI history
|
| 43 |
-
history.append(
|
| 44 |
-
history.append({"role": "assistant", "content": ""})
|
| 45 |
yield history
|
| 46 |
|
| 47 |
# 5. Stream from Groq
|
|
@@ -58,12 +60,12 @@ def chat_stream(message, history, model, system_prompt, temperature, max_tokens)
|
|
| 58 |
for chunk in stream:
|
| 59 |
if chunk.choices[0].delta.content:
|
| 60 |
partial_response += chunk.choices[0].delta.content
|
| 61 |
-
# Update the very last
|
| 62 |
-
history[-1][
|
| 63 |
yield history
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
-
history[-1][
|
| 67 |
yield history
|
| 68 |
|
| 69 |
# Build the layout manually
|
|
@@ -107,8 +109,8 @@ with gr.Blocks() as demo:
|
|
| 107 |
|
| 108 |
# --- RIGHT COLUMN: CHAT INTERFACE ---
|
| 109 |
with gr.Column(scale=3):
|
| 110 |
-
#
|
| 111 |
-
chatbot = gr.Chatbot(elem_classes="chat-window"
|
| 112 |
|
| 113 |
msg_input = gr.Textbox(
|
| 114 |
placeholder="Type your message here and press Enter...",
|
|
|
|
| 22 |
@spaces.GPU
|
| 23 |
def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
|
| 24 |
"""
|
| 25 |
+
Handles streaming responses using the standard tuple-based chat history format.
|
| 26 |
+
history layout: [[user_msg1, bot_msg1], [user_msg2, bot_msg2], ...]
|
| 27 |
"""
|
| 28 |
if not message.strip():
|
| 29 |
yield history
|
| 30 |
return
|
| 31 |
|
| 32 |
+
# 1. Initialize message list with system prompt for Groq API
|
| 33 |
api_messages = [{"role": "system", "content": system_prompt}]
|
| 34 |
|
| 35 |
+
# 2. Append existing conversation history seamlessly
|
| 36 |
+
for user_msg, bot_msg in history:
|
| 37 |
+
if user_msg:
|
| 38 |
+
api_messages.append({"role": "user", "content": user_msg})
|
| 39 |
+
if bot_msg:
|
| 40 |
+
api_messages.append({"role": "assistant", "content": bot_msg})
|
| 41 |
|
| 42 |
# 3. Append the newest user message to the API list
|
| 43 |
api_messages.append({"role": "user", "content": message})
|
| 44 |
|
| 45 |
+
# 4. Update the Gradio UI history with an empty slot for the bot's upcoming message
|
| 46 |
+
history.append([message, ""])
|
|
|
|
| 47 |
yield history
|
| 48 |
|
| 49 |
# 5. Stream from Groq
|
|
|
|
| 60 |
for chunk in stream:
|
| 61 |
if chunk.choices[0].delta.content:
|
| 62 |
partial_response += chunk.choices[0].delta.content
|
| 63 |
+
# Update the very last bot message slot in history
|
| 64 |
+
history[-1][1] = partial_response
|
| 65 |
yield history
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
+
history[-1][1] = f"⚠️ Error connecting to Groq: {str(e)}"
|
| 69 |
yield history
|
| 70 |
|
| 71 |
# Build the layout manually
|
|
|
|
| 109 |
|
| 110 |
# --- RIGHT COLUMN: CHAT INTERFACE ---
|
| 111 |
with gr.Column(scale=3):
|
| 112 |
+
# Removed the `type="messages"` keyword argument entirely to prevent the TypeError
|
| 113 |
+
chatbot = gr.Chatbot(elem_classes="chat-window")
|
| 114 |
|
| 115 |
msg_input = gr.Textbox(
|
| 116 |
placeholder="Type your message here and press Enter...",
|