import os import gradio as gr from groq import Groq # Get API key from Hugging Face Secrets GROQ_API_KEY = os.environ.get("Keypass") if GROQ_API_KEY is None: raise ValueError("GROQ_API_KEY not found. Add it in Hugging Face → Settings → Secrets.") client = Groq(api_key=GROQ_API_KEY) # Chat function def chat(message, history): try: messages = [] # Add previous conversation for user, bot in history: messages.append({"role": "user", "content": user}) messages.append({"role": "assistant", "content": bot}) # Add current message messages.append({"role": "user", "content": message}) response = client.chat.completions.create( model="llama3-70b-8192", messages=messages, temperature=0.7, max_tokens=512, ) reply = response.choices[0].message.content return reply except Exception as e: return f"Error: {str(e)}" # Gradio UI (Stable Version) with gr.Blocks() as demo: gr.Markdown("## 🚀 Groq AI Chatbot") gr.Markdown("Simple chatbot using Groq API + Gradio") chatbot = gr.Chatbot() msg = gr.Textbox(label="Your Message") clear = gr.Button("Clear Chat") def respond(message, chat_history): bot_message = chat(message, chat_history) chat_history.append((message, bot_message)) return "", chat_history msg.submit(respond, [msg, chatbot], [msg, chatbot]) clear.click(lambda: None, None, chatbot, queue=False) demo.launch()