Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
import torch
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
import tempfile
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from gtts import gTTS
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
|
| 10 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 11 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
| 12 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
| 13 |
+
|
| 14 |
+
def text_and_speak(text):
|
| 15 |
+
speaker_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 16 |
+
speaker_embedding = torch.tensor(speaker_dataset[0]["xvector"]).unsqueeze(0) # Use the first speaker as an example
|
| 17 |
+
inputs = processor(text=text, return_tensors="pt")
|
| 18 |
+
speech_audio = model.generate_speech(inputs["input_ids"], speaker_embedding, vocoder=vocoder)
|
| 19 |
+
#sf.write("speech.wav", speech_audio.numpy(), samplerate=16000)
|
| 20 |
+
# Save the audio to a temporary file
|
| 21 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
|
| 22 |
+
sf.write(temp_file.name, speech_audio.numpy(), samplerate=16000)
|
| 23 |
+
audio_path = temp_file.name
|
| 24 |
+
|
| 25 |
+
return audio_path
|
| 26 |
+
def text_and_speak(text):
|
| 27 |
+
# Function to generate audio from text
|
| 28 |
+
tts = gTTS(text)
|
| 29 |
+
audio_file = BytesIO()
|
| 30 |
+
tts.write_to_fp(audio_file)
|
| 31 |
+
audio_file.seek(0)
|
| 32 |
+
return audio_file
|
| 33 |
+
|
| 34 |
+
st.title("Text to Speech")
|
| 35 |
+
input_text = st.text_area("Input text", height=200)
|
| 36 |
+
if st.button("Generate Audio"):
|
| 37 |
+
if input_text.strip():
|
| 38 |
+
audio = text_and_speak(input_text)
|
| 39 |
+
st.audio(audio, format="audio/mp3", start_time=0)
|
| 40 |
+
else:
|
| 41 |
+
st.warning("Please enter some text to generate audio.")
|