Spaces:
Runtime error
Runtime error
import gradio as gr | |
import openai | |
import os | |
# Set the base URL for the Groq API | |
openai.api_base = "https://api.groq.com/openai/v1" | |
openai.api_key = os.getenv("GROQ_API_KEY") # Fetch API key from environment variable | |
def chat_function(message): | |
# Define the request payload | |
payload = { | |
"model": "mixtral-8x7b-32768", # Specify your model here (e.g., Groq's supported model) | |
"messages": [{"role": "user", "content": message}], | |
"max_tokens": 150, | |
"temperature": 1 | |
} | |
try: | |
# Send the request to the Groq API and get the response | |
response = openai.ChatCompletion.create(**payload) | |
return response["choices"][0]["message"]["content"] | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Set up the Gradio interface | |
gr.Interface( | |
fn=chat_function, # The function that handles user input | |
inputs=gr.Textbox(placeholder="Ask something..."), # User input text box | |
outputs="text", # Output text box for the response | |
title="Groq-Gradio Chat", # Interface title | |
theme="huggingface", # Use a valid Gradio theme | |
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" | |
] # Example prompts | |
).launch() | |