import gradio as gr from groq import Groq import os # Replace with your Groq API Key (or use HF Secrets on Spaces) GROQ_API_KEY = os.getenv("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") client = Groq(api_key=GROQ_API_KEY) MODEL = "llama3-8b-8192" chat_history = [] def chat(user_input, history): chat_history.append({"role": "user", "content": user_input}) response = client.chat.completions.create( model=MODEL, messages=chat_history, temperature=0.7 ) reply = response.choices[0].message.content chat_history.append({"role": "assistant", "content": reply}) history.append((user_input, reply)) return "", history def clear_chat(): global chat_history chat_history = [] return [], "" with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("

๐Ÿค– AI Chatbot with Groq + LLaMA 3

") chatbot = gr.Chatbot(label="Chat", bubble_full_width=False) msg = gr.Textbox(placeholder="Ask me anything...", show_label=False) with gr.Row(): send_btn = gr.Button("Send ๐Ÿš€", variant="primary") clear_btn = gr.Button("Clear ๐Ÿงน") send_btn.click(chat, [msg, chatbot], [msg, chatbot]) msg.submit(chat, [msg, chatbot], [msg, chatbot]) clear_btn.click(clear_chat, outputs=[chatbot, msg]) demo.launch()