enotkrutoy commited on
Commit
0017a07
·
verified ·
1 Parent(s): d8da427

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -1,30 +1,39 @@
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)
 
 
 
 
 
 
 
 
 
 
 
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()