tahirsher commited on
Commit
c75d7b1
·
verified ·
1 Parent(s): 4fee66b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torchaudio
3
+ import streamlit as st
4
+ from tortoise.api import TextToSpeech
5
+
6
+ # Initialize Tortoise TTS
7
+ tts = TextToSpeech()
8
+
9
+ # Function to generate speech from text
10
+ def text_to_speech(text, preset="fast"):
11
+ st.write("Generating speech... This may take a while.")
12
+
13
+ # Generate speech using the Tortoise TTS API
14
+ generated = tts.tts_with_preset(text, preset=preset)
15
+
16
+ # Save the generated audio as a wav file using torchaudio
17
+ output_path = "output.wav"
18
+ torchaudio.save(output_path, generated.squeeze(0).cpu(), sample_rate=24000)
19
+ return output_path
20
+
21
+ # Streamlit interface
22
+ st.title("Text to Speech Converter")
23
+
24
+ # Take user input as text
25
+ text = st.text_input("Enter text you want to convert to speech", "Hello, welcome to the Tortoise TTS text-to-speech demonstration.")
26
+
27
+ # Button to generate speech
28
+ if st.button("Generate Speech"):
29
+ output_path = text_to_speech(text)
30
+
31
+ # Read and play the audio file in the Streamlit app
32
+ audio_file = open(output_path, 'rb')
33
+ audio_bytes = audio_file.read()
34
+ st.audio(audio_bytes, format='audio/wav')