Spaces:
Sleeping
Sleeping
Update pws_service.py
Browse files- pws_service.py +37 -29
pws_service.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# pws_service.py
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
from config import PWS_API_URL
|
|
@@ -6,41 +6,49 @@ from config import PWS_API_URL
|
|
| 6 |
def fetch_latest_pws_info() -> str:
|
| 7 |
"""
|
| 8 |
從 MCP PWS 伺服器擷取最新的 PWS (Public Weather Service) 發布情形。
|
| 9 |
-
|
| 10 |
"""
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
-
# 使用 stream=True
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# SSE 事件的資料通常以 "data: " 開頭
|
| 21 |
-
if decoded_line.startswith('data:'):
|
| 22 |
-
# 移除 "data: " 前綴並去除多餘空格,取得 JSON 字串
|
| 23 |
-
json_data_str = decoded_line[5:].strip()
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return "❌ PWS 查詢失敗:無法從伺服器串流中解析到有效的 PWS 資料。"
|
| 42 |
|
|
|
|
|
|
|
| 43 |
except requests.exceptions.RequestException as e:
|
| 44 |
-
return f"❌ PWS
|
| 45 |
except Exception as e:
|
| 46 |
return f"❌ PWS 查詢失敗:發生未預期的錯誤。\n錯誤訊息:{e}"
|
|
|
|
| 1 |
+
# pws_service.py (Robust Version)
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
from config import PWS_API_URL
|
|
|
|
| 6 |
def fetch_latest_pws_info() -> str:
|
| 7 |
"""
|
| 8 |
從 MCP PWS 伺服器擷取最新的 PWS (Public Weather Service) 發布情形。
|
| 9 |
+
此版本經過優化,能穩定處理 Gradio API 的 Server-Sent Events (SSE) 串流。
|
| 10 |
"""
|
| 11 |
+
final_report = None
|
| 12 |
+
|
| 13 |
try:
|
| 14 |
+
# 使用 stream=True 保持連線,接收伺服器持續發送的事件
|
| 15 |
+
with requests.get(PWS_API_URL, timeout=20, stream=True) as r:
|
| 16 |
+
r.raise_for_status() # 確保 HTTP 連線成功
|
| 17 |
|
| 18 |
+
# 迭代處理從伺服器接收到的每一行資料
|
| 19 |
+
for line in r.iter_lines():
|
| 20 |
+
if line:
|
| 21 |
+
decoded_line = line.decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# SSE 事件的資料以 "data: " 開頭
|
| 24 |
+
if decoded_line.startswith('data:'):
|
| 25 |
+
json_data_str = decoded_line[len('data:'):].strip()
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
data = json.loads(json_data_str)
|
| 29 |
+
# Gradio API 在完成時會發送 'process_completed' 訊息
|
| 30 |
+
if data.get("msg") == "process_completed":
|
| 31 |
+
# 最終結果通常在 output['data'][0] 中
|
| 32 |
+
output_data = data.get("output", {}).get("data", [])
|
| 33 |
+
if output_data:
|
| 34 |
+
final_report = output_data[0]
|
| 35 |
+
break # 收到最終結果,跳出迴圈
|
| 36 |
|
| 37 |
+
except (json.JSONDecodeError, KeyError, IndexError):
|
| 38 |
+
# 如果中途的訊息格式不符或不含所需資料,靜默忽略並繼續等待
|
| 39 |
+
continue
|
| 40 |
|
| 41 |
+
# 迴圈結束後,檢查是否成功取得最終報告
|
| 42 |
+
if final_report:
|
| 43 |
+
header = "🛰️ 最新 PWS 發布情形:"
|
| 44 |
+
separator = "-" * 20
|
| 45 |
+
return f"{header}\n{separator}\n{final_report}"
|
| 46 |
+
else:
|
| 47 |
+
return "❌ PWS 查詢失敗:已連接伺服器,但未收到有效的最終報告資料。"
|
|
|
|
| 48 |
|
| 49 |
+
except requests.exceptions.Timeout:
|
| 50 |
+
return f"❌ PWS 查詢失敗:連線超時,伺服器沒有在 20 秒內回應。"
|
| 51 |
except requests.exceptions.RequestException as e:
|
| 52 |
+
return f"❌ PWS 查詢失敗:網路連線錯誤。\n錯誤訊息:{e}"
|
| 53 |
except Exception as e:
|
| 54 |
return f"❌ PWS 查詢失敗:發生未預期的錯誤。\n錯誤訊息:{e}"
|