Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,16 +17,27 @@ app.add_middleware(
|
|
| 17 |
)
|
| 18 |
|
| 19 |
# ==========================================
|
| 20 |
-
#
|
| 21 |
# ==========================================
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def parse_dialect(dialect_result):
|
| 31 |
if isinstance(dialect_result, dict) and 'value' in dialect_result:
|
| 32 |
return dialect_result['value']
|
|
@@ -34,9 +45,21 @@ def parse_dialect(dialect_result):
|
|
| 34 |
return dialect_result[0]
|
| 35 |
return dialect_result
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# ==========================================
|
| 42 |
# 🐉 核心進化:雅美語即時雙語字幕 (WebSocket)
|
|
@@ -45,15 +68,13 @@ async def root():
|
|
| 45 |
async def websocket_subtitle(websocket: WebSocket):
|
| 46 |
await websocket.accept()
|
| 47 |
audio_buffer = bytearray()
|
| 48 |
-
|
| 49 |
-
TAO_MT_CODE = "tao_Yami"
|
| 50 |
|
| 51 |
try:
|
| 52 |
while True:
|
| 53 |
audio_chunk = await websocket.receive_bytes()
|
| 54 |
audio_buffer.extend(audio_chunk)
|
| 55 |
|
| 56 |
-
# 累積約 2.5 秒音訊進行辨識
|
| 57 |
if len(audio_buffer) > 110000:
|
| 58 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
|
| 59 |
with wave.open(temp_wav.name, 'wb') as wav_file:
|
|
@@ -65,32 +86,15 @@ async def websocket_subtitle(websocket: WebSocket):
|
|
| 65 |
|
| 66 |
ind_text, zh_text = "", ""
|
| 67 |
try:
|
| 68 |
-
|
| 69 |
-
asr_res = asr_client.predict(
|
| 70 |
-
dialect_id="formosan_tao",
|
| 71 |
-
audio_data=handle_file(temp_path),
|
| 72 |
-
api_name="/automatic_speech_recognition"
|
| 73 |
-
)
|
| 74 |
ind_text = str(asr_res).strip()
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
if ind_text and ind_text != "...":
|
| 78 |
-
zh_res = trans_client.predict(
|
| 79 |
-
text=ind_text,
|
| 80 |
-
src_lang=TAO_MT_CODE,
|
| 81 |
-
tgt_lang="zho_Hant",
|
| 82 |
-
api_name="/translate"
|
| 83 |
-
)
|
| 84 |
zh_text = str(zh_res)
|
| 85 |
except Exception as e:
|
| 86 |
-
print(f"⚠️ 即時
|
| 87 |
|
| 88 |
-
await websocket.send_json({
|
| 89 |
-
"status": "recognizing",
|
| 90 |
-
"indigenous": ind_text if ind_text else "...",
|
| 91 |
-
"chinese": zh_text
|
| 92 |
-
})
|
| 93 |
-
|
| 94 |
audio_buffer = bytearray()
|
| 95 |
if os.path.exists(temp_path): os.remove(temp_path)
|
| 96 |
else:
|
|
@@ -99,20 +103,15 @@ async def websocket_subtitle(websocket: WebSocket):
|
|
| 99 |
except Exception: pass
|
| 100 |
|
| 101 |
# ==========================================
|
| 102 |
-
# 📚 保
|
| 103 |
# ==========================================
|
| 104 |
@app.post("/translate")
|
| 105 |
async def translate(data: dict):
|
| 106 |
-
source_text = data.get("text")
|
| 107 |
-
direction = data.get("direction", "zh2indigenous")
|
| 108 |
-
ethnicity = data.get("ethnicity", "太魯閣")
|
| 109 |
try:
|
| 110 |
-
if direction in ["zh2trv", "zh2indigenous", "中翻族"]
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
else:
|
| 114 |
-
dialect_code = parse_dialect(trans_client.predict(ethnicity=ethnicity, api_name="/lambda"))
|
| 115 |
-
result = trans_client.predict(text=source_text, src_lang=dialect_code, tgt_lang="zho_Hant", api_name="/translate")
|
| 116 |
return {"result": result}
|
| 117 |
except Exception as e: return {"result": str(e)}
|
| 118 |
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
# ==========================================
|
| 20 |
+
# 🏠 首頁入口 (解決 Not Found 的第一步)
|
| 21 |
# ==========================================
|
| 22 |
+
@app.get("/")
|
| 23 |
+
async def root():
|
| 24 |
+
return {"status": "running", "message": "16 族語 AI 雙語神獸大腦完全體,請透過套件呼叫。"}
|
| 25 |
+
|
| 26 |
+
# ==========================================
|
| 27 |
+
# 🔗 連結核心大腦
|
| 28 |
+
# ==========================================
|
| 29 |
+
def safe_client_init(url, name):
|
| 30 |
+
try:
|
| 31 |
+
return Client(url)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"⚠️ {name} 連線失敗: {e}")
|
| 34 |
+
return None
|
| 35 |
|
| 36 |
+
asr_client = safe_client_init("https://ai-labs.ilrdf.org.tw/sapolita-kaldi/", "語音辨識")
|
| 37 |
+
trans_client = safe_client_init("https://ai-labs.ilrdf.org.tw/kari-seejiq-tnpusu-ai-hmjil/", "翻譯")
|
| 38 |
+
tts_client = safe_client_init("https://ai-labs.ilrdf.org.tw/hnang-kari-ai-asi-sluhay/", "語音合成")
|
| 39 |
+
|
| 40 |
+
# 🛠️ 解析工具
|
| 41 |
def parse_dialect(dialect_result):
|
| 42 |
if isinstance(dialect_result, dict) and 'value' in dialect_result:
|
| 43 |
return dialect_result['value']
|
|
|
|
| 45 |
return dialect_result[0]
|
| 46 |
return dialect_result
|
| 47 |
|
| 48 |
+
# ==========================================
|
| 49 |
+
# 🧪 大腦健檢 (請測試此路徑: /test_api)
|
| 50 |
+
# ==========================================
|
| 51 |
+
@app.get("/test_api")
|
| 52 |
+
async def test_api():
|
| 53 |
+
results = {"asr": "待測", "mt_status": "待測", "tao_code": "tao_Yami"}
|
| 54 |
+
if asr_client: results["asr"] = "✅ 已連線"
|
| 55 |
+
if trans_client:
|
| 56 |
+
try:
|
| 57 |
+
# 測試雅美語翻譯
|
| 58 |
+
test_res = trans_client.predict(text="Akokay", src_lang="tao_Yami", tgt_lang="zho_Hant", api_name="/translate")
|
| 59 |
+
results["mt_status"] = f"✅ 成功! Akokay -> {test_res}"
|
| 60 |
+
except Exception as e:
|
| 61 |
+
results["mt_status"] = f"❌ 報錯: {str(e)}"
|
| 62 |
+
return results
|
| 63 |
|
| 64 |
# ==========================================
|
| 65 |
# 🐉 核心進化:雅美語即時雙語字幕 (WebSocket)
|
|
|
|
| 68 |
async def websocket_subtitle(websocket: WebSocket):
|
| 69 |
await websocket.accept()
|
| 70 |
audio_buffer = bytearray()
|
| 71 |
+
TAO_MT_CODE = "tao_Yami" # 鎖定正確代碼
|
|
|
|
| 72 |
|
| 73 |
try:
|
| 74 |
while True:
|
| 75 |
audio_chunk = await websocket.receive_bytes()
|
| 76 |
audio_buffer.extend(audio_chunk)
|
| 77 |
|
|
|
|
| 78 |
if len(audio_buffer) > 110000:
|
| 79 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
|
| 80 |
with wave.open(temp_wav.name, 'wb') as wav_file:
|
|
|
|
| 86 |
|
| 87 |
ind_text, zh_text = "", ""
|
| 88 |
try:
|
| 89 |
+
asr_res = asr_client.predict(dialect_id="formosan_tao", audio_data=handle_file(temp_path), api_name="/automatic_speech_recognition")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
ind_text = str(asr_res).strip()
|
| 91 |
+
if ind_text and ind_text != "..." and trans_client:
|
| 92 |
+
zh_res = trans_client.predict(text=ind_text, src_lang=TAO_MT_CODE, tgt_lang="zho_Hant", api_name="/translate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
zh_text = str(zh_res)
|
| 94 |
except Exception as e:
|
| 95 |
+
print(f"⚠️ 即時辨識出錯: {e}")
|
| 96 |
|
| 97 |
+
await websocket.send_json({"status": "recognizing", "indigenous": ind_text, "chinese": zh_text})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
audio_buffer = bytearray()
|
| 99 |
if os.path.exists(temp_path): os.remove(temp_path)
|
| 100 |
else:
|
|
|
|
| 103 |
except Exception: pass
|
| 104 |
|
| 105 |
# ==========================================
|
| 106 |
+
# 📚 保持 16 族 REST 功能 (保留舊版優點)
|
| 107 |
# ==========================================
|
| 108 |
@app.post("/translate")
|
| 109 |
async def translate(data: dict):
|
| 110 |
+
source_text, direction, ethnicity = data.get("text"), data.get("direction", "zh2indigenous"), data.get("ethnicity", "太魯閣")
|
|
|
|
|
|
|
| 111 |
try:
|
| 112 |
+
api_name = "/translate_1" if direction in ["zh2trv", "zh2indigenous", "中翻族"] else "/translate"
|
| 113 |
+
src_l, tgt_l = ("zho_Hant", parse_dialect(trans_client.predict(ethnicity=ethnicity, api_name="/lambda_1"))) if "/translate_1" in api_name else (parse_dialect(trans_client.predict(ethnicity=ethnicity, api_name="/lambda")), "zho_Hant")
|
| 114 |
+
result = trans_client.predict(text=source_text, src_lang=src_l, tgt_lang=tgt_l, api_name=api_name)
|
|
|
|
|
|
|
|
|
|
| 115 |
return {"result": result}
|
| 116 |
except Exception as e: return {"result": str(e)}
|
| 117 |
|