Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,27 +3,27 @@ import gradio as gr
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# -------------------------------------------------
|
| 6 |
-
# Groq Client (
|
| 7 |
# -------------------------------------------------
|
| 8 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 9 |
if not api_key:
|
| 10 |
raise RuntimeError(
|
| 11 |
-
"GROQ_API_KEY is not set.
|
| 12 |
)
|
| 13 |
|
| 14 |
client = Groq(api_key=api_key)
|
| 15 |
|
| 16 |
# -------------------------------------------------
|
| 17 |
-
# Chat
|
| 18 |
# -------------------------------------------------
|
| 19 |
def chat(user_input, history):
|
| 20 |
if not user_input or not user_input.strip():
|
| 21 |
return history, ""
|
| 22 |
|
| 23 |
messages = []
|
| 24 |
-
for
|
| 25 |
-
messages.append({"role": "user", "content":
|
| 26 |
-
messages.append({"role": "assistant", "content":
|
| 27 |
|
| 28 |
messages.append({"role": "user", "content": user_input})
|
| 29 |
|
|
@@ -34,13 +34,13 @@ def chat(user_input, history):
|
|
| 34 |
)
|
| 35 |
response = completion.choices[0].message.content
|
| 36 |
except Exception as e:
|
| 37 |
-
response = f"⚠️ Error
|
| 38 |
|
| 39 |
history.append((user_input, response))
|
| 40 |
return history, ""
|
| 41 |
|
| 42 |
# -------------------------------------------------
|
| 43 |
-
# Theme &
|
| 44 |
# -------------------------------------------------
|
| 45 |
theme = gr.themes.Soft(
|
| 46 |
primary_hue="blue",
|
|
@@ -58,9 +58,9 @@ footer {display: none;}
|
|
| 58 |
"""
|
| 59 |
|
| 60 |
# -------------------------------------------------
|
| 61 |
-
#
|
| 62 |
# -------------------------------------------------
|
| 63 |
-
with gr.Blocks(
|
| 64 |
with gr.Column(elem_id="container"):
|
| 65 |
gr.Markdown(
|
| 66 |
"""
|
|
@@ -71,7 +71,6 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 71 |
|
| 72 |
chatbot = gr.Chatbot(
|
| 73 |
height=420,
|
| 74 |
-
bubble_full_width=False,
|
| 75 |
show_label=False
|
| 76 |
)
|
| 77 |
|
|
@@ -86,11 +85,12 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 86 |
with gr.Row():
|
| 87 |
clear_btn = gr.Button("Clear Chat", variant="secondary")
|
| 88 |
|
| 89 |
-
# -----------------------------
|
| 90 |
# Interactions
|
| 91 |
-
# -----------------------------
|
| 92 |
send_btn.click(chat, [user_input, chatbot], [chatbot, user_input])
|
| 93 |
user_input.submit(chat, [user_input, chatbot], [chatbot, user_input])
|
| 94 |
clear_btn.click(lambda: [], None, chatbot)
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# -------------------------------------------------
|
| 6 |
+
# Groq Client (HF Secrets)
|
| 7 |
# -------------------------------------------------
|
| 8 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 9 |
if not api_key:
|
| 10 |
raise RuntimeError(
|
| 11 |
+
"GROQ_API_KEY is not set. Add it in Hugging Face Spaces → Settings → Secrets."
|
| 12 |
)
|
| 13 |
|
| 14 |
client = Groq(api_key=api_key)
|
| 15 |
|
| 16 |
# -------------------------------------------------
|
| 17 |
+
# Chat Logic
|
| 18 |
# -------------------------------------------------
|
| 19 |
def chat(user_input, history):
|
| 20 |
if not user_input or not user_input.strip():
|
| 21 |
return history, ""
|
| 22 |
|
| 23 |
messages = []
|
| 24 |
+
for u, a in history:
|
| 25 |
+
messages.append({"role": "user", "content": u})
|
| 26 |
+
messages.append({"role": "assistant", "content": a})
|
| 27 |
|
| 28 |
messages.append({"role": "user", "content": user_input})
|
| 29 |
|
|
|
|
| 34 |
)
|
| 35 |
response = completion.choices[0].message.content
|
| 36 |
except Exception as e:
|
| 37 |
+
response = f"⚠️ Error: {str(e)}"
|
| 38 |
|
| 39 |
history.append((user_input, response))
|
| 40 |
return history, ""
|
| 41 |
|
| 42 |
# -------------------------------------------------
|
| 43 |
+
# Theme & CSS (Gradio 6 compatible)
|
| 44 |
# -------------------------------------------------
|
| 45 |
theme = gr.themes.Soft(
|
| 46 |
primary_hue="blue",
|
|
|
|
| 58 |
"""
|
| 59 |
|
| 60 |
# -------------------------------------------------
|
| 61 |
+
# UI
|
| 62 |
# -------------------------------------------------
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
with gr.Column(elem_id="container"):
|
| 65 |
gr.Markdown(
|
| 66 |
"""
|
|
|
|
| 71 |
|
| 72 |
chatbot = gr.Chatbot(
|
| 73 |
height=420,
|
|
|
|
| 74 |
show_label=False
|
| 75 |
)
|
| 76 |
|
|
|
|
| 85 |
with gr.Row():
|
| 86 |
clear_btn = gr.Button("Clear Chat", variant="secondary")
|
| 87 |
|
|
|
|
| 88 |
# Interactions
|
|
|
|
| 89 |
send_btn.click(chat, [user_input, chatbot], [chatbot, user_input])
|
| 90 |
user_input.submit(chat, [user_input, chatbot], [chatbot, user_input])
|
| 91 |
clear_btn.click(lambda: [], None, chatbot)
|
| 92 |
|
| 93 |
+
# -------------------------------------------------
|
| 94 |
+
# Launch (theme & css moved here for Gradio 6)
|
| 95 |
+
# -------------------------------------------------
|
| 96 |
+
demo.launch(theme=theme, css=custom_css)
|