amkj84 commited on
Commit
9190d00
·
verified ·
1 Parent(s): ab34178

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -8,9 +8,13 @@ client = Groq(api_key=os.getenv("apikey"))
8
  # Function to interact with the LLM using Groq's API
9
  def chatbot(messages):
10
  try:
 
 
 
 
11
  # Extract the latest user message
12
  user_input = messages[-1][0] if messages else ""
13
-
14
  # Send user input to the LLM and get a response
15
  chat_completion = client.chat.completions.create(
16
  messages=[{"role": "user", "content": user_input}],
@@ -20,7 +24,10 @@ def chatbot(messages):
20
  messages.append((user_input, response)) # Append user input and response to chat
21
  return messages
22
  except Exception as e:
23
- return messages + [(None, f"An error occurred: {str(e)}")]
 
 
 
24
 
25
  # Function to reset the chat
26
  def reset_chat():
@@ -54,11 +61,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
54
  gr.Examples(
55
  examples=["Hello!", "Tell me a joke.", "What's the weather like?"],
56
  inputs=user_input,
57
- )
58
-
59
- # Link components to functions
60
- send_button.click(chatbot, inputs=[chatbot_ui], outputs=chatbot_ui)
61
- clear_button.click(reset_chat, inputs=[], outputs=chatbot_ui)
62
-
63
- # Launch the Gradio app
64
- iface.launch()
 
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
  # Send user input to the LLM and get a response
19
  chat_completion = client.chat.completions.create(
20
  messages=[{"role": "user", "content": user_input}],
 
24
  messages.append((user_input, response)) # Append user input and response to chat
25
  return messages
26
  except Exception as e:
27
+ # If error occurs, return the message with error info
28
+ error_message = f"An error occurred: {str(e)}"
29
+ messages.append((None, error_message)) # Append error message to chat
30
+ return messages
31
 
32
  # Function to reset the chat
33
  def reset_chat():
 
61
  gr.Examples(
62
  examples=["Hello!", "Tell me a joke.", "What's the weather like?"],
63
  inputs=user_input,
64
+