JulienChoukroun commited on
Commit
1362d67
1 Parent(s): 3a12322

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ from datasets import load_dataset
5
+
6
+ from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
7
+
8
+
9
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
10
+ print("> DEVICE: ", device)
11
+
12
+ # load speech translation checkpoint
13
+ asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
14
+
15
+ # load text-to-speech checkpoint and speaker embeddings
16
+ processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
17
+
18
+ model = SpeechT5ForTextToSpeech.from_pretrained("jalal-elzein/speecht5_tts_voxpopuli_it_finetuned").to(device)
19
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
20
+
21
+ embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
22
+ speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
23
+
24
+
25
+ def translate(audio):
26
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate", "language": "italian"})
27
+ return outputs["text"]
28
+
29
+
30
+ def synthesise(text):
31
+ inputs = processor(text=text, return_tensors="pt")
32
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
33
+ return speech.cpu()
34
+
35
+
36
+ def speech_to_speech_translation(audio):
37
+ translated_text = translate(audio)
38
+ print("> Translated Text: ", translated_text)
39
+ synthesised_speech = synthesise(translated_text)
40
+ synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
41
+ return 16000, synthesised_speech
42
+
43
+
44
+ title = "Cascaded STST"
45
+ description = """
46
+ 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
47
+ [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
48
+ ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
49
+ """
50
+
51
+ demo = gr.Blocks()
52
+
53
+ mic_translate = gr.Interface(
54
+ fn=speech_to_speech_translation,
55
+ inputs=gr.Audio(source="microphone", type="filepath"),
56
+ outputs=gr.Audio(label="Generated Speech", type="numpy"),
57
+ title=title,
58
+ description=description,
59
+ )
60
+
61
+ file_translate = gr.Interface(
62
+ fn=speech_to_speech_translation,
63
+ inputs=gr.Audio(source="upload", type="filepath"),
64
+ outputs=gr.Audio(label="Generated Speech", type="numpy"),
65
+ examples=[["./example.wav"]],
66
+ title=title,
67
+ description=description,
68
+ )
69
+
70
+ with demo:
71
+ gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
72
+
73
+ demo.launch()