revolunet Sandiago21 commited on
Commit
7fee4a9
0 Parent(s):

Duplicate from Sandiago21/speech-to-speech-translation-italian

Browse files

Co-authored-by: Sandi Koka <[email protected]>

Files changed (6) hide show
  1. .gitattributes +35 -0
  2. README.md +7 -0
  3. app.py +97 -0
  4. example.wav +0 -0
  5. packages.txt +2 -0
  6. requirements.txt +6 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: speech-to-speech-translation-italian
3
+ app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 3.36.0
6
+ duplicated_from: Sandiago21/speech-to-speech-translation-italian
7
+ ---
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ processor = SpeechT5Processor.from_pretrained(model_id)
22
+
23
+ replacements = [
24
+ ("á", "a"),
25
+ ("ç", "c"),
26
+ ("è", "e"),
27
+ ("ì", "i"),
28
+ ("í", "i"),
29
+ ("ò", "o"),
30
+ ("ó", "o"),
31
+ ("ù", "u"),
32
+ ("ú", "u"),
33
+ ("š", "s"),
34
+ ("ï", "i"),
35
+ ]
36
+
37
+ def cleanup_text(text):
38
+ for src, dst in replacements:
39
+ text = text.replace(src, dst)
40
+ return text
41
+
42
+ def synthesize_speech(text):
43
+ text = cleanup_text(text)
44
+ inputs = processor(text=text, return_tensors="pt")
45
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
46
+
47
+ return gr.Audio.update(value=(16000, speech.cpu().numpy()))
48
+
49
+ def translate(audio):
50
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "italian"})
51
+ return outputs["text"]
52
+
53
+
54
+ def synthesise(text):
55
+ text = cleanup_text(text)
56
+ inputs = processor(text=text, return_tensors="pt")
57
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
58
+ return speech.cpu()
59
+
60
+
61
+ def speech_to_speech_translation(audio):
62
+ translated_text = translate(audio)
63
+ synthesised_speech = synthesise(translated_text)
64
+ synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
65
+ return 16000, synthesised_speech
66
+
67
+
68
+ title = "Cascaded STST"
69
+ description = """
70
+ Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in Italian. Demo uses OpenAI's [Whisper Large v2](https://huggingface.co/openai/whisper-large-v2) model for speech translation, and [Sandiago21/speecht5_finetuned_voxpopuli_it](https://huggingface.co/Sandiago21/speecht5_finetuned_voxpopuli_it) checkpoint for text-to-speech, which is based on Microsoft's
71
+ [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech, fine-tuned in Italian Audio dataset:
72
+ ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
73
+ """
74
+
75
+ demo = gr.Blocks()
76
+
77
+ mic_translate = gr.Interface(
78
+ fn=speech_to_speech_translation,
79
+ inputs=gr.Audio(source="microphone", type="filepath"),
80
+ outputs=gr.Audio(label="Generated Speech", type="numpy"),
81
+ title=title,
82
+ description=description,
83
+ )
84
+
85
+ file_translate = gr.Interface(
86
+ fn=speech_to_speech_translation,
87
+ inputs=gr.Audio(source="upload", type="filepath"),
88
+ outputs=gr.Audio(label="Generated Speech", type="numpy"),
89
+ examples=[["./example.wav"]],
90
+ title=title,
91
+ description=description,
92
+ )
93
+
94
+ with demo:
95
+ gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
96
+
97
+ demo.launch()
example.wav ADDED
Binary file (603 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
+