usamaalam commited on
Commit
b4c95f4
·
verified ·
1 Parent(s): d21d216

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -9
app.py CHANGED
@@ -2,30 +2,60 @@ import os
2
  import gradio as gr
3
  from groq import Groq
4
 
5
- # Read API key from environment variable on Hugging Face Spaces
6
  api_key = os.getenv("GROQ_API_KEY")
7
-
8
  if not api_key:
9
- raise ValueError("GROQ_API_KEY not found. Please add it in Hugging Face Space Secrets.")
10
 
11
  client = Groq(api_key=api_key)
12
  MODEL_NAME = "llama-3.1-8b-instant"
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def chat_groq(message, history):
15
  try:
16
- system_prompt = "You are a helpful AI assistant."
17
 
 
18
  messages = [{"role": "system", "content": system_prompt}]
19
 
20
- # Previous chat history
21
- for user_msg, bot_msg in history:
22
  messages.append({"role": "user", "content": user_msg})
23
  if bot_msg:
24
  messages.append({"role": "assistant", "content": bot_msg})
25
 
26
- # Latest message
27
  messages.append({"role": "user", "content": message})
28
 
 
29
  completion = client.chat.completions.create(
30
  model=MODEL_NAME,
31
  messages=messages,
@@ -33,12 +63,12 @@ def chat_groq(message, history):
33
  temperature=0.7,
34
  )
35
 
36
- reply = completion.choices[0].message.content
37
- return reply
38
 
39
  except Exception as e:
40
  return f"Error: {e}"
41
 
 
42
  ui = gr.ChatInterface(
43
  fn=chat_groq,
44
  title="Sajid's GenAI App (Groq)",
 
2
  import gradio as gr
3
  from groq import Groq
4
 
 
5
  api_key = os.getenv("GROQ_API_KEY")
 
6
  if not api_key:
7
+ raise ValueError("GROQ_API_KEY not found in environment variables.")
8
 
9
  client = Groq(api_key=api_key)
10
  MODEL_NAME = "llama-3.1-8b-instant"
11
 
12
+ def normalize_history(history):
13
+ """
14
+ Convert Gradio history to (user, assistant) pairs.
15
+ Handles both:
16
+ - [["hi", "hello"], ["ok", "sure"]]
17
+ - [{"role":"user","content":"hi"}, {"role":"assistant","content":"hello"}, ...]
18
+ """
19
+ normalized = []
20
+
21
+ if not history:
22
+ return normalized
23
+
24
+ if isinstance(history[0], list):
25
+ # Format: [["hi", "hello"], ["ok", "sure"]]
26
+ return history
27
+
28
+ # Format: [{"role": "...", "content": "..."}]
29
+ temp_pair = []
30
+ for msg in history:
31
+ if msg["role"] == "user":
32
+ temp_pair = [msg["content"], None]
33
+ elif msg["role"] == "assistant":
34
+ if temp_pair:
35
+ temp_pair[1] = msg["content"]
36
+ normalized.append(temp_pair)
37
+ temp_pair = []
38
+
39
+ return normalized
40
+
41
+
42
  def chat_groq(message, history):
43
  try:
44
+ history_pairs = normalize_history(history)
45
 
46
+ system_prompt = "You are a helpful AI assistant."
47
  messages = [{"role": "system", "content": system_prompt}]
48
 
49
+ # Add normalized history
50
+ for user_msg, bot_msg in history_pairs:
51
  messages.append({"role": "user", "content": user_msg})
52
  if bot_msg:
53
  messages.append({"role": "assistant", "content": bot_msg})
54
 
55
+ # Add latest message
56
  messages.append({"role": "user", "content": message})
57
 
58
+ # Groq API
59
  completion = client.chat.completions.create(
60
  model=MODEL_NAME,
61
  messages=messages,
 
63
  temperature=0.7,
64
  )
65
 
66
+ return completion.choices[0].message.content
 
67
 
68
  except Exception as e:
69
  return f"Error: {e}"
70
 
71
+
72
  ui = gr.ChatInterface(
73
  fn=chat_groq,
74
  title="Sajid's GenAI App (Groq)",