Spaces:
Runtime error
Runtime error
Upload 10 files
Browse files- app_text (2).py +56 -0
- requirements.txt +9 -0
app_text (2).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 |
+
|
40 |
+
model_id = "Harveenchadha/vakyansh-wav2vec2-hindi-him-4200"
|
41 |
+
processor = Wav2Vec2Processor.from_pretrained(model_id)
|
42 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_id)
|
43 |
+
|
44 |
+
input_ = gr.Audio(source="microphone", type="filepath")
|
45 |
+
txtbox = gr.Textbox(
|
46 |
+
label="Hindi text output:",
|
47 |
+
lines=5
|
48 |
+
)
|
49 |
+
|
50 |
+
title = "Speech-to-Text (Hindi) using Vakyansh"
|
51 |
+
description = "Upload a hindi audio clip, and let AI do the hard work of transcribing."
|
52 |
+
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>"
|
53 |
+
|
54 |
+
gr.Interface(parse, inputs=input_, outputs=txtbox, title=title, description=description, article=article,
|
55 |
+
streaming=True, interactive=True,
|
56 |
+
analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False,share=True);
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
soundfile
|
3 |
+
torch
|
4 |
+
transformers
|
5 |
+
sox
|
6 |
+
sentencepiece
|
7 |
+
scipy
|
8 |
+
gpt-index
|
9 |
+
PyPDF2
|