Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,20 @@
|
|
1 |
-
# Install necessary libraries (if not installed)
|
2 |
-
|
3 |
|
4 |
-
import gradio as gr
|
5 |
import torch
|
6 |
import soundfile as sf
|
|
|
7 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor, SpeechT5HifiGan
|
8 |
|
9 |
-
# Load
|
10 |
model = SpeechT5ForTextToSpeech.from_pretrained("krishna195/speecht5_krishna_finatuned")
|
11 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
12 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
13 |
|
14 |
-
#
|
15 |
-
speaker_embeddings = torch.randn(1, 512) # Example
|
16 |
|
17 |
-
# Function to generate speech from
|
18 |
def text_to_speech(input_text):
|
19 |
# Process the input text
|
20 |
inputs = processor(text=input_text, return_tensors="pt")
|
@@ -22,23 +22,26 @@ def text_to_speech(input_text):
|
|
22 |
# Generate speech using the model and vocoder
|
23 |
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
24 |
|
25 |
-
# Save the
|
26 |
output_file = "generated_speech.wav"
|
27 |
sf.write(output_file, speech.numpy(), 16000)
|
28 |
|
29 |
-
# Return the path to the audio file for Gradio to play
|
30 |
return output_file
|
31 |
|
32 |
-
# Create Gradio UI
|
33 |
iface = gr.Interface(
|
34 |
fn=text_to_speech,
|
35 |
-
inputs=
|
36 |
outputs="audio",
|
37 |
-
title="Text to Speech
|
38 |
-
description="Enter
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
)
|
42 |
|
43 |
-
# Launch the Gradio
|
44 |
iface.launch()
|
|
|
1 |
+
# Install necessary libraries (if not already installed)
|
2 |
+
!pip install gradio transformers soundfile torch
|
3 |
|
|
|
4 |
import torch
|
5 |
import soundfile as sf
|
6 |
+
import gradio as gr
|
7 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor, SpeechT5HifiGan
|
8 |
|
9 |
+
# Load your fine-tuned model, processor, and vocoder
|
10 |
model = SpeechT5ForTextToSpeech.from_pretrained("krishna195/speecht5_krishna_finatuned")
|
11 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
12 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
13 |
|
14 |
+
# Use pre-defined speaker embeddings (you can replace this with your actual embeddings)
|
15 |
+
speaker_embeddings = torch.randn(1, 512) # Example embedding size, adjust to your speaker embeddings
|
16 |
|
17 |
+
# Function to generate speech from text
|
18 |
def text_to_speech(input_text):
|
19 |
# Process the input text
|
20 |
inputs = processor(text=input_text, return_tensors="pt")
|
|
|
22 |
# Generate speech using the model and vocoder
|
23 |
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
24 |
|
25 |
+
# Save the generated speech to a temporary file
|
26 |
output_file = "generated_speech.wav"
|
27 |
sf.write(output_file, speech.numpy(), 16000)
|
28 |
|
29 |
+
# Return the path to the audio file for Gradio to play
|
30 |
return output_file
|
31 |
|
32 |
+
# Create the Gradio UI interface
|
33 |
iface = gr.Interface(
|
34 |
fn=text_to_speech,
|
35 |
+
inputs="text",
|
36 |
outputs="audio",
|
37 |
+
title="Text to Speech Converter",
|
38 |
+
description="Enter text and convert it into speech using a fine-tuned SpeechT5 model.",
|
39 |
+
examples=[
|
40 |
+
["Hello, how are you doing today?"],
|
41 |
+
["Speech synthesis is amazing with deep learning models."],
|
42 |
+
["TensorFlow and PyTorch are powerful machine learning frameworks."]
|
43 |
+
]
|
44 |
)
|
45 |
|
46 |
+
# Launch the Gradio app
|
47 |
iface.launch()
|