Upload 3 files
Browse files
main.py
CHANGED
|
@@ -1,5 +1,38 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Main entrypoint for Uvicorn / Hugging Face.
|
| 2 |
|
| 3 |
+
This file tries to dynamically locate the FastAPI `app` instance
|
| 4 |
+
from a few common module locations, so it works regardless of whether
|
| 5 |
+
the project structure uses `app.py` at root or an `app/` package.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from importlib import import_module
|
| 9 |
+
from typing import Any
|
| 10 |
+
|
| 11 |
+
def _try_import(path: str) -> Any:
|
| 12 |
+
try:
|
| 13 |
+
module = import_module(path)
|
| 14 |
+
except Exception:
|
| 15 |
+
return None
|
| 16 |
+
return getattr(module, "app", None)
|
| 17 |
+
|
| 18 |
+
# Candidate modules (ordered)
|
| 19 |
+
_CANDIDATES = [
|
| 20 |
+
"app", # root app.py OR app/__init__.py exposes `app`
|
| 21 |
+
"app.app", # app/app.py -> app
|
| 22 |
+
"app.main", # app/main.py -> app
|
| 23 |
+
"backend.app", # backend/app.py -> app
|
| 24 |
+
"backend.main", # backend/main.py -> app
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
app = None
|
| 28 |
+
for candidate in _CANDIDATES:
|
| 29 |
+
app_candidate = _try_import(candidate)
|
| 30 |
+
if app_candidate is not None:
|
| 31 |
+
app = app_candidate
|
| 32 |
+
break
|
| 33 |
+
|
| 34 |
+
if app is None:
|
| 35 |
+
raise RuntimeError(
|
| 36 |
+
"Could not locate FastAPI `app` instance. "
|
| 37 |
+
"Tried modules: " + ", ".join(_CANDIDATES)
|
| 38 |
+
)
|