Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
os.system("pip install transformers")
|
4 |
+
os.system("pip install https://github.com/kpu/kenlm/archive/master.zip")
|
5 |
+
os.system("pip install pyctcdecode")
|
6 |
+
os.system("pip install gradio")
|
7 |
+
os.system("pip install librosa")
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
import librosa
|
11 |
+
import torch
|
12 |
+
|
13 |
+
from transformers import Wav2Vec2CTCTokenizer
|
14 |
+
from transformers import Wav2Vec2FeatureExtractor
|
15 |
+
from transformers import Wav2Vec2Processor
|
16 |
+
from transformers import Wav2Vec2ForCTC
|
17 |
+
from transformers import Wav2Vec2ProcessorWithLM
|
18 |
+
|
19 |
+
repo_name = "aiface/vietnamese_s2t"
|
20 |
+
|
21 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
22 |
+
processor = Wav2Vec2ProcessorWithLM.from_pretrained(repo_name, token="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")
|
23 |
+
model = Wav2Vec2ForCTC.from_pretrained(repo_name, token="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").to(device)
|
24 |
+
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(repo_name, token="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")
|
25 |
+
tokenizer = Wav2Vec2CTCTokenizer.from_pretrained(repo_name, token="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")
|
26 |
+
|
27 |
+
def process_audio_file(file):
|
28 |
+
data, sr = librosa.load(file, sr = 16000)
|
29 |
+
|
30 |
+
return data
|
31 |
+
|
32 |
+
def transcribe(file_mic, file_upload):
|
33 |
+
warn_output = ""
|
34 |
+
if (file_mic is not None) and (file_upload is not None):
|
35 |
+
warn_output = "WARNING: You've uploaded an audio file and used the microphone. The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
|
36 |
+
file = file_mic
|
37 |
+
elif (file_mic is None) and (file_upload is None):
|
38 |
+
return "ERROR: You have to either use the microphone or upload an audio file"
|
39 |
+
elif file_mic is not None:
|
40 |
+
file = file_mic
|
41 |
+
else:
|
42 |
+
file = file_upload
|
43 |
+
|
44 |
+
input_values = process_audio_file(file)
|
45 |
+
input_dict = processor(input_values, sampling_rate=16_000, return_tensors="pt", padding=True)
|
46 |
+
logits = model(input_dict.input_values.to(device)).logits
|
47 |
+
|
48 |
+
pred_ids = torch.argmax(logits, dim=-1)[0]
|
49 |
+
pres = processor.batch_decode(logits.to("cpu").detach().numpy()).text
|
50 |
+
|
51 |
+
return warn_output + str(pres[0])
|
52 |
+
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=transcribe,
|
55 |
+
inputs=[
|
56 |
+
gr.inputs.Audio(source="microphone", type='filepath', optional=True),
|
57 |
+
gr.inputs.Audio(source="upload", type='filepath', optional=True),
|
58 |
+
],
|
59 |
+
outputs="text",
|
60 |
+
layout="horizontal",
|
61 |
+
theme="huggingface",
|
62 |
+
title="Speech to text MMS With Language Model",
|
63 |
+
description="Demo đơn giản speech to text",
|
64 |
+
)
|
65 |
+
iface.launch(share=True)
|