Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 = "facebook/wav2vec2-base-100h"
|
| 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 |
+
placeholder="متن نوشتاری گفتار شما",
|
| 48 |
+
label="متن نوشتاری گفتار شما",
|
| 49 |
+
show_label=True,
|
| 50 |
+
container=True,
|
| 51 |
+
text_align="right",
|
| 52 |
+
show_copy_button=True,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
title = "Speech-to-Text (persian)"
|
| 56 |
+
description = "Upload a prsian audio, and let AI do the hard work of transcribing."
|
| 57 |
+
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>"
|
| 58 |
+
|
| 59 |
+
demo = gr.Interface(fn=parse, inputs = input_, outputs=txtbox, title=title, description=description, article = article,
|
| 60 |
+
streaming=True, interactive=True,
|
| 61 |
+
analytics_enabled=False, show_tips=False, enable_queue=True)
|
| 62 |
+
demo.launch(share=True)
|