Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,10 @@ import gradio as gr
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# Read API key from Hugging Face Space secret
|
| 6 |
-
api_key =
|
| 7 |
|
| 8 |
if not api_key:
|
| 9 |
-
|
| 10 |
|
| 11 |
# Initialize Groq client
|
| 12 |
client = Groq(api_key=api_key)
|
|
@@ -15,44 +15,45 @@ models = client.models.list()
|
|
| 15 |
|
| 16 |
print("=== AVAILABLE MODELS ===\n")
|
| 17 |
for m in models.data:
|
| 18 |
-
|
| 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 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
|
|
|
|
|
|
| 52 |
|
| 53 |
ui = gr.ChatInterface(
|
| 54 |
fn=chat_groq,
|
| 55 |
-
title="Affan's GenAI App(Groq)"
|
| 56 |
)
|
| 57 |
|
| 58 |
-
ui.launch()
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 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)
|
|
|
|
| 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)
|
| 26 |
+
history: list of [user,bot] pairs from Gradio
|
| 27 |
+
"""
|
| 28 |
+
try:
|
| 29 |
+
system_prompt = "You are a helpful AI assistant."
|
| 30 |
+
|
| 31 |
+
messages = [{"role": "system", "content": system_prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 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 |
+
completion = client.chat.completions.create(
|
| 42 |
+
model=MODEL_NAME,
|
| 43 |
+
messages=messages,
|
| 44 |
+
max_tokens=1024,
|
| 45 |
+
temperature=0.7
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
reply = completion.choices[0].message["content"]
|
| 49 |
+
return reply
|
| 50 |
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Error: {e}"
|
| 53 |
|
| 54 |
ui = gr.ChatInterface(
|
| 55 |
fn=chat_groq,
|
| 56 |
+
title="Affan's GenAI App (Groq)"
|
| 57 |
)
|
| 58 |
|
| 59 |
+
ui.launch()
|