Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,15 +4,16 @@ import torchaudio
|
|
4 |
import numpy as np
|
5 |
from ichigo_asr.demo.utils import load_model
|
6 |
|
7 |
-
# Hàm tải mô hình Ichigo Whisper
|
8 |
def init_model():
|
9 |
-
# Tải Ichigo Whisper
|
10 |
try:
|
|
|
11 |
ichigo_model = load_model(
|
12 |
ref="homebrewltd/ichigo-whisper:merge-medium-vi-2d-2560c-dim64.pth",
|
13 |
size="merge-medium-vi-2d-2560c-dim64",
|
|
|
14 |
)
|
15 |
-
device = "cpu"
|
16 |
ichigo_model.ensure_whisper(device)
|
17 |
ichigo_model.to(device)
|
18 |
return ichigo_model, device
|
@@ -20,7 +21,7 @@ def init_model():
|
|
20 |
print(f"Lỗi khi tải mô hình: {e}")
|
21 |
return None, "cpu"
|
22 |
|
23 |
-
# Khởi tạo mô hình
|
24 |
ichigo_model, device = init_model()
|
25 |
|
26 |
def transcribe(audio_path):
|
@@ -38,8 +39,11 @@ def transcribe(audio_path):
|
|
38 |
if wav.shape[0] > 1:
|
39 |
wav = wav.mean(dim=0, keepdim=True)
|
40 |
|
|
|
|
|
|
|
41 |
# Thực hiện dự đoán
|
42 |
-
transcribe_result = ichigo_model.inference(wav
|
43 |
|
44 |
# Trả về kết quả
|
45 |
return transcribe_result[0].text
|
@@ -52,38 +56,16 @@ description = """
|
|
52 |
# 🍓 Ichigo Whisper Speech Recognition
|
53 |
Sử dụng mô hình Ichigo-whisper để nhận dạng giọng nói.
|
54 |
Mô hình này có hiệu suất tốt cho cả tiếng Anh và tiếng Việt!
|
55 |
-
|
56 |
-
## Cách sử dụng:
|
57 |
-
1. Nhấn vào nút microphone và nói
|
58 |
-
2. Hoặc tải lên file audio
|
59 |
-
3. Mô hình sẽ chuyển đổi giọng nói thành văn bản
|
60 |
-
|
61 |
-
Chi tiết về mô hình: [Menlo/Ichigo-whisper-v0.1](https://huggingface.co/Menlo/Ichigo-whisper-v0.1)
|
62 |
"""
|
63 |
|
64 |
-
|
65 |
-
mic_transcribe = gr.Interface(
|
66 |
fn=transcribe,
|
67 |
-
inputs=gr.Audio(sources="microphone", type="filepath"),
|
68 |
outputs=gr.Textbox(label="Phiên âm"),
|
69 |
title=title,
|
70 |
description=description
|
71 |
)
|
72 |
|
73 |
-
file_transcribe = gr.Interface(
|
74 |
-
fn=transcribe,
|
75 |
-
inputs=gr.Audio(sources="upload", type="filepath"),
|
76 |
-
outputs=gr.Textbox(label="Phiên âm"),
|
77 |
-
title=title,
|
78 |
-
description=description
|
79 |
-
)
|
80 |
-
|
81 |
-
# Kết hợp các tab
|
82 |
-
demo = gr.TabbedInterface(
|
83 |
-
[mic_transcribe, file_transcribe],
|
84 |
-
["Microphone", "Upload Audio"]
|
85 |
-
)
|
86 |
-
|
87 |
# Khởi chạy ứng dụng
|
88 |
if __name__ == "__main__":
|
89 |
demo.launch()
|
|
|
4 |
import numpy as np
|
5 |
from ichigo_asr.demo.utils import load_model
|
6 |
|
7 |
+
# Hàm tải mô hình Ichigo Whisper với map_location=cpu
|
8 |
def init_model():
|
|
|
9 |
try:
|
10 |
+
# Chỉ định rõ ràng map_location='cpu' để tải mô hình trên CPU
|
11 |
ichigo_model = load_model(
|
12 |
ref="homebrewltd/ichigo-whisper:merge-medium-vi-2d-2560c-dim64.pth",
|
13 |
size="merge-medium-vi-2d-2560c-dim64",
|
14 |
+
map_location=torch.device('cpu') # Thêm tham số này
|
15 |
)
|
16 |
+
device = "cpu" # Chỉ sử dụng CPU
|
17 |
ichigo_model.ensure_whisper(device)
|
18 |
ichigo_model.to(device)
|
19 |
return ichigo_model, device
|
|
|
21 |
print(f"Lỗi khi tải mô hình: {e}")
|
22 |
return None, "cpu"
|
23 |
|
24 |
+
# Khởi tạo mô hình
|
25 |
ichigo_model, device = init_model()
|
26 |
|
27 |
def transcribe(audio_path):
|
|
|
39 |
if wav.shape[0] > 1:
|
40 |
wav = wav.mean(dim=0, keepdim=True)
|
41 |
|
42 |
+
# Đảm bảo dữ liệu nằm trên CPU
|
43 |
+
wav = wav.to(device)
|
44 |
+
|
45 |
# Thực hiện dự đoán
|
46 |
+
transcribe_result = ichigo_model.inference(wav)
|
47 |
|
48 |
# Trả về kết quả
|
49 |
return transcribe_result[0].text
|
|
|
56 |
# 🍓 Ichigo Whisper Speech Recognition
|
57 |
Sử dụng mô hình Ichigo-whisper để nhận dạng giọng nói.
|
58 |
Mô hình này có hiệu suất tốt cho cả tiếng Anh và tiếng Việt!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
"""
|
60 |
|
61 |
+
demo = gr.Interface(
|
|
|
62 |
fn=transcribe,
|
63 |
+
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
|
64 |
outputs=gr.Textbox(label="Phiên âm"),
|
65 |
title=title,
|
66 |
description=description
|
67 |
)
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Khởi chạy ứng dụng
|
70 |
if __name__ == "__main__":
|
71 |
demo.launch()
|