Lowking commited on
Commit
3f481b2
·
verified ·
1 Parent(s): 0876484

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -39
app.py CHANGED
@@ -1,10 +1,9 @@
1
- from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from gradio_client import Client, handle_file
4
  import tempfile
5
  import wave
6
  import os
7
- import json
8
 
9
  app = FastAPI()
10
 
@@ -15,46 +14,35 @@ app.add_middleware(
15
  allow_headers=["*"],
16
  )
17
 
18
- # --- 🧠 延遲連線大腦防止啟動超時 ---
19
- brain_cache = {"asr": None, "mt": None}
20
 
21
  def get_brain(key):
22
- if brain_cache[key] is None:
23
  urls = {
24
  "asr": "https://ai-labs.ilrdf.org.tw/sapolita-kaldi/",
25
  "mt": "https://ai-labs.ilrdf.org.tw/kari-seejiq-tnpusu-ai-hmjil/"
26
  }
27
- brain_cache[key] = Client(urls[key])
28
- return brain_cache[key]
29
-
30
- def parse_dialect(dialect_result):
31
- if isinstance(dialect_result, dict) and 'value' in dialect_result:
32
- return dialect_result['value']
33
- elif isinstance(dialect_result, list) and len(dialect_result) > 0:
34
- return dialect_result[0]
35
- return dialect_result
36
 
37
  @app.get("/")
38
  async def root():
39
- return {"status": "online", "mode": "太魯閣語全能模式"}
40
 
41
- # ==========================================
42
- # 🐉 即時字幕 WebSocket (太魯閣語版本)
43
- # ==========================================
44
  @app.websocket("/ws/subtitle")
45
  async def websocket_subtitle(websocket: WebSocket):
46
  await websocket.accept()
47
  audio_buffer = bytearray()
48
- ETHNICITY = "太魯閣"
49
- DIALECT_ID = "formosan_trv" # ✨ ASR 指定太魯閣語代碼
50
 
51
  try:
52
  while True:
53
  audio_chunk = await websocket.receive_bytes()
54
  audio_buffer.extend(audio_chunk)
55
 
56
- # 累積 3 秒音訊 (260,000 bytes) 提高辨識率
57
- if len(audio_buffer) >= 260000:
58
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
59
  with wave.open(temp_wav.name, 'wb') as wav_file:
60
  wav_file.setnchannels(1)
@@ -63,38 +51,37 @@ async def websocket_subtitle(websocket: WebSocket):
63
  wav_file.writeframes(audio_buffer)
64
  temp_path = temp_wav.name
65
 
66
- ind_text, zh_text = "", ""
67
  try:
68
- # 1. ASR 辨識 (太魯閣語)
69
  asr_res = get_brain("asr").predict(
70
- dialect_id=DIALECT_ID,
71
  audio_data=handle_file(temp_path),
72
  api_name="/automatic_speech_recognition"
73
  )
74
  ind_text = str(asr_res).strip()
75
 
76
- # 2. MT 翻譯 (過濾點點雜音)
77
- if ind_text and ind_text not in [".", "...", "。", ""]:
 
78
  mt_brain = get_brain("mt")
79
- # 動態獲取太魯閣語代碼
80
- d_code = parse_dialect(mt_brain.predict(ethnicity=ETHNICITY, api_name="/lambda"))
81
  zh_res = mt_brain.predict(text=ind_text, src_lang=d_code, tgt_lang="zho_Hant", api_name="/translate")
82
  zh_text = str(zh_res).strip()
83
- print(f"🚀 [太魯閣語成功] 族語: {ind_text} | 中文: {zh_text}")
 
 
 
 
 
84
  except Exception as e:
85
- print(f"⚠️ 辨識失敗: {e}")
86
 
87
- await websocket.send_json({
88
- "status": "recognizing",
89
- "indigenous": ind_text if ind_text else "",
90
- "chinese": zh_text
91
- })
92
-
93
  audio_buffer = bytearray()
94
  if os.path.exists(temp_path): os.remove(temp_path)
95
  else:
96
- if len(audio_buffer) % 65000 == 0:
97
- await websocket.send_json({"status": "buffering", "text": "🟢 神獸聽太魯閣語..."})
98
  except Exception: pass
99
 
100
  if __name__ == "__main__":
 
1
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from gradio_client import Client, handle_file
4
  import tempfile
5
  import wave
6
  import os
 
7
 
8
  app = FastAPI()
9
 
 
14
  allow_headers=["*"],
15
  )
16
 
17
+ # 🧠 懶加載大腦防止啟動超時
18
+ brain = {"asr": None, "mt": None}
19
 
20
  def get_brain(key):
21
+ if brain[key] is None:
22
  urls = {
23
  "asr": "https://ai-labs.ilrdf.org.tw/sapolita-kaldi/",
24
  "mt": "https://ai-labs.ilrdf.org.tw/kari-seejiq-tnpusu-ai-hmjil/"
25
  }
26
+ brain[key] = Client(urls[key])
27
+ return brain[key]
 
 
 
 
 
 
 
28
 
29
  @app.get("/")
30
  async def root():
31
+ return {"status": "online", "mode": "太魯閣語測試模式"}
32
 
 
 
 
33
  @app.websocket("/ws/subtitle")
34
  async def websocket_subtitle(websocket: WebSocket):
35
  await websocket.accept()
36
  audio_buffer = bytearray()
37
+ print("📢 太魯閣語專線已啟動")
 
38
 
39
  try:
40
  while True:
41
  audio_chunk = await websocket.receive_bytes()
42
  audio_buffer.extend(audio_chunk)
43
 
44
+ # 累積 25 bytes ( 3 秒) 提高太魯閣語辨識率
45
+ if len(audio_buffer) >= 250000:
46
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
47
  with wave.open(temp_wav.name, 'wb') as wav_file:
48
  wav_file.setnchannels(1)
 
51
  wav_file.writeframes(audio_buffer)
52
  temp_path = temp_wav.name
53
 
 
54
  try:
55
+ # 1. 太魯閣語 ASR
56
  asr_res = get_brain("asr").predict(
57
+ dialect_id="formosan_trv",
58
  audio_data=handle_file(temp_path),
59
  api_name="/automatic_speech_recognition"
60
  )
61
  ind_text = str(asr_res).strip()
62
 
63
+ # 2. 太魯閣語翻譯
64
+ zh_text = ""
65
+ if ind_text and ind_text not in [".", "...", ""]:
66
  mt_brain = get_brain("mt")
67
+ # 取太魯閣語代碼
68
+ d_code = "trv_Truku" # 太魯閣語固定代碼
69
  zh_res = mt_brain.predict(text=ind_text, src_lang=d_code, tgt_lang="zho_Hant", api_name="/translate")
70
  zh_text = str(zh_res).strip()
71
+
72
+ await websocket.send_json({
73
+ "status": "recognizing",
74
+ "indigenous": ind_text,
75
+ "chinese": zh_text
76
+ })
77
  except Exception as e:
78
+ print(f"辨識出錯: {e}")
79
 
 
 
 
 
 
 
80
  audio_buffer = bytearray()
81
  if os.path.exists(temp_path): os.remove(temp_path)
82
  else:
83
+ if len(audio_buffer) % 60000 == 0:
84
+ await websocket.send_json({"status": "buffering", "text": "🟢 神獸正在聽太魯閣語..."})
85
  except Exception: pass
86
 
87
  if __name__ == "__main__":