Spaces:
Runtime error
Runtime error
shahukareem
commited on
Commit
•
810776d
1
Parent(s):
7a59a23
initial commit
Browse files- app.py +36 -0
- packages.txt +2 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import soundfile as sf
|
2 |
+
import torch
|
3 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
4 |
+
import gradio as gr
|
5 |
+
import sox
|
6 |
+
import os
|
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 |
+
api_token = os.getenv("API_TOKEN")
|
15 |
+
model_name = "shahukareem/Wav2Vec2-Large-XLSR-53-Dhivehi"
|
16 |
+
processor = Wav2Vec2Processor.from_pretrained(model_name, use_auth_token=api_token)
|
17 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_name, use_auth_token=api_token)
|
18 |
+
def parse_transcription(wav_file):
|
19 |
+
filename = wav_file.name.split('.')[0]
|
20 |
+
convert(wav_file.name, filename + "16k.wav")
|
21 |
+
speech, _ = sf.read(filename + "16k.wav")
|
22 |
+
input_values = processor(speech, sampling_rate=16_000, return_tensors="pt").input_values
|
23 |
+
logits = model(input_values).logits
|
24 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
25 |
+
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
26 |
+
return transcription
|
27 |
+
output = gr.outputs.Textbox(label="The transcript")
|
28 |
+
input_ = gr.inputs.Audio(source="microphone", type="file")
|
29 |
+
gr.Interface(parse_transcription, inputs=input_, outputs=[output],
|
30 |
+
analytics_enabled=False,
|
31 |
+
show_tips=False,
|
32 |
+
theme='huggingface',
|
33 |
+
layout='vertical',
|
34 |
+
title="Speech Recognition for Dhivehi",
|
35 |
+
description="Speech Recognition Live Demo for Dhivehi",
|
36 |
+
enable_queue=True).launch( inline=False)
|
packages.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
libsndfile1
|
2 |
+
sox
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
soundfile
|
3 |
+
torch
|
4 |
+
transformers
|
5 |
+
sox
|
6 |
+
sentencepiece
|