Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import json | |
# Function to interact with the Groq API using the curl request approach | |
def chat_function(message): | |
# Define the request payload | |
payload = { | |
"model": "mixtral-8x7b-32768", | |
"messages": [ | |
{"role": "user", "content": message} | |
] | |
} | |
# Set the headers including your API key | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer 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" # Replace with your API key | |
} | |
try: | |
# Make the POST request to the Groq API | |
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, data=json.dumps(payload)) | |
# Check if the request was successful | |
if response.status_code == 200: | |
data = response.json() | |
return data["choices"][0]["message"]["content"] # Return the model's response | |
else: | |
return f"Error: {response.status_code}, {response.text}" | |
except requests.exceptions.RequestException as e: | |
return f"Error: {e}" | |
# Set up Gradio interface | |
gr.Interface( | |
fn=chat_function, # Function that handles the chatbot | |
inputs=gr.Textbox(placeholder="Ask something..."), # Input for user message | |
outputs="text", # Output as text | |
title="Groq-Gradio Chat", # Title of the interface | |
theme="default", # Ensure theme is valid | |
examples=[ | |
"Tell me a short story about a puppy", | |
"Write a 14-line poem about travelling in Shakespeare style", | |
"What are the wonders of the world?", | |
"List the countries in Africa and their capitals" | |
] | |
).launch() |