Eyadddddddd commited on
Commit
8ba7726
·
verified ·
1 Parent(s): a9e9f1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -102
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 ValueError("GROQ_API_KEY is not set.")
11
 
12
- GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
13
- TEXT_MODEL = "llama-3.1-8b-instant"
14
 
15
- # -----------------------------
16
- # MODE PROMPTS
17
- # -----------------------------
18
  MODE_PROMPTS = {
19
- "Normal": "You are NeoHelper, a helpful, concise assistant.",
20
- "School": "You are NeoHelper, explaining clearly for a Grade 5 student.",
21
- "Shopping": "You are NeoHelper, a shopping assistant in Saudi Arabia.",
22
- "Gaming": "You are NeoHelper, a practical gaming helper.",
23
  }
24
 
25
- # -----------------------------
26
- # FIXED GROQ CHAT FUNCTION
27
- # -----------------------------
28
- def groq_chat(user_msg, history, mode):
29
- system_prompt = MODE_PROMPTS.get(mode, MODE_PROMPTS["Normal"])
30
-
31
- # Start with system message
32
- messages = [{"role": "system", "content": system_prompt}]
33
-
34
- # Sanitize history to avoid Groq format errors
35
- if history:
36
- for pair in history:
37
- if isinstance(pair, list) and len(pair) == 2:
38
- user, bot = pair
39
- if isinstance(user, str) and user.strip():
40
- messages.append({"role": "user", "content": user.strip()})
41
- if isinstance(bot, str) and bot.strip():
42
- messages.append({"role": "assistant", "content": bot.strip()})
43
-
44
- # Add current user message
45
- if user_msg and user_msg.strip():
46
- messages.append({"role": "user", "content": user_msg.strip()})
47
- else:
48
- return "Please type something."
49
-
50
- payload = {
51
- "model": TEXT_MODEL,
52
- "messages": messages,
53
- "max_tokens": 400,
54
- "temperature": 0.4,
55
- }
56
-
57
- headers = {
58
- "Authorization": f"Bearer {GROQ_API_KEY}",
59
- "Content-Type": "application/json",
60
- }
61
-
62
- resp = requests.post(GROQ_URL, json=payload, headers=headers, timeout=30)
63
- resp.raise_for_status()
64
- data = resp.json()
65
- return data["choices"][0]["message"]["content"]
66
-
67
- # -----------------------------
68
- # GRADIO UI
69
- # -----------------------------
70
- def build_ui():
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
- clear_btn.click(lambda: ([], ""), None, [chatbot, user_input])
108
-
109
- return demo
110
-
111
-
112
- app = build_ui()
113
 
114
- if __name__ == "__main__":
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()