Files
stemengine_old/backend/main.py
T

85 lines
2.9 KiB
Python
Executable File

"""
StemEngine — FastAPI application entry point.
Mounts all API routers and serves the built React frontend as static files.
"""
import logging
import logging.config
import warnings
from pathlib import Path
# Suppress FutureWarnings from third-party libraries that use deprecated
# PyTorch APIs (torch.cuda.amp.autocast, torch.backends.cuda.sdp_kernel).
# These are upstream issues in rotary_embedding_torch and similar packages;
# the warnings are harmless and clutter the logs.
warnings.filterwarnings(
"ignore",
message=r".*torch\.cuda\.amp\.autocast.*",
category=FutureWarning,
)
warnings.filterwarnings(
"ignore",
message=r".*torch\.backends\.cuda\.sdp_kernel.*",
category=FutureWarning,
)
from fastapi import FastAPI
# Load logging config from the discrete YAML file next to this package.
# Edit backend/logging.yaml to change log levels without touching code.
_log_cfg = Path(__file__).parent / "logging.yaml"
if _log_cfg.exists():
import yaml
with open(_log_cfg) as _f:
logging.config.dictConfig(yaml.safe_load(_f))
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from .routers import separate, transcribe, abc_convert, pipeline, jobs, lrc, subsonic, projects, models, lyric_transcribe, system, settings
app = FastAPI(title="StemEngine", version="1.0.0")
# Allow all origins during development; tighten in production if needed.
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# API routers
app.include_router(jobs.router)
app.include_router(separate.router)
app.include_router(transcribe.router)
app.include_router(abc_convert.router)
app.include_router(pipeline.router)
app.include_router(lrc.router)
app.include_router(subsonic.router)
app.include_router(projects.router)
app.include_router(models.router)
app.include_router(lyric_transcribe.router)
app.include_router(system.router)
app.include_router(settings.router)
# Serve built React frontend from /app/backend/static
# Mount order matters: API routes (above) always win over static assets.
_static_dir = Path(__file__).parent / "static"
_assets_dir = _static_dir / "assets"
if _static_dir.exists():
# Serve Vite-generated hashed assets (JS, CSS, images)
if _assets_dir.exists():
app.mount("/assets", StaticFiles(directory=str(_assets_dir)), name="assets")
@app.get("/", include_in_schema=False)
async def root():
return FileResponse(str(_static_dir / "index.html"))
@app.get("/{full_path:path}", include_in_schema=False)
async def spa_fallback(full_path: str):
"""Serve index.html for all non-API routes to support SPA client-side routing."""
candidate = _static_dir / full_path
if candidate.is_file():
return FileResponse(str(candidate))
return FileResponse(str(_static_dir / "index.html"))