Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,24 +13,44 @@ client = Groq(api_key=GROQ_API_KEY)
|
|
| 13 |
print("Groq Client Ready π")
|
| 14 |
|
| 15 |
# ---------------------------
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# ---------------------------
|
| 18 |
def chat_with_groq(history):
|
| 19 |
try:
|
| 20 |
-
|
|
|
|
| 21 |
chat_completion = client.chat.completions.create(
|
| 22 |
-
messages=
|
| 23 |
-
model="mixtral-8x7b-32768", # or
|
| 24 |
)
|
| 25 |
reply = chat_completion.choices[0].message.content
|
| 26 |
return reply
|
| 27 |
except Exception as e:
|
| 28 |
import traceback
|
| 29 |
-
traceback.print_exc()
|
| 30 |
return f"β Error: {str(e)}"
|
| 31 |
|
| 32 |
# ---------------------------
|
| 33 |
-
# Gradio UI (
|
| 34 |
# ---------------------------
|
| 35 |
custom_css = """
|
| 36 |
body {
|
|
@@ -74,7 +94,6 @@ with gr.Blocks() as demo:
|
|
| 74 |
"""
|
| 75 |
)
|
| 76 |
|
| 77 |
-
# No 'type' argument β uses default format (messages dicts in Gradio 6)
|
| 78 |
chatbot = gr.Chatbot(elem_id="chatbox", height=450)
|
| 79 |
msg = gr.Textbox(placeholder="Type something cool...", scale=8)
|
| 80 |
clear = gr.Button("β¨ Clear Chat")
|
|
|
|
| 13 |
print("Groq Client Ready π")
|
| 14 |
|
| 15 |
# ---------------------------
|
| 16 |
+
# Helper to convert Gradio's message dict to simple format for Groq
|
| 17 |
+
# ---------------------------
|
| 18 |
+
def convert_to_groq_messages(gradio_history):
|
| 19 |
+
groq_messages = []
|
| 20 |
+
for msg in gradio_history:
|
| 21 |
+
role = msg.get("role")
|
| 22 |
+
content = msg.get("content")
|
| 23 |
+
# Gradio 6 may send content as a list of parts (for multimodal)
|
| 24 |
+
if isinstance(content, list):
|
| 25 |
+
# Extract text from parts with type "text"
|
| 26 |
+
text_parts = [part.get("text", "") for part in content if part.get("type") == "text"]
|
| 27 |
+
content = " ".join(text_parts)
|
| 28 |
+
elif content is None:
|
| 29 |
+
continue
|
| 30 |
+
# Ensure content is a string
|
| 31 |
+
groq_messages.append({"role": role, "content": str(content)})
|
| 32 |
+
return groq_messages
|
| 33 |
+
|
| 34 |
+
# ---------------------------
|
| 35 |
+
# Core chat function β accepts a list of message dicts (Gradio format)
|
| 36 |
# ---------------------------
|
| 37 |
def chat_with_groq(history):
|
| 38 |
try:
|
| 39 |
+
groq_messages = convert_to_groq_messages(history)
|
| 40 |
+
print("Sending messages to Groq:", groq_messages)
|
| 41 |
chat_completion = client.chat.completions.create(
|
| 42 |
+
messages=groq_messages,
|
| 43 |
+
model="mixtral-8x7b-32768", # or your preferred model
|
| 44 |
)
|
| 45 |
reply = chat_completion.choices[0].message.content
|
| 46 |
return reply
|
| 47 |
except Exception as e:
|
| 48 |
import traceback
|
| 49 |
+
traceback.print_exc()
|
| 50 |
return f"β Error: {str(e)}"
|
| 51 |
|
| 52 |
# ---------------------------
|
| 53 |
+
# Gradio UI (unchanged)
|
| 54 |
# ---------------------------
|
| 55 |
custom_css = """
|
| 56 |
body {
|
|
|
|
| 94 |
"""
|
| 95 |
)
|
| 96 |
|
|
|
|
| 97 |
chatbot = gr.Chatbot(elem_id="chatbox", height=450)
|
| 98 |
msg = gr.Textbox(placeholder="Type something cool...", scale=8)
|
| 99 |
clear = gr.Button("β¨ Clear Chat")
|