File size: 2,765 Bytes
eb774c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e40922c
 
 
 
 
 
eb774c4
e40922c
 
 
 
 
 
 
eb774c4
e40922c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb774c4
e40922c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from transformers import WhisperForConditionalGeneration, WhisperProcessor
import gradio as gr
import torch
import librosa
import numpy as np

# Cấu hình thiết bị
device = "cuda:0" if torch.cuda.is_available() else "cpu"

# Tải model và processor
model = WhisperForConditionalGeneration.from_pretrained("ntviet/whisper-small-hre5.2").to(device)
processor = WhisperProcessor.from_pretrained("ntviet/whisper-small-hre5.2")

# Tắt tính năng forced_decoder_ids trong generation config
if hasattr(model.generation_config, "forced_decoder_ids"):
    model.generation_config.forced_decoder_ids = None

def transcribe(audio_path):
    try:
        # Đọc file âm thanh với librosa
        audio, sr = librosa.load(audio_path, sr=16000)
        
        # Chuẩn hóa dữ liệu âm thanh
        audio = librosa.util.normalize(audio) * 0.9  # Giảm volume để tránh clipping
        
        # Xử lý âm thanh
        inputs = processor(
            audio, 
            sampling_rate=16000,
            return_tensors="pt"
        ).to(device)
        
        # Generate với cấu hình tùy chỉnh
        outputs = model.generate(
            inputs.input_features,
            generation_config=model.generation_config
        )
        
        # Giải mã kết quả
        text = processor.batch_decode(outputs, skip_special_tokens=True)[0]
        return text
        
    except Exception as e:
        return f"Lỗi khi xử lý audio: {str(e)}"

# # Giao diện Gradio
# with gr.Blocks() as demo:
#     gr.Markdown("""
#     # Nhận dạng giọng nói tiếng Việt
#     Model: whisper-small-hre5.2 (đã fine-tune)
#     """)
    
#     with gr.Row():
#         audio_input = gr.Audio(
#             sources=["upload", "microphone"],
#             type="filepath",
#             label="Tải lên hoặc ghi âm"
#         )
#         output_text = gr.Textbox(label="Kết quả nhận dạng")
    
#     submit_btn = gr.Button("Bắt đầu nhận dạng")
#     submit_btn.click(
#         fn=transcribe,
#         inputs=audio_input,
#         outputs=output_text,
#         api_name="/predict"
#     )

# demo.launch(debug=True, show_error=True)

# Giao diện Gradio cho phép ghi âm hoặc upload
demo = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(sources=["upload", "microphone"], type="filepath", label="🎙️ Upload hoặc ghi âm (.mp3)"),
    outputs=gr.Textbox(label="📝 Kết quả chuyển văn bản"),
    title="Whisper HRE Demo",
    description="🔤 Nhận dạng tiếng H'Re bằng mô hình Whisper fine-tuned.",
    theme="soft",
    api_name="/predict"  # <-- Đảm bảo endpoint cho gradio_client
)

# Chạy app với hiển thị lỗi đầy đủ
demo.launch(show_error=True)