| from __future__ import annotations |
|
|
| import importlib.util |
| import os |
| import sys |
| from pathlib import Path |
|
|
|
|
| ROOT = Path(__file__).parent |
| BUILD_ROOT = ROOT / "build" / "pyc" |
| ASSET_ROOT = ROOT / "assets" |
| COMPILED_ASSET_ROOT = BUILD_ROOT / "assets" |
|
|
| for entry in [str(BUILD_ROOT), str(ROOT)]: |
| if entry not in sys.path: |
| sys.path.insert(0, entry) |
|
|
| if ASSET_ROOT.exists() and not COMPILED_ASSET_ROOT.exists(): |
| try: |
| COMPILED_ASSET_ROOT.parent.mkdir(parents=True, exist_ok=True) |
| COMPILED_ASSET_ROOT.symlink_to(ASSET_ROOT, target_is_directory=True) |
| except OSError: |
| import shutil |
|
|
| shutil.copytree(ASSET_ROOT, COMPILED_ASSET_ROOT, dirs_exist_ok=True) |
|
|
| compiled = BUILD_ROOT / "space_app_impl.pyc" |
| if not compiled.exists(): |
| raise RuntimeError("Missing compiled HF Space runtime: build/pyc/space_app_impl.pyc") |
|
|
| spec = importlib.util.spec_from_file_location("space_app_impl_compiled", compiled) |
| if spec is None or spec.loader is None: |
| raise RuntimeError("Failed to load compiled HF Space runtime.") |
|
|
| module = importlib.util.module_from_spec(spec) |
| try: |
| spec.loader.exec_module(module) |
| except ImportError as exc: |
| raise RuntimeError( |
| "Failed to load compiled HF Space runtime. " |
| "The published .pyc files were likely built with a different Python version or " |
| "the runtime dependencies are missing. Rebuild .public-dist with Python 3.13 " |
| "and ensure requirements are installed." |
| ) from exc |
|
|
| app = getattr(module, "app", None) |
| demo = getattr(module, "demo", None) |
| if app is None: |
| raise RuntimeError("Compiled HF Space runtime did not expose 'app'.") |
|
|
| if __name__ == "__main__": |
| import uvicorn |
|
|
| port = int(os.environ.get("PORT", "7860")) |
| uvicorn.run(app, host="0.0.0.0", port=port, log_level="info") |
|
|