import os import asyncio import gradio as gr from groq import AsyncGroq # Changed to the asynchronous client
Secure API key loading
API_KEY = os.environ.get("GROQ_API_KEY", "YOUR_FALLBACK_KEY_HERE") client = AsyncGroq(api_key=API_KEY) # Initialized as AsyncGroq
Valid models available on Groq
MODELS = [ "llama-3.3-70b-versatile", "llama-3.1-8b-instant", "deepseek-r1-distill-llama-70b", "qwen/qwen3.6-27b" ]
Changed to an async generator function
async def chat_groq(message, history, model_name, system_prompt, temperature): messages = [{"role": "system", "content": system_prompt}]
# Add conversation history
for user_msg, assistant_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": assistant_msg})
# Add current user prompt
messages.append({"role": "user", "content": message})
try:
# Await the async streaming completion creation
response_stream = await client.chat.completions.create(
model=model_name,
messages=messages,
temperature=temperature,
max_tokens=2048,
stream=True
)
partial_response = ""
# Use async for to safely iterate over the asynchronous stream chunks
async for chunk in response_stream:
if chunk.choices and len(chunk.choices) > 0:
token = chunk.choices[0].delta.content or ""
partial_response += token
yield partial_response
except Exception as e:
yield f"An error occurred: {str(e)}"
with gr.Blocks(title="Groq AI Chatbot") as demo: gr.Markdown("## ⚡ Groq Ultra-Fast AI Assistant")
with gr.Row():
with gr.Column(scale=1):
model_selector = gr.Dropdown(
choices=MODELS,
value="llama-3.3-70b-versatile",
label="Select Groq Model"
)
temp_slider = gr.Slider(
minimum=0.0, maximum=1.0, value=0.7, step=0.1, label="Temperature"
)
system_box = gr.Textbox(
value="You are a helpful and concise AI assistant.",
label="System Prompt",
lines=3
)
with gr.Column(scale=3):
chatbot = gr.ChatInterface(
fn=chat_groq,
additional_inputs=[model_selector, system_box, temp_slider]
)
demo.launch(debug=True)
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support
Model tree for Jonnathan20202/AURRA
Base model
openai/gpt-oss-120b