import os from groq import Groq import gradio as gr client = Groq(api_key=os.environ.get("GROQ_API_KEY")) # ✅ Convert history to correct format def convert_history(history): messages = [] for chat in history: user = chat[0] bot = chat[1] if user: messages.append({"role": "user", "content": user}) if bot: messages.append({"role": "assistant", "content": bot}) return messages def respond(message, history): if history is None: history = [] # Add user message history.append([message, None]) messages = convert_history(history) # Call Groq response = client.chat.completions.create( messages=messages, model="llama-3.3-70b-versatile" ) bot_reply = response.choices[0].message.content # Save bot reply history[-1][1] = bot_reply return history, history with gr.Blocks() as demo: gr.Markdown("## 🤖 Groq Chatbot (Fixed Final)") chatbot = gr.Chatbot() msg = gr.Textbox() clear = gr.Button("Clear") state = gr.State([]) msg.submit(respond, [msg, state], [chatbot, state]) clear.click(lambda: ([], []), None, [chatbot, state]) demo.launch()