enotkrutoy commited on
Commit
7826328
·
verified ·
1 Parent(s): d48263d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -1,22 +1,38 @@
1
- import os
2
- import openai
3
  import gradio as gr
 
 
4
 
5
- os.environ["OPENAI_API_KEY"] = os.environ.get("GROQ_API_KEY")
 
6
 
7
- client = openai.OpenAI(
8
- base_url="https://api.groq.com/v1"
9
- )
10
 
11
- # Define the chat function
12
  def chat_function(message):
13
- # Call Groq API for completion using the correct method: completions.create
14
- response = client.completions.create(
15
- model="lama3-8b-8192", # Specify the model
16
- prompt=message, # Send the user's message as the prompt
17
- max_tokens=150 # Optionally, specify the maximum number of tokens in the response
18
- )
19
- return response.choices[0].text.strip() # Extract and return the text of the response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Gradio interface setup
22
  gr.Interface(
@@ -24,11 +40,11 @@ gr.Interface(
24
  inputs=gr.Textbox(placeholder="Ask something..."), # Input for user message
25
  outputs="text", # Output as text
26
  title="Groq-Gradio Chat",
27
- theme="upsatwal/mlsc_tiet", # Ensure theme is valid
28
  examples=[
29
  "Tell me a short story about a puppy",
30
  "Write a 14-line poem about travelling in Shakespeare style",
31
  "What are the wonders of the world?",
32
  "List the countries in Africa and their capitals"
33
  ]
34
- ).launch()
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import json
4
 
5
+ # Set the API key
6
+ api_key = "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"
7
 
8
+ # Define the endpoint URL
9
+ url = "https://api.groq.com/openai/v1/chat/completions"
 
10
 
11
+ # Define the request payload
12
  def chat_function(message):
13
+ payload = {
14
+ "model": "lama3-8b-8192",
15
+ "messages": [
16
+ {"role": "system", "content": "You are a helpful assistant."},
17
+ {"role": "user", "content": message}
18
+ ],
19
+ "max_tokens": 150,
20
+ "temperature": 1,
21
+ "presence_penalty": 0,
22
+ "frequency_penalty": 0
23
+ }
24
+
25
+ headers = {
26
+ "Authorization": f"Bearer {api_key}",
27
+ "Content-Type": "application/json"
28
+ }
29
+
30
+ response = requests.post(url, headers=headers, data=json.dumps(payload))
31
+ if response.status_code == 200:
32
+ data = response.json()
33
+ return data["choices"][0]["message"]["content"]
34
+ else:
35
+ return f"Error: {response.status_code}, {response.text}"
36
 
37
  # Gradio interface setup
38
  gr.Interface(
 
40
  inputs=gr.Textbox(placeholder="Ask something..."), # Input for user message
41
  outputs="text", # Output as text
42
  title="Groq-Gradio Chat",
43
+ theme="gradio", # Use a valid theme
44
  examples=[
45
  "Tell me a short story about a puppy",
46
  "Write a 14-line poem about travelling in Shakespeare style",
47
  "What are the wonders of the world?",
48
  "List the countries in Africa and their capitals"
49
  ]
50
+ ).launch()