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()