weiren119 commited on
Commit
1996d21
β€’
1 Parent(s): c89c7e7

initial commit

Browse files
Files changed (5) hide show
  1. README copy.md +13 -0
  2. app.py +82 -0
  3. example.wav +0 -0
  4. packages.txt +1 -0
  5. requirements.txt +4 -0
README copy.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Speech To Speech Translation
3
+ emoji: πŸ†
4
+ colorFrom: pink
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 3.36.1
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: course-demos/speech-to-speech-translation
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ from datasets import load_dataset
5
+ from transformers import (
6
+ SpeechT5ForTextToSpeech,
7
+ SpeechT5HifiGan,
8
+ SpeechT5Processor,
9
+ pipeline,
10
+ )
11
+
12
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
13
+
14
+ # load speech translation checkpoint
15
+ asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
16
+
17
+ # load text-to-speech checkpoint and speaker embeddings
18
+ processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
19
+
20
+
21
+ machine_translate_pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
22
+
23
+ model = SpeechT5ForTextToSpeech.from_pretrained("Sandiago21/speecht5_finetuned_facebook_voxpopuli_french").to(device)
24
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
25
+
26
+ embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
27
+ speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
28
+
29
+
30
+ def translate(audio):
31
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
32
+ return outputs["text"]
33
+
34
+ def machine_translate(text):
35
+ outputs = machine_translate_pipe(text)
36
+ return outputs[0]['translation_text']
37
+
38
+ def synthesise(text):
39
+ inputs = processor(text=text, return_tensors="pt")
40
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
41
+ return speech.cpu()
42
+
43
+
44
+ def speech_to_speech_translation(audio):
45
+ translated_text = translate(audio)
46
+ translated_text = machine_translate(translated_text)
47
+ synthesised_speech = synthesise(translated_text)
48
+ synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
49
+ return 16000, synthesised_speech
50
+
51
+
52
+ title = "Cascaded STST"
53
+ description = """
54
+ Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in French. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and Microsoft's
55
+ [SpeechT5 TTS finetuned for french by Sandiago](https://huggingface.co/Sandiago21/speecht5_finetuned_facebook_voxpopuli_french) model for text-to-speech:
56
+
57
+ ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
58
+ """
59
+
60
+ demo = gr.Blocks()
61
+
62
+ mic_translate = gr.Interface(
63
+ fn=speech_to_speech_translation,
64
+ inputs=gr.Audio(source="microphone", type="filepath"),
65
+ outputs=gr.Audio(label="Generated Speech", type="numpy"),
66
+ title=title,
67
+ description=description,
68
+ )
69
+
70
+ file_translate = gr.Interface(
71
+ fn=speech_to_speech_translation,
72
+ inputs=gr.Audio(source="upload", type="filepath"),
73
+ outputs=gr.Audio(label="Generated Speech", type="numpy"),
74
+ examples=[["./example.wav"]],
75
+ title=title,
76
+ description=description,
77
+ )
78
+
79
+ with demo:
80
+ gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
81
+
82
+ demo.launch()
example.wav ADDED
Binary file (263 kB). View file
 
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ git+https://github.com/huggingface/transformers
3
+ datasets
4
+ sentencepiece