Update app.py
Browse files
app.py
CHANGED
|
@@ -5,21 +5,6 @@ from groq import Groq
|
|
| 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)
|
| 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 |
def chat_groq(message, history):
|
| 24 |
"""
|
| 25 |
message: latest user message (string)
|
|
@@ -35,20 +20,18 @@ def chat_groq(message, history):
|
|
| 35 |
if bot_msg:
|
| 36 |
messages.append({"role": "user", "content": message})
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# Access the content attribute directly
|
| 46 |
-
reply = completion.choices[0].message.content
|
| 47 |
-
return reply
|
| 48 |
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
ui = gr.ChatInterface(
|
| 54 |
fn=chat_groq,
|
|
|
|
| 5 |
# Read API key from Hugging Face Space secret
|
| 6 |
api_key = os.environ.get("GROQ_API")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def chat_groq(message, history):
|
| 9 |
"""
|
| 10 |
message: latest user message (string)
|
|
|
|
| 20 |
if bot_msg:
|
| 21 |
messages.append({"role": "user", "content": message})
|
| 22 |
|
| 23 |
+
completion =client.chat.completions.create(
|
| 24 |
+
model= MODEL_NAME,
|
| 25 |
+
messages=messages,
|
| 26 |
+
max_tokens=1024,
|
| 27 |
+
temperature=0.7
|
| 28 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
reply = completion.choices[0].message.content
|
| 31 |
+
return reply
|
| 32 |
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error: {e}"
|
| 35 |
|
| 36 |
ui = gr.ChatInterface(
|
| 37 |
fn=chat_groq,
|