vankienemk commited on
Commit
9b6c3a3
·
verified ·
1 Parent(s): ab43d17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -39
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
- # 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()
 
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 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()