aneesqumar commited on
Commit
9cb862b
·
verified ·
1 Parent(s): 46e3f18

Create model_service.py

Browse files
Files changed (1) hide show
  1. model_service.py +25 -0
model_service.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from groq import Groq
3
+
4
+ def get_client():
5
+ api_key = os.environ.get("GROQ_API_KEY")
6
+ if not api_key:
7
+ raise RuntimeError(
8
+ "GROQ_API_KEY is not set. Add it in Hugging Face Spaces → Settings → Secrets."
9
+ )
10
+ return Groq(api_key=api_key)
11
+
12
+ def ask_ai(question: str) -> str:
13
+ client = get_client()
14
+
15
+ messages = [
16
+ {"role": "system", "content": "You are a helpful AI assistant."},
17
+ {"role": "user", "content": question}
18
+ ]
19
+
20
+ completion = client.chat.completions.create(
21
+ model="llama-3.3-70b-versatile",
22
+ messages=messages
23
+ )
24
+
25
+ return completion.choices[0].message.content