tsobolev commited on
Commit
d6d8c9a
1 Parent(s): 34aca44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -49
app.py CHANGED
@@ -21,52 +21,55 @@ embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validat
21
  speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
22
 
23
 
24
- def translate(audio):
25
- outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
26
- return outputs["text"]
27
-
28
-
29
- def synthesise(text):
30
- inputs = processor(text=text, return_tensors="pt")
31
- speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
32
- return speech.cpu()
33
-
34
-
35
- def speech_to_speech_translation(audio):
36
- translated_text = translate(audio)
37
- synthesised_speech = synthesise(translated_text)
38
- synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
39
- return 16000, synthesised_speech
40
-
41
-
42
- title = "Cascaded STST"
43
- description = """
44
- 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
45
- [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
46
-
47
- ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
48
- """
49
-
50
- demo = gr.Blocks()
51
-
52
- mic_translate = gr.Interface(
53
- fn=speech_to_speech_translation,
54
- inputs=gr.Audio(source="microphone", type="filepath"),
55
- outputs=gr.Audio(label="Generated Speech", type="numpy"),
56
- title=title,
57
- description=description,
58
- )
59
-
60
- file_translate = gr.Interface(
61
- fn=speech_to_speech_translation,
62
- inputs=gr.Audio(source="upload", type="filepath"),
63
- outputs=gr.Audio(label="Generated Speech", type="numpy"),
64
- examples=[["./example.wav"]],
65
- title=title,
66
- description=description,
67
- )
68
-
69
- with demo:
70
- gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
71
-
72
- demo.launch()
 
 
 
 
21
  speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
22
 
23
 
24
+ print(speaker_embeddings.shape)
25
+
26
+ if 0:
27
+ def translate(audio):
28
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
29
+ return outputs["text"]
30
+
31
+
32
+ def synthesise(text):
33
+ inputs = processor(text=text, return_tensors="pt")
34
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
35
+ return speech.cpu()
36
+
37
+
38
+ def speech_to_speech_translation(audio):
39
+ translated_text = translate(audio)
40
+ synthesised_speech = synthesise(translated_text)
41
+ synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
42
+ return 16000, synthesised_speech
43
+
44
+
45
+ title = "Cascaded STST"
46
+ description = """
47
+ 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
48
+ [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
49
+
50
+ ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
51
+ """
52
+
53
+ demo = gr.Blocks()
54
+
55
+ mic_translate = gr.Interface(
56
+ fn=speech_to_speech_translation,
57
+ inputs=gr.Audio(source="microphone", type="filepath"),
58
+ outputs=gr.Audio(label="Generated Speech", type="numpy"),
59
+ title=title,
60
+ description=description,
61
+ )
62
+
63
+ file_translate = gr.Interface(
64
+ fn=speech_to_speech_translation,
65
+ inputs=gr.Audio(source="upload", type="filepath"),
66
+ outputs=gr.Audio(label="Generated Speech", type="numpy"),
67
+ examples=[["./example.wav"]],
68
+ title=title,
69
+ description=description,
70
+ )
71
+
72
+ with demo:
73
+ gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
74
+
75
+ demo.launch()