import requests import gradio as gr import os # Get Groq API Key from Hugging Face Secrets (best practice) GROQ_API_KEY = os.getenv("APIKEY") # If you are testing locally, you can uncomment this line and hardcode your key (NOT recommended on Spaces) # GROQ_API_KEY = "your-groq-api-key-here" # Define the chatbot function def chat_with_groq(message): url = "https://api.groq.com/openai/v1/chat/completions" headers = { "Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json" } payload = { "model": "llama3-70b-8192", "messages": [ {"role": "system", "content": "You are a helpful and friendly chatbot."}, {"role": "user", "content": message} ] } response = requests.post(url, headers=headers, json=payload) if response.status_code == 200: result = response.json() return result['choices'][0]['message']['content'] else: return f"Error: {response.status_code}, {response.text}" # Create Gradio interface iface = gr.Interface( fn=chat_with_groq, inputs="text", outputs="text", title="Groq Chatbot", description="A simple chatbot powered by Groq API. Type your message and get a reply!", theme="default" ) # Launch interface if __name__ == "__main__": iface.launch()