Aahi8828 commited on
Commit
a65b0ad
·
verified ·
1 Parent(s): a67595f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -8
app.py CHANGED
@@ -1,25 +1,35 @@
1
  import gradio as gr
2
  import openai
3
  import os
 
4
 
5
  # Ensure the API key is loaded correctly
6
- openai.api_key = os.getenv("GROQ_API_KEY")
 
 
 
7
  openai.api_base = "https://api.groq.com/openai/v1"
8
 
9
  def get_groq_response(message):
10
  try:
11
- # Logging the message before sending
12
  print(f"Sending message: {message}")
13
 
 
14
  response = openai.ChatCompletion.create(
15
  model="llama-3.3-70b-versatile",
16
  messages=[
17
  {"role": "system", "content": "Precise answer"},
18
  {"role": "user", "content": message}
19
- ]
 
20
  )
21
-
22
- # Correcting the response structure
 
 
 
 
23
  bot_message = response['choices'][0]['message']['content']
24
 
25
  # Log the bot's response
@@ -27,17 +37,41 @@ def get_groq_response(message):
27
 
28
  return bot_message
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  except Exception as e:
 
31
  print(f"Error: {str(e)}")
32
- return f"Error: {str(e)}"
33
-
34
 
35
  def chatbot(user_input, history=[]):
 
 
 
 
 
36
  bot_response = get_groq_response(user_input)
37
  history.append((user_input, bot_response))
38
  return history, history
39
 
40
-
41
  chat_interface = gr.Interface(
42
  fn=chatbot,
43
  inputs=["text", "state"],
 
1
  import gradio as gr
2
  import openai
3
  import os
4
+ import time
5
 
6
  # Ensure the API key is loaded correctly
7
+ api_key = os.getenv("GROQ_API_KEY")
8
+ if not api_key:
9
+ raise ValueError("API key is missing. Please set the GROQ_API_KEY environment variable.")
10
+ openai.api_key = api_key
11
  openai.api_base = "https://api.groq.com/openai/v1"
12
 
13
  def get_groq_response(message):
14
  try:
15
+ # Log the message before sending
16
  print(f"Sending message: {message}")
17
 
18
+ # Call the API with a timeout to avoid hanging requests
19
  response = openai.ChatCompletion.create(
20
  model="llama-3.3-70b-versatile",
21
  messages=[
22
  {"role": "system", "content": "Precise answer"},
23
  {"role": "user", "content": message}
24
+ ],
25
+ timeout=30 # Set a reasonable timeout (30 seconds)
26
  )
27
+
28
+ # Check if the response structure is as expected
29
+ if 'choices' not in response or len(response['choices']) == 0:
30
+ raise ValueError("Unexpected API response structure.")
31
+
32
+ # Extract the bot's message
33
  bot_message = response['choices'][0]['message']['content']
34
 
35
  # Log the bot's response
 
37
 
38
  return bot_message
39
 
40
+ except openai.error.AuthenticationError as e:
41
+ # Handle authentication error (invalid API key)
42
+ print(f"Authentication error: {str(e)}")
43
+ return "Authentication failed. Please check your API key."
44
+
45
+ except openai.error.RateLimitError as e:
46
+ # Handle rate limit error (too many requests)
47
+ print(f"Rate limit exceeded: {str(e)}")
48
+ return "Rate limit exceeded. Please try again later."
49
+
50
+ except openai.error.APIError as e:
51
+ # Handle other API-related errors
52
+ print(f"API error: {str(e)}")
53
+ return "API error occurred. Please try again later."
54
+
55
+ except openai.error.Timeout as e:
56
+ # Handle timeout errors
57
+ print(f"Request timed out: {str(e)}")
58
+ return "The request timed out. Please try again later."
59
+
60
  except Exception as e:
61
+ # Handle all other generic errors
62
  print(f"Error: {str(e)}")
63
+ return f"An error occurred: {str(e)}"
 
64
 
65
  def chatbot(user_input, history=[]):
66
+ # Handle empty user input
67
+ if not user_input.strip():
68
+ return history, history
69
+
70
+ # Get the bot's response
71
  bot_response = get_groq_response(user_input)
72
  history.append((user_input, bot_response))
73
  return history, history
74
 
 
75
  chat_interface = gr.Interface(
76
  fn=chatbot,
77
  inputs=["text", "state"],