Chandranshu Jain
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from IPython.display import Audio as IPythonAudio
|
| 6 |
+
|
| 7 |
+
#Audio to text
|
| 8 |
+
asr = pipeline(task="automatic-speech-recognition",
|
| 9 |
+
model="distil-whisper/distil-small.en")
|
| 10 |
+
#Text to text
|
| 11 |
+
translator = pipeline(task="translation",
|
| 12 |
+
model="facebook/nllb-200-distilled-600M",
|
| 13 |
+
torch_dtype=torch.bfloat16)
|
| 14 |
+
#Text to audio
|
| 15 |
+
pipe = pipeline("text-to-speech", model="suno/bark-small")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
demo = gr.Blocks()
|
| 19 |
+
def transcribe_speech(filepath):
|
| 20 |
+
if filepath is None:
|
| 21 |
+
gr.Warning("No audio found, please retry.")
|
| 22 |
+
return ""
|
| 23 |
+
output = translator(asr(filepath)["text"],
|
| 24 |
+
src_lang="eng_Latn",
|
| 25 |
+
tgt_lang="hin_Deva")
|
| 26 |
+
narrated_text=pipe(output[0]['translation_text'])
|
| 27 |
+
output=IPythonAudio(narrated_text["audio"][0],
|
| 28 |
+
rate=narrated_text["sampling_rate"])
|
| 29 |
+
return output
|
| 30 |
+
|
| 31 |
+
mic_transcribe = gr.Interface(
|
| 32 |
+
fn=transcribe_speech,
|
| 33 |
+
inputs=gr.Audio(sources="microphone",
|
| 34 |
+
type="filepath"),
|
| 35 |
+
outputs=gr.Audio(label="Translated Message"),
|
| 36 |
+
allow_flagging="never")
|
| 37 |
+
|
| 38 |
+
file_transcribe = gr.Interface(
|
| 39 |
+
fn=transcribe_speech,
|
| 40 |
+
inputs=gr.Audio(sources="upload",
|
| 41 |
+
type="filepath"),
|
| 42 |
+
outputs=gr.Audio(label="Translated Message"),
|
| 43 |
+
allow_flagging="never",
|
| 44 |
+
)
|
| 45 |
+
with demo:
|
| 46 |
+
gr.TabbedInterface(
|
| 47 |
+
[mic_transcribe,
|
| 48 |
+
file_transcribe],
|
| 49 |
+
["Transcribe Microphone",
|
| 50 |
+
"Transcribe Audio File"],
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
demo.launch(share=True)
|
| 54 |
+
demo.close()
|