Spaces:
Running
Running
Update backend/app.py
Browse files- backend/app.py +39 -0
backend/app.py
CHANGED
|
@@ -236,6 +236,26 @@ os.makedirs(IMAGES_DIR, exist_ok=True)
|
|
| 236 |
|
| 237 |
app.mount("/outputs", StaticFiles(directory=OUTPUT_DIR), name="outputs")
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 240 |
|
| 241 |
# =====================================================
|
|
@@ -1021,6 +1041,25 @@ async def serve_frontend():
|
|
| 1021 |
return FileResponse(index_path)
|
| 1022 |
return JSONResponse({"message": "Backend is running. Frontend build not found."})
|
| 1023 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1024 |
if __name__ == "__main__":
|
| 1025 |
# Use PORT provided by the environment (Hugging Face Spaces sets PORT=7860)
|
| 1026 |
port = int(os.environ.get("PORT", 7860))
|
|
|
|
| 236 |
|
| 237 |
app.mount("/outputs", StaticFiles(directory=OUTPUT_DIR), name="outputs")
|
| 238 |
|
| 239 |
+
# Mount public sample images from frontend dist (Vite copies public/ to dist/ root)
|
| 240 |
+
# Check both possible locations: frontend/dist (Docker) and ../frontend/dist (local dev)
|
| 241 |
+
FRONTEND_DIST_CHECK = os.path.join(os.path.dirname(__file__), "frontend/dist")
|
| 242 |
+
if not os.path.isdir(FRONTEND_DIST_CHECK):
|
| 243 |
+
FRONTEND_DIST_CHECK = os.path.abspath(os.path.join(os.path.dirname(__file__), "../frontend/dist"))
|
| 244 |
+
|
| 245 |
+
for sample_dir in ["cyto", "colpo", "histo"]:
|
| 246 |
+
sample_path = os.path.join(FRONTEND_DIST_CHECK, sample_dir)
|
| 247 |
+
if os.path.isdir(sample_path):
|
| 248 |
+
app.mount(f"/{sample_dir}", StaticFiles(directory=sample_path), name=sample_dir)
|
| 249 |
+
print(f"✅ Mounted /{sample_dir} from {sample_path}")
|
| 250 |
+
else:
|
| 251 |
+
print(f"⚠️ Sample directory not found: {sample_path}")
|
| 252 |
+
|
| 253 |
+
# Mount other static assets (logos, banners) from dist root
|
| 254 |
+
for static_file in ["banner.jpeg", "white_logo.png", "black_logo.png", "manalife_LOGO.jpg"]:
|
| 255 |
+
static_path = os.path.join(FRONTEND_DIST_CHECK, static_file)
|
| 256 |
+
if os.path.isfile(static_path):
|
| 257 |
+
print(f"✅ Static file available: /{static_file}")
|
| 258 |
+
|
| 259 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 260 |
|
| 261 |
# =====================================================
|
|
|
|
| 1041 |
return FileResponse(index_path)
|
| 1042 |
return JSONResponse({"message": "Backend is running. Frontend build not found."})
|
| 1043 |
|
| 1044 |
+
@app.get("/{file_path:path}")
|
| 1045 |
+
async def serve_static_files(file_path: str):
|
| 1046 |
+
"""Serve static files from frontend dist (images, logos, etc.)"""
|
| 1047 |
+
# Skip API routes
|
| 1048 |
+
if file_path.startswith(("predict", "reports", "models", "health", "outputs", "assets", "cyto", "colpo", "histo")):
|
| 1049 |
+
return JSONResponse({"error": "Not found"}, status_code=404)
|
| 1050 |
+
|
| 1051 |
+
# Try to serve file from dist root
|
| 1052 |
+
static_file = os.path.join(FRONTEND_DIST, file_path)
|
| 1053 |
+
if os.path.isfile(static_file):
|
| 1054 |
+
return FileResponse(static_file)
|
| 1055 |
+
|
| 1056 |
+
# Fallback to index.html for client-side routing
|
| 1057 |
+
index_path = os.path.join(FRONTEND_DIST, "index.html")
|
| 1058 |
+
if os.path.isfile(index_path):
|
| 1059 |
+
return FileResponse(index_path)
|
| 1060 |
+
|
| 1061 |
+
return JSONResponse({"error": "Not found"}, status_code=404)
|
| 1062 |
+
|
| 1063 |
if __name__ == "__main__":
|
| 1064 |
# Use PORT provided by the environment (Hugging Face Spaces sets PORT=7860)
|
| 1065 |
port = int(os.environ.get("PORT", 7860))
|