Harveenchadha commited on
Commit
f0af1cc
1 Parent(s): 0798a11

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import soundfile as sf
2
+ import torch
3
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
4
+ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
5
+ import gradio as gr
6
+ import sox
7
+
8
+ def convert(inputfile, outfile):
9
+ sox_tfm = sox.Transformer()
10
+ sox_tfm.set_output_format(
11
+ file_type="wav", channels=1, encoding="signed-integer", rate=16000, bits=16
12
+ )
13
+ sox_tfm.build(inputfile, outfile)
14
+ def parse_transcription(wav_file):
15
+ filename = wav_file.name.split('.')[0]
16
+ convert(wav_file.name, filename + "16k.wav")
17
+ speech, _ = sf.read(filename + "16k.wav")
18
+ input_values = processor(speech, sampling_rate=16_000, return_tensors="pt").input_values
19
+ logits = model(input_values).logits
20
+ predicted_ids = torch.argmax(logits, dim=-1)
21
+ transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
22
+ return transcription
23
+
24
+
25
+ model_translate = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
26
+ tokenizer_translate = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
27
+ inlang='hi'
28
+ outlang='en'
29
+ tokenizer_translate.src_lang = inlang
30
+
31
+ def translate(text):
32
+ encoded_hi = tokenizer_translate(text, return_tensors="pt")
33
+ generated_tokens = model_translate.generate(**encoded_hi, forced_bos_token_id=tokenizer_translate.get_lang_id(outlang))
34
+ return tokenizer_translate.batch_decode(generated_tokens, skip_special_tokens=True)[0]
35
+
36
+
37
+ processor = Wav2Vec2Processor.from_pretrained("Harveenchadha/vakyansh-wav2vec2-hindi-him-4200")
38
+ model = Wav2Vec2ForCTC.from_pretrained("Harveenchadha/vakyansh-wav2vec2-hindi-him-4200")
39
+
40
+ output1 = gr.outputs.Textbox(label="Hindi Output from ASR")
41
+ output2 = gr.outputs.Textbox(label="English Translated Output")
42
+
43
+ input_ = gr.inputs.Audio(source="microphone", type="file")
44
+ #gr.Interface(parse_transcription, inputs = input_, outputs="text",
45
+ # analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False);
46
+
47
+ gr.Interface(parse_transcription, inputs = input_, outputs=[output1, output2], analytics_enabled=False,
48
+ show_tips=False,
49
+ theme='huggingface',
50
+ layout='vertical',
51
+ title="Vakyansh: Speech To text for Indic Languages",
52
+ description="This is a live demo for Speech to Text Translation. Models used: vakyansh wav2vec2 hindi + m2m100", enable_queue=True).launch( inline=False)