Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
|
|
|
|
3 |
import numpy as np
|
4 |
-
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
5 |
-
from scipy.signal import resample
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
model = torch.compile(model)
|
11 |
-
def transcribe(audio):
|
12 |
-
if audio is None:
|
13 |
-
return "Không có âm thanh."
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
-
if
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
return transcription
|
31 |
|
32 |
-
#
|
33 |
-
gr.Interface(
|
34 |
-
fn=
|
35 |
-
inputs=
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
import torchaudio
|
4 |
+
from transformers import pipeline
|
5 |
import numpy as np
|
|
|
|
|
6 |
|
7 |
+
# Tải mô hình Ichigo-whisper
|
8 |
+
model_id = "Menlo/Ichigo-whisper-v0.1"
|
9 |
+
transcriber = pipeline("automatic-speech-recognition", model=model_id)
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
def transcribe_stream(stream, new_chunk):
|
12 |
+
# Trích xuất sample rate và dữ liệu âm thanh
|
13 |
+
sr, y = new_chunk
|
14 |
+
|
15 |
+
# Chuyển về mono nếu là stereo
|
16 |
+
if y.ndim > 1:
|
17 |
+
y = y.mean(axis=1)
|
18 |
+
|
19 |
+
# Chuẩn hóa âm thanh
|
20 |
+
y = y.astype(np.float32)
|
21 |
+
y /= np.max(np.abs(y)) if np.max(np.abs(y)) > 0 else 1.0
|
22 |
|
23 |
+
# Nối với audio trước đó
|
24 |
+
if stream is not None:
|
25 |
+
stream = np.concatenate([stream, y])
|
26 |
+
else:
|
27 |
+
stream = y
|
28 |
+
|
29 |
+
# Dự đoán kết quả
|
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 Streaming Demo"
|
35 |
+
description = """
|
36 |
+
# 🍓 Ichigo Whisper Streaming Recognition
|
37 |
+
Nhận dạng giọng nói theo thời gian thực với mô hình Menlo/Ichigo-whisper-v0.1.
|
38 |
+
"""
|
|
|
39 |
|
40 |
+
# Tạo giao diện streaming
|
41 |
+
streaming_demo = gr.Interface(
|
42 |
+
fn=transcribe_stream,
|
43 |
+
inputs=[
|
44 |
+
"state",
|
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 |
+
streaming_demo.launch()
|