JLW commited on
Commit
01f613e
·
1 Parent(s): 5c7becd

Handle some errors, giving messages to the user.

Browse files
Files changed (2) hide show
  1. app.py +39 -5
  2. videos/tempfile.mp4 +2 -2
app.py CHANGED
@@ -20,11 +20,15 @@ from io import StringIO
20
  import sys
21
  import re
22
 
 
 
23
  news_api_key = os.environ["NEWS_API_KEY"]
24
  tmdb_bearer_token = os.environ["TMDB_BEARER_TOKEN"]
25
 
26
- TOOLS_LIST = ['serpapi', 'wolfram-alpha', 'google-search', 'pal-math', 'pal-colored-objects', 'news-api', 'tmdb-api', 'open-meteo-api']
 
27
  TOOLS_DEFAULT_LIST = ['serpapi', 'pal-math', 'pal-colored-objects']
 
28
 
29
 
30
  # UNCOMMENT TO USE WHISPER
@@ -76,11 +80,22 @@ def run_chain(chain, inp, capture_hidden_text):
76
  output = ""
77
  hidden_text = None
78
  if capture_hidden_text:
 
79
  tmp = sys.stdout
80
  hidden_text_io = StringIO()
81
  sys.stdout = hidden_text_io
82
 
83
- output = chain.run(input=inp)
 
 
 
 
 
 
 
 
 
 
84
 
85
  sys.stdout = tmp
86
  hidden_text = hidden_text_io.getvalue()
@@ -100,8 +115,24 @@ def run_chain(chain, inp, capture_hidden_text):
100
  hidden_text = re.sub(r"Observation:", "\n\nObservation:", hidden_text)
101
  hidden_text = re.sub(r"Input:", "\n\nInput:", hidden_text)
102
  hidden_text = re.sub(r"AI:", "\n\nAI:", hidden_text)
 
 
 
 
 
103
  else:
104
- output = chain.run(input=inp)
 
 
 
 
 
 
 
 
 
 
 
105
  return output, hidden_text
106
 
107
 
@@ -115,6 +146,7 @@ def chat(
115
  history = history or []
116
  # If chain is None, that is because no API key was provided.
117
  output = "Please paste your OpenAI key to use this application."
 
118
 
119
  if chain and chain != "":
120
  output, hidden_text = run_chain(chain, inp, capture_hidden_text=trace_chain)
@@ -231,8 +263,10 @@ with block:
231
 
232
  gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
233
 
234
- message.submit(chat, inputs=[message, history_state, chain_state, trace_chain_state], outputs=[chatbot, history_state, video_html, my_file, message])
235
- submit.click(chat, inputs=[message, history_state, chain_state, trace_chain_state], outputs=[chatbot, history_state, video_html, my_file, message])
 
 
236
 
237
  openai_api_key_textbox.change(set_openai_api_key,
238
  inputs=[openai_api_key_textbox],
 
20
  import sys
21
  import re
22
 
23
+ from openai.error import AuthenticationError, InvalidRequestError
24
+
25
  news_api_key = os.environ["NEWS_API_KEY"]
26
  tmdb_bearer_token = os.environ["TMDB_BEARER_TOKEN"]
27
 
28
+ TOOLS_LIST = ['serpapi', 'wolfram-alpha', 'google-search', 'pal-math', 'pal-colored-objects', 'news-api', 'tmdb-api',
29
+ 'open-meteo-api']
30
  TOOLS_DEFAULT_LIST = ['serpapi', 'pal-math', 'pal-colored-objects']
31
+ BUG_FOUND_MSG = "Congratulations, you've found a bug in this application!"
32
 
33
 
34
  # UNCOMMENT TO USE WHISPER
 
80
  output = ""
81
  hidden_text = None
82
  if capture_hidden_text:
83
+ error_msg = None
84
  tmp = sys.stdout
85
  hidden_text_io = StringIO()
86
  sys.stdout = hidden_text_io
87
 
88
+ try:
89
+ output = chain.run(input=inp)
90
+ except AuthenticationError as ae:
91
+ error_msg = "AuthenticationError: " + str(ae)
92
+ # print("\nAuthenticationError: ", ae)
93
+ except InvalidRequestError as ire:
94
+ error_msg = "\n\n" + BUG_FOUND_MSG + " Here are the details: InvalidRequestError, " + str(ire)
95
+ # print("\nInvalidRequestError: ", ire)
96
+ except Exception as e:
97
+ error_msg = "\n" + BUG_FOUND_MSG + " Here are the details: Exception, " + str(e)
98
+ # print("\nException: ", e)
99
 
100
  sys.stdout = tmp
101
  hidden_text = hidden_text_io.getvalue()
 
115
  hidden_text = re.sub(r"Observation:", "\n\nObservation:", hidden_text)
116
  hidden_text = re.sub(r"Input:", "\n\nInput:", hidden_text)
117
  hidden_text = re.sub(r"AI:", "\n\nAI:", hidden_text)
118
+
119
+ if error_msg:
120
+ hidden_text += error_msg
121
+
122
+ print("hidden_text: ", hidden_text)
123
  else:
124
+ try:
125
+ output = chain.run(input=inp)
126
+ except AuthenticationError as ae:
127
+ output = "AuthenticationError: " + str(ae)
128
+ print("\nAuthenticationError: ", ae)
129
+ except InvalidRequestError as ire:
130
+ output = BUG_FOUND_MSG + " Here are the details: InvalidRequestError, " + str(ire)
131
+ print("\nInvalidRequestError: ", ire)
132
+ except Exception as e:
133
+ output = BUG_FOUND_MSG + " Here are the details: Exception, " + str(e)
134
+ print("\nException: ", e)
135
+
136
  return output, hidden_text
137
 
138
 
 
146
  history = history or []
147
  # If chain is None, that is because no API key was provided.
148
  output = "Please paste your OpenAI key to use this application."
149
+ hidden_text = output
150
 
151
  if chain and chain != "":
152
  output, hidden_text = run_chain(chain, inp, capture_hidden_text=trace_chain)
 
263
 
264
  gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
265
 
266
+ message.submit(chat, inputs=[message, history_state, chain_state, trace_chain_state],
267
+ outputs=[chatbot, history_state, video_html, my_file, message])
268
+ submit.click(chat, inputs=[message, history_state, chain_state, trace_chain_state],
269
+ outputs=[chatbot, history_state, video_html, my_file, message])
270
 
271
  openai_api_key_textbox.change(set_openai_api_key,
272
  inputs=[openai_api_key_textbox],
videos/tempfile.mp4 CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:7e8f7c952f368c8b66d0eace21d820f1549e4fa153088879bc80103b64d3ecdb
3
- size 307463
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7ad6ea94ca0de42304c461a30340e259f9943ef79c9aaa68d8eef2087ee398a6
3
+ size 135190