Spaces:
Runtime error
Runtime error
Download app.py from msamiather/chatBot: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/spaces/msamiather/chatBot/resolve/main/app.py
- Command line
-
hf download hf://spaces/msamiather/chatBot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/msamiather/chatBot/resolve/main/app.py
1.36 kB
| 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("<h1 style='text-align:center;'>🤖 AI Chatbot with Groq + LLaMA 3</h1>") | |
| 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() | |