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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ 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()