Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,30 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import json
|
4 |
import os
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
|
10 |
def chat_function(message):
|
11 |
-
#
|
12 |
payload = {
|
13 |
-
"model": "mixtral-8x7b-32768",
|
14 |
-
"messages": [
|
15 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
16 |
-
{"role": "user", "content": message}
|
17 |
-
],
|
18 |
"max_tokens": 150,
|
19 |
-
"temperature": 1
|
20 |
-
"presence_penalty": 0,
|
21 |
-
"frequency_penalty": 0
|
22 |
}
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
return f"Error: {response.status_code}, {response.text}"
|
39 |
-
|
40 |
-
# Set up Gradio interface
|
41 |
-
gr.Interface(
|
42 |
-
fn=chat_function, # Function that handles the chatbot
|
43 |
-
inputs=gr.Textbox(placeholder="Ask something..."), # Input for user message
|
44 |
-
outputs="text", # Output as text
|
45 |
-
title="Groq-Gradio Chat", # Title of the interface
|
46 |
-
theme="upsatwal/mlsc_tiet", # Ensure theme is valid
|
47 |
-
examples=[
|
48 |
-
"Tell me a short story about a puppy",
|
49 |
-
"Write a 14-line poem about travelling in Shakespeare style",
|
50 |
-
"What are the wonders of the world?",
|
51 |
-
"List the countries in Africa and their capitals"
|
52 |
-
]
|
53 |
-
).launch()
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import openai
|
3 |
|
4 |
+
# Set the base URL to the Groq endpoint
|
5 |
+
openai.api_base = "https://api.groq.com/openai/v1"
|
6 |
+
openai.api_key = os.getenv("GROQ_API_KEY") # Fetch the API key from environment variable
|
7 |
|
8 |
def chat_function(message):
|
9 |
+
# Prepare the request payload
|
10 |
payload = {
|
11 |
+
"model": "mixtral-8x7b-32768", # Adjust the model name as per Groq's available models
|
12 |
+
"messages": [{"role": "user", "content": message}],
|
|
|
|
|
|
|
13 |
"max_tokens": 150,
|
14 |
+
"temperature": 1
|
|
|
|
|
15 |
}
|
16 |
|
17 |
+
try:
|
18 |
+
# Make the API request to the Groq server
|
19 |
+
response = openai.ChatCompletion.create(**payload)
|
20 |
+
|
21 |
+
# Check if the response is successful and return the model's response
|
22 |
+
return response["choices"][0]["message"]["content"]
|
23 |
+
|
24 |
+
except Exception as e:
|
25 |
+
return f"Error: {str(e)}"
|
26 |
+
|
27 |
+
# Example usage
|
28 |
+
message = "Tell me about the wonders of the world"
|
29 |
+
response = chat_function(message)
|
30 |
+
print(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|