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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -588,30 +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
-
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)
 
 
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
  # 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
+ history = [(input_text, None)] # Initialize history with user input
594
+ story_text = generate_local(input_text, history) # calls your generate_local function
595
+ # Serialize story_text to a single string
596
+ story_text = ' '.join(sentence for sentence, _ in story_text)
597
+
598
+ synthesized_speech = generate_speech_for_sentence(history, chatbot_role, story_text)
599
+ # generate_speech_for_sentence returns a tuple, where the second item is a gr.Audio object
600
+ speech_audio_bytes = synthesized_speech[1].data.getvalue() # Access the BytesIO object and extract bytes
601
+
602
+ # Convert the speech to base64 to include in JSON response
603
  speech_audio_base64 = base64.b64encode(speech_audio_bytes).decode('utf8')
604
 
605
+ # Return JSON object with text and base64 audio
606
  return {"text": story_text, "audio": speech_audio_base64}
607
 
608
+ # Define your Gradio app API
609
+ iface = gr.Interface(
610
  fn=generate_story_and_speech,
611
+ inputs=[gr.Textbox(label="Enter your text"), gr.Dropdown(choices=ROLES, label="Chatbot Role")],
612
  outputs="json"
613
  )
614
 
615
+ # Launch the app
616
+ if __name__ == "__main__":
617
+ iface.launch(debug=True, enable_queue=True, api_mode=True)