Affan8 commited on
Commit
efac976
·
verified ·
1 Parent(s): bd2ae9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -3,10 +3,10 @@ import gradio as gr
3
  from groq import Groq
4
 
5
  # Read API key from Hugging Face Space secret
6
- api_key = userdata.get('GROQ_API')
7
 
8
  if not api_key:
9
- raise ValueError(" No Groq API key found in Colab Secrets. Please add it first.")
10
 
11
  # Initialize Groq client
12
  client = Groq(api_key=api_key)
@@ -15,44 +15,45 @@ models = client.models.list()
15
 
16
  print("=== AVAILABLE MODELS ===\n")
17
  for m in models.data:
18
- print("-", m.id)
19
 
20
  # You can change this later if needed
21
  MODEL_NAME = "openai/gpt-oss-safeguard-20b"
22
 
23
-
24
  def chat_groq(message, history):
25
- """
26
- message: latest user message (string)
27
- history: list of [user,bot] pairs from Gradio
28
- """
29
- try:
30
- system_prompt = "You are a helpful AI assistant."
31
-
32
- messages = [{"role": "system", "content": system_prompt}]
33
-
34
- for user_msg, bot_msg in history:
35
- messages.append({"role": "user", "content": user_msg})
36
- if bot_msg:
37
- messages.append({"role": "user", "content": message})
38
 
39
- completion =client.chat.completions.create(
40
- model= MODEL_NAME,
41
- messages=messages,
42
- max_tokens=1024,
43
- temperature=0.7
44
- )
 
45
 
46
- reply = completion.choices[0].message.content
47
- return reply
 
 
 
 
48
 
49
- except Exception as e:
50
- return f"Error: {e}"
51
 
 
 
52
 
53
  ui = gr.ChatInterface(
54
  fn=chat_groq,
55
- title="Affan's GenAI App(Groq)"
56
  )
57
 
58
- ui.launch()
 
3
  from groq import Groq
4
 
5
  # Read API key from Hugging Face Space secret
6
+ api_key = os.environ.get("GROQ_API")
7
 
8
  if not api_key:
9
+ raise ValueError("No Groq API key found. Please add it as a secret.")
10
 
11
  # Initialize Groq client
12
  client = Groq(api_key=api_key)
 
15
 
16
  print("=== AVAILABLE MODELS ===\n")
17
  for m in models.data:
18
+ print("-", m.id)
19
 
20
  # You can change this later if needed
21
  MODEL_NAME = "openai/gpt-oss-safeguard-20b"
22
 
 
23
  def chat_groq(message, history):
24
+ """
25
+ message: latest user message (string)
26
+ history: list of [user,bot] pairs from Gradio
27
+ """
28
+ try:
29
+ system_prompt = "You are a helpful AI assistant."
30
+
31
+ messages = [{"role": "system", "content": system_prompt}]
 
 
 
 
 
32
 
33
+ for user_msg, bot_msg in history:
34
+ messages.append({"role": "user", "content": user_msg})
35
+ if bot_msg:
36
+ messages.append({"role": "assistant", "content": bot_msg})
37
+
38
+ # Add latest user message
39
+ messages.append({"role": "user", "content": message})
40
 
41
+ completion = client.chat.completions.create(
42
+ model=MODEL_NAME,
43
+ messages=messages,
44
+ max_tokens=1024,
45
+ temperature=0.7
46
+ )
47
 
48
+ reply = completion.choices[0].message["content"]
49
+ return reply
50
 
51
+ except Exception as e:
52
+ return f"Error: {e}"
53
 
54
  ui = gr.ChatInterface(
55
  fn=chat_groq,
56
+ title="Affan's GenAI App (Groq)"
57
  )
58
 
59
+ ui.launch()