File size: 2,295 Bytes
157f27b
a8f06f7
0c2f2f9
f8da254
9b6c3a3
157f27b
34f8403
9b6c3a3
 
34f8403
9b6c3a3
 
 
34f8403
9b6c3a3
34f8403
9b6c3a3
 
 
 
 
 
e387fff
34f8403
9b6c3a3
 
 
 
 
0c2f2f9
9b6c3a3
 
 
 
 
 
0c2f2f9
9b6c3a3
 
 
 
34f8403
 
 
9b6c3a3
34f8403
9b6c3a3
 
 
 
 
901f41e
0c2f2f9
9b6c3a3
0c2f2f9
9b6c3a3
 
 
0c2f2f9
157f27b
34f8403
9b6c3a3
34f8403
9b6c3a3
0c2f2f9
 
 
 
 
 
9b6c3a3
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
import gradio as gr
import torch
import torchaudio
import numpy as np
from ichigo_asr.demo.utils import load_model

# Hàm tải mô hình Ichigo Whisper với map_location=cpu
def init_model():
    try:
        # Chỉ định rõ ràng map_location='cpu' để tải mô hình trên CPU
        ichigo_model = load_model(
            ref="homebrewltd/ichigo-whisper:merge-medium-vi-2d-2560c-dim64.pth",
            size="merge-medium-vi-2d-2560c-dim64",
            map_location=torch.device('cpu')  # Thêm tham số này
        )
        device = "cpu"  # Chỉ sử dụng CPU
        ichigo_model.ensure_whisper(device)
        ichigo_model.to(device)
        return ichigo_model, device
    except Exception as e:
        print(f"Lỗi khi tải mô hình: {e}")
        return None, "cpu"

# Khởi tạo mô hình
ichigo_model, device = init_model()

def transcribe(audio_path):
    if ichigo_model is None:
        return "Không thể tải mô hình. Vui lòng kiểm tra logs."
    
    try:
        # Tải file âm thanh
        wav, sr = torchaudio.load(audio_path)
        # Chuyển đổi sang 16kHz nếu cần
        if sr != 16000:
            wav = torchaudio.functional.resample(wav, sr, 16000)
        
        # Chuyển đổi sang mono nếu là stereo
        if wav.shape[0] > 1:
            wav = wav.mean(dim=0, keepdim=True)
        
        # Đảm bảo dữ liệu nằm trên CPU
        wav = wav.to(device)
        
        # Thực hiện dự đoán
        transcribe_result = ichigo_model.inference(wav)
        
        # Trả về kết quả
        return transcribe_result[0].text
    except Exception as e:
        return f"Lỗi khi nhận dạng giọng nói: {str(e)}"

# Tạo giao diện Gradio
title = "Ichigo Whisper Speech Recognition Demo"
description = """
# 🍓 Ichigo Whisper Speech Recognition
Sử dụng mô hình Ichigo-whisper để nhận dạng giọng nói.
Mô hình này có hiệu suất tốt cho cả tiếng Anh và tiếng Việt!
"""

demo = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
    outputs=gr.Textbox(label="Phiên âm"),
    title=title,
    description=description
)

# Khởi chạy ứng dụng
if __name__ == "__main__":
    demo.launch()