rydlrKE commited on
Commit
4fc5451
·
1 Parent(s): 1ceeba7

Space: redirect to native proxy without iframe recursion; keep Qwen 09 direct load

Browse files
Files changed (1) hide show
  1. app.py +75 -15
app.py CHANGED
@@ -1,9 +1,12 @@
1
- """Movimento Space entrypoint: run native Kimodo demo directly."""
2
  from __future__ import annotations
3
 
 
4
  import os
5
  import traceback
6
- import time
 
 
7
 
8
  try:
9
  import spaces # type: ignore
@@ -18,38 +21,95 @@ except Exception:
18
 
19
  spaces = _SpacesFallback()
20
 
21
- PORT = int(os.environ.get("PORT", "7860"))
22
  os.environ.setdefault("SERVER_NAME", "0.0.0.0")
23
- os.environ["SERVER_PORT"] = str(PORT)
24
  os.environ.setdefault("HF_MODE", "1")
25
  # Avoid local LLM2Vec fallback on Spaces (requires gated Llama weights).
26
  os.environ.setdefault("TEXT_ENCODER_MODE", "api")
27
  # Prefer CPU on ZeroGPU to avoid low-level CUDA init crashes during model load.
28
  os.environ.setdefault("KIMODO_DEVICE", "cpu")
29
 
 
 
 
 
 
 
 
30
 
31
  @spaces.GPU(duration=60)
32
  def _gpu_healthcheck() -> str:
33
  # Required by ZeroGPU startup policy; native demo does not invoke this.
34
  return "ok"
35
 
36
- def main() -> None:
 
37
  try:
 
 
 
38
  import kimodo
39
  from kimodo.demo.app import Demo
40
 
41
  print(f"[movimento][boot] kimodo_module={getattr(kimodo, '__file__', 'unknown')}")
42
- print(f"[movimento][boot] mode=native_direct port={PORT}")
43
- Demo()
44
-
45
- # Keep the process alive while Viser serves on SERVER_PORT.
46
- while True:
47
- time.sleep(3600)
48
- except Exception: # noqa: BLE001
 
 
49
  print("[movimento][boot][fatal] native demo failed to start")
50
- print(traceback.format_exc(limit=12))
51
- raise
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
 
54
  if __name__ == "__main__":
55
- main()
 
1
+ """Movimento Space: bootstrap native Kimodo demo and redirect to proxy."""
2
  from __future__ import annotations
3
 
4
+ import importlib.util
5
  import os
6
  import traceback
7
+ import threading
8
+
9
+ import gradio as gr
10
 
11
  try:
12
  import spaces # type: ignore
 
21
 
22
  spaces = _SpacesFallback()
23
 
24
+ NATIVE_PORT = int(os.environ.get("KIMODO_NATIVE_PORT", "8080"))
25
  os.environ.setdefault("SERVER_NAME", "0.0.0.0")
26
+ os.environ["SERVER_PORT"] = str(NATIVE_PORT)
27
  os.environ.setdefault("HF_MODE", "1")
28
  # Avoid local LLM2Vec fallback on Spaces (requires gated Llama weights).
29
  os.environ.setdefault("TEXT_ENCODER_MODE", "api")
30
  # Prefer CPU on ZeroGPU to avoid low-level CUDA init crashes during model load.
31
  os.environ.setdefault("KIMODO_DEVICE", "cpu")
32
 
33
+ _state: dict[str, object] = {
34
+ "ok": False,
35
+ "error": None,
36
+ "trace": None,
37
+ "demo": None,
38
+ }
39
+
40
 
41
  @spaces.GPU(duration=60)
42
  def _gpu_healthcheck() -> str:
43
  # Required by ZeroGPU startup policy; native demo does not invoke this.
44
  return "ok"
45
 
46
+
47
+ def _boot_native_demo() -> None:
48
  try:
49
+ if importlib.util.find_spec("viser") is None:
50
+ raise RuntimeError("Missing dependency: viser")
51
+
52
  import kimodo
53
  from kimodo.demo.app import Demo
54
 
55
  print(f"[movimento][boot] kimodo_module={getattr(kimodo, '__file__', 'unknown')}")
56
+ print(f"[movimento][boot] demo_module={getattr(importlib.util.find_spec('kimodo.demo.app'), 'origin', 'unknown')}")
57
+ _state["demo"] = Demo()
58
+ _state["ok"] = True
59
+ _state["error"] = None
60
+ _state["trace"] = None
61
+ except Exception as exc: # noqa: BLE001
62
+ _state["ok"] = False
63
+ _state["error"] = str(exc)
64
+ _state["trace"] = traceback.format_exc(limit=12)
65
  print("[movimento][boot][fatal] native demo failed to start")
66
+ print(_state["trace"])
67
+
68
+
69
+ threading.Thread(target=_boot_native_demo, daemon=True).start()
70
+
71
+
72
+ def _status_markdown() -> str:
73
+ if bool(_state.get("ok")):
74
+ return f"Native demo ready. Redirecting to /proxy/{NATIVE_PORT}/ ..."
75
+ err = _state.get("error")
76
+ if err:
77
+ return f"Native demo failed to start: {err}"
78
+ return f"Starting native demo on /proxy/{NATIVE_PORT}/ ..."
79
+
80
+
81
+ def _redirect_html() -> str:
82
+ target = f"/proxy/{NATIVE_PORT}/"
83
+ return (
84
+ "<div style='padding:8px 0;color:#4d6372;font-size:13px;'>Preparing native UI...</div>"
85
+ f"<div><a href='{target}' target='_self' style='font-size:14px;'>Open native UI now</a></div>"
86
+ "<script>"
87
+ "(function(){"
88
+ f"const target='{target}';"
89
+ "async function step(){"
90
+ "try{"
91
+ "const r=await fetch(target,{method:'GET',cache:'no-store'});"
92
+ "if(r.ok){window.top.location.href=target;return;}"
93
+ "}catch(e){}"
94
+ "setTimeout(step,2500);"
95
+ "}"
96
+ "step();"
97
+ "})();"
98
+ "</script>"
99
+ )
100
+
101
+
102
+ def _refresh() -> tuple[str, str]:
103
+ return _status_markdown(), _redirect_html()
104
+
105
+
106
+ with gr.Blocks(title="Movimento") as demo:
107
+ gr.Markdown("# Movimento")
108
+ status_md = gr.Markdown(_status_markdown())
109
+ viewer = gr.HTML(_redirect_html())
110
+ refresh_btn = gr.Button("Refresh UI Status")
111
+ refresh_btn.click(fn=_refresh, inputs=[], outputs=[status_md, viewer])
112
 
113
 
114
  if __name__ == "__main__":
115
+ demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", "7860")))