barunsaha commited on
Commit
e9617d4
1 Parent(s): 2752a4b

Add more exception handling

Browse files
Files changed (1) hide show
  1. app.py +38 -8
app.py CHANGED
@@ -10,6 +10,7 @@ import tempfile
10
  from typing import List, Union
11
 
12
  import json5
 
13
  import streamlit as st
14
  from langchain_community.chat_message_histories import StreamlitChatMessageHistory
15
  from langchain_core.messages import HumanMessage
@@ -198,15 +199,24 @@ def set_up_chat_ui():
198
  progress_bar = st.progress(0, 'Preparing to call LLM...')
199
  response = ''
200
 
201
- for chunk in _get_llm().stream(formatted_template):
202
- response += chunk
203
-
204
- # Update the progress bar
205
- progress_percentage = min(len(response) / APPROX_TARGET_LENGTH, 0.95)
206
- progress_bar.progress(
207
- progress_percentage,
208
- text='Streaming content...this might take a while...'
 
 
 
 
 
 
209
  )
 
 
 
210
 
211
  history.add_user_message(prompt)
212
  history.add_ai_message(response)
@@ -272,6 +282,26 @@ def generate_slide_deck(json_str: str) -> Union[pathlib.Path, None]:
272
  )
273
 
274
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
 
276
  if DOWNLOAD_FILE_KEY in st.session_state:
277
  path = pathlib.Path(st.session_state[DOWNLOAD_FILE_KEY])
 
10
  from typing import List, Union
11
 
12
  import json5
13
+ import requests
14
  import streamlit as st
15
  from langchain_community.chat_message_histories import StreamlitChatMessageHistory
16
  from langchain_core.messages import HumanMessage
 
199
  progress_bar = st.progress(0, 'Preparing to call LLM...')
200
  response = ''
201
 
202
+ try:
203
+ for chunk in _get_llm().stream(formatted_template):
204
+ response += chunk
205
+
206
+ # Update the progress bar
207
+ progress_percentage = min(len(response) / APPROX_TARGET_LENGTH, 0.95)
208
+ progress_bar.progress(
209
+ progress_percentage,
210
+ text='Streaming content...this might take a while...'
211
+ )
212
+ except requests.exceptions.ConnectionError:
213
+ msg = (
214
+ 'A connection error occurred while streaming content from the LLM endpoint.'
215
+ ' Unfortunately, the slide deck cannot be generated. Please try again later.'
216
  )
217
+ logger.error(msg)
218
+ st.error(msg)
219
+ return
220
 
221
  history.add_user_message(prompt)
222
  history.add_ai_message(response)
 
282
  )
283
 
284
  return None
285
+ except RecursionError:
286
+ st.error(
287
+ 'Encountered an error while parsing JSON...'
288
+ 'the slide deck cannot be created, unfortunately ☹'
289
+ '\nPlease try again later.'
290
+ )
291
+ logger.error('Caught RecursionError while parsing JSON. Cannot generate the slide deck!')
292
+
293
+ return None
294
+ except Exception:
295
+ st.error(
296
+ 'Encountered an error while parsing JSON...'
297
+ 'the slide deck cannot be created, unfortunately ☹'
298
+ '\nPlease try again later.'
299
+ )
300
+ logger.error(
301
+ 'Caught ValueError: failed to parse JSON!'
302
+ )
303
+
304
+ return None
305
 
306
  if DOWNLOAD_FILE_KEY in st.session_state:
307
  path = pathlib.Path(st.session_state[DOWNLOAD_FILE_KEY])