Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,81 +7,101 @@ from groq import Groq
|
|
| 7 |
# ---------------------------
|
| 8 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 9 |
if not GROQ_API_KEY:
|
| 10 |
-
raise ValueError("GROQ_API_KEY not found. Add it in Space Settings →
|
| 11 |
|
| 12 |
client = Groq(api_key=GROQ_API_KEY)
|
|
|
|
| 13 |
|
| 14 |
# ---------------------------
|
| 15 |
-
#
|
| 16 |
# ---------------------------
|
| 17 |
def chat_with_groq(message, history):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
if user_msg:
|
| 26 |
-
messages.append({"role": "user", "content": str(user_msg)})
|
| 27 |
-
if bot_msg:
|
| 28 |
-
messages.append({"role": "assistant", "content": str(bot_msg)})
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
response = client.chat.completions.create(
|
| 36 |
-
model="mixtral-8x7b-32768", # ✅ change to a valid model
|
| 37 |
messages=messages,
|
|
|
|
| 38 |
)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return
|
| 42 |
except Exception as e:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
history.append((message, error_msg))
|
| 46 |
-
return "", history
|
| 47 |
|
| 48 |
# ---------------------------
|
| 49 |
-
#
|
| 50 |
# ---------------------------
|
| 51 |
custom_css = """
|
| 52 |
body {
|
| 53 |
background: linear-gradient(135deg, #667eea, #764ba2, #ff6ec4);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
#chatbox {
|
| 56 |
backdrop-filter: blur(20px);
|
| 57 |
-
background: rgba(255, 255, 255, 0.
|
| 58 |
border-radius: 20px;
|
| 59 |
padding: 15px;
|
| 60 |
box-shadow: 0px 8px 32px rgba(0, 0, 0, 0.3);
|
| 61 |
}
|
|
|
|
|
|
|
|
|
|
| 62 |
button {
|
| 63 |
-
background: linear-gradient(90deg, #ff6ec4, #7873f5);
|
| 64 |
-
color: white;
|
| 65 |
-
border-radius: 12px;
|
| 66 |
-
|
| 67 |
-
|
| 68 |
}
|
| 69 |
button:hover {
|
| 70 |
-
opacity: 0.85;
|
| 71 |
}
|
| 72 |
"""
|
| 73 |
|
| 74 |
-
with gr.Blocks(css=custom_css) as demo:
|
| 75 |
gr.Markdown(
|
| 76 |
-
"
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
-
chatbot = gr.Chatbot(elem_id="chatbox", height=450)
|
| 81 |
-
msg = gr.Textbox(placeholder="Type something cool...")
|
| 82 |
clear = gr.Button("✨ Clear Chat")
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
demo.launch(
|
|
|
|
| 7 |
# ---------------------------
|
| 8 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 9 |
if not GROQ_API_KEY:
|
| 10 |
+
raise ValueError("GROQ_API_KEY not found. Add it in Space Settings → Secrets.")
|
| 11 |
|
| 12 |
client = Groq(api_key=GROQ_API_KEY)
|
| 13 |
+
print("Groq Client Ready 🚀") # This will appear in the HF Space logs
|
| 14 |
|
| 15 |
# ---------------------------
|
| 16 |
+
# Core chat function (identical to Colab version)
|
| 17 |
# ---------------------------
|
| 18 |
def chat_with_groq(message, history):
|
| 19 |
+
try:
|
| 20 |
+
# Convert Gradio history into Groq message format
|
| 21 |
+
messages = []
|
| 22 |
+
for human, assistant in history:
|
| 23 |
+
messages.append({"role": "user", "content": human})
|
| 24 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 25 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# 👇 Debug: print the exact message list (check logs!)
|
| 28 |
+
print("Sending messages to Groq:", messages)
|
| 29 |
|
| 30 |
+
# Call Groq API with the same model that works in Colab
|
| 31 |
+
chat_completion = client.chat.completions.create(
|
|
|
|
|
|
|
| 32 |
messages=messages,
|
| 33 |
+
model="openai/gpt-oss-120b", # keep the working model
|
| 34 |
)
|
| 35 |
+
|
| 36 |
+
reply = chat_completion.choices[0].message.content
|
| 37 |
+
return reply
|
| 38 |
except Exception as e:
|
| 39 |
+
print("Groq API error:", str(e)) # 👈 also print error
|
| 40 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# ---------------------------
|
| 43 |
+
# Gradio UI (mirrors Colab pattern)
|
| 44 |
# ---------------------------
|
| 45 |
custom_css = """
|
| 46 |
body {
|
| 47 |
background: linear-gradient(135deg, #667eea, #764ba2, #ff6ec4);
|
| 48 |
+
font-family: 'Poppins', sans-serif;
|
| 49 |
+
}
|
| 50 |
+
.gradio-container {
|
| 51 |
+
background: transparent !important;
|
| 52 |
}
|
| 53 |
#chatbox {
|
| 54 |
backdrop-filter: blur(20px);
|
| 55 |
+
background: rgba(255, 255, 255, 0.1);
|
| 56 |
border-radius: 20px;
|
| 57 |
padding: 15px;
|
| 58 |
box-shadow: 0px 8px 32px rgba(0, 0, 0, 0.3);
|
| 59 |
}
|
| 60 |
+
textarea {
|
| 61 |
+
border-radius: 15px !important;
|
| 62 |
+
}
|
| 63 |
button {
|
| 64 |
+
background: linear-gradient(90deg, #ff6ec4, #7873f5) !important;
|
| 65 |
+
color: white !important;
|
| 66 |
+
border-radius: 12px !important;
|
| 67 |
+
font-weight: bold !important;
|
| 68 |
+
border: none !important;
|
| 69 |
}
|
| 70 |
button:hover {
|
| 71 |
+
opacity: 0.85 !important;
|
| 72 |
}
|
| 73 |
"""
|
| 74 |
|
| 75 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 76 |
gr.Markdown(
|
| 77 |
+
"""
|
| 78 |
+
<h1 style='text-align: center; color: white;'>
|
| 79 |
+
🤖 Groq Gen-Z Chatbot
|
| 80 |
+
</h1>
|
| 81 |
+
<p style='text-align: center; color: white; font-size: 18px;'>
|
| 82 |
+
Fast. Smart. Stylish. ⚡
|
| 83 |
+
</p>
|
| 84 |
+
"""
|
| 85 |
)
|
| 86 |
|
| 87 |
+
chatbot = gr.Chatbot(elem_id="chatbox", height=450, bubble_full_width=False)
|
| 88 |
+
msg = gr.Textbox(placeholder="Type something cool...", scale=8)
|
| 89 |
clear = gr.Button("✨ Clear Chat")
|
| 90 |
|
| 91 |
+
# Two-step interaction exactly as in Colab
|
| 92 |
+
def user_message(message, history):
|
| 93 |
+
return "", history + [[message, None]]
|
| 94 |
+
|
| 95 |
+
def bot_response(history):
|
| 96 |
+
user_msg = history[-1][0]
|
| 97 |
+
reply = chat_with_groq(user_msg, history[:-1])
|
| 98 |
+
history[-1][1] = reply
|
| 99 |
+
return history
|
| 100 |
+
|
| 101 |
+
msg.submit(user_message, [msg, chatbot], [msg, chatbot]).then(
|
| 102 |
+
bot_response, chatbot, chatbot
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
clear.click(lambda: [], None, chatbot, queue=False)
|
| 106 |
|
| 107 |
+
demo.launch()
|