jbilcke-hf HF staff commited on
Commit
cb57d1e
1 Parent(s): 0774599

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -52
app.py CHANGED
@@ -588,59 +588,30 @@ latent_map = {}
588
  latent_map["Julian"] = get_latents("voices/julian-bedtime-style-1.wav")
589
  latent_map["Pirate"] = get_latents("voices/pirate_by_coqui.wav")
590
 
591
- #### GRADIO INTERFACE ####
592
 
593
- with gr.Blocks(title=title) as demo:
594
- chatbot = gr.Chatbot(
595
- [],
596
- elem_id="chatbot",
597
- bubble_full_width=False,
598
- )
599
-
600
- chatbot_role = gr.Dropdown(
601
- label="Role of the Chatbot",
602
- info="How should Chatbot talk like",
603
- choices=ROLES,
604
- max_choices=1,
605
- value=ROLES[0],
606
- )
607
-
608
- txt = gr.Textbox(
609
- scale=3,
610
- show_label=False,
611
- placeholder="Enter text and press enter, or speak to your microphone",
612
- container=False,
613
- interactive=True,
614
- )
615
- txt_btn = gr.Button(value="Submit text", scale=1)
616
-
617
- with gr.Row():
618
- sentence = gr.Textbox(visible=False)
619
- audio = gr.Audio(
620
- value=None,
621
- label="Generated audio response",
622
- streaming=True,
623
- autoplay=True,
624
- interactive=False,
625
- show_label=True,
626
- )
627
-
628
- def clear_inputs(chatbot):
629
- return None
630
- clear_btn = gr.ClearButton([chatbot, audio])
631
- chatbot_role.change(fn=clear_inputs, inputs=[chatbot], outputs=[chatbot])
632
-
633
- txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
634
- generate_speech, [chatbot,chatbot_role], [chatbot,chatbot_role, sentence, audio]
635
- )
636
-
637
- txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
638
-
639
- txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
640
- generate_speech, [chatbot,chatbot_role], [chatbot,chatbot_role, sentence, audio]
641
- )
642
-
643
- txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
644
 
645
  demo.queue()
646
  demo.launch(debug=True)
 
588
  latent_map["Julian"] = get_latents("voices/julian-bedtime-style-1.wav")
589
  latent_map["Pirate"] = get_latents("voices/pirate_by_coqui.wav")
590
 
 
591
 
592
+ # Define the main function for the API endpoint that takes the input text and chatbot role
593
+ def generate_story_and_speech(input_text, chatbot_role):
594
+ # We assume that other necessary components have been initialized and are ready to use here
595
+
596
+ # Here, we'll integrate the story generation, language detection, and speech synthesis logic
597
+ # Let's assume `generate_story()` is a function that generates the story based on the input text
598
+ # And `synthesize_speech()` is a function that synthesizes speech from text
599
+ story_text = generate_story(input_text, chatbot_role)
600
+ language = detect_language(story_text)
601
+ speech_audio_bytes = synthesize_speech(story_text, language)
602
+
603
+ # Convert the speech to base64 to include in the JSON response
604
+ speech_audio_base64 = base64.b64encode(speech_audio_bytes).decode('utf8')
605
+
606
+ # Return the story and speech audio in base64 format
607
+ return {"text": story_text, "audio": speech_audio_base64}
608
+
609
+ # Create a Gradio Interface using only the `generate_story_and_speech()` function and the 'json' output type
610
+ demo = gr.Interface(
611
+ fn=generate_story_and_speech,
612
+ inputs=[gr.Textbox(placeholder="Enter your text here"), gr.Dropdown(choices=ROLES, label="Select Chatbot Role")],
613
+ outputs="json"
614
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
615
 
616
  demo.queue()
617
  demo.launch(debug=True)