JLW commited on
Commit
99a7e94
·
1 Parent(s): f4fc33c

Implemented fix based upon feedback

Browse files
Files changed (1) hide show
  1. app.py +249 -226
app.py CHANGED
@@ -17,6 +17,8 @@ from langchain import ConversationChain, LLMChain
17
  from langchain.agents import load_tools, initialize_agent
18
  from langchain.chains.conversation.memory import ConversationBufferMemory
19
  from langchain.llms import OpenAI
 
 
20
 
21
  # Console to variable
22
  from io import StringIO
@@ -191,7 +193,7 @@ def load_chain(tools_list, llm):
191
  chain = None
192
  express_chain = None
193
  if llm:
194
- print("tools_list", tools_list)
195
  tool_names = tools_list
196
  tools = load_tools(tool_names, llm=llm, news_api_key=news_api_key, tmdb_bearer_token=tmdb_bearer_token)
197
 
@@ -208,8 +210,17 @@ def set_openai_api_key(api_key):
208
  If no api_key, then None is returned.
209
  """
210
  if api_key and api_key.startswith("sk-") and len(api_key) > 50:
211
- llm = OpenAI(temperature=0, openai_api_key=api_key, max_tokens=MAX_TOKENS)
 
212
  chain, express_chain = load_chain(TOOLS_DEFAULT_LIST, llm)
 
 
 
 
 
 
 
 
213
  return chain, express_chain, llm
214
  return None, None, None
215
 
@@ -276,41 +287,57 @@ def run_chain(chain, inp, capture_hidden_text):
276
  return output, hidden_text
277
 
278
 
279
- def chat(
280
- inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain],
 
 
 
 
281
  trace_chain: bool, speak_text: bool, express_chain: Optional[LLMChain],
282
  num_words, formality, anticipation_level, joy_level, trust_level,
283
  fear_level, surprise_level, sadness_level, disgust_level, anger_level,
284
- translate_to, literary_style):
285
- """Execute the chat functionality."""
286
- print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
287
- print("inp: " + inp)
288
- print("trace_chain: ", trace_chain)
289
- print("speak_text: ", speak_text)
290
- history = history or []
291
- # If chain is None, that is because no API key was provided.
292
- output = "Please paste your OpenAI key to use this application."
293
- hidden_text = output
294
-
295
- if chain and chain != "":
296
- output, hidden_text = run_chain(chain, inp, capture_hidden_text=trace_chain)
297
-
298
- output = transform_text(output, express_chain, num_words, formality, anticipation_level, joy_level, trust_level,
299
- fear_level, surprise_level, sadness_level, disgust_level, anger_level,
300
- translate_to, literary_style)
301
-
302
- text_to_display = output
303
- if trace_chain:
304
- text_to_display = hidden_text + "\n\n" + output
305
- history.append((inp, text_to_display))
306
-
307
- # html_video, temp_file = do_html_video_speak(output)
308
- html_audio, temp_aud_file = None, None
309
- if speak_text:
310
- html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
311
-
312
- # return history, history, html_video, temp_file, ""
313
- return history, history, html_audio, temp_aud_file, ""
 
 
 
 
 
 
 
 
 
 
 
 
314
 
315
 
316
  def do_html_audio_speak(words_to_speak, polly_language):
@@ -417,197 +444,193 @@ with gr.Blocks(css=".gradio-container {background-color: lightgray}") as block:
417
  translate_to_state = gr.State(TRANSLATE_TO_DEFAULT)
418
  literary_style_state = gr.State(LITERARY_STYLE_DEFAULT)
419
 
420
- gr.Markdown("<h4><center>Conversational Agent using GPT-3.5 & LangChain</center></h4>")
421
- gr.Markdown(
422
- "<b><i><center>Down while implementing some great feedback gathered from the last few days</center></i></b>")
423
-
424
- # with gr.Tab("Chat"):
425
- # with gr.Row():
426
- # with gr.Column():
427
- # gr.Markdown("<h4><center>Conversational Agent using GPT-3.5 & LangChain</center></h4>")
428
- #
429
- # openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...)",
430
- # show_label=False, lines=1, type='password')
431
- #
432
- # with gr.Row():
433
- # with gr.Column(scale=1, min_width=100, visible=False):
434
- # my_file = gr.File(label="Upload a file", type="file", visible=False)
435
- # tmp_file = gr.File("videos/Masahiro.mp4", visible=False)
436
- # tmp_file_url = "/file=" + tmp_file.value['name']
437
- # htm_video = f'<video width="256" height="256" autoplay muted loop><source src={tmp_file_url} type="video/mp4" poster="Masahiro.png"></video>'
438
- # video_html = gr.HTML(htm_video)
439
- #
440
- # # my_aud_file = gr.File(label="Audio file", type="file", visible=True)
441
- # tmp_aud_file = gr.File("audios/tempfile.mp3", visible=False)
442
- # tmp_aud_file_url = "/file=" + tmp_aud_file.value['name']
443
- # htm_audio = f'<audio><source src={tmp_aud_file_url} type="audio/mp3"></audio>'
444
- # audio_html = gr.HTML(htm_audio)
445
- #
446
- # with gr.Column(scale=3):
447
- # chatbot = gr.Chatbot()
448
- #
449
- # with gr.Row():
450
- # message = gr.Textbox(label="What's on your mind??",
451
- # placeholder="What's the answer to life, the universe, and everything?",
452
- # lines=1)
453
- # submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
454
- #
455
- # # UNCOMMENT TO USE WHISPER
456
- # # with gr.Row():
457
- # # audio_comp = gr.Microphone(source="microphone", type="filepath", label="Just say it!",
458
- # # interactive=True, streaming=False)
459
- # # audio_comp.change(transcribe, inputs=[audio_comp], outputs=[message])
460
- #
461
- # gr.Examples(
462
- # examples=["How many people live in Canada?",
463
- # "What is 2 to the 30th power?",
464
- # "How much did it rain in SF today?",
465
- # "Get me information about the movie 'Avatar'",
466
- # "What are the top tech headlines in the US?",
467
- # "On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses - "
468
- # "if I remove all the pairs of sunglasses from the desk, how many purple items remain on it?"],
469
- # inputs=message
470
- # )
471
- #
472
- # with gr.Tab("Settings"):
473
- # tools_cb_group = gr.CheckboxGroup(label="Tools:", choices=TOOLS_LIST,
474
- # value=TOOLS_DEFAULT_LIST)
475
- # tools_cb_group.change(update_selected_tools,
476
- # inputs=[tools_cb_group, tools_list_state, llm_state],
477
- # outputs=[tools_list_state, llm_state, chain_state, express_chain_state])
478
- #
479
- # trace_chain_cb = gr.Checkbox(label="Show reasoning chain in chat bubble", value=False)
480
- # trace_chain_cb.change(update_foo, inputs=[trace_chain_cb, trace_chain_state],
481
- # outputs=[trace_chain_state])
482
- #
483
- # speak_text_cb = gr.Checkbox(label="Speak text from agent", value=False)
484
- # speak_text_cb.change(update_foo, inputs=[speak_text_cb, speak_text_state],
485
- # outputs=[speak_text_state])
486
- #
487
- # with gr.Tab("Formality"):
488
- # formality_radio = gr.Radio(label="Formality:",
489
- # choices=[FORMALITY_DEFAULT, "Casual", "Polite", "Honorific"],
490
- # value=FORMALITY_DEFAULT)
491
- # formality_radio.change(update_foo,
492
- # inputs=[formality_radio, formality_state],
493
- # outputs=[formality_state])
494
- #
495
- # with gr.Tab("Translate to"):
496
- # translate_to_radio = gr.Radio(label="Translate to:", choices=[
497
- # TRANSLATE_TO_DEFAULT, "Arabic", "Arabic (Gulf)", "Catalan", "Chinese (Cantonese)", "Chinese (Mandarin)",
498
- # "Danish", "Dutch", "English (Australian)", "English (British)", "English (Indian)", "English (New Zealand)",
499
- # "English (South African)", "English (US)", "English (Welsh)", "Finnish", "French", "French (Canadian)",
500
- # "German", "German (Austrian)", "Hindi", "Icelandic", "Italian", "Japanese", "Korean", "Norwegian", "Polish",
501
- # "Portuguese (Brazilian)", "Portuguese (European)", "Romanian", "Russian", "Spanish (European)",
502
- # "Spanish (Mexican)", "Spanish (US)", "Swedish", "Turkish", "Ukrainian", "Welsh",
503
- # "emojis", "Gen Z slang", "how the stereotypical Karen would say it", "Klingon",
504
- # "Pirate", "Strange Planet expospeak technical talk", "Yoda"],
505
- # value=TRANSLATE_TO_DEFAULT)
506
- #
507
- # translate_to_radio.change(update_foo,
508
- # inputs=[translate_to_radio, translate_to_state],
509
- # outputs=[translate_to_state])
510
- #
511
- # with gr.Tab("Lit style"):
512
- # literary_style_radio = gr.Radio(label="Literary style:", choices=[
513
- # LITERARY_STYLE_DEFAULT, "Prose", "Summary", "Outline", "Bullets", "Poetry", "Haiku", "Limerick", "Joke",
514
- # "Knock-knock"],
515
- # value=LITERARY_STYLE_DEFAULT)
516
- #
517
- # literary_style_radio.change(update_foo,
518
- # inputs=[literary_style_radio, literary_style_state],
519
- # outputs=[literary_style_state])
520
- #
521
- # with gr.Tab("Emotions"):
522
- # anticipation_level_radio = gr.Radio(label="Anticipation level:",
523
- # choices=[EMOTION_DEFAULT, "Interest", "Anticipation", "Vigilance"],
524
- # value=EMOTION_DEFAULT)
525
- # anticipation_level_radio.change(update_foo,
526
- # inputs=[anticipation_level_radio, anticipation_level_state],
527
- # outputs=[anticipation_level_state])
528
- #
529
- # joy_level_radio = gr.Radio(label="Joy level:",
530
- # choices=[EMOTION_DEFAULT, "Serenity", "Joy", "Ecstasy"],
531
- # value=EMOTION_DEFAULT)
532
- # joy_level_radio.change(update_foo,
533
- # inputs=[joy_level_radio, joy_level_state],
534
- # outputs=[joy_level_state])
535
- #
536
- # trust_level_radio = gr.Radio(label="Trust level:",
537
- # choices=[EMOTION_DEFAULT, "Acceptance", "Trust", "Admiration"],
538
- # value=EMOTION_DEFAULT)
539
- # trust_level_radio.change(update_foo,
540
- # inputs=[trust_level_radio, trust_level_state],
541
- # outputs=[trust_level_state])
542
- #
543
- # fear_level_radio = gr.Radio(label="Fear level:",
544
- # choices=[EMOTION_DEFAULT, "Apprehension", "Fear", "Terror"],
545
- # value=EMOTION_DEFAULT)
546
- # fear_level_radio.change(update_foo,
547
- # inputs=[fear_level_radio, fear_level_state],
548
- # outputs=[fear_level_state])
549
- #
550
- # surprise_level_radio = gr.Radio(label="Surprise level:",
551
- # choices=[EMOTION_DEFAULT, "Distraction", "Surprise", "Amazement"],
552
- # value=EMOTION_DEFAULT)
553
- # surprise_level_radio.change(update_foo,
554
- # inputs=[surprise_level_radio, surprise_level_state],
555
- # outputs=[surprise_level_state])
556
- #
557
- # sadness_level_radio = gr.Radio(label="Sadness level:",
558
- # choices=[EMOTION_DEFAULT, "Pensiveness", "Sadness", "Grief"],
559
- # value=EMOTION_DEFAULT)
560
- # sadness_level_radio.change(update_foo,
561
- # inputs=[sadness_level_radio, sadness_level_state],
562
- # outputs=[sadness_level_state])
563
- #
564
- # disgust_level_radio = gr.Radio(label="Disgust level:",
565
- # choices=[EMOTION_DEFAULT, "Boredom", "Disgust", "Loathing"],
566
- # value=EMOTION_DEFAULT)
567
- # disgust_level_radio.change(update_foo,
568
- # inputs=[disgust_level_radio, disgust_level_state],
569
- # outputs=[disgust_level_state])
570
- #
571
- # anger_level_radio = gr.Radio(label="Anger level:",
572
- # choices=[EMOTION_DEFAULT, "Annoyance", "Anger", "Rage"],
573
- # value=EMOTION_DEFAULT)
574
- # anger_level_radio.change(update_foo,
575
- # inputs=[anger_level_radio, anger_level_state],
576
- # outputs=[anger_level_state])
577
- #
578
- # with gr.Tab("Max words"):
579
- # num_words_slider = gr.Slider(label="Max number of words to generate (0 for don't care)",
580
- # value=NUM_WORDS_DEFAULT, minimum=0, maximum=MAX_WORDS, step=10)
581
- # num_words_slider.change(update_foo,
582
- # inputs=[num_words_slider, num_words_state],
583
- # outputs=[num_words_state])
584
- #
585
- # gr.HTML("""
586
- # This application demonstrates a conversational agent implemented with OpenAI GPT-3.5 and LangChain.
587
- # When necessary, it leverages tools for complex math, searching the internet, and accessing news and weather.
588
- # On a desktop, the agent will often speak using using an animated avatar from
589
- # <a href='https://exh.ai/'>Ex-Human</a>.""")
590
- #
591
- # gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
592
- #
593
- # message.submit(chat, inputs=[message, history_state, chain_state, trace_chain_state, speak_text_state,
594
- # express_chain_state, num_words_state, formality_state,
595
- # anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
596
- # surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
597
- # translate_to_state, literary_style_state],
598
- # # outputs=[chatbot, history_state, video_html, my_file, message])
599
- # outputs=[chatbot, history_state, audio_html, tmp_aud_file, message])
600
- #
601
- # submit.click(chat, inputs=[message, history_state, chain_state, trace_chain_state, speak_text_state,
602
- # express_chain_state, num_words_state, formality_state,
603
- # anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
604
- # surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
605
- # translate_to_state, literary_style_state],
606
- # # outputs=[chatbot, history_state, video_html, my_file, message])
607
- # outputs=[chatbot, history_state, audio_html, tmp_aud_file, message])
608
- #
609
- # openai_api_key_textbox.change(set_openai_api_key,
610
- # inputs=[openai_api_key_textbox],
611
- # outputs=[chain_state, express_chain_state, llm_state])
612
 
613
  block.launch(debug=True)
 
17
  from langchain.agents import load_tools, initialize_agent
18
  from langchain.chains.conversation.memory import ConversationBufferMemory
19
  from langchain.llms import OpenAI
20
+ from threading import Lock
21
+
22
 
23
  # Console to variable
24
  from io import StringIO
 
193
  chain = None
194
  express_chain = None
195
  if llm:
196
+ print("\ntools_list", tools_list)
197
  tool_names = tools_list
198
  tools = load_tools(tool_names, llm=llm, news_api_key=news_api_key, tmdb_bearer_token=tmdb_bearer_token)
199
 
 
210
  If no api_key, then None is returned.
211
  """
212
  if api_key and api_key.startswith("sk-") and len(api_key) > 50:
213
+ os.environ["OPENAI_API_KEY"] = api_key
214
+ llm = OpenAI(temperature=0, max_tokens=MAX_TOKENS)
215
  chain, express_chain = load_chain(TOOLS_DEFAULT_LIST, llm)
216
+ os.environ["OPENAI_API_KEY"] = ""
217
+
218
+ # print the object identifier of the llm, chain, and express_chain
219
+ print("== In set_openai_api_key ==")
220
+ print("llm id: " + str(id(llm)))
221
+ print("chain id: " + str(id(chain)))
222
+ print("express_chain id: " + str(id(express_chain)))
223
+
224
  return chain, express_chain, llm
225
  return None, None, None
226
 
 
287
  return output, hidden_text
288
 
289
 
290
+ class ChatWrapper:
291
+
292
+ def __init__(self):
293
+ self.lock = Lock()
294
+ def __call__(
295
+ self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain],
296
  trace_chain: bool, speak_text: bool, express_chain: Optional[LLMChain],
297
  num_words, formality, anticipation_level, joy_level, trust_level,
298
  fear_level, surprise_level, sadness_level, disgust_level, anger_level,
299
+ translate_to, literary_style
300
+ ):
301
+ """Execute the chat functionality."""
302
+ self.lock.acquire()
303
+ try:
304
+ print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
305
+ print("inp: " + inp)
306
+ print("trace_chain: ", trace_chain)
307
+ print("speak_text: ", speak_text)
308
+ history = history or []
309
+ # If chain is None, that is because no API key was provided.
310
+ output = "Please paste your OpenAI key to use this application."
311
+ hidden_text = output
312
+
313
+ if chain and chain != "":
314
+ # Set OpenAI key
315
+ import openai
316
+ openai.api_key = api_key
317
+ output, hidden_text = run_chain(chain, inp, capture_hidden_text=trace_chain)
318
+
319
+ output = transform_text(output, express_chain, num_words, formality, anticipation_level, joy_level, trust_level,
320
+ fear_level, surprise_level, sadness_level, disgust_level, anger_level,
321
+ translate_to, literary_style)
322
+
323
+ text_to_display = output
324
+ if trace_chain:
325
+ text_to_display = hidden_text + "\n\n" + output
326
+ history.append((inp, text_to_display))
327
+
328
+ # html_video, temp_file = do_html_video_speak(output)
329
+ html_audio, temp_aud_file = None, None
330
+ if speak_text:
331
+ html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
332
+ except Exception as e:
333
+ raise e
334
+ finally:
335
+ self.lock.release()
336
+ # return history, history, html_video, temp_file, ""
337
+ return history, history, html_audio, temp_aud_file, ""
338
+
339
+
340
+ chat = ChatWrapper()
341
 
342
 
343
  def do_html_audio_speak(words_to_speak, polly_language):
 
444
  translate_to_state = gr.State(TRANSLATE_TO_DEFAULT)
445
  literary_style_state = gr.State(LITERARY_STYLE_DEFAULT)
446
 
447
+ with gr.Tab("Chat"):
448
+ with gr.Row():
449
+ with gr.Column():
450
+ gr.Markdown("<h4><center>Conversational Agent using GPT-3.5 & LangChain</center></h4>")
451
+
452
+ openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...)",
453
+ show_label=False, lines=1, type='password')
454
+
455
+ with gr.Row():
456
+ with gr.Column(scale=1, min_width=100, visible=False):
457
+ my_file = gr.File(label="Upload a file", type="file", visible=False)
458
+ tmp_file = gr.File("videos/Masahiro.mp4", visible=False)
459
+ tmp_file_url = "/file=" + tmp_file.value['name']
460
+ htm_video = f'<video width="256" height="256" autoplay muted loop><source src={tmp_file_url} type="video/mp4" poster="Masahiro.png"></video>'
461
+ video_html = gr.HTML(htm_video)
462
+
463
+ # my_aud_file = gr.File(label="Audio file", type="file", visible=True)
464
+ tmp_aud_file = gr.File("audios/tempfile.mp3", visible=False)
465
+ tmp_aud_file_url = "/file=" + tmp_aud_file.value['name']
466
+ htm_audio = f'<audio><source src={tmp_aud_file_url} type="audio/mp3"></audio>'
467
+ audio_html = gr.HTML(htm_audio)
468
+
469
+ with gr.Column(scale=3):
470
+ chatbot = gr.Chatbot()
471
+
472
+ with gr.Row():
473
+ message = gr.Textbox(label="What's on your mind??",
474
+ placeholder="What's the answer to life, the universe, and everything?",
475
+ lines=1)
476
+ submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
477
+
478
+ # UNCOMMENT TO USE WHISPER
479
+ # with gr.Row():
480
+ # audio_comp = gr.Microphone(source="microphone", type="filepath", label="Just say it!",
481
+ # interactive=True, streaming=False)
482
+ # audio_comp.change(transcribe, inputs=[audio_comp], outputs=[message])
483
+
484
+ gr.Examples(
485
+ examples=["How many people live in Canada?",
486
+ "What is 2 to the 30th power?",
487
+ "How much did it rain in SF today?",
488
+ "Get me information about the movie 'Avatar'",
489
+ "What are the top tech headlines in the US?",
490
+ "On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses - "
491
+ "if I remove all the pairs of sunglasses from the desk, how many purple items remain on it?"],
492
+ inputs=message
493
+ )
494
+
495
+ with gr.Tab("Settings"):
496
+ tools_cb_group = gr.CheckboxGroup(label="Tools:", choices=TOOLS_LIST,
497
+ value=TOOLS_DEFAULT_LIST)
498
+ tools_cb_group.change(update_selected_tools,
499
+ inputs=[tools_cb_group, tools_list_state, llm_state],
500
+ outputs=[tools_list_state, llm_state, chain_state, express_chain_state])
501
+
502
+ trace_chain_cb = gr.Checkbox(label="Show reasoning chain in chat bubble", value=False)
503
+ trace_chain_cb.change(update_foo, inputs=[trace_chain_cb, trace_chain_state],
504
+ outputs=[trace_chain_state])
505
+
506
+ speak_text_cb = gr.Checkbox(label="Speak text from agent", value=False)
507
+ speak_text_cb.change(update_foo, inputs=[speak_text_cb, speak_text_state],
508
+ outputs=[speak_text_state])
509
+
510
+ with gr.Tab("Formality"):
511
+ formality_radio = gr.Radio(label="Formality:",
512
+ choices=[FORMALITY_DEFAULT, "Casual", "Polite", "Honorific"],
513
+ value=FORMALITY_DEFAULT)
514
+ formality_radio.change(update_foo,
515
+ inputs=[formality_radio, formality_state],
516
+ outputs=[formality_state])
517
+
518
+ with gr.Tab("Translate to"):
519
+ translate_to_radio = gr.Radio(label="Translate to:", choices=[
520
+ TRANSLATE_TO_DEFAULT, "Arabic", "Arabic (Gulf)", "Catalan", "Chinese (Cantonese)", "Chinese (Mandarin)",
521
+ "Danish", "Dutch", "English (Australian)", "English (British)", "English (Indian)", "English (New Zealand)",
522
+ "English (South African)", "English (US)", "English (Welsh)", "Finnish", "French", "French (Canadian)",
523
+ "German", "German (Austrian)", "Hindi", "Icelandic", "Italian", "Japanese", "Korean", "Norwegian", "Polish",
524
+ "Portuguese (Brazilian)", "Portuguese (European)", "Romanian", "Russian", "Spanish (European)",
525
+ "Spanish (Mexican)", "Spanish (US)", "Swedish", "Turkish", "Ukrainian", "Welsh",
526
+ "emojis", "Gen Z slang", "how the stereotypical Karen would say it", "Klingon",
527
+ "Pirate", "Strange Planet expospeak technical talk", "Yoda"],
528
+ value=TRANSLATE_TO_DEFAULT)
529
+
530
+ translate_to_radio.change(update_foo,
531
+ inputs=[translate_to_radio, translate_to_state],
532
+ outputs=[translate_to_state])
533
+
534
+ with gr.Tab("Lit style"):
535
+ literary_style_radio = gr.Radio(label="Literary style:", choices=[
536
+ LITERARY_STYLE_DEFAULT, "Prose", "Summary", "Outline", "Bullets", "Poetry", "Haiku", "Limerick", "Joke",
537
+ "Knock-knock"],
538
+ value=LITERARY_STYLE_DEFAULT)
539
+
540
+ literary_style_radio.change(update_foo,
541
+ inputs=[literary_style_radio, literary_style_state],
542
+ outputs=[literary_style_state])
543
+
544
+ with gr.Tab("Emotions"):
545
+ anticipation_level_radio = gr.Radio(label="Anticipation level:",
546
+ choices=[EMOTION_DEFAULT, "Interest", "Anticipation", "Vigilance"],
547
+ value=EMOTION_DEFAULT)
548
+ anticipation_level_radio.change(update_foo,
549
+ inputs=[anticipation_level_radio, anticipation_level_state],
550
+ outputs=[anticipation_level_state])
551
+
552
+ joy_level_radio = gr.Radio(label="Joy level:",
553
+ choices=[EMOTION_DEFAULT, "Serenity", "Joy", "Ecstasy"],
554
+ value=EMOTION_DEFAULT)
555
+ joy_level_radio.change(update_foo,
556
+ inputs=[joy_level_radio, joy_level_state],
557
+ outputs=[joy_level_state])
558
+
559
+ trust_level_radio = gr.Radio(label="Trust level:",
560
+ choices=[EMOTION_DEFAULT, "Acceptance", "Trust", "Admiration"],
561
+ value=EMOTION_DEFAULT)
562
+ trust_level_radio.change(update_foo,
563
+ inputs=[trust_level_radio, trust_level_state],
564
+ outputs=[trust_level_state])
565
+
566
+ fear_level_radio = gr.Radio(label="Fear level:",
567
+ choices=[EMOTION_DEFAULT, "Apprehension", "Fear", "Terror"],
568
+ value=EMOTION_DEFAULT)
569
+ fear_level_radio.change(update_foo,
570
+ inputs=[fear_level_radio, fear_level_state],
571
+ outputs=[fear_level_state])
572
+
573
+ surprise_level_radio = gr.Radio(label="Surprise level:",
574
+ choices=[EMOTION_DEFAULT, "Distraction", "Surprise", "Amazement"],
575
+ value=EMOTION_DEFAULT)
576
+ surprise_level_radio.change(update_foo,
577
+ inputs=[surprise_level_radio, surprise_level_state],
578
+ outputs=[surprise_level_state])
579
+
580
+ sadness_level_radio = gr.Radio(label="Sadness level:",
581
+ choices=[EMOTION_DEFAULT, "Pensiveness", "Sadness", "Grief"],
582
+ value=EMOTION_DEFAULT)
583
+ sadness_level_radio.change(update_foo,
584
+ inputs=[sadness_level_radio, sadness_level_state],
585
+ outputs=[sadness_level_state])
586
+
587
+ disgust_level_radio = gr.Radio(label="Disgust level:",
588
+ choices=[EMOTION_DEFAULT, "Boredom", "Disgust", "Loathing"],
589
+ value=EMOTION_DEFAULT)
590
+ disgust_level_radio.change(update_foo,
591
+ inputs=[disgust_level_radio, disgust_level_state],
592
+ outputs=[disgust_level_state])
593
+
594
+ anger_level_radio = gr.Radio(label="Anger level:",
595
+ choices=[EMOTION_DEFAULT, "Annoyance", "Anger", "Rage"],
596
+ value=EMOTION_DEFAULT)
597
+ anger_level_radio.change(update_foo,
598
+ inputs=[anger_level_radio, anger_level_state],
599
+ outputs=[anger_level_state])
600
+
601
+ with gr.Tab("Max words"):
602
+ num_words_slider = gr.Slider(label="Max number of words to generate (0 for don't care)",
603
+ value=NUM_WORDS_DEFAULT, minimum=0, maximum=MAX_WORDS, step=10)
604
+ num_words_slider.change(update_foo,
605
+ inputs=[num_words_slider, num_words_state],
606
+ outputs=[num_words_state])
607
+
608
+ gr.HTML("""
609
+ This application demonstrates a conversational agent implemented with OpenAI GPT-3.5 and LangChain.
610
+ When necessary, it leverages tools for complex math, searching the internet, and accessing news and weather.
611
+ On a desktop, the agent will often speak using using an animated avatar from
612
+ <a href='https://exh.ai/'>Ex-Human</a>.""")
613
+
614
+ gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
615
+
616
+ message.submit(chat, inputs=[openai_api_key_textbox, message, history_state, chain_state, trace_chain_state, speak_text_state,
617
+ express_chain_state, num_words_state, formality_state,
618
+ anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
619
+ surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
620
+ translate_to_state, literary_style_state],
621
+ # outputs=[chatbot, history_state, video_html, my_file, message])
622
+ outputs=[chatbot, history_state, audio_html, tmp_aud_file, message])
623
+
624
+ submit.click(chat, inputs=[openai_api_key_textbox, message, history_state, chain_state, trace_chain_state, speak_text_state,
625
+ express_chain_state, num_words_state, formality_state,
626
+ anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
627
+ surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
628
+ translate_to_state, literary_style_state],
629
+ # outputs=[chatbot, history_state, video_html, my_file, message])
630
+ outputs=[chatbot, history_state, audio_html, tmp_aud_file, message])
631
+
632
+ openai_api_key_textbox.change(set_openai_api_key,
633
+ inputs=[openai_api_key_textbox],
634
+ outputs=[chain_state, express_chain_state, llm_state])
 
 
 
 
635
 
636
  block.launch(debug=True)