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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -47
app.py CHANGED
@@ -1,55 +1,30 @@
1
- import gradio as gr
2
- import requests
3
- import json
4
  import os
 
5
 
6
- # Ensure the environment variable is set securely (store your API key as an environment variable)
7
- api_key = os.getenv("GROQ_API_KEY")
8
- url = "https://api.groq.com/openai/v1/chat/completions"
9
 
10
  def chat_function(message):
11
- # Define the request payload
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
- # Set the headers including your API key
25
- headers = {
26
- "Authorization": f"Bearer {api_key}",
27
- "Content-Type": "application/json"
28
- }
29
-
30
- # Make the POST request to the API
31
- response = requests.post(url, headers=headers, data=json.dumps(payload))
32
-
33
- # Check if the request was successful
34
- if response.status_code == 200:
35
- data = response.json()
36
- return data["choices"][0]["message"]["content"] # Return the model's response
37
- else:
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)