Ridealist commited on
Commit
7d16d28
1 Parent(s): a035719

feat: implement error handling for OpenAI api server error

Browse files
Files changed (1) hide show
  1. app.py +44 -14
app.py CHANGED
@@ -349,7 +349,6 @@ def page4():
349
  options=topic_list,
350
  format_func=lambda x: x[:45] + "...",
351
  # help="This is help message",
352
- # on_change=
353
  )
354
  st.write("> Topic : ", topic)
355
 
@@ -424,13 +423,18 @@ def page4():
424
  placeholder="Input text here",
425
  height=100)
426
  output = st.sidebar.button("Ask")
427
- input_error_message = st.empty()
428
  if output:
429
  if not user_input:
430
- input_error_message.error("Please enter your question")
431
  result = ""
432
  else:
433
- result = gpt_call(user_input)
 
 
 
 
 
434
 
435
  # save user_prompt and bot_response to database
436
  put_item(
@@ -457,7 +461,6 @@ def page4():
457
  # Page5
458
  #########################################################
459
 
460
- # generate response
461
  def generate_response(prompt):
462
  st.session_state['user_debate_history'].append(prompt)
463
  st.session_state['total_debate_history'].append({"role": "user", "content": prompt})
@@ -466,13 +469,18 @@ def generate_response(prompt):
466
  st.session_state['total_debate_history'].append({"role": "assistant", "content": response})
467
  return response
468
 
469
- def execute_stt(audio):
470
  wav_file = open("audio/audio.wav", "wb")
471
  wav_file.write(audio.tobytes())
472
  wav_file.close()
473
 
474
  audio_file= open("audio/audio.wav", "rb")
475
- user_input = openai.Audio.transcribe("whisper-1", audio_file).text
 
 
 
 
 
476
  audio_file.close()
477
  return user_input
478
 
@@ -491,13 +499,19 @@ def page5():
491
  placeholder="Input text here",
492
  height=100)
493
  output = st.sidebar.button("Ask")
494
- input_error_message = st.empty()
495
  if output:
496
  if not user_input:
497
- input_error_message.error("Please enter your question")
498
  result = ""
499
  else:
500
- result = gpt_call(user_input)
 
 
 
 
 
 
501
  put_item(
502
  table=dynamodb.Table('DEBO_gpt_ask'),
503
  item={
@@ -517,6 +531,9 @@ def page5():
517
  value=result,
518
  height=400)
519
 
 
 
 
520
  # default system prompt settings
521
  if not st.session_state['total_debate_history']:
522
 
@@ -539,7 +556,13 @@ def page5():
539
  st.session_state['total_debate_history'] = [
540
  {"role": "system", "content": debate_preset}
541
  ]
542
- response = gpt_call(debate_preset + "\n" + first_prompt, role="system")
 
 
 
 
 
 
543
  st.session_state['total_debate_history'].append({"role": "assistant", "content": response})
544
  st.session_state['bot_debate_history'].append(response)
545
 
@@ -551,6 +574,8 @@ def page5():
551
 
552
  # container for chat history
553
  response_container = st.container()
 
 
554
  # container for text box
555
  container = st.container()
556
  reload = False
@@ -564,14 +589,19 @@ def page5():
564
  audio = np.array([])
565
 
566
  #user_input = st.text_area("You:", key='input', height=100)
567
- submit_buttom = st.form_submit_button(label='Send')
568
  send_error_message = st.empty()
569
 
570
  #if submit_buttom and user_input:
571
  if submit_buttom:
572
  if audio.any():
573
- user_input = execute_stt(audio)
574
- response = generate_response(user_input)
 
 
 
 
 
575
  st.session_state['pre_audio'] = audio
576
 
577
  debate_main_latest_data = get_lastest_item(
 
349
  options=topic_list,
350
  format_func=lambda x: x[:45] + "...",
351
  # help="This is help message",
 
352
  )
353
  st.write("> Topic : ", topic)
354
 
 
423
  placeholder="Input text here",
424
  height=100)
425
  output = st.sidebar.button("Ask")
426
+ error_message = st.empty()
427
  if output:
428
  if not user_input:
429
+ error_message.error("Please enter your question")
430
  result = ""
431
  else:
432
+ try:
433
+ result = gpt_call(user_input)
434
+ except:
435
+ error_message.error("Chat-GPT Error : The engine is currently overloaded, it will be auto-reloaded in a second")
436
+ time.sleep(0.5)
437
+ st.experimental_rerun()
438
 
439
  # save user_prompt and bot_response to database
440
  put_item(
 
461
  # Page5
462
  #########################################################
463
 
 
464
  def generate_response(prompt):
465
  st.session_state['user_debate_history'].append(prompt)
466
  st.session_state['total_debate_history'].append({"role": "user", "content": prompt})
 
469
  st.session_state['total_debate_history'].append({"role": "assistant", "content": response})
470
  return response
471
 
472
+ def execute_stt(audio, error_message):
473
  wav_file = open("audio/audio.wav", "wb")
474
  wav_file.write(audio.tobytes())
475
  wav_file.close()
476
 
477
  audio_file= open("audio/audio.wav", "rb")
478
+ try:
479
+ user_input = openai.Audio.transcribe("whisper-1", audio_file).text
480
+ except:
481
+ error_message.error("Whisper Error : The engine is currently overloaded, it will be auto-reloaded in a second")
482
+ time.sleep(1)
483
+ st.experimental_rerun()
484
  audio_file.close()
485
  return user_input
486
 
 
499
  placeholder="Input text here",
500
  height=100)
501
  output = st.sidebar.button("Ask")
502
+ error_message = st.empty()
503
  if output:
504
  if not user_input:
505
+ error_message.error("Please enter your question")
506
  result = ""
507
  else:
508
+ try:
509
+ result = gpt_call(user_input)
510
+ except:
511
+ error_message.error("Chat-GPT Error : The engine is currently overloaded, it will be auto-reloaded in a second")
512
+ time.sleep(0.5)
513
+ st.experimental_rerun()
514
+
515
  put_item(
516
  table=dynamodb.Table('DEBO_gpt_ask'),
517
  item={
 
531
  value=result,
532
  height=400)
533
 
534
+ # Chat-GPT api error handling
535
+ gpt_error_top = st.empty()
536
+
537
  # default system prompt settings
538
  if not st.session_state['total_debate_history']:
539
 
 
556
  st.session_state['total_debate_history'] = [
557
  {"role": "system", "content": debate_preset}
558
  ]
559
+ try:
560
+ response = gpt_call(debate_preset + "\n" + first_prompt, role="system")
561
+ except:
562
+ gpt_error_top.error("Chat-GPT Error : The engine is currently overloaded, it will be auto-reloaded in a second")
563
+ time.sleep(1)
564
+ st.experimental_rerun()
565
+
566
  st.session_state['total_debate_history'].append({"role": "assistant", "content": response})
567
  st.session_state['bot_debate_history'].append(response)
568
 
 
574
 
575
  # container for chat history
576
  response_container = st.container()
577
+ # Chat-GPT & Whisper api error handling
578
+ openai_error_bottom = st.empty()
579
  # container for text box
580
  container = st.container()
581
  reload = False
 
589
  audio = np.array([])
590
 
591
  #user_input = st.text_area("You:", key='input', height=100)
592
+ submit_buttom = st.form_submit_button(label='💬 Send')
593
  send_error_message = st.empty()
594
 
595
  #if submit_buttom and user_input:
596
  if submit_buttom:
597
  if audio.any():
598
+ user_input = execute_stt(audio, openai_error_bottom)
599
+ try :
600
+ response = generate_response(user_input)
601
+ except:
602
+ openai_error_bottom.error("Chat-GPT Error : The engine is currently overloaded, it will be auto-reloaded in a second")
603
+ time.sleep(1)
604
+ st.experimental_rerun()
605
  st.session_state['pre_audio'] = audio
606
 
607
  debate_main_latest_data = get_lastest_item(