| """ |
| A robust wrapper for the Face/Off application that extracts a bundled zip file and |
| locates the real `app.py` file within it. This wrapper avoids executing the |
| embedded app's `if __name__ == '__main__'` block and supports multiple archive |
| layouts (with or without a top-level directory). |
| |
| When deployed on Hugging Face Spaces, the repository root is `/home/user/app`. |
| This script will extract `faceoff-space.zip` into a dedicated directory and |
| search for `app.py` within the extracted contents. The first match is loaded |
| with `runpy.run_path` using a custom module name to prevent side effects. |
| """ |
|
|
| import os |
| import zipfile |
| import sys |
| import runpy |
|
|
| |
| ZIP_NAME = "faceoff-space-updated.zip" |
|
|
| |
| base_dir = os.path.dirname(__file__) |
| zip_path = os.path.join(base_dir, ZIP_NAME) |
|
|
| |
| |
| extract_target = os.path.join(base_dir, "faceoff-space-extracted") |
|
|
| |
| |
| if os.path.isfile(zip_path): |
| |
| if os.path.isdir(extract_target): |
| import shutil |
| shutil.rmtree(extract_target, ignore_errors=True) |
| os.makedirs(extract_target, exist_ok=True) |
| with zipfile.ZipFile(zip_path, "r") as archive: |
| archive.extractall(extract_target) |
|
|
| |
| |
| if os.path.isdir(extract_target) and extract_target not in sys.path: |
| sys.path.insert(0, extract_target) |
| |
| for entry in os.listdir(extract_target): |
| candidate = os.path.join(extract_target, entry) |
| if os.path.isdir(candidate) and candidate not in sys.path: |
| sys.path.insert(0, candidate) |
|
|
| def find_app_py(root: str) -> str: |
| """Recursively search for an `app.py` file starting at `root` and return |
| its path. Raises FileNotFoundError if none is found. |
| """ |
| for current_dir, subdirs, files in os.walk(root): |
| if "app.py" in files: |
| return os.path.join(current_dir, "app.py") |
| raise FileNotFoundError(f"`app.py` not found under {root}") |
|
|
| |
| try: |
| app_path = find_app_py(extract_target) |
| except FileNotFoundError: |
| |
| try: |
| app_path = find_app_py(base_dir) |
| except FileNotFoundError as exc: |
| raise RuntimeError( |
| "Unable to locate the Face/Off application (`app.py`). Ensure that " |
| f"{ZIP_NAME} contains the app files." |
| ) from exc |
|
|
| |
| app_globals = runpy.run_path(app_path, run_name="faceoff_app") |
|
|
| |
| |
| |
| |
| demo = app_globals.get("demo") |
| app = app_globals.get("app") |
|
|
| |
| |
| if demo is None and app is None: |
| raise RuntimeError( |
| "Neither `demo` nor `app` was found in the extracted Face/Off project. " |
| "Ensure that `demo` (Gradio Blocks) or `app` (FastAPI) is defined." |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| if __name__ == "__main__": |
| import os |
| if demo is not None: |
| |
| port = int(os.getenv("PORT", "7860")) |
| |
| demo.queue().launch(server_name="0.0.0.0", server_port=port, show_api=False, share=False) |
| elif app is not None: |
| |
| import uvicorn |
| port = int(os.getenv("PORT", "7860")) |
| uvicorn.run(app, host="0.0.0.0", port=port) |
|
|