Spaces:
Sleeping
Sleeping
File size: 1,764 Bytes
1741cc0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import os
import requests
import gradio as gr
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Get the Groq API key from environment variables
groq_api_key = os.environ.get("GROQ_API_KEY")
# Check if the Groq API key is set
if groq_api_key is None:
raise ValueError("Groq API key is not set in environment variables.")
# Define the URL for the Groq API endpoint
url = "https://api.groq.com/openai/v1/chat/completions"
# Function to interact with the Groq API
def groq_chat(prompt):
headers = {
"Authorization": f"Bearer {groq_api_key}"
}
body = {
"model": "llama-3.1-8b-instant",
"messages": [
{
"role": "user",
"content": prompt
}
]
}
# Send a POST request to the Groq API
response = requests.post(url, headers=headers, json=body)
if response.status_code == 200:
# Extract and return the content of the first message choice
return response.json().get('choices', [{}])[0].get('message', {}).get('content', "No response found.")
else:
# Return the error details
return f"Error {response.status_code}: {response.text}"
# Define the Gradio interface
with gr.Blocks() as interface:
gr.Markdown("# Chat with Groq API")
with gr.Row():
user_input = gr.Textbox(label="Enter your prompt", placeholder="Type something funny or interesting...")
with gr.Row():
output = gr.Textbox(label="Response from Groq API")
with gr.Row():
submit_button = gr.Button("Get Response")
submit_button.click(fn=groq_chat, inputs=user_input, outputs=output)
# Launch the interface
if __name__ == "__main__":
interface.launch()
|