mcwm04 commited on
Commit
fb090fb
·
verified ·
1 Parent(s): ac4b6b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -40
app.py CHANGED
@@ -6,38 +6,26 @@ from groq import Groq
6
  # Environment Variable Check
7
  # -----------------------------
8
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
9
-
10
  if not GROQ_API_KEY:
11
- raise RuntimeError(
12
- "GROQ_API_KEY environment variable not set. "
13
- "Please set it before running the app."
14
- )
15
 
16
  # -----------------------------
17
  # Initialize Groq Client
18
  # -----------------------------
19
  client = Groq(api_key=GROQ_API_KEY)
20
-
21
  MODEL_NAME = "llama-3.3-70b-versatile"
22
 
23
  # -----------------------------
24
  # Chat Function
25
  # -----------------------------
26
  def chat_fn(message, history):
27
- if history is None:
28
- history = []
 
 
29
 
30
- # Build Groq messages from history
31
- messages = []
32
- for item in history:
33
- # Ensure tuple unpacking safety
34
- if not isinstance(item, (list, tuple)) or len(item) != 2:
35
- continue
36
- user_msg, bot_msg = item
37
- messages.append({"role": "user", "content": str(user_msg)})
38
- messages.append({"role": "assistant", "content": str(bot_msg)})
39
-
40
- messages.append({"role": "user", "content": str(message)})
41
 
42
  try:
43
  response = client.chat.completions.create(
@@ -45,22 +33,11 @@ def chat_fn(message, history):
45
  messages=messages,
46
  )
47
 
48
- # 🔒 Force string, never None
49
  reply = response.choices[0].message.content
50
- if reply is None:
51
- reply = ""
52
-
53
- reply = str(reply)
54
-
55
- # 🔒 Append strictly valid tuple
56
- history = history + [(str(message), reply)]
57
-
58
- return reply, history
59
 
60
  except Exception as e:
61
- err_msg = f"⚠️ Error: {str(e)}"
62
- history = history + [(str(message), err_msg)]
63
- return err_msg, history
64
 
65
  # -----------------------------
66
  # Gradio UI
@@ -68,17 +45,11 @@ def chat_fn(message, history):
68
  chat_ui = gr.ChatInterface(
69
  fn=chat_fn,
70
  title="Groq-Powered Chatbot",
71
- description="A simple chatbot built with Gradio and Groq LLMs.",
72
- examples=[
73
- "What is machine learning?",
74
- "Explain recursion in simple terms",
75
- "How does a neural network work?"
76
- ],
77
  )
78
 
79
  # -----------------------------
80
- # Launch App
81
  # -----------------------------
82
  if __name__ == "__main__":
83
  chat_ui.launch()
84
-
 
6
  # Environment Variable Check
7
  # -----------------------------
8
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
 
9
  if not GROQ_API_KEY:
10
+ raise RuntimeError("GROQ_API_KEY environment variable not set.")
 
 
 
11
 
12
  # -----------------------------
13
  # Initialize Groq Client
14
  # -----------------------------
15
  client = Groq(api_key=GROQ_API_KEY)
 
16
  MODEL_NAME = "llama-3.3-70b-versatile"
17
 
18
  # -----------------------------
19
  # Chat Function
20
  # -----------------------------
21
  def chat_fn(message, history):
22
+ """
23
+ message: str
24
+ history: list of {'role': 'user'|'assistant', 'content': str}
25
+ """
26
 
27
+ # Gradio already provides history in OpenAI/Groq format
28
+ messages = history + [{"role": "user", "content": message}]
 
 
 
 
 
 
 
 
 
29
 
30
  try:
31
  response = client.chat.completions.create(
 
33
  messages=messages,
34
  )
35
 
 
36
  reply = response.choices[0].message.content
37
+ return reply or ""
 
 
 
 
 
 
 
 
38
 
39
  except Exception as e:
40
+ return f"⚠️ Error: {str(e)}"
 
 
41
 
42
  # -----------------------------
43
  # Gradio UI
 
45
  chat_ui = gr.ChatInterface(
46
  fn=chat_fn,
47
  title="Groq-Powered Chatbot",
48
+ description="A simple chatbot using Gradio + Groq LLMs",
 
 
 
 
 
49
  )
50
 
51
  # -----------------------------
52
+ # Launch
53
  # -----------------------------
54
  if __name__ == "__main__":
55
  chat_ui.launch()