Chat_bot / app.py
Reju983's picture
Upload 2 files
1e9af20 verified
Raw History Blame Contribute Delete
2.19 kB
import os
import spaces
import gradio as gr
from dotenv import load_dotenv
from groq import Groq
# Load environment variables
load_dotenv()
# Get Groq API key
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
raise ValueError("GROQ_API_KEY is not set.")
# Create Groq client
client = Groq(api_key=api_key)
# --------------------------------------------------
# ZeroGPU startup compatibility
# --------------------------------------------------
@spaces.GPU(duration=1)
def _zerogpu_startup_probe():
"""
This function exists only to satisfy
Hugging Face ZeroGPU's startup detector.
It is never called.
"""
return None
# --------------------------------------------------
# Chatbot function
# --------------------------------------------------
def chatbot(message, history):
messages = [
{
"role": "system",
"content": "You are a helpful and friendly AI assistant."
}
]
# Add previous conversation
for item in history:
messages.append({
"role": item["role"],
"content": item["content"]
})
# Add current user message
messages.append({
"role": "user",
"content": message
})
# Call Groq
response = client.chat.completions.create(
model="openai/gpt-oss-20b",
messages=messages,
temperature=0.7,
max_tokens=500
)
return response.choices[0].message.content
# --------------------------------------------------
# Gradio UI
# --------------------------------------------------
demo = gr.ChatInterface(
fn=chatbot,
title="🤖 Groq AI Chatbot",
description="Simple chatbot powered by Groq",
examples=[
"What is artificial intelligence?",
"Explain Python in simple words",
"What is machine learning?"
]
)
# --------------------------------------------------
# Start application
# --------------------------------------------------
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860
)