Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import gradio as gr
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# =====================================================
|
| 6 |
-
# Groq Client Initialization (GLOBAL
|
| 7 |
# =====================================================
|
| 8 |
def get_groq_client():
|
| 9 |
api_key = os.getenv("GROQ_API_KEY")
|
|
@@ -11,10 +11,10 @@ def get_groq_client():
|
|
| 11 |
raise RuntimeError("GROQ_API_KEY not set")
|
| 12 |
return Groq(api_key=api_key)
|
| 13 |
|
| 14 |
-
client = get_groq_client()
|
| 15 |
|
| 16 |
# =====================================================
|
| 17 |
-
#
|
| 18 |
# =====================================================
|
| 19 |
def chat_with_groq(user_message, history):
|
| 20 |
if not user_message or not user_message.strip():
|
|
@@ -26,7 +26,7 @@ def chat_with_groq(user_message, history):
|
|
| 26 |
"content": user_message
|
| 27 |
})
|
| 28 |
|
| 29 |
-
# Add
|
| 30 |
history.append({
|
| 31 |
"role": "assistant",
|
| 32 |
"content": "⚡ Groq is thinking..."
|
|
@@ -39,17 +39,14 @@ def chat_with_groq(user_message, history):
|
|
| 39 |
]
|
| 40 |
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
model="llama-3.3-70b-versatile",
|
| 44 |
messages=messages,
|
| 45 |
)
|
| 46 |
|
| 47 |
-
response = chat_completion.choices[0].message.content
|
| 48 |
-
|
| 49 |
-
# Replace thinking message
|
| 50 |
history[-1] = {
|
| 51 |
"role": "assistant",
|
| 52 |
-
"content":
|
| 53 |
}
|
| 54 |
|
| 55 |
except Exception as e:
|
|
@@ -61,13 +58,13 @@ def chat_with_groq(user_message, history):
|
|
| 61 |
return history, ""
|
| 62 |
|
| 63 |
# =====================================================
|
| 64 |
-
# Clear Chat
|
| 65 |
# =====================================================
|
| 66 |
def clear_chat():
|
| 67 |
return [], ""
|
| 68 |
|
| 69 |
# =====================================================
|
| 70 |
-
# CSS (
|
| 71 |
# =====================================================
|
| 72 |
custom_css = """
|
| 73 |
:root {
|
|
@@ -174,15 +171,15 @@ button:hover {
|
|
| 174 |
"""
|
| 175 |
|
| 176 |
# =====================================================
|
| 177 |
-
# Gradio UI
|
| 178 |
# =====================================================
|
| 179 |
-
with gr.Blocks(
|
| 180 |
gr.Markdown("# 🤖 GROQ AI CHATBOT")
|
| 181 |
gr.Markdown(
|
| 182 |
"<div class='subtitle'>Fast, reliable AI powered by Groq & LLaMA-3.3</div>"
|
| 183 |
)
|
| 184 |
|
| 185 |
-
chatbot = gr.Chatbot(
|
| 186 |
state = gr.State([])
|
| 187 |
|
| 188 |
with gr.Row():
|
|
@@ -198,5 +195,8 @@ with gr.Blocks(css=custom_css, title="GROQ AI CHATBOT") as demo:
|
|
| 198 |
txt.submit(chat_with_groq, [txt, state], [chatbot, txt])
|
| 199 |
clear.click(clear_chat, outputs=[chatbot, txt], show_progress=False)
|
| 200 |
|
|
|
|
|
|
|
|
|
|
| 201 |
if __name__ == "__main__":
|
| 202 |
-
demo.launch()
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# =====================================================
|
| 6 |
+
# Groq Client Initialization (GLOBAL)
|
| 7 |
# =====================================================
|
| 8 |
def get_groq_client():
|
| 9 |
api_key = os.getenv("GROQ_API_KEY")
|
|
|
|
| 11 |
raise RuntimeError("GROQ_API_KEY not set")
|
| 12 |
return Groq(api_key=api_key)
|
| 13 |
|
| 14 |
+
client = get_groq_client()
|
| 15 |
|
| 16 |
# =====================================================
|
| 17 |
+
# Chat Logic (Gradio 6 compatible)
|
| 18 |
# =====================================================
|
| 19 |
def chat_with_groq(user_message, history):
|
| 20 |
if not user_message or not user_message.strip():
|
|
|
|
| 26 |
"content": user_message
|
| 27 |
})
|
| 28 |
|
| 29 |
+
# Add thinking placeholder
|
| 30 |
history.append({
|
| 31 |
"role": "assistant",
|
| 32 |
"content": "⚡ Groq is thinking..."
|
|
|
|
| 39 |
]
|
| 40 |
|
| 41 |
try:
|
| 42 |
+
completion = client.chat.completions.create(
|
| 43 |
model="llama-3.3-70b-versatile",
|
| 44 |
messages=messages,
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
history[-1] = {
|
| 48 |
"role": "assistant",
|
| 49 |
+
"content": completion.choices[0].message.content
|
| 50 |
}
|
| 51 |
|
| 52 |
except Exception as e:
|
|
|
|
| 58 |
return history, ""
|
| 59 |
|
| 60 |
# =====================================================
|
| 61 |
+
# Clear Chat
|
| 62 |
# =====================================================
|
| 63 |
def clear_chat():
|
| 64 |
return [], ""
|
| 65 |
|
| 66 |
# =====================================================
|
| 67 |
+
# CSS (Light + Dark mode safe)
|
| 68 |
# =====================================================
|
| 69 |
custom_css = """
|
| 70 |
:root {
|
|
|
|
| 171 |
"""
|
| 172 |
|
| 173 |
# =====================================================
|
| 174 |
+
# Gradio UI
|
| 175 |
# =====================================================
|
| 176 |
+
with gr.Blocks(title="GROQ AI CHATBOT") as demo:
|
| 177 |
gr.Markdown("# 🤖 GROQ AI CHATBOT")
|
| 178 |
gr.Markdown(
|
| 179 |
"<div class='subtitle'>Fast, reliable AI powered by Groq & LLaMA-3.3</div>"
|
| 180 |
)
|
| 181 |
|
| 182 |
+
chatbot = gr.Chatbot(elem_classes="chatbot")
|
| 183 |
state = gr.State([])
|
| 184 |
|
| 185 |
with gr.Row():
|
|
|
|
| 195 |
txt.submit(chat_with_groq, [txt, state], [chatbot, txt])
|
| 196 |
clear.click(clear_chat, outputs=[chatbot, txt], show_progress=False)
|
| 197 |
|
| 198 |
+
# =====================================================
|
| 199 |
+
# Launch (CSS moved here in Gradio 6)
|
| 200 |
+
# =====================================================
|
| 201 |
if __name__ == "__main__":
|
| 202 |
+
demo.launch(css=custom_css)
|