nimool commited on
Commit
95759a3
1 Parent(s): 1ab159d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import soundfile as sf
2
+ import torch
3
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
4
+ import gradio as gr
5
+ import sox
6
+ import subprocess
7
+
8
+
9
+ def read_file_and_process(wav_file):
10
+ filename = wav_file.split('.')[0]
11
+ filename_16k = filename + "16k.wav"
12
+ resampler(wav_file, filename_16k)
13
+ speech, _ = sf.read(filename_16k)
14
+ inputs = processor(speech, sampling_rate=16_000, return_tensors="pt", padding=True)
15
+
16
+ return inputs
17
+
18
+
19
+ def resampler(input_file_path, output_file_path):
20
+ command = (
21
+ f"ffmpeg -hide_banner -loglevel panic -i {input_file_path} -ar 16000 -ac 1 -bits_per_raw_sample 16 -vn "
22
+ f"{output_file_path}"
23
+ )
24
+ subprocess.call(command, shell=True)
25
+
26
+
27
+ def parse_transcription(logits):
28
+ predicted_ids = torch.argmax(logits, dim=-1)
29
+ transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
30
+ return transcription
31
+
32
+
33
+ def parse(wav_file):
34
+ input_values = read_file_and_process(wav_file)
35
+ with torch.no_grad():
36
+ logits = model(**input_values).logits
37
+ return parse_transcription(logits)
38
+
39
+ model_id = "SeyedAli/Persian-Speech-Transcription-Wav2Vec2-V1"
40
+ processor = Wav2Vec2Processor.from_pretrained(model_id)
41
+ model = Wav2Vec2ForCTC.from_pretrained(model_id)
42
+
43
+ input_ = gr.Audio(source="microphone", type="filepath")
44
+ txtbox = gr.Textbox(
45
+ label="persian text output:",
46
+ lines=5
47
+ )
48
+
49
+ title = "Speech-to-Text (persian)"
50
+ description = "Upload a prsian audio, and let AI do the hard work of transcribing."
51
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2104.06678'>Large-Scale Self- and Semi-Supervised Learning for Speech Translation</a></p>"
52
+
53
+ demo = gr.Interface(fn=parse, inputs = input_, outputs=txtbox, title=title, description=description, article = article,
54
+ streaming=True, interactive=True,
55
+ analytics_enabled=False, show_tips=False, enable_queue=True)
56
+ demo.launch(share=True)