Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,30 +3,32 @@ 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 |
-
|
|
|
|
|
|
|
7 |
|
8 |
# Function to interact with the LLM using Groq's API
|
9 |
def chatbot(messages):
|
10 |
try:
|
11 |
-
# Check if messages is empty and initialize it if necessary
|
12 |
if not messages:
|
13 |
messages = []
|
14 |
|
15 |
-
# Extract the latest user message
|
16 |
user_input = messages[-1][0] if messages else ""
|
17 |
|
18 |
-
#
|
19 |
chat_completion = client.chat.completions.create(
|
20 |
messages=[{"role": "user", "content": user_input}],
|
21 |
-
model="llama3-8b-8192", # Replace with the
|
22 |
)
|
|
|
23 |
response = chat_completion.choices[0].message.content
|
24 |
-
messages.append((user_input, response)) # Append
|
25 |
return messages
|
26 |
except Exception as e:
|
27 |
-
#
|
28 |
-
|
29 |
-
messages.append((None,
|
30 |
return messages
|
31 |
|
32 |
# Function to reset the chat
|
@@ -61,4 +63,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
61 |
gr.Examples(
|
62 |
examples=["Hello!", "Tell me a joke.", "What's the weather like?"],
|
63 |
inputs=user_input,
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
if not messages:
|
15 |
messages = []
|
16 |
|
|
|
17 |
user_input = messages[-1][0] if messages else ""
|
18 |
|
19 |
+
# Sending request to Groq API
|
20 |
chat_completion = client.chat.completions.create(
|
21 |
messages=[{"role": "user", "content": user_input}],
|
22 |
+
model="llama3-8b-8192", # Replace with the correct model name
|
23 |
)
|
24 |
+
|
25 |
response = chat_completion.choices[0].message.content
|
26 |
+
messages.append((user_input, response)) # Append the conversation
|
27 |
return messages
|
28 |
except Exception as e:
|
29 |
+
# Capture the specific error message for debugging
|
30 |
+
print(f"Error Details: {str(e)}") # Print error details in the console (logs)
|
31 |
+
messages.append((None, f"An error occurred: {str(e)}")) # Show the error in chat
|
32 |
return messages
|
33 |
|
34 |
# Function to reset the chat
|
|
|
63 |
gr.Examples(
|
64 |
examples=["Hello!", "Tell me a joke.", "What's the weather like?"],
|
65 |
inputs=user_input,
|
66 |
+
)
|
67 |
+
|
68 |
+
# Link components to functions
|
69 |
+
send_button.click(chatbot, inputs=[chatbot_ui], outputs=chatbot_ui)
|
70 |
+
clear_button.click(reset_chat, inputs=[], outputs=chatbot_ui)
|
71 |
+
|
72 |
+
# Launch the Gradio app
|
73 |
+
iface.launch()
|