File size: 1,744 Bytes
596a2f9 fb090fb 596a2f9 fb090fb 8f763ae fb090fb fe5c4df 8f763ae 596a2f9 8f763ae 596a2f9 fe5c4df ac4b6b4 fb090fb 596a2f9 fb090fb fe5c4df 8f763ae 596a2f9 fb090fb 596a2f9 fb090fb 596a2f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import os
import gradio as gr
from groq import Groq
# -----------------------------
# Environment Variable Check
# -----------------------------
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
if not GROQ_API_KEY:
raise RuntimeError("GROQ_API_KEY environment variable not set.")
# -----------------------------
# Initialize Groq Client
# -----------------------------
client = Groq(api_key=GROQ_API_KEY)
MODEL_NAME = "llama-3.3-70b-versatile"
# -----------------------------
# Chat Function
# -----------------------------
def chat_fn(message, history):
"""
message: str
history: list of dicts (from Gradio, includes metadata)
"""
# 🔒 Sanitize history for Groq (remove metadata)
messages = []
for item in history:
if (
isinstance(item, dict)
and "role" in item
and "content" in item
):
messages.append({
"role": item["role"],
"content": str(item["content"])
})
# Add current user message
messages.append({
"role": "user",
"content": str(message)
})
try:
response = client.chat.completions.create(
model=MODEL_NAME,
messages=messages
)
reply = response.choices[0].message.content
return reply or ""
except Exception as e:
return f"⚠️ Error: {str(e)}"
# -----------------------------
# Gradio UI
# -----------------------------
chat_ui = gr.ChatInterface(
fn=chat_fn,
title="Groq-Powered Chatbot",
description="A simple chatbot using Gradio + Groq LLMs",
)
# -----------------------------
# Launch
# -----------------------------
if __name__ == "__main__":
chat_ui.launch()
|