Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
return result["text"]
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
)
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import soundfile as sf
|
4 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
5 |
|
6 |
+
# Load model
|
7 |
+
processor = Wav2Vec2Processor.from_pretrained("vlsp2020/wav2vec2-base-vietnamese-250h")
|
8 |
+
model = Wav2Vec2ForCTC.from_pretrained("vlsp2020/wav2vec2-base-vietnamese-250h")
|
9 |
|
10 |
+
def transcribe(audio):
|
11 |
+
# Load audio
|
12 |
+
speech, rate = sf.read(audio)
|
13 |
+
if rate != 16000:
|
14 |
+
return "Vui lòng cung cấp file audio 16kHz."
|
|
|
15 |
|
16 |
+
# Preprocess and predict
|
17 |
+
inputs = processor(speech, sampling_rate=16000, return_tensors="pt", padding=True)
|
18 |
+
with torch.no_grad():
|
19 |
+
logits = model(**inputs).logits
|
20 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
21 |
+
transcription = processor.decode(predicted_ids[0])
|
22 |
+
return transcription
|
|
|
23 |
|
24 |
+
# Gradio UI
|
25 |
+
gr.Interface(
|
26 |
+
fn=transcribe,
|
27 |
+
inputs=gr.Audio(source="upload", type="filepath", label="Upload audio (16kHz, mono)"),
|
28 |
+
outputs="text",
|
29 |
+
title="Wav2Vec2 Vietnamese STT",
|
30 |
+
description="Nhận dạng giọng nói tiếng Việt bằng mô hình wav2vec2-base-vietnamese-250h từ VLSP."
|
31 |
+
).launch()
|