Affan8 commited on
Commit
e493800
·
verified ·
1 Parent(s): a50704f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -44
app.py CHANGED
@@ -3,64 +3,57 @@ import gradio as gr
3
  from groq import Groq
4
 
5
  # Read API key from Hugging Face Space secret
6
- GROQ_API_KEY = os.environ.get("GROQ_API")
7
 
8
- if not GROQ_API_KEY:
9
- raise ValueError(
10
- "GROQ_API_KEY is not set. "
11
- "Go to your Space Settings -> Variables and secrets -> add it as a secret."
12
- )
13
 
14
  # Initialize Groq client
15
- client = Groq(api_key=GROQ_API_KEY)
16
 
17
- # You can change this later if needed
18
- DEFAULT_MODEL = os.environ.get("GROQ_MODEL", "openai/gpt-oss-safeguard-20b")
19
 
 
 
 
20
 
21
- def chat_with_groq(message, history):
22
- """
23
- Gradio passes:
24
- - message: latest user input (str)
25
- - history: list of [user, bot] pairs
26
- """
27
- try:
28
- system_prompt = "You are a helpful AI assistant for Sajid. Answer clearly and simply."
29
 
30
- messages = [{"role": "system", "content": system_prompt}]
31
 
32
- # Add previous messages
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
- response = client.chat.completions.create(
42
- model=DEFAULT_MODEL,
43
- messages=messages,
44
- max_tokens=512,
45
- temperature=0.7,
46
- )
47
 
48
- reply = response.choices[0].message.content
49
- return reply
50
 
51
- except Exception as e:
52
- # Show error inside the UI, useful if model name is wrong, etc.
53
- return f"⚠ Error from Groq API: {e}"
54
 
55
 
56
- demo = gr.ChatInterface(
57
- fn=chat_with_groq,
58
- title="Sajid's GenAI Chat (Groq + Hugging Face Space)",
59
- description=(
60
- "A simple LLM chat app using Groq's API, deployed on Hugging Face Spaces. "
61
- "Powered by Gradio."
62
- ),
63
  )
64
 
65
- if _name_ == "_main_":
66
- demo.launch()
 
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)
13
 
14
+ 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()
59
+ )