Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import spaces
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
# Initialize the Groq client via Hugging Face Secrets
|
|
@@ -19,38 +19,36 @@ custom_css = """
|
|
| 19 |
.chat-window { border: 1px solid #1f2937 !important; border-radius: 12px !important; background: #111827 !important; }
|
| 20 |
"""
|
| 21 |
|
| 22 |
-
# 2. Add the ZeroGPU decorator here to make Hugging Face happy
|
| 23 |
@spaces.GPU
|
| 24 |
def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
|
| 25 |
"""
|
| 26 |
-
Handles streaming responses using the
|
| 27 |
"""
|
| 28 |
if not message.strip():
|
| 29 |
yield history
|
| 30 |
return
|
| 31 |
|
| 32 |
-
# Initialize message list with system prompt
|
| 33 |
-
|
| 34 |
|
| 35 |
-
#
|
| 36 |
for turn in history:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
if turn[1]:
|
| 40 |
-
messages.append({"role": "assistant", "content": turn[1]})
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
history.append(
|
|
|
|
| 47 |
yield history
|
| 48 |
|
| 49 |
-
# Stream from Groq
|
| 50 |
try:
|
| 51 |
stream = client.chat.completions.create(
|
| 52 |
model=model,
|
| 53 |
-
messages=
|
| 54 |
temperature=temperature,
|
| 55 |
max_tokens=max_tokens,
|
| 56 |
stream=True,
|
|
@@ -60,11 +58,12 @@ def chat_stream(message, history, model, system_prompt, temperature, max_tokens)
|
|
| 60 |
for chunk in stream:
|
| 61 |
if chunk.choices[0].delta.content:
|
| 62 |
partial_response += chunk.choices[0].delta.content
|
| 63 |
-
history
|
|
|
|
| 64 |
yield history
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
-
history[-1][
|
| 68 |
yield history
|
| 69 |
|
| 70 |
# Build the layout manually
|
|
@@ -108,7 +107,8 @@ with gr.Blocks() as demo:
|
|
| 108 |
|
| 109 |
# --- RIGHT COLUMN: CHAT INTERFACE ---
|
| 110 |
with gr.Column(scale=3):
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
msg_input = gr.Textbox(
|
| 114 |
placeholder="Type your message here and press Enter...",
|
|
@@ -129,6 +129,7 @@ with gr.Blocks() as demo:
|
|
| 129 |
outputs=[msg_input]
|
| 130 |
)
|
| 131 |
|
|
|
|
| 132 |
clear_btn.click(fn=lambda: [], inputs=None, outputs=[chatbot])
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import spaces
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
# Initialize the Groq client via Hugging Face Secrets
|
|
|
|
| 19 |
.chat-window { border: 1px solid #1f2937 !important; border-radius: 12px !important; background: #111827 !important; }
|
| 20 |
"""
|
| 21 |
|
|
|
|
| 22 |
@spaces.GPU
|
| 23 |
def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
|
| 24 |
"""
|
| 25 |
+
Handles streaming responses using the modern list-of-dicts (messages) history format.
|
| 26 |
"""
|
| 27 |
if not message.strip():
|
| 28 |
yield history
|
| 29 |
return
|
| 30 |
|
| 31 |
+
# 1. Initialize message list with system prompt for the Groq API payload
|
| 32 |
+
api_messages = [{"role": "system", "content": system_prompt}]
|
| 33 |
|
| 34 |
+
# 2. Append existing conversation history safely
|
| 35 |
for turn in history:
|
| 36 |
+
# turn is a dictionary: {"role": "user"|"assistant", "content": "..."}
|
| 37 |
+
api_messages.append({"role": turn["role"], "content": turn["content"]})
|
|
|
|
|
|
|
| 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 using the dictionary format
|
| 43 |
+
history.append({"role": "user", "content": message})
|
| 44 |
+
history.append({"role": "assistant", "content": ""})
|
| 45 |
yield history
|
| 46 |
|
| 47 |
+
# 5. Stream from Groq
|
| 48 |
try:
|
| 49 |
stream = client.chat.completions.create(
|
| 50 |
model=model,
|
| 51 |
+
messages=api_messages,
|
| 52 |
temperature=temperature,
|
| 53 |
max_tokens=max_tokens,
|
| 54 |
stream=True,
|
|
|
|
| 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 item in history (the assistant's content dictionary)
|
| 62 |
+
history[-1]["content"] = partial_response
|
| 63 |
yield history
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
+
history[-1]["content"] = f"⚠️ Error connecting to Groq: {str(e)}"
|
| 67 |
yield history
|
| 68 |
|
| 69 |
# Build the layout manually
|
|
|
|
| 107 |
|
| 108 |
# --- RIGHT COLUMN: CHAT INTERFACE ---
|
| 109 |
with gr.Column(scale=3):
|
| 110 |
+
# Explicitly state we are using the modern messages type for the component layout
|
| 111 |
+
chatbot = gr.Chatbot(elem_classes="chat-window", type="messages")
|
| 112 |
|
| 113 |
msg_input = gr.Textbox(
|
| 114 |
placeholder="Type your message here and press Enter...",
|
|
|
|
| 129 |
outputs=[msg_input]
|
| 130 |
)
|
| 131 |
|
| 132 |
+
# Clear chat utility logic
|
| 133 |
clear_btn.click(fn=lambda: [], inputs=None, outputs=[chatbot])
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|