jbilcke-hf HF staff commited on
Commit
abb29e6
1 Parent(s): a8336bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -20
app.py CHANGED
@@ -590,26 +590,32 @@ latent_map["Pirate"] = get_latents("voices/pirate_by_coqui.wav")
590
 
591
  # Define the main function for the API endpoint that takes the input text and chatbot role
592
  def generate_story_and_speech(input_text, chatbot_role):
593
- story_sentences = get_sentence(None, chatbot_role) # calls your get_sentence function
594
-
595
- # Initialize story_text
596
- story_text = ""
597
-
598
- # Iterate over sentences generated by get_sentence and concatenate them into story_text
599
- for sentence, _ in story_sentences:
600
- # Each 'sentence' is a tuple, where the first item is the text of the sentence
601
- story_text += sentence + ' '
602
-
603
- # Generate synthesized speech for the full story
604
- synthesized_speech = generate_speech_for_sentence(history, chatbot_role, story_text)
605
- # generate_speech_for_sentence returns a tuple, where the second item is a gr.Audio object
606
- speech_audio_bytes = synthesized_speech[1].data.getvalue() # Access the BytesIO object and extract bytes
607
-
608
- # Convert the speech to base64 to include in JSON response
609
- speech_audio_base64 = base64.b64encode(speech_audio_bytes).decode('utf8')
610
-
611
- # Return JSON object with text and base64 audio
612
- return {"text": story_text.strip(), "audio": speech_audio_base64}
 
 
 
 
 
 
613
 
614
  # Create a Gradio Interface using only the `generate_story_and_speech()` function and the 'json' output type
615
  demo = gr.Interface(
 
590
 
591
  # Define the main function for the API endpoint that takes the input text and chatbot role
592
  def generate_story_and_speech(input_text, chatbot_role):
593
+ # Initialize a list of lists for history with the user input as the first entry
594
+ history = [[input_text, None]]
595
+ story_sentences = get_sentence(history, chatbot_role) # get_sentence function generates text
596
+
597
+ story_text = "" # Initialize variable to hold the full story text
598
+ last_history = None # To store the last history after all sentences
599
+
600
+ # Iterate over the sentences generated by get_sentence and concatenate them
601
+ for sentence, updated_history in story_sentences:
602
+ if sentence:
603
+ story_text += sentence.strip() + " " # Add each sentence to the story_text
604
+ last_history = updated_history # Keep track of the last history update
605
+
606
+ if last_history is not None:
607
+ # Convert the list of lists back into a list of tuples for the history
608
+ history_tuples = [tuple(entry) for entry in last_history]
609
+ synthesized_speech = generate_speech_for_sentence(history_tuples, chatbot_role, story_text)
610
+ if synthesized_speech:
611
+ # Access the BytesIO object containing the WAV file and extract bytes
612
+ speech_audio = synthesized_speech[1]["value"] if return_as_byte else synthesized_speech[1].data.getvalue()
613
+ # Convert the speech audio bytes to base64 for JSON serialization
614
+ speech_audio_base64 = base64.b64encode(speech_audio).decode('utf8')
615
+
616
+ return {"text": story_text.strip(), "audio": speech_audio_base64}
617
+ else:
618
+ return {"text": "Failed to generate story", "audio": None}
619
 
620
  # Create a Gradio Interface using only the `generate_story_and_speech()` function and the 'json' output type
621
  demo = gr.Interface(