ntviet commited on
Commit
eb774c4
·
verified ·
1 Parent(s): 794fb25

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import WhisperForConditionalGeneration, WhisperProcessor
2
+ import gradio as gr
3
+ import torch
4
+ import librosa
5
+ import numpy as np
6
+
7
+ # Cấu hình thiết bị
8
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
+
10
+ # Tải model và processor
11
+ model = WhisperForConditionalGeneration.from_pretrained("ntviet/whisper-small-hre5.2").to(device)
12
+ processor = WhisperProcessor.from_pretrained("ntviet/whisper-small-hre5.2")
13
+
14
+ # Tắt tính năng forced_decoder_ids trong generation config
15
+ if hasattr(model.generation_config, "forced_decoder_ids"):
16
+ model.generation_config.forced_decoder_ids = None
17
+
18
+ def transcribe(audio_path):
19
+ try:
20
+ # Đọc file âm thanh với librosa
21
+ audio, sr = librosa.load(audio_path, sr=16000)
22
+
23
+ # Chuẩn hóa dữ liệu âm thanh
24
+ audio = librosa.util.normalize(audio) * 0.9 # Giảm volume để tránh clipping
25
+
26
+ # Xử lý âm thanh
27
+ inputs = processor(
28
+ audio,
29
+ sampling_rate=16000,
30
+ return_tensors="pt"
31
+ ).to(device)
32
+
33
+ # Generate với cấu hình tùy chỉnh
34
+ outputs = model.generate(
35
+ inputs.input_features,
36
+ generation_config=model.generation_config
37
+ )
38
+
39
+ # Giải mã kết quả
40
+ text = processor.batch_decode(outputs, skip_special_tokens=True)[0]
41
+ return text
42
+
43
+ except Exception as e:
44
+ return f"Lỗi khi xử lý audio: {str(e)}"
45
+
46
+ # Giao diện Gradio
47
+ with gr.Blocks() as demo:
48
+ gr.Markdown("""
49
+ # Nhận dạng giọng nói tiếng Việt
50
+ Model: whisper-small-hre5.2 (đã fine-tune)
51
+ """)
52
+
53
+ with gr.Row():
54
+ audio_input = gr.Audio(
55
+ sources=["upload", "microphone"],
56
+ type="filepath",
57
+ label="Tải lên hoặc ghi âm"
58
+ )
59
+ output_text = gr.Textbox(label="Kết quả nhận dạng")
60
+
61
+ submit_btn = gr.Button("Bắt đầu nhận dạng")
62
+ submit_btn.click(
63
+ fn=transcribe,
64
+ inputs=audio_input,
65
+ outputs=output_text
66
+ )
67
+
68
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch>=2.0.1
2
+ transformers>=4.31.0
3
+ gradio>=3.36.0
4
+ librosa>=0.10.0
5
+ numpy>=1.23.0
6
+ soundfile>=0.12.1