enotkrutoy commited on
Commit
265e3fb
·
verified ·
1 Parent(s): 388f98a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -1,39 +1,47 @@
1
  import gradio as gr
2
- import openai
3
- import os
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", # Specify your model here (e.g., Groq's supported model)
13
- "messages": [{"role": "user", "content": message}],
14
- "max_tokens": 150,
15
- "temperature": 1
 
 
 
 
 
 
16
  }
17
-
18
  try:
19
- # Send the request to the Groq API and get the response
20
- response = openai.ChatCompletion.create(**payload)
21
- return response["choices"][0]["message"]["content"]
22
-
23
- except Exception as e:
24
- return f"Error: {str(e)}"
 
 
 
 
 
25
 
26
- # Set up the Gradio interface
27
  gr.Interface(
28
- fn=chat_function, # The function that handles user input
29
- inputs=gr.Textbox(placeholder="Ask something..."), # User input text box
30
- outputs="text", # Output text box for the response
31
- title="Groq-Gradio Chat", # Interface title
32
- theme="huggingface", # Use a valid Gradio 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
- ] # Example prompts
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()