shanzaejaz commited on
Commit
0698a9b
·
verified ·
1 Parent(s): 0955e0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -3,11 +3,14 @@ from groq import Groq
3
  import os
4
 
5
  # -----------------------------
6
- # GROQ CLIENT
7
  # -----------------------------
8
- # Make sure you add your Groq API key as a "Secret" on HF Spaces:
9
- # Go to Settings -> Secrets -> Add "GROQ_API_KEY"
10
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
 
 
11
 
12
  # -----------------------------
13
  # CHAT FUNCTION
@@ -16,35 +19,36 @@ def chat_with_ai(message, history):
16
  if history is None:
17
  history = []
18
 
19
- # Convert history into proper format for Groq
20
- messages = [
21
- {"role": "system", "content": "You are a helpful AI assistant."}
22
- ]
23
 
 
 
24
  for user, bot in history:
25
  messages.append({"role": "user", "content": user})
26
  messages.append({"role": "assistant", "content": bot})
27
-
28
  messages.append({"role": "user", "content": message})
29
 
30
- # Call Groq
31
- response = client.chat.completions.create(
32
- model="llama-3.3-70b-versatile", # latest working model
33
- messages=messages,
34
- temperature=0.7,
35
- max_tokens=1024
36
- )
37
-
38
- reply = response.choices[0].message.content
 
39
 
40
  history.append((message, reply))
41
- # Return updated history for chatbot AND empty string for textbox
42
  return history, history, ""
43
 
44
  # -----------------------------
45
  # GRADIO UI
46
  # -----------------------------
47
- with gr.Blocks(title="AI Chatbot") as app:
48
  gr.Markdown("# 🤖 Simple Groq Chatbot")
49
 
50
  chatbot = gr.Chatbot(height=400)
@@ -53,7 +57,7 @@ with gr.Blocks(title="AI Chatbot") as app:
53
 
54
  state = gr.State([])
55
 
56
- # IMPORTANT: add msg as OUTPUT to clear it
57
  msg.submit(chat_with_ai, [msg, state], [chatbot, state, msg])
58
  clear.click(lambda: ([], [], ""), None, [chatbot, state, msg])
59
 
 
3
  import os
4
 
5
  # -----------------------------
6
+ # GROQ CLIENT SETUP
7
  # -----------------------------
8
+ api_key = os.environ.get("GROQ_API_KEY")
9
+
10
+ if api_key:
11
+ client = Groq(api_key=api_key)
12
+ else:
13
+ client = None
14
 
15
  # -----------------------------
16
  # CHAT FUNCTION
 
19
  if history is None:
20
  history = []
21
 
22
+ if client is None:
23
+ reply = "❌ Groq API key not found. Please add it in Settings → Secrets → GROQ_API_KEY."
24
+ history.append((message, reply))
25
+ return history, history, ""
26
 
27
+ # Prepare messages for Groq
28
+ messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
29
  for user, bot in history:
30
  messages.append({"role": "user", "content": user})
31
  messages.append({"role": "assistant", "content": bot})
 
32
  messages.append({"role": "user", "content": message})
33
 
34
+ try:
35
+ response = client.chat.completions.create(
36
+ model="llama-3.3-70b-versatile",
37
+ messages=messages,
38
+ temperature=0.7,
39
+ max_tokens=1024
40
+ )
41
+ reply = response.choices[0].message.content
42
+ except Exception as e:
43
+ reply = f"❌ Error from Groq API: {str(e)}"
44
 
45
  history.append((message, reply))
 
46
  return history, history, ""
47
 
48
  # -----------------------------
49
  # GRADIO UI
50
  # -----------------------------
51
+ with gr.Blocks(title="Simple Groq Chatbot") as app:
52
  gr.Markdown("# 🤖 Simple Groq Chatbot")
53
 
54
  chatbot = gr.Chatbot(height=400)
 
57
 
58
  state = gr.State([])
59
 
60
+ # Submit message
61
  msg.submit(chat_with_ai, [msg, state], [chatbot, state, msg])
62
  clear.click(lambda: ([], [], ""), None, [chatbot, state, msg])
63