vankienemk commited on
Commit
a8f06f7
·
verified ·
1 Parent(s): ff06dc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -18
app.py CHANGED
@@ -1,23 +1,31 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
3
 
4
- # Tạo pipeline nhận diện giọng nói
5
- asr = pipeline("automatic-speech-recognition", model="openai/whisper-base")
 
6
 
7
- # Hàm xử lý âm thanh
8
- def transcribe(audio_file):
9
- if audio_file is None:
10
- return "Chưa file âm thanh."
11
- result = asr(audio_file)
12
- return result["text"]
13
 
14
- # Giao diện Gradio
15
- demo = gr.Interface(
16
- fn=transcribe,
17
- inputs=gr.Audio(type="filepath", label="Tải lên file âm thanh (.wav, .mp3...)"),
18
- outputs=gr.Textbox(label="Kết quả chuyển văn bản"),
19
- title="Nhận diện giọng nói bằng Whisper",
20
- description="Tải file âm thanh và hệ thống sẽ nhận diện nội dung giọng nói bằng mô hình Whisper của OpenAI."
21
- )
22
 
23
- demo.launch()
 
 
 
 
 
 
 
 
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()