Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from groq import Groq | |
| # --------------------------- | |
| # Configure API key | |
| # --------------------------- | |
| # In Hugging Face, set your API key as a secret: https://huggingface.co/settings/tokens | |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "") | |
| client = Groq(api_key=GROQ_API_KEY) | |
| # --------------------------- | |
| # Chatbot function | |
| # --------------------------- | |
| def chat_with_groq(message, history): | |
| """Handles chat interactions with the Groq API.""" | |
| messages = [] | |
| for user, bot in history: | |
| messages.append({"role": "user", "content": user}) | |
| messages.append({"role": "assistant", "content": bot}) | |
| messages.append({"role": "user", "content": message}) | |
| try: | |
| completion = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", # supported Groq model | |
| messages=messages | |
| ) | |
| reply = completion.choices[0].message.content | |
| except Exception as e: | |
| reply = f"(Error: {e})" | |
| return reply | |
| # --------------------------- | |
| # Build Gradio UI | |
| # --------------------------- | |
| chatbot = gr.ChatInterface( | |
| fn=chat_with_groq, | |
| title="🤖 Groq AI Chatbot", | |
| description="Chat with a Groq-powered model (Llama 3.1 8B Instant).", | |
| theme="soft", | |
| ) | |
| # Launch app | |
| if __name__ == "__main__": | |
| chatbot.launch() | |