Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| import gradio as gr | |
| # Get API key from environment variable | |
| API_KEY = os.environ.get("GROQ_API_KEY") | |
| API_URL = "https://api.groq.com/v1/chat/completions" # <-- FIXED | |
| # Function to call GROQ API | |
| def query_groq(prompt): | |
| headers = { | |
| "Authorization": f"Bearer {API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "model": "llama3-8b-8192", | |
| "messages": [ | |
| {"role": "system", "content": "You are an expert Programming Tutor."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| "temperature": 0.7 | |
| } | |
| response = requests.post(API_URL, headers=headers, json=data) | |
| if response.status_code == 200: | |
| return response.json()["choices"][0]["message"]["content"] | |
| else: | |
| return f"Error: {response.status_code} - {response.text}" | |
| # Gradio interface | |
| iface = gr.Interface(fn=query_groq, inputs="text", outputs="text", title="Groq Chat") | |
| iface.launch() |