sheep52031 commited on
Commit
c989afe
·
verified ·
1 Parent(s): e3aaa9a

🎤 MediaTek ASR 台灣國語測試 Space 初始版本

Browse files
Files changed (1) hide show
  1. app.py +97 -43
app.py CHANGED
@@ -1,6 +1,7 @@
1
  """
2
  MediaTek Breeze-ASR-25 台灣國語識別測試 Space
3
  適用於 HuggingFace Zero GPU Spaces 部署
 
4
  """
5
 
6
  import gradio as gr
@@ -10,17 +11,20 @@ import torch
10
  import time
11
  import torchaudio
12
 
13
- # 全域模型變數
14
- asr_model = None
15
-
16
- @spaces.GPU(duration=60)
17
- def load_asr_model():
18
- """載入 MediaTek ASR 模型"""
19
- global asr_model
20
 
21
- print("🔄 載入 MediaTek Breeze-ASR-25 模型...")
22
 
23
  try:
 
 
 
 
24
  asr_model = pipeline(
25
  "automatic-speech-recognition",
26
  model="MediaTek-Research/Breeze-ASR-25",
@@ -29,80 +33,130 @@ def load_asr_model():
29
  return_timestamps=True
30
  )
31
 
32
- print("✅ MediaTek Breeze-ASR-25 載入成功")
33
- return "✅ 模型載入完成"
34
 
35
- except Exception as e:
36
- print(f"❌ 模型載入失敗: {str(e)}")
37
- return f"❌ 模型載入失敗: {str(e)}"
38
-
39
- # 載入模型
40
- load_status = load_asr_model()
41
-
42
- @spaces.GPU(duration=30)
43
- def transcribe_audio(audio_file):
44
- """ASR 推論與效能測試"""
45
- global asr_model
46
-
47
- if audio_file is None:
48
- return "❌ 請上傳音訊檔案", "", ""
49
-
50
- if asr_model is None:
51
- return "❌ 模型尚未載入", "", ""
52
-
53
- start_time = time.time()
54
-
55
- try:
56
  # 載入音訊檔案獲取長度
57
  waveform, sample_rate = torchaudio.load(audio_file)
58
  audio_duration = waveform.shape[1] / sample_rate
59
 
60
  # 執行 ASR 推論
 
61
  result = asr_model(audio_file)
 
62
 
63
- # 計算處理時間
64
- process_time = time.time() - start_time
65
- rtf = process_time / audio_duration
66
 
67
  # 提取識別結果
68
  transcript = result["text"] if isinstance(result, dict) else str(result)
69
 
 
 
 
 
 
 
70
  # 格式化性能指標
71
- performance = f"""⏱️ 處理時間: {process_time:.2f}s
 
 
72
  🎵 音訊長度: {audio_duration:.2f}s
73
  📈 RTF: {rtf:.3f} ({'實時' if rtf < 1.0 else '非實時'})
74
- 💾 模型: MediaTek Breeze-ASR-25"""
 
75
 
76
  return transcript, performance, "✅ 識別成功"
77
 
78
  except Exception as e:
79
- return f"❌ 處理失敗: {str(e)}", "", "❌ 處理失敗"
 
 
 
 
 
 
 
 
 
 
80
 
81
  # Gradio 界面
82
  with gr.Blocks(title="MediaTek ASR 台灣國語測試") as demo:
83
  gr.Markdown("# 🎤 MediaTek Breeze-ASR-25 台灣國語識別測試")
84
- gr.Markdown(f"**模型狀態**: {load_status}")
 
 
 
 
 
 
 
 
 
85
 
86
  with gr.Row():
87
  with gr.Column():
 
88
  audio_input = gr.Audio(
89
  type="filepath",
90
- label="上傳音訊檔案 (wav, mp3, m4a)"
 
91
  )
92
- submit_btn = gr.Button("🚀 開始識別", variant="primary")
 
 
 
 
 
 
 
 
 
93
 
94
  with gr.Column():
 
95
  transcript_output = gr.Textbox(
96
- label="✨ 識別結果",
97
  lines=5,
98
  placeholder="識別結果將顯示在這裡..."
99
  )
 
100
  performance_output = gr.Textbox(
101
  label="⚡ 性能指標",
102
- lines=4
 
103
  )
104
- status_output = gr.Textbox(label="📊 狀態")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
 
106
  submit_btn.click(
107
  transcribe_audio,
108
  inputs=[audio_input],
 
1
  """
2
  MediaTek Breeze-ASR-25 台灣國語識別測試 Space
3
  適用於 HuggingFace Zero GPU Spaces 部署
4
+ 修復版:解決 ZeroGPU 會話間模型載入問題
5
  """
6
 
7
  import gradio as gr
 
11
  import time
12
  import torchaudio
13
 
14
+ @spaces.GPU(duration=60)
15
+ def transcribe_audio(audio_file):
16
+ """ASR 推論與效能測試 - 每次調用時載入模型"""
17
+
18
+ if audio_file is None:
19
+ return "❌ 請上傳音訊檔案", "", ""
 
20
 
21
+ start_total = time.time()
22
 
23
  try:
24
+ # 每次推論時載入模型(ZeroGPU 限制)
25
+ print("🔄 載入 MediaTek Breeze-ASR-25 模型...")
26
+ model_load_start = time.time()
27
+
28
  asr_model = pipeline(
29
  "automatic-speech-recognition",
30
  model="MediaTek-Research/Breeze-ASR-25",
 
33
  return_timestamps=True
34
  )
35
 
36
+ model_load_time = time.time() - model_load_start
37
+ print(f"✅ 模型載入完成 ({model_load_time:.2f}s)")
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # 載入音訊檔案獲取長度
40
  waveform, sample_rate = torchaudio.load(audio_file)
41
  audio_duration = waveform.shape[1] / sample_rate
42
 
43
  # 執行 ASR 推論
44
+ inference_start = time.time()
45
  result = asr_model(audio_file)
46
+ inference_time = time.time() - inference_start
47
 
48
+ # 計算總處理時間
49
+ total_time = time.time() - start_total
50
+ rtf = total_time / audio_duration
51
 
52
  # 提取識別結果
53
  transcript = result["text"] if isinstance(result, dict) else str(result)
54
 
55
+ # 檢查 GPU 記憶體使用
56
+ gpu_info = ""
57
+ if torch.cuda.is_available():
58
+ gpu_memory = torch.cuda.memory_allocated() / 1024**3
59
+ gpu_info = f"💾 GPU 記憶體: {gpu_memory:.2f}GB"
60
+
61
  # 格式化性能指標
62
+ performance = f"""⏱️ 總處理時間: {total_time:.2f}s
63
+ 🔄 模型載入時間: {model_load_time:.2f}s
64
+ 🎯 推論時間: {inference_time:.2f}s
65
  🎵 音訊長度: {audio_duration:.2f}s
66
  📈 RTF: {rtf:.3f} ({'實時' if rtf < 1.0 else '非實時'})
67
+ 💾 模型: MediaTek Breeze-ASR-25
68
+ {gpu_info}"""
69
 
70
  return transcript, performance, "✅ 識別成功"
71
 
72
  except Exception as e:
73
+ error_msg = f"❌ 處理失敗: {str(e)}"
74
+ print(error_msg)
75
+ return error_msg, "", "❌ 處理失敗"
76
+
77
+ def get_model_info():
78
+ """獲取模型資訊 (CPU 函數)"""
79
+ return """🤖 MediaTek Breeze-ASR-25 模型資訊:
80
+ - 基於 Whisper 架構,專為台灣國語優化
81
+ - 支援繁體中文語音識別
82
+ - ZeroGPU 動態載入模式
83
+ - 每次推論重新載入以確保穩定性"""
84
 
85
  # Gradio 界面
86
  with gr.Blocks(title="MediaTek ASR 台灣國語測試") as demo:
87
  gr.Markdown("# 🎤 MediaTek Breeze-ASR-25 台灣國語識別測試")
88
+ gr.Markdown("**專為台灣國語優化的語音識別測試平台**")
89
+
90
+ # 模型資訊顯示
91
+ with gr.Accordion("🤖 模型資訊", open=False):
92
+ model_info = gr.Textbox(
93
+ value=get_model_info(),
94
+ label="模型詳細資訊",
95
+ lines=6,
96
+ interactive=False
97
+ )
98
 
99
  with gr.Row():
100
  with gr.Column():
101
+ gr.Markdown("### 🎙️ 音訊輸入")
102
  audio_input = gr.Audio(
103
  type="filepath",
104
+ label="上傳音訊檔案 (wav, mp3, m4a)",
105
+ format="wav"
106
  )
107
+
108
+ gr.Markdown("### 📋 測試說明")
109
+ gr.Markdown("""
110
+ - 🎯 上傳 5-60 秒的台灣國語音訊
111
+ - 🔊 建議使用清晰、低噪音的錄音
112
+ - ⚡ 每次識別會重新載入模型 (ZeroGPU 限制)
113
+ - 📊 系統會顯示詳細的性能指標
114
+ """)
115
+
116
+ submit_btn = gr.Button("🚀 開始識別", variant="primary", size="lg")
117
 
118
  with gr.Column():
119
+ gr.Markdown("### 📄 識別結果")
120
  transcript_output = gr.Textbox(
121
+ label="✨ 識別文字",
122
  lines=5,
123
  placeholder="識別結果將顯示在這裡..."
124
  )
125
+
126
  performance_output = gr.Textbox(
127
  label="⚡ 性能指標",
128
+ lines=8,
129
+ placeholder="性能數據將顯示在這裡..."
130
  )
131
+
132
+ status_output = gr.Textbox(
133
+ label="📊 處理狀態",
134
+ lines=2
135
+ )
136
+
137
+ # 使用範例
138
+ with gr.Accordion("📖 使用範例與 API", open=False):
139
+ gr.Markdown("""
140
+ ## 🔗 Gradio Client API 使用
141
+
142
+ ```python
143
+ from gradio_client import Client
144
+
145
+ client = Client("sheep52031/mediatek-asr-test")
146
+ result = client.predict("audio_file.wav", api_name="/predict")
147
+
148
+ transcript = result[0] # 識別文字
149
+ performance = result[1] # 性能指標
150
+ status = result[2] # 處理狀態
151
+ ```
152
+
153
+ ## 📊 評估指標
154
+ - **RTF < 1.0**: 實時處理能力
155
+ - **準確度**: 台灣國語識別正確率
156
+ - **處理時間**: 總耗時包含模型載入
157
+ """)
158
 
159
+ # 事件綁定
160
  submit_btn.click(
161
  transcribe_audio,
162
  inputs=[audio_input],