Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,89 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import torchaudio
|
4 |
-
from transformers import pipeline
|
5 |
import numpy as np
|
|
|
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 |
-
result = transcriber({"sampling_rate": sr, "raw": stream})
|
31 |
-
return stream, result["text"]
|
32 |
|
33 |
# Tạo giao diện Gradio
|
34 |
-
title = "Ichigo Whisper
|
35 |
description = """
|
36 |
-
# 🍓 Ichigo Whisper
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
"""
|
39 |
|
40 |
-
# Tạo giao diện
|
41 |
-
|
42 |
-
fn=
|
43 |
-
inputs=
|
44 |
-
|
45 |
-
gr.Audio(sources=["microphone"], streaming=True)
|
46 |
-
],
|
47 |
-
outputs=[
|
48 |
-
"state",
|
49 |
-
gr.Textbox(label="Phiên âm theo thời gian thực")
|
50 |
-
],
|
51 |
-
live=True,
|
52 |
title=title,
|
53 |
description=description
|
54 |
)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# Khởi chạy ứng dụng
|
57 |
if __name__ == "__main__":
|
58 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
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 = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
+
ichigo_model.ensure_whisper(device)
|
17 |
+
ichigo_model.to(device)
|
18 |
+
return ichigo_model, device
|
19 |
+
except Exception as e:
|
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 khi ứng dụng bắt đầu
|
24 |
+
ichigo_model, device = init_model()
|
25 |
+
|
26 |
+
def transcribe(audio_path):
|
27 |
+
if ichigo_model is None:
|
28 |
+
return "Không thể tải mô hình. Vui lòng kiểm tra logs."
|
29 |
|
30 |
+
try:
|
31 |
+
# Tải file âm thanh
|
32 |
+
wav, sr = torchaudio.load(audio_path)
|
33 |
+
# Chuyển đổi sang 16kHz nếu cần
|
34 |
+
if sr != 16000:
|
35 |
+
wav = torchaudio.functional.resample(wav, sr, 16000)
|
36 |
|
37 |
+
# Chuyển đổi sang mono nếu là stereo
|
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.to(device))
|
43 |
+
|
44 |
+
# Trả về kết quả
|
45 |
+
return transcribe_result[0].text
|
46 |
+
except Exception as e:
|
47 |
+
return f"Lỗi khi nhận dạng giọng nói: {str(e)}"
|
|
|
|
|
48 |
|
49 |
# Tạo giao diện Gradio
|
50 |
+
title = "Ichigo Whisper Speech Recognition Demo"
|
51 |
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 |
+
# Tạo giao diện với hai tab: Microphone và Upload
|
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()
|