chatbot / app.py
SajidMajeed's picture
Update app.py
7df0a57 verified
Raw
History Blame Contribute Delete
1.86 kB
import os
import gradio as gr
from groq import Groq
# Read API key from Hugging Face Space secret
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
if not GROQ_API_KEY:
raise ValueError(
"GROQ_API_KEY is not set. "
"Go to your Space Settings -> Variables and secrets -> add it as a secret."
)
# Initialize Groq client
client = Groq(api_key=GROQ_API_KEY)
# You can change this later if needed
DEFAULT_MODEL = os.environ.get("GROQ_MODEL", "llama-3.1-8b-instant")
def chat_with_groq(message, history):
"""
Gradio passes:
- message: latest user input (str)
- history: list of [user, bot] pairs
"""
try:
system_prompt = "You are a helpful AI assistant for Sajid. Answer clearly and simply."
messages = [{"role": "system", "content": system_prompt}]
# Add previous messages
for user_msg, bot_msg in history:
messages.append({"role": "user", "content": user_msg})
if bot_msg:
messages.append({"role": "assistant", "content": bot_msg})
# Add latest user message
messages.append({"role": "user", "content": message})
response = client.chat.completions.create(
model=DEFAULT_MODEL,
messages=messages,
max_tokens=512,
temperature=0.7,
)
reply = response.choices[0].message.content
return reply
except Exception as e:
# Show error inside the UI, useful if model name is wrong, etc.
return f"⚠️ Error from Groq API: {e}"
demo = gr.ChatInterface(
fn=chat_with_groq,
title="Sajid's GenAI Chat (Groq + Hugging Face Space)",
description=(
"A simple LLM chat app using Groq's API, deployed on Hugging Face Spaces. "
"Powered by Gradio."
),
)
if __name__ == "__main__":
demo.launch()