Commit
•
abb29e6
1
Parent(s):
a8336bb
Update app.py
Browse files
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 |
-
|
594 |
-
|
595 |
-
#
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|