VinayHajare
commited on
Commit
·
34e015f
1
Parent(s):
db8009d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from transformers import AutoProcessor, pipeline, BarkModel
|
5 |
+
|
6 |
+
ASR_MODEL_NAME = "VinayHajare/whisper-small-finetuned-common-voice-mr"
|
7 |
+
TTS_MODEL_NAME = "suno/bark-small"
|
8 |
+
BATCH_SIZE = 8
|
9 |
+
voices = {
|
10 |
+
"male" : "v2/en_speaker_6",
|
11 |
+
"female" : "v2/en_speaker_9"
|
12 |
+
}
|
13 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
14 |
+
|
15 |
+
# load speech translation checkpoint
|
16 |
+
asr_pipe = pipeline("automatic-speech-recognition", model=ASR_MODEL_NAME, chunk_length_s=30,device=device)
|
17 |
+
|
18 |
+
# load text-to-speech checkpoint
|
19 |
+
processor = AutoProcessor.from_pretrained("suno/bark-small")
|
20 |
+
model = BarkModel.from_pretrained("suno/bark-small").to(device)
|
21 |
+
sampling_rate = model.generation_config.sample_rate
|
22 |
+
|
23 |
+
def translate(audio):
|
24 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
|
25 |
+
return outputs["text"]
|
26 |
+
|
27 |
+
def synthesise(text, voice_preset):
|
28 |
+
inputs = processor(text=text, return_tensors="pt",voice_preset=voice_preset)
|
29 |
+
speech = model.generate(**inputs.to(device))
|
30 |
+
return speech[0]
|
31 |
+
|
32 |
+
def speech_to_speech_translation(audio, voice):
|
33 |
+
voice_preset = None
|
34 |
+
translated_text = translate(audio)
|
35 |
+
print(translated_text)
|
36 |
+
if voice == "Female":
|
37 |
+
voice_preset = voices["female"]
|
38 |
+
else:
|
39 |
+
voice_preset = voices["male"]
|
40 |
+
synthesised_speech = synthesise(translated_text, voice_preset)
|
41 |
+
synthesised_speech = (synthesised_speech.cpu().numpy() * 32767).astype(np.int16)
|
42 |
+
return sampling_rate, synthesised_speech
|
43 |
+
|
44 |
+
title = "Cascaded STST for Marathi to English"
|
45 |
+
description = """
|
46 |
+
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any Marathi to target speech in English. Demo uses OpenAI's [Whisper Small](https://huggingface.co/VinayHajare/whisper-small-finetuned-common-voice-mr) model for speech translation, and Suno's
|
47 |
+
[Bark-large](https://huggingface.co/suno/bark-small) model for text-to-speech:
|
48 |
+
|
49 |
+
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
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 |
+
gr.inputs.Radio(["Male", "Female"], label="Voice", default="Male")],
|
57 |
+
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
58 |
+
title=title,
|
59 |
+
description=description,
|
60 |
+
allow_flagging="never"
|
61 |
+
)
|
62 |
+
|
63 |
+
file_translate = gr.Interface(
|
64 |
+
fn=speech_to_speech_translation,
|
65 |
+
inputs=[gr.Audio(source="upload", type="filepath"),
|
66 |
+
gr.inputs.Radio(["Male", "Female"], label="Voice", default="Male")],
|
67 |
+
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
68 |
+
title=title,
|
69 |
+
description=description,
|
70 |
+
allow_flagging="never"
|
71 |
+
)
|
72 |
+
|
73 |
+
with demo:
|
74 |
+
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
75 |
+
|
76 |
+
demo.launch(enable_queue=True)
|