Breeze1124 commited on
Commit
f8ccc62
·
1 Parent(s): a247643
Files changed (1) hide show
  1. app.py +44 -36
app.py CHANGED
@@ -34,49 +34,57 @@ def load_scene(evt: gr.SelectData, request: gr.Request):
34
  index = evt.index
35
  scene = SCENES[index]
36
 
37
- # 組合絕對路徑
38
- full_model_path = os.path.join(BASE_DIR, scene["model"])
39
 
40
- # 1. 檢查檔案是否存在
41
- if not os.path.exists(full_model_path):
42
- return (
43
- "<div style='text-align:center; padding:50px; color:red'>⚠️ File not found</div>",
44
- None,
45
- f"**⚠️ Error:** File not found: {full_model_path}"
46
- )
47
 
48
- # 2. 處理路徑 (Linux/HF Spaces 相容性)
49
- # HF Spaces Linux 環境,路徑通常是 /home/user/app/...
50
- clean_path = full_model_path
51
-
52
- # 3. 判斷網址 (這是修正的關鍵!)
53
- # 預設值
54
- base_url = "http://127.0.0.1:7860"
55
-
56
- if request:
57
- host = request.headers.get("host") # 獲取網域,例如 yfyangd-rgs-slam-demo.hf.space
58
 
59
- # --- 強制轉 HTTPS 的邏輯 ---
60
- # 如果網址包含 'hf.space' (Hugging Face) 或 'gradio.live',強制用 https
61
- if "hf.space" in host or "gradio.live" in host:
62
- base_url = f"https://{host}"
63
- else:
64
- base_url = f"http://{host}"
65
-
66
- # 4. 組合檔案連結
67
- file_url = f"{base_url}/file={clean_path}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- # 5. 編碼網址
70
- # 這裡很關鍵:我們要把 file_url 變成外部 viewer 看得懂的參數
71
- # 使用 quote 將特殊符號 (如空格、=、?) 轉碼,但保留 : 和 / 以維持網址結構
72
  encoded_file_url = urllib.parse.quote(file_url, safe=':/')
73
 
74
- # 6. 組合外部 Viewer 連結
75
  viewer_url = f"https://antimatter15.com/splat/?url={encoded_file_url}"
76
 
77
- print(f"🚀 Final URL (Should be HTTPS): {viewer_url}")
78
-
79
- # 7. 生成 HTML
80
  html = f"""
81
  <iframe
82
  src="{viewer_url}"
@@ -88,7 +96,7 @@ def load_scene(evt: gr.SelectData, request: gr.Request):
88
  ></iframe>
89
  """
90
 
91
- status_msg = f"**{scene['name']}** - Loaded from {base_url}"
92
 
93
  return (
94
  html,
 
34
  index = evt.index
35
  scene = SCENES[index]
36
 
37
+ # 取得 Hugging Face Space 的 ID (例如: yfyangd/rgs-slam-demo)
38
+ space_id = os.environ.get("SPACE_ID")
39
 
40
+ # --- 關鍵修正邏輯 ---
 
 
 
 
 
 
41
 
42
+ if space_id:
43
+ # 【情境 A:在 Hugging Face Spaces 線上執行】
44
+ # 我們改用 Hugging Face 的 "resolve" 網址。
45
+ # 這是直接指向 Git 倉庫的原始檔案,速度快且通常不會被 CORS 擋住。
46
+ # 格式: https://huggingface.co/spaces/{SPACE_ID}/resolve/main/{相對路徑}
47
+
48
+ # 注意:這裡直接用 scene["model"] (例如 assets/office0.ply),不需要絕對路徑
49
+ file_url = f"https://huggingface.co/spaces/{space_id}/resolve/main/{scene['model']}"
 
 
50
 
51
+ # 有些 viewer 需要加上 download=true 才能正確觸發 header,但 antimatter 通常不用
52
+ print(f"🌍 Using HF Repo URL: {file_url}")
53
+
54
+ else:
55
+ # 【情境 B:在 本機 (Localhost) 執行】
56
+ # 保持原本的邏輯,使用 localhost 的連結
57
+ full_model_path = os.path.join(BASE_DIR, scene["model"])
58
+
59
+ if not os.path.exists(full_model_path):
60
+ return (
61
+ "<div style='text-align:center; padding:50px; color:red'>⚠️ File not found</div>",
62
+ None,
63
+ f"**⚠️ Error:** File not found: {full_model_path}"
64
+ )
65
+
66
+ # 處理 Windows 路徑
67
+ clean_path = full_model_path.replace("\\", "/")
68
+
69
+ base_url = "http://127.0.0.1:7860"
70
+ if request:
71
+ host = request.headers.get("host")
72
+ if "gradio.live" in host:
73
+ base_url = f"https://{host}"
74
+ else:
75
+ base_url = f"http://{host}"
76
+
77
+ file_url = f"{base_url}/file={clean_path}"
78
+ print(f"🏠 Using Local URL: {file_url}")
79
+
80
+ # --- 通用部分:編碼並生成 iframe ---
81
 
82
+ # 編碼 file_url (處理特殊字元)
 
 
83
  encoded_file_url = urllib.parse.quote(file_url, safe=':/')
84
 
85
+ # 組合外部 Viewer 連結
86
  viewer_url = f"https://antimatter15.com/splat/?url={encoded_file_url}"
87
 
 
 
 
88
  html = f"""
89
  <iframe
90
  src="{viewer_url}"
 
96
  ></iframe>
97
  """
98
 
99
+ status_msg = f"**{scene['name']}** - Loaded via {'HF CDN' if space_id else 'Local Server'}"
100
 
101
  return (
102
  html,