vankienemk commited on
Commit
0c2f2f9
·
verified ·
1 Parent(s): 15eda7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -31
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
- # Load model
8
- processor = Wav2Vec2Processor.from_pretrained("Menlo/Ichigo-whisper-v0.1")
9
- model = Wav2Vec2ForCTC.from_pretrained("Menlo/Ichigo-whisper-v0.1")
10
- model = torch.compile(model)
11
- def transcribe(audio):
12
- if audio is None:
13
- return "Không có âm thanh."
14
 
15
- sample_rate, audio_data = audio
16
- target_rate = 16000
 
 
 
 
 
 
 
 
 
17
 
18
- # Nếu sample rate khác 16kHz thì chuyển về
19
- if sample_rate != target_rate:
20
- duration = len(audio_data) / sample_rate
21
- new_length = int(duration * target_rate)
22
- audio_data = resample(audio_data, new_length)
 
 
 
 
23
 
24
- # Dự đoán
25
- inputs = processor(audio_data, sampling_rate=target_rate, return_tensors="pt", padding=True)
26
- with torch.no_grad():
27
- logits = model(**inputs).logits
28
- predicted_ids = torch.argmax(logits, dim=-1)
29
- transcription = processor.decode(predicted_ids[0])
30
- return transcription
31
 
32
- # Gradio UI
33
- gr.Interface(
34
- fn=transcribe,
35
- inputs=gr.Audio(sources=["microphone"], type="numpy", label="Ghi âm từ micro (16kHz mono)"),
36
- outputs="text",
37
- title="STT Tiếng Việt với Wav2Vec2",
38
- description="Ghi âm và nhận dạng giọng nói tiếng Việt bằng mô hình FPTAI/wav2vec2-base"
39
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
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()