gaetokk commited on
Commit
b4ae7f4
β€’
1 Parent(s): f1e14fe

first commit

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