Download app.py from Maryam587/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/Maryam587/chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Maryam587/chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Maryam587/chatbot/resolve/main/app.py
1.04 kB
| import gradio as gr | |
| from groq import Groq | |
| import os | |
| # 🔐 Set your Groq API key here OR use Hugging Face Secrets | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| # Initialize client | |
| client = Groq(api_key=GROQ_API_KEY) | |
| # Chat function | |
| def chatbot(message, history): | |
| 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}) | |
| # Call Groq API | |
| response = client.chat.completions.create( | |
| model="llama3-70b-8192", # fast + powerful | |
| messages=messages, | |
| temperature=0.7, | |
| max_tokens=512, | |
| ) | |
| reply = response.choices[0].message.content | |
| return reply | |
| # Gradio UI | |
| demo = gr.ChatInterface( | |
| fn=chatbot, | |
| title="💬 AI Chatbot (Groq + Hugging Face)", | |
| description="Ask anything! Powered by Groq API ⚡", | |
| ) | |
| # Run app | |
| if __name__ == "__main__": | |
| demo.launch() |