Spaces:
Runtime error
Runtime error
Sandiago21
commited on
Commit
•
974f879
1
Parent(s):
c54a4dd
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- app.py +98 -0
- example.wav +0 -0
- packages.txt +2 -0
- requirements.txt +6 -0
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji: 🌖
|
4 |
-
colorFrom: indigo
|
5 |
-
colorTo: yellow
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 3.36.1
|
8 |
app_file: app.py
|
9 |
-
|
|
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: speech-to-speech-translation-italian
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 3.36.0
|
6 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from datasets import load_dataset
|
5 |
+
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
6 |
+
|
7 |
+
|
8 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
9 |
+
|
10 |
+
# load speech translation checkpoint
|
11 |
+
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2", device=device)
|
12 |
+
|
13 |
+
# load text-to-speech checkpoint and speaker embeddings
|
14 |
+
model_id = "Sandiago21/speecht5_finetuned_voxpopuli_it" # update with your model id
|
15 |
+
# pipe = pipeline("automatic-speech-recognition", model=model_id)
|
16 |
+
model = SpeechT5ForTextToSpeech.from_pretrained(model_id)
|
17 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
18 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
19 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7440]["xvector"]).unsqueeze(0)
|
20 |
+
|
21 |
+
checkpoint = "microsoft/speecht5_tts"
|
22 |
+
processor = SpeechT5Processor.from_pretrained(checkpoint)
|
23 |
+
|
24 |
+
replacements = [
|
25 |
+
("á", "a"),
|
26 |
+
("ç", "c"),
|
27 |
+
("è", "e"),
|
28 |
+
("ì", "i"),
|
29 |
+
("í", "i"),
|
30 |
+
("ò", "o"),
|
31 |
+
("ó", "o"),
|
32 |
+
("ù", "u"),
|
33 |
+
("ú", "u"),
|
34 |
+
("š", "s"),
|
35 |
+
("ï", "i"),
|
36 |
+
]
|
37 |
+
|
38 |
+
def cleanup_text(text):
|
39 |
+
for src, dst in replacements:
|
40 |
+
text = text.replace(src, dst)
|
41 |
+
return text
|
42 |
+
|
43 |
+
def synthesize_speech(text):
|
44 |
+
text = cleanup_text(text)
|
45 |
+
inputs = processor(text=text, return_tensors="pt")
|
46 |
+
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
47 |
+
|
48 |
+
return gr.Audio.update(value=(16000, speech.cpu().numpy()))
|
49 |
+
|
50 |
+
def translate(audio):
|
51 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
|
52 |
+
return outputs["text"]
|
53 |
+
|
54 |
+
|
55 |
+
def synthesise(text):
|
56 |
+
text = cleanup_text(text)
|
57 |
+
inputs = processor(text=text, return_tensors="pt")
|
58 |
+
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
59 |
+
return speech.cpu()
|
60 |
+
|
61 |
+
|
62 |
+
def speech_to_speech_translation(audio):
|
63 |
+
translated_text = translate(audio)
|
64 |
+
synthesised_speech = synthesise(translated_text)
|
65 |
+
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
66 |
+
return 16000, synthesised_speech
|
67 |
+
|
68 |
+
|
69 |
+
title = "Cascaded STST"
|
70 |
+
description = """
|
71 |
+
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in English. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and Microsoft's
|
72 |
+
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
|
73 |
+
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
74 |
+
"""
|
75 |
+
|
76 |
+
demo = gr.Blocks()
|
77 |
+
|
78 |
+
mic_translate = gr.Interface(
|
79 |
+
fn=speech_to_speech_translation,
|
80 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
81 |
+
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
82 |
+
title=title,
|
83 |
+
description=description,
|
84 |
+
)
|
85 |
+
|
86 |
+
file_translate = gr.Interface(
|
87 |
+
fn=speech_to_speech_translation,
|
88 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
89 |
+
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
90 |
+
examples=[["./example.wav"]],
|
91 |
+
title=title,
|
92 |
+
description=description,
|
93 |
+
)
|
94 |
+
|
95 |
+
with demo:
|
96 |
+
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
97 |
+
|
98 |
+
demo.launch()
|
example.wav
ADDED
Binary file (263 kB). View file
|
|
packages.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
ffmpeg
|
2 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
git+https://github.com/huggingface/transformers
|
3 |
+
datasets
|
4 |
+
torchaudio
|
5 |
+
sentencepiece
|
6 |
+
|