Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1420,126 +1420,3 @@ print("✅ CMT Holographic Visualization Suite Ready!")
|
|
| 1420 |
if __name__ == "__main__":
|
| 1421 |
pass
|
| 1422 |
|
| 1423 |
-
# ================================================================
|
| 1424 |
-
# Private Space Host/Proxy (per blog instructions)
|
| 1425 |
-
# - Hosts a private HF Space inside this public Space via Gradio
|
| 1426 |
-
# - Caches the private repo inside a visible directory
|
| 1427 |
-
# - Dynamically imports and exposes its Gradio demo as `demo`
|
| 1428 |
-
# ================================================================
|
| 1429 |
-
|
| 1430 |
-
import os as _os
|
| 1431 |
-
import sys as _sys
|
| 1432 |
-
from pathlib import Path as _Path
|
| 1433 |
-
import importlib.util as _importlib_util
|
| 1434 |
-
|
| 1435 |
-
try:
|
| 1436 |
-
from huggingface_hub import snapshot_download as _snapshot_download, hf_hub_download as _hf_hub_download
|
| 1437 |
-
except Exception as _e: # pragma: no cover
|
| 1438 |
-
_snapshot_download = None
|
| 1439 |
-
_hf_hub_download = None
|
| 1440 |
-
|
| 1441 |
-
|
| 1442 |
-
_PRIVATE_SPACE_REPO: str = _os.getenv("PRIVATE_SPACE_REPO", "Severian/CMT-Mapping")
|
| 1443 |
-
|
| 1444 |
-
|
| 1445 |
-
def setup_cache_directory() -> _Path:
|
| 1446 |
-
"""Setup and return cache directory for private space files."""
|
| 1447 |
-
cache_dir = _Path(_os.getenv("PRIVATE_SPACE_CACHE_DIR", "private_space_cache"))
|
| 1448 |
-
cache_dir.mkdir(parents=True, exist_ok=True)
|
| 1449 |
-
return cache_dir
|
| 1450 |
-
|
| 1451 |
-
|
| 1452 |
-
def download_private_assets(cache_dir: _Path) -> _Path:
|
| 1453 |
-
"""Download private Space snapshot into a local directory and return its root."""
|
| 1454 |
-
if _snapshot_download is None:
|
| 1455 |
-
raise RuntimeError("huggingface_hub is required to download the private Space")
|
| 1456 |
-
|
| 1457 |
-
token = _os.getenv("HF_TOKEN")
|
| 1458 |
-
local_dir = cache_dir / "private_space"
|
| 1459 |
-
local_dir.mkdir(parents=True, exist_ok=True)
|
| 1460 |
-
|
| 1461 |
-
_snapshot_download(
|
| 1462 |
-
repo_id=_PRIVATE_SPACE_REPO,
|
| 1463 |
-
repo_type="space",
|
| 1464 |
-
local_dir=str(local_dir),
|
| 1465 |
-
token=token,
|
| 1466 |
-
)
|
| 1467 |
-
|
| 1468 |
-
return local_dir
|
| 1469 |
-
|
| 1470 |
-
|
| 1471 |
-
def _load_private_space_app(module_root: _Path):
|
| 1472 |
-
"""Load the private Space's app.py and return (module, gradio_app)."""
|
| 1473 |
-
app_path = module_root / "app.py"
|
| 1474 |
-
token = _os.getenv("HF_TOKEN")
|
| 1475 |
-
|
| 1476 |
-
if not app_path.exists():
|
| 1477 |
-
if _hf_hub_download is None:
|
| 1478 |
-
raise RuntimeError("huggingface_hub is required to locate app.py in the private Space")
|
| 1479 |
-
downloaded_app = _hf_hub_download(
|
| 1480 |
-
repo_id=_PRIVATE_SPACE_REPO,
|
| 1481 |
-
filename="app.py",
|
| 1482 |
-
repo_type="space",
|
| 1483 |
-
token=token,
|
| 1484 |
-
)
|
| 1485 |
-
app_path = _Path(downloaded_app)
|
| 1486 |
-
module_root = app_path.parent
|
| 1487 |
-
|
| 1488 |
-
# Ensure private repo root is importable for any relative imports
|
| 1489 |
-
if str(module_root) not in _sys.path:
|
| 1490 |
-
_sys.path.insert(0, str(module_root))
|
| 1491 |
-
|
| 1492 |
-
spec = _importlib_util.spec_from_file_location("private_space_app", str(app_path))
|
| 1493 |
-
if spec is None or spec.loader is None:
|
| 1494 |
-
raise ImportError(f"Unable to load spec for private app at {app_path}")
|
| 1495 |
-
module = _importlib_util.module_from_spec(spec)
|
| 1496 |
-
spec.loader.exec_module(module)
|
| 1497 |
-
|
| 1498 |
-
# Try to find a Gradio app instance
|
| 1499 |
-
private_demo = None
|
| 1500 |
-
candidate_names = ("demo", "app", "iface")
|
| 1501 |
-
for name in candidate_names:
|
| 1502 |
-
obj = getattr(module, name, None)
|
| 1503 |
-
if isinstance(obj, (gr.Blocks, gr.Interface)):
|
| 1504 |
-
private_demo = obj
|
| 1505 |
-
break
|
| 1506 |
-
if private_demo is None:
|
| 1507 |
-
for obj in module.__dict__.values():
|
| 1508 |
-
if isinstance(obj, (gr.Blocks, gr.Interface)):
|
| 1509 |
-
private_demo = obj
|
| 1510 |
-
break
|
| 1511 |
-
|
| 1512 |
-
return module, private_demo
|
| 1513 |
-
|
| 1514 |
-
|
| 1515 |
-
# Resolve the private Space and expose its `demo`
|
| 1516 |
-
try:
|
| 1517 |
-
_cache_dir = setup_cache_directory()
|
| 1518 |
-
# Propagate the cache dir so the private app imports use the same visible path
|
| 1519 |
-
_os.environ.setdefault("PRIVATE_SPACE_CACHE_DIR", str(_cache_dir.resolve()))
|
| 1520 |
-
_repo_root = download_private_assets(_cache_dir)
|
| 1521 |
-
_module, _private_demo = _load_private_space_app(_repo_root)
|
| 1522 |
-
|
| 1523 |
-
if _private_demo is not None:
|
| 1524 |
-
demo = _private_demo # type: ignore[assignment]
|
| 1525 |
-
else:
|
| 1526 |
-
with gr.Blocks() as demo: # type: ignore[no-redef]
|
| 1527 |
-
gr.Markdown(
|
| 1528 |
-
"""
|
| 1529 |
-
### Error
|
| 1530 |
-
Could not locate a Gradio app object in the private Space. Expected a variable like `demo`, `app`, or `iface`.
|
| 1531 |
-
"""
|
| 1532 |
-
)
|
| 1533 |
-
except Exception as _err: # pragma: no cover
|
| 1534 |
-
with gr.Blocks() as demo: # type: ignore[no-redef]
|
| 1535 |
-
gr.Markdown(
|
| 1536 |
-
f"""
|
| 1537 |
-
### Private Space Load Failure
|
| 1538 |
-
Failed to load `{_PRIVATE_SPACE_REPO}` due to: `{type(_err).__name__}: {_err}`
|
| 1539 |
-
"""
|
| 1540 |
-
)
|
| 1541 |
-
|
| 1542 |
-
|
| 1543 |
-
if __name__ == "__main__":
|
| 1544 |
-
# Launch for local testing; on Spaces, the platform will pick up `demo`
|
| 1545 |
-
demo.launch(server_name="0.0.0.0", server_port=int(_os.getenv("PORT", "7860")))
|
|
|
|
| 1420 |
if __name__ == "__main__":
|
| 1421 |
pass
|
| 1422 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|