Update app.py
Browse files
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 |
-
|
| 7 |
|
| 8 |
-
if not
|
| 9 |
-
|
| 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=
|
| 16 |
|
| 17 |
-
|
| 18 |
-
DEFAULT_MODEL = os.environ.get("GROQ_MODEL", "openai/gpt-oss-safeguard-20b")
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 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 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
messages.append({"role": "user", "content": message})
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return f"⚠ Error from Groq API: {e}"
|
| 54 |
|
| 55 |
|
| 56 |
-
|
| 57 |
-
fn=
|
| 58 |
-
title="
|
| 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 |
-
|
| 66 |
-
|
|
|
|
| 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 |
+
)
|