Spaces:
Runtime error
Runtime error
harshitv804
commited on
Commit
•
0e24f14
1
Parent(s):
c28927c
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
3 |
+
import torchaudio
|
4 |
+
|
5 |
+
def translate(audio):
|
6 |
+
model_id_asr = "openai/whisper-medium"
|
7 |
+
processor_asr = WhisperProcessor.from_pretrained(model_id_asr)
|
8 |
+
model_asr = WhisperForConditionalGeneration.from_pretrained(model_id_asr)
|
9 |
+
forced_decoder_ids = processor_asr.get_decoder_prompt_ids(language="tamil", task="translate")
|
10 |
+
input_features = processor_asr(audio["audio"]["array"], sampling_rate=audio["audio"]["sampling_rate"], return_tensors="pt").input_features
|
11 |
+
predicted_ids = model_asr.generate(input_features,forced_decoder_ids=forced_decoder_ids)
|
12 |
+
transcription = processor_asr.batch_decode(predicted_ids, skip_special_tokens=True)
|
13 |
+
return transcription[0]
|
14 |
+
|
15 |
+
def speech_to_speech_translation(audio_filepath):
|
16 |
+
waveform, sampling_rate = torchaudio.load(audio_filepath)
|
17 |
+
if sampling_rate != 16000:
|
18 |
+
resampler = torchaudio.transforms.Resample(orig_freq=sampling_rate, new_freq=16000)
|
19 |
+
waveform = resampler(waveform)
|
20 |
+
sampling_rate = 16000
|
21 |
+
audio_dict = {
|
22 |
+
"audio": {
|
23 |
+
"array": waveform.numpy(),
|
24 |
+
"sampling_rate": sampling_rate
|
25 |
+
}
|
26 |
+
}
|
27 |
+
translated_text = translate(audio_dict)
|
28 |
+
return translated_text
|
29 |
+
|
30 |
+
demo = gr.Blocks()
|
31 |
+
|
32 |
+
mic_translate = gr.Interface(
|
33 |
+
fn=speech_to_speech_translation,
|
34 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
35 |
+
outputs="text",allow_flagging="never")
|
36 |
+
|
37 |
+
with demo:
|
38 |
+
gr.TabbedInterface([mic_translate], ["Local Tamil Translator"])
|
39 |
+
demo.launch(debug=True, share=False)
|