Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
# Set the base URL for the Groq API
|
6 |
-
openai.api_base = "https://api.groq.com/openai/v1"
|
7 |
-
openai.api_key = os.getenv("GROQ_API_KEY") # Fetch API key from environment variable
|
8 |
|
|
|
9 |
def chat_function(message):
|
10 |
# Define the request payload
|
11 |
payload = {
|
12 |
-
"model": "mixtral-8x7b-32768",
|
13 |
-
"messages": [
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
}
|
17 |
-
|
18 |
try:
|
19 |
-
#
|
20 |
-
response = openai.
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
# Set up
|
27 |
gr.Interface(
|
28 |
-
fn=chat_function, #
|
29 |
-
inputs=gr.Textbox(placeholder="Ask something..."), #
|
30 |
-
outputs="text", # Output text
|
31 |
-
title="Groq-Gradio Chat", #
|
32 |
-
theme="
|
33 |
examples=[
|
34 |
"Tell me a short story about a puppy",
|
35 |
"Write a 14-line poem about travelling in Shakespeare style",
|
36 |
"What are the wonders of the world?",
|
37 |
"List the countries in Africa and their capitals"
|
38 |
-
]
|
39 |
-
).launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Function to interact with the Groq API using the curl request approach
|
6 |
def chat_function(message):
|
7 |
# Define the request payload
|
8 |
payload = {
|
9 |
+
"model": "mixtral-8x7b-32768",
|
10 |
+
"messages": [
|
11 |
+
{"role": "user", "content": message}
|
12 |
+
]
|
13 |
+
}
|
14 |
+
|
15 |
+
# Set the headers including your API key
|
16 |
+
headers = {
|
17 |
+
"Content-Type": "application/json",
|
18 |
+
"Authorization": "Bearer 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" # Replace with your API key
|
19 |
}
|
20 |
+
|
21 |
try:
|
22 |
+
# Make the POST request to the Groq API
|
23 |
+
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, data=json.dumps(payload))
|
24 |
+
|
25 |
+
# Check if the request was successful
|
26 |
+
if response.status_code == 200:
|
27 |
+
data = response.json()
|
28 |
+
return data["choices"][0]["message"]["content"] # Return the model's response
|
29 |
+
else:
|
30 |
+
return f"Error: {response.status_code}, {response.text}"
|
31 |
+
except requests.exceptions.RequestException as e:
|
32 |
+
return f"Error: {e}"
|
33 |
|
34 |
+
# Set up Gradio interface
|
35 |
gr.Interface(
|
36 |
+
fn=chat_function, # Function that handles the chatbot
|
37 |
+
inputs=gr.Textbox(placeholder="Ask something..."), # Input for user message
|
38 |
+
outputs="text", # Output as text
|
39 |
+
title="Groq-Gradio Chat", # Title of the interface
|
40 |
+
theme="default", # Ensure theme is valid
|
41 |
examples=[
|
42 |
"Tell me a short story about a puppy",
|
43 |
"Write a 14-line poem about travelling in Shakespeare style",
|
44 |
"What are the wonders of the world?",
|
45 |
"List the countries in Africa and their capitals"
|
46 |
+
]
|
47 |
+
).launch()
|