Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,73 @@
|
|
| 1 |
import os
|
| 2 |
-
import requests
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
# ENVIRONMENT
|
| 7 |
-
# -----------------------------
|
| 8 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
if not GROQ_API_KEY:
|
| 10 |
-
raise
|
| 11 |
|
| 12 |
-
|
| 13 |
-
TEXT_MODEL = "llama-3.1-8b-instant"
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
# MODE PROMPTS
|
| 17 |
-
# -----------------------------
|
| 18 |
MODE_PROMPTS = {
|
| 19 |
-
"Normal": "You are NeoHelper,
|
| 20 |
-
"School": "You are NeoHelper,
|
| 21 |
-
"Shopping": "You are NeoHelper, a shopping assistant
|
| 22 |
-
"Gaming": "You are NeoHelper,
|
| 23 |
}
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
with gr.Blocks(title="NeoHelper (Text Only)") as demo:
|
| 72 |
-
gr.Markdown("# **NeoHelper — Text‑Only (Groq)**")
|
| 73 |
-
|
| 74 |
-
with gr.Row():
|
| 75 |
-
with gr.Column(scale=3):
|
| 76 |
-
chatbot = gr.Chatbot(height=520, label="NeoHelper")
|
| 77 |
-
|
| 78 |
-
with gr.Row():
|
| 79 |
-
user_input = gr.Textbox(
|
| 80 |
-
placeholder="Type your message…",
|
| 81 |
-
lines=3,
|
| 82 |
-
show_label=False,
|
| 83 |
-
)
|
| 84 |
-
send_btn = gr.Button("Send")
|
| 85 |
-
|
| 86 |
-
clear_btn = gr.Button("Clear chat")
|
| 87 |
-
|
| 88 |
-
with gr.Column(scale=1):
|
| 89 |
-
mode = gr.Radio(
|
| 90 |
-
["Normal", "School", "Shopping", "Gaming"],
|
| 91 |
-
value="Normal",
|
| 92 |
-
label="Mode",
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
def on_send(msg, history, mode):
|
| 96 |
-
history = history or []
|
| 97 |
-
reply = groq_chat(msg, history, mode)
|
| 98 |
-
history.append([msg, reply])
|
| 99 |
-
return history, ""
|
| 100 |
-
|
| 101 |
-
send_btn.click(
|
| 102 |
-
on_send,
|
| 103 |
-
inputs=[user_input, chatbot, mode],
|
| 104 |
-
outputs=[chatbot, user_input],
|
| 105 |
)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
app = build_ui()
|
| 113 |
|
| 114 |
-
|
| 115 |
-
app.launch(server_name="0.0.0.0", server_port=7860, theme="gradio/soft")
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
|
| 5 |
+
# ===== Setup =====
|
|
|
|
|
|
|
| 6 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
if not GROQ_API_KEY:
|
| 8 |
+
raise RuntimeError("⚠️ GROQ_API_KEY is missing! Add it in Hugging Face → Settings → Secrets.")
|
| 9 |
|
| 10 |
+
client = Groq(api_key=GROQ_API_KEY)
|
|
|
|
| 11 |
|
| 12 |
+
# Mode-specific system prompts
|
|
|
|
|
|
|
| 13 |
MODE_PROMPTS = {
|
| 14 |
+
"Normal Chat": "You are NeoHelper, Eyad’s branded assistant. Be concise, friendly, and always respond in English.",
|
| 15 |
+
"School Helper": "You are NeoHelper, a school helper. Explain clearly, step-by-step, in simple English. Keep it encouraging.",
|
| 16 |
+
"Shopping Assistant": "You are NeoHelper, a shopping assistant for Saudi Arabia. Compare specs and value, be concise, and respond in English.",
|
| 17 |
+
"Gaming Help": "You are NeoHelper, an energetic gaming helper. Provide practical tips, fixes, and short steps. Respond in English."
|
| 18 |
}
|
| 19 |
|
| 20 |
+
DEFAULT_MODEL = "llama-3.1-8b-instant"
|
| 21 |
+
|
| 22 |
+
# ===== Core chat function =====
|
| 23 |
+
def chat_fn(message, history, mode="Normal Chat", model=DEFAULT_MODEL):
|
| 24 |
+
try:
|
| 25 |
+
# Build messages list from history
|
| 26 |
+
messages = [{"role": "system", "content": MODE_PROMPTS.get(mode, MODE_PROMPTS["Normal Chat"])}]
|
| 27 |
+
for user_msg, assistant_msg in history:
|
| 28 |
+
if user_msg:
|
| 29 |
+
messages.append({"role": "user", "content": user_msg})
|
| 30 |
+
if assistant_msg:
|
| 31 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 32 |
+
|
| 33 |
+
# Add current user message
|
| 34 |
+
messages.append({"role": "user", "content": message})
|
| 35 |
+
|
| 36 |
+
# Call Groq API
|
| 37 |
+
response = client.chat.completions.create(
|
| 38 |
+
model=model,
|
| 39 |
+
messages=messages
|
| 40 |
+
)
|
| 41 |
+
assistant_response = response.choices[0].message.content
|
| 42 |
+
return assistant_response
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"⚠️ Error: {str(e)}"
|
| 45 |
+
|
| 46 |
+
# ===== UI =====
|
| 47 |
+
with gr.Blocks(title="🦙 NeoHelper — Eyad’s Groq-powered Assistant") as demo:
|
| 48 |
+
gr.Markdown("## 🦙 NeoHelper — Eyad’s Groq-powered Assistant")
|
| 49 |
+
gr.Markdown("Modes: School Helper, Shopping Assistant, Gaming Help, Normal Chat (English only).")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
mode_dd = gr.Dropdown(
|
| 53 |
+
choices=list(MODE_PROMPTS.keys()),
|
| 54 |
+
value="Normal Chat",
|
| 55 |
+
label="Mode"
|
| 56 |
+
)
|
| 57 |
+
model_dd = gr.Dropdown(
|
| 58 |
+
choices=[
|
| 59 |
+
"llama-3.1-8b-instant",
|
| 60 |
+
"llama-3.1-70b-versatile",
|
| 61 |
+
"mixtral-8x7b-instruct",
|
| 62 |
+
"gemma2-9b-it"
|
| 63 |
+
],
|
| 64 |
+
value=DEFAULT_MODEL,
|
| 65 |
+
label="Model"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
+
chat = gr.ChatInterface(
|
| 69 |
+
fn=lambda msg, hist: chat_fn(msg, hist, mode_dd.value, model_dd.value),
|
| 70 |
+
title=None
|
| 71 |
+
)
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
demo.launch()
|
|
|