Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,34 +13,24 @@ client = Groq(api_key=GROQ_API_KEY)
|
|
| 13 |
print("Groq Client Ready π")
|
| 14 |
|
| 15 |
# ---------------------------
|
| 16 |
-
# Core chat function
|
| 17 |
# ---------------------------
|
| 18 |
-
def chat_with_groq(
|
| 19 |
try:
|
| 20 |
-
messages
|
| 21 |
-
for human, assistant in history:
|
| 22 |
-
messages.append({"role": "user", "content": human})
|
| 23 |
-
messages.append({"role": "assistant", "content": assistant})
|
| 24 |
-
messages.append({"role": "user", "content": message})
|
| 25 |
-
|
| 26 |
-
print("Sending messages to Groq:", messages)
|
| 27 |
-
|
| 28 |
-
# Try a standard Groq model to rule out model issues
|
| 29 |
chat_completion = client.chat.completions.create(
|
| 30 |
-
messages=
|
| 31 |
-
model="mixtral-8x7b-32768", #
|
| 32 |
)
|
| 33 |
-
|
| 34 |
reply = chat_completion.choices[0].message.content
|
| 35 |
return reply
|
| 36 |
except Exception as e:
|
| 37 |
-
# Print full exception details
|
| 38 |
import traceback
|
| 39 |
-
traceback.print_exc()
|
| 40 |
return f"β Error: {str(e)}"
|
| 41 |
|
| 42 |
# ---------------------------
|
| 43 |
-
# Gradio UI (
|
| 44 |
# ---------------------------
|
| 45 |
custom_css = """
|
| 46 |
body {
|
|
@@ -72,7 +62,7 @@ button:hover {
|
|
| 72 |
}
|
| 73 |
"""
|
| 74 |
|
| 75 |
-
with gr.Blocks() as demo:
|
| 76 |
gr.Markdown(
|
| 77 |
"""
|
| 78 |
<h1 style='text-align: center; color: white;'>
|
|
@@ -84,19 +74,18 @@ with gr.Blocks() as demo: # theme & css moved to launch()
|
|
| 84 |
"""
|
| 85 |
)
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
chatbot = gr.Chatbot(elem_id="chatbox", height=450)
|
| 89 |
msg = gr.Textbox(placeholder="Type something cool...", scale=8)
|
| 90 |
clear = gr.Button("β¨ Clear Chat")
|
| 91 |
|
| 92 |
-
# Two-step interaction as in Colab
|
| 93 |
def user_message(message, history):
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
def bot_response(history):
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
history[-1][1] = reply
|
| 100 |
return history
|
| 101 |
|
| 102 |
msg.submit(user_message, [msg, chatbot], [msg, chatbot]).then(
|
|
@@ -105,5 +94,4 @@ with gr.Blocks() as demo: # theme & css moved to launch()
|
|
| 105 |
|
| 106 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 107 |
|
| 108 |
-
# Launch with theme and CSS
|
| 109 |
demo.launch(theme=gr.themes.Soft(), css=custom_css)
|
|
|
|
| 13 |
print("Groq Client Ready π")
|
| 14 |
|
| 15 |
# ---------------------------
|
| 16 |
+
# Core chat function β accepts a list of message dicts
|
| 17 |
# ---------------------------
|
| 18 |
+
def chat_with_groq(history):
|
| 19 |
try:
|
| 20 |
+
print("Sending messages to Groq:", history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
chat_completion = client.chat.completions.create(
|
| 22 |
+
messages=history,
|
| 23 |
+
model="mixtral-8x7b-32768", # or "openai/gpt-oss-120b" if it works
|
| 24 |
)
|
|
|
|
| 25 |
reply = chat_completion.choices[0].message.content
|
| 26 |
return reply
|
| 27 |
except Exception as e:
|
|
|
|
| 28 |
import traceback
|
| 29 |
+
traceback.print_exc() # full error in logs
|
| 30 |
return f"β Error: {str(e)}"
|
| 31 |
|
| 32 |
# ---------------------------
|
| 33 |
+
# Gradio UI (with the new message format)
|
| 34 |
# ---------------------------
|
| 35 |
custom_css = """
|
| 36 |
body {
|
|
|
|
| 62 |
}
|
| 63 |
"""
|
| 64 |
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
gr.Markdown(
|
| 67 |
"""
|
| 68 |
<h1 style='text-align: center; color: white;'>
|
|
|
|
| 74 |
"""
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# π Use the new "messages" format
|
| 78 |
+
chatbot = gr.Chatbot(elem_id="chatbox", height=450, type="messages")
|
| 79 |
msg = gr.Textbox(placeholder="Type something cool...", scale=8)
|
| 80 |
clear = gr.Button("β¨ Clear Chat")
|
| 81 |
|
|
|
|
| 82 |
def user_message(message, history):
|
| 83 |
+
history.append({"role": "user", "content": message})
|
| 84 |
+
return "", history
|
| 85 |
|
| 86 |
def bot_response(history):
|
| 87 |
+
reply = chat_with_groq(history)
|
| 88 |
+
history.append({"role": "assistant", "content": reply})
|
|
|
|
| 89 |
return history
|
| 90 |
|
| 91 |
msg.submit(user_message, [msg, chatbot], [msg, chatbot]).then(
|
|
|
|
| 94 |
|
| 95 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 96 |
|
|
|
|
| 97 |
demo.launch(theme=gr.themes.Soft(), css=custom_css)
|