Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from datasets import load_dataset
|
4 |
+
from transformers import SpeechT5Processor, SpeechT5HifiGan, SpeechT5ForTextToSpeech
|
5 |
+
|
6 |
+
# Load the fine-tuned model and vocoder for Italian
|
7 |
+
model_id = "Sandiago21/speecht5_finetuned_voxpopuli_it"
|
8 |
+
model = SpeechT5ForTextToSpeech.from_pretrained(model_id)
|
9 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
10 |
+
|
11 |
+
# Load speaker embeddings dataset
|
12 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
13 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7440]["xvector"]).unsqueeze(0)
|
14 |
+
|
15 |
+
# Load processor for the Italian model
|
16 |
+
processor = SpeechT5Processor.from_pretrained(model_id)
|
17 |
+
|
18 |
+
# Optional: Text cleanup for Italian-specific characters
|
19 |
+
replacements = [
|
20 |
+
("à", "a"),
|
21 |
+
("è", "e"),
|
22 |
+
("é", "e"),
|
23 |
+
("ì", "i"),
|
24 |
+
("ò", "o"),
|
25 |
+
("ù", "u"),
|
26 |
+
]
|
27 |
+
|
28 |
+
# Text-to-speech synthesis function
|
29 |
+
def synthesize_speech(text):
|
30 |
+
# Clean up text
|
31 |
+
for src, dst in replacements:
|
32 |
+
text = text.replace(src, dst)
|
33 |
+
|
34 |
+
# Process input text
|
35 |
+
inputs = processor(text=text, return_tensors="pt")
|
36 |
+
|
37 |
+
# Generate speech using the model and vocoder
|
38 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
39 |
+
|
40 |
+
# Return the generated speech as (sample_rate, audio_array)
|
41 |
+
return (16000, speech.cpu().numpy())
|
42 |
+
|
43 |
+
# Title and description for the Gradio interface
|
44 |
+
title = "Italian Text-to-Speech with SpeechT5"
|
45 |
+
description = """
|
46 |
+
This demo generates speech in Italian using the fine-tuned SpeechT5 model from Hugging Face.
|
47 |
+
The model is fine-tuned on the VoxPopuli Italian dataset.
|
48 |
+
"""
|
49 |
+
|
50 |
+
# Create Gradio interface
|
51 |
+
interface = gr.Interface(
|
52 |
+
fn=synthesize_speech,
|
53 |
+
inputs=gr.Textbox(label="Input Text", placeholder="Enter Italian text here..."),
|
54 |
+
outputs=gr.Audio(label="Generated Speech"),
|
55 |
+
title=title,
|
56 |
+
description=description,
|
57 |
+
examples=["Questa è una dimostrazione di sintesi vocale in italiano."]
|
58 |
+
)
|
59 |
+
|
60 |
+
# Launch the interface
|
61 |
+
interface.launch()
|