amkj84 commited on
Commit
26e4452
·
verified ·
1 Parent(s): 1dacdc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -24
app.py CHANGED
@@ -2,34 +2,30 @@ import os
2
  import gradio as gr
3
  from groq import Groq
4
 
5
- # Set up Groq API client (ensure GROQ_API_KEY is set in your environment or as a Hugging Face secret for deployment)
6
- apikey = os.getenv("apikey")
7
- print(f"API Key: {apikey}") # Debugging line, remove it before pushing to Hugging Face
8
-
9
  client = Groq(api_key=apikey)
10
 
11
- # Function to interact with the LLM using Groq's API
12
- def chatbot(messages):
13
  try:
14
- # Ensure the messages list is not empty
15
- if not messages:
16
- messages = [("System", "Hello! How can I assist you today?")]
17
-
18
- user_input = messages[-1][0] # Last user input message
19
-
20
- if not user_input.strip(): # Check for empty input
21
- messages.append(("System", "It seems like you may have accidentally sent an empty message. Please rephrase."))
22
- return messages
23
-
24
- # Sending request to Groq API
25
  chat_completion = client.chat.completions.create(
26
  messages=[{"role": "user", "content": user_input}],
27
- model="llama3-8b-8192", # Replace with the correct model name
28
  )
29
-
30
- response = chat_completion.choices[0].message.content
31
- messages.append((user_input, response)) # Append user input and bot response as a tuple
32
- return messages
33
  except Exception as e:
34
- # Capture the specific error message for debugging
35
- print
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  from groq import Groq
4
 
5
+ # Set up the Groq API client
6
+ apikey = os.getenv("apikey") # Ensure your API key is set as a secret
7
+ if not apikey:
8
+ raise ValueError("API Key is not set. Add it in the Secrets tab.")
9
  client = Groq(api_key=apikey)
10
 
11
+ # Function to interact with the LLM
12
+ def chatbot(user_input):
13
  try:
 
 
 
 
 
 
 
 
 
 
 
14
  chat_completion = client.chat.completions.create(
15
  messages=[{"role": "user", "content": user_input}],
16
+ model="llama3-8b-8192",
17
  )
18
+ return chat_completion.choices[0].message.content
 
 
 
19
  except Exception as e:
20
+ return f"An error occurred: {str(e)}"
21
+
22
+ # Create the Gradio interface
23
+ iface = gr.Interface(
24
+ fn=chatbot,
25
+ inputs=gr.Textbox(label="Your Message", placeholder="Type your message here..."),
26
+ outputs=gr.Textbox(label="Response"),
27
+ title="Groq LLM Chatbot",
28
+ description="Interact with a powerful LLM via Groq's API."
29
+ )
30
+
31
+ iface.launch()