Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,34 +16,27 @@ app.add_middleware(
|
|
| 16 |
)
|
| 17 |
|
| 18 |
# ==========================================
|
| 19 |
-
# 🔗 連結
|
| 20 |
# ==========================================
|
| 21 |
def safe_client_init(url, name):
|
| 22 |
try:
|
| 23 |
-
print(f"🧬
|
| 24 |
return Client(url)
|
| 25 |
except Exception as e:
|
| 26 |
print(f"⚠️ {name} 連線失敗: {e}")
|
| 27 |
return None
|
| 28 |
|
|
|
|
| 29 |
asr_client = safe_client_init("https://ai-labs.ilrdf.org.tw/sapolita-kaldi/", "語音辨識")
|
| 30 |
-
trans_client = safe_client_init("https://ai-labs.ilrdf.org.tw/kari-seejiq-tnpusu-ai-hmjil/", "翻譯")
|
| 31 |
-
|
| 32 |
-
# 解析語言代碼的小工具
|
| 33 |
-
def parse_dialect(dialect_result):
|
| 34 |
-
if isinstance(dialect_result, dict) and 'value' in dialect_result:
|
| 35 |
-
return dialect_result['value']
|
| 36 |
-
elif isinstance(dialect_result, list):
|
| 37 |
-
return dialect_result[0]
|
| 38 |
-
return dialect_result
|
| 39 |
|
| 40 |
# ==========================================
|
| 41 |
-
# 🐉 核心功能:
|
| 42 |
# ==========================================
|
| 43 |
@app.websocket("/ws/subtitle")
|
| 44 |
async def websocket_subtitle(websocket: WebSocket):
|
| 45 |
await websocket.accept()
|
| 46 |
-
print("📢 神獸雙語專線已
|
| 47 |
audio_buffer = bytearray()
|
| 48 |
|
| 49 |
try:
|
|
@@ -51,9 +44,9 @@ async def websocket_subtitle(websocket: WebSocket):
|
|
| 51 |
audio_chunk = await websocket.receive_bytes()
|
| 52 |
audio_buffer.extend(audio_chunk)
|
| 53 |
|
| 54 |
-
# 累積約 2
|
| 55 |
-
if len(audio_buffer) >
|
| 56 |
-
print(f"🧠
|
| 57 |
|
| 58 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
|
| 59 |
with wave.open(temp_wav.name, 'wb') as wav_file:
|
|
@@ -68,53 +61,48 @@ async def websocket_subtitle(websocket: WebSocket):
|
|
| 68 |
|
| 69 |
if asr_client:
|
| 70 |
try:
|
| 71 |
-
# 1. 雅美語辨識
|
| 72 |
-
|
| 73 |
dialect_id="formosan_tao",
|
| 74 |
audio_data=handle_file(temp_path),
|
| 75 |
api_name="/automatic_speech_recognition"
|
| 76 |
)
|
| 77 |
-
indigenous_text = str(
|
| 78 |
-
|
| 79 |
-
# 2. 如果辨識
|
| 80 |
-
if indigenous_text and indigenous_text
|
| 81 |
-
print(f"✨ 辨識
|
| 82 |
if trans_client:
|
| 83 |
-
#
|
| 84 |
-
dialect_code = parse_dialect(trans_client.predict(ethnicity="雅美", api_name="/lambda"))
|
| 85 |
translation = trans_client.predict(
|
| 86 |
text=indigenous_text,
|
| 87 |
-
src_lang=
|
| 88 |
tgt_lang="zho_Hant",
|
| 89 |
api_name="/translate"
|
| 90 |
)
|
| 91 |
chinese_text = str(translation)
|
| 92 |
-
print(f"🧧 翻譯
|
| 93 |
-
|
| 94 |
except Exception as e:
|
| 95 |
-
print(f"⚠️ 處理
|
|
|
|
| 96 |
|
| 97 |
-
# 3. 回傳雙語
|
| 98 |
await websocket.send_json({
|
| 99 |
"status": "recognizing",
|
| 100 |
"indigenous": indigenous_text if indigenous_text else "...",
|
| 101 |
-
"chinese": chinese_text if chinese_text else "
|
| 102 |
})
|
| 103 |
|
| 104 |
audio_buffer = bytearray()
|
| 105 |
-
if os.path.exists(temp_path):
|
| 106 |
-
os.remove(temp_path)
|
| 107 |
else:
|
| 108 |
-
# 緩衝
|
| 109 |
-
if len(audio_buffer) %
|
| 110 |
await websocket.send_json({"status": "buffering", "text": "🟢 神獸正在聽雅美語..."})
|
| 111 |
|
| 112 |
except WebSocketDisconnect:
|
| 113 |
-
print("❌
|
| 114 |
-
except Exception as e:
|
| 115 |
-
print(f"⚠️ 系統錯誤: {e}")
|
| 116 |
|
| 117 |
-
# (保持其餘 translate, synthesize API 不變以便相容舊功能)
|
| 118 |
if __name__ == "__main__":
|
| 119 |
import uvicorn
|
| 120 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
# ==========================================
|
| 19 |
+
# 🔗 連結核心大腦
|
| 20 |
# ==========================================
|
| 21 |
def safe_client_init(url, name):
|
| 22 |
try:
|
| 23 |
+
print(f"🧬 連線至 {name}: {url}")
|
| 24 |
return Client(url)
|
| 25 |
except Exception as e:
|
| 26 |
print(f"⚠️ {name} 連線失敗: {e}")
|
| 27 |
return None
|
| 28 |
|
| 29 |
+
# 使用您提供的最新 ASR 地址
|
| 30 |
asr_client = safe_client_init("https://ai-labs.ilrdf.org.tw/sapolita-kaldi/", "語音辨識")
|
| 31 |
+
trans_client = safe_client_init("https://ai-labs.ilrdf.org.tw/kari-seejiq-tnpusu-ai-hmjil/", "翻譯(MT)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# ==========================================
|
| 34 |
+
# 🐉 核心功能:雅美語 ASR + 中文翻譯
|
| 35 |
# ==========================================
|
| 36 |
@app.websocket("/ws/subtitle")
|
| 37 |
async def websocket_subtitle(websocket: WebSocket):
|
| 38 |
await websocket.accept()
|
| 39 |
+
print("📢 神獸雙語專線已接通")
|
| 40 |
audio_buffer = bytearray()
|
| 41 |
|
| 42 |
try:
|
|
|
|
| 44 |
audio_chunk = await websocket.receive_bytes()
|
| 45 |
audio_buffer.extend(audio_chunk)
|
| 46 |
|
| 47 |
+
# ✨ 調低門檻:累積約 2 秒音訊 (約 80,000 bytes) 就送辨識,縮短等待時間
|
| 48 |
+
if len(audio_buffer) > 80000:
|
| 49 |
+
print(f"🧠 數據量達標,啟動 AI 辨識...")
|
| 50 |
|
| 51 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
|
| 52 |
with wave.open(temp_wav.name, 'wb') as wav_file:
|
|
|
|
| 61 |
|
| 62 |
if asr_client:
|
| 63 |
try:
|
| 64 |
+
# 1. 呼叫雅美語 (Tao) 辨識
|
| 65 |
+
asr_res = asr_client.predict(
|
| 66 |
dialect_id="formosan_tao",
|
| 67 |
audio_data=handle_file(temp_path),
|
| 68 |
api_name="/automatic_speech_recognition"
|
| 69 |
)
|
| 70 |
+
indigenous_text = str(asr_res).strip()
|
| 71 |
+
|
| 72 |
+
# 2. 如果辨識成功,立刻進行中文翻譯
|
| 73 |
+
if indigenous_text and len(indigenous_text) > 1:
|
| 74 |
+
print(f"✨ 辨識成功: {indigenous_text}")
|
| 75 |
if trans_client:
|
| 76 |
+
# 自動翻譯 (預設雅美語代碼)
|
|
|
|
| 77 |
translation = trans_client.predict(
|
| 78 |
text=indigenous_text,
|
| 79 |
+
src_lang="tao_Latn", # 雅美語翻譯代碼
|
| 80 |
tgt_lang="zho_Hant",
|
| 81 |
api_name="/translate"
|
| 82 |
)
|
| 83 |
chinese_text = str(translation)
|
| 84 |
+
print(f"🧧 翻譯成功: {chinese_text}")
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
+
print(f"⚠️ AI 處理中斷: {e}")
|
| 87 |
+
indigenous_text = "AI 正在思考中..."
|
| 88 |
|
| 89 |
+
# 3. 回傳雙語數據給網頁
|
| 90 |
await websocket.send_json({
|
| 91 |
"status": "recognizing",
|
| 92 |
"indigenous": indigenous_text if indigenous_text else "...",
|
| 93 |
+
"chinese": chinese_text if chinese_text else "辨識中..."
|
| 94 |
})
|
| 95 |
|
| 96 |
audio_buffer = bytearray()
|
| 97 |
+
if os.path.exists(temp_path): os.remove(temp_path)
|
|
|
|
| 98 |
else:
|
| 99 |
+
# 緩衝中狀態 (每收到 30 個封包更新一次)
|
| 100 |
+
if len(audio_buffer) % 30 == 0:
|
| 101 |
await websocket.send_json({"status": "buffering", "text": "🟢 神獸正在聽雅美語..."})
|
| 102 |
|
| 103 |
except WebSocketDisconnect:
|
| 104 |
+
print("❌ 神獸連線中斷")
|
|
|
|
|
|
|
| 105 |
|
|
|
|
| 106 |
if __name__ == "__main__":
|
| 107 |
import uvicorn
|
| 108 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|