BrianChuan commited on
Commit
4139bc4
·
1 Parent(s): 861a64a

address fix test

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -1233,18 +1233,24 @@ with gr.Blocks(title="模擬校園入口與資安防護系統") as demo:
1233
  # =====================================================================
1234
 
1235
  if __name__ == "__main__":
1236
- # Serve both:
1237
- # 1) Gradio UI
1238
- # 2) /verify_identity callback endpoint (FastAPI)
 
1239
  try:
1240
  import uvicorn
1241
- if hasattr(gr, "mount_gradio_app"):
1242
- gr.mount_gradio_app(api_app, demo, path="/")
1243
- host = os.getenv("HOST", "127.0.0.1")
1244
- port = int(os.getenv("PORT", "7860"))
1245
- uvicorn.run(api_app, host=host, port=port)
1246
- else:
1247
- demo.launch(css=custom_css)
 
 
 
 
1248
  except Exception as e:
1249
- print(f"⚠️ FastAPI mount failed: {e}")
1250
- demo.launch(css=custom_css)
 
 
1233
  # =====================================================================
1234
 
1235
  if __name__ == "__main__":
1236
+ # 1. 取得環境變數,HF 預設 PORT 通常是 7860,HOST 務必用 0.0.0.0
1237
+ host = os.getenv("HOST", "0.0.0.0")
1238
+ port = int(os.getenv("PORT", 7860))
1239
+
1240
  try:
1241
  import uvicorn
1242
+
1243
+ # 2. 將 Gradio 掛載到 FastAPI (api_app)
1244
+ # 這樣兩者會「合體」在同一個 Port 執行
1245
+ gr.mount_gradio_app(api_app, demo, path="/")
1246
+
1247
+ print(f"🚀 數位保鏢系統啟動中:http://{host}:{port}")
1248
+
1249
+ # 3. 唯一啟動點:交給 uvicorn 負責
1250
+ # 不要再 except 裡面放 demo.launch(),避免自動跳埠到 7861
1251
+ uvicorn.run(api_app, host=host, port=port, log_level="info")
1252
+
1253
  except Exception as e:
1254
+ # 如果啟動失敗,直接印出錯誤,不要嘗試備援啟動
1255
+ print(f"❌ 伺服器啟動嚴重錯誤: {e}")
1256
+ print("💡 提示:如果是 Port 7860 被佔用,請至 HF Settings 點擊 'Factory Reboot'")