File size: 5,656 Bytes
891fa77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""Local research demo API. Run exactly ONE uvicorn worker, default loopback."""
from contextlib import asynccontextmanager
from pathlib import Path
import secrets
import uuid

from fastapi import FastAPI, File, Header, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles

from .config import CODE_ROOT, Settings, MODEL_ID, REVISION
from .inference import asset_status
from .jobs import JobManager, QueueFull

EXTENSIONS = {'.wav', '.mp3', '.flac', '.ogg'}


def create_app(settings=None, worker=None, readiness=None):
    settings = settings or Settings.from_env()
    manager = JobManager(settings, worker)
    status_check = readiness or asset_status

    @asynccontextmanager
    async def lifespan(app):
        await manager.start()
        try:
            yield
        finally:
            await manager.close()

    app = FastAPI(
        title='Music Emotion CPU Demo', version='1.0.0', lifespan=lifespan,
        docs_url=None if settings.serve_frontend else '/docs',
        redoc_url=None if settings.serve_frontend else '/redoc',
        openapi_url=None if settings.serve_frontend else '/openapi.json',
    )
    app.state.manager = manager
    from .middleware import UploadBoundaryMiddleware
    app.add_middleware(UploadBoundaryMiddleware, max_bytes=settings.max_upload_bytes,
                       capacity=settings.max_queue_jobs + 1, api_token=settings.api_token)
    app.add_middleware(CORSMiddleware, allow_origins=list(settings.cors_origins),
                       allow_credentials=False, allow_methods=['GET', 'POST'],
                       allow_headers=['Content-Type', 'X-Job-Token', 'Authorization'])

    def authorize(header):
        if settings.api_token and not secrets.compare_digest(header or '', 'Bearer ' + settings.api_token):
            raise HTTPException(401, '需要有效的演示访问令牌。')

    @app.get('/api/health')
    async def health():
        available, reason = status_check(settings)
        return dict(status='ready' if available else 'not_ready', device='cpu',
                    model=dict(loaded=manager.worker.loaded, assets_available=available,
                               reason=reason, model_id=MODEL_ID, revision=REVISION, head_seed=20261011),
                    limits=dict(max_upload_bytes=settings.max_upload_bytes,
                                max_duration_seconds=settings.max_duration_seconds,
                                min_duration_seconds=settings.min_duration_seconds,
                                max_queue_jobs=settings.max_queue_jobs,
                                job_ttl_seconds=settings.job_ttl_seconds),
                    queue=dict(active=manager.active, queued=manager.queue.qsize()),
                    prediction_task='overall_static_valence_arousal', calibration='none')

    @app.post('/api/jobs', status_code=202)
    async def upload(file: UploadFile = File(...), authorization: str | None = Header(default=None)):
        path = None
        reserved = False
        submitted = False
        try:
            authorize(authorization)
            available, reason = status_check(settings)
            if not available:
                raise HTTPException(503, reason)
            suffix = Path(file.filename or '').suffix.lower()
            if suffix not in EXTENSIONS:
                raise HTTPException(415, '仅支持WAV、MP3、FLAC和OGG;M4A请先转换格式。')
            try:
                manager.reserve()
                reserved = True
            except QueueFull:
                raise HTTPException(429, '任务队列已满,请稍后重试。')
            # UUID filename and exclusive open: no user path, no overwrites.
            path = settings.upload_dir / ('mer-upload-' + uuid.uuid4().hex + '.audio')
            size = 0
            with path.open('xb') as output:
                while chunk := await file.read(65536):
                    size += len(chunk)
                    if size > settings.max_upload_bytes:
                        raise HTTPException(413, '音频文件超过20MB限制。')
                    output.write(chunk)
            if size == 0:
                raise HTTPException(400, '上传文件为空。')
            try:
                job = manager.submit(path)
            except QueueFull:
                raise HTTPException(429, '任务队列已满,请稍后重试。')
            submitted = True
            return dict(job_id=job.job_id, access_token=job.access_token, status=job.status)
        finally:
            await file.close()
            if reserved:
                manager.release()
            if path is not None and not submitted:
                path.unlink(missing_ok=True)

    @app.get('/api/jobs/{job_id}')
    async def job_status(job_id: str, x_job_token: str | None = Header(default=None),
                         authorization: str | None = Header(default=None)):
        authorize(authorization)
        job = manager.get(job_id, x_job_token)
        if job is None:
            raise HTTPException(404, '任务不存在、已过期或访问令牌无效。')
        return job.public()

    if settings.serve_frontend:
        frontend = CODE_ROOT / 'frontend' / 'apps' / 'web' / 'dist' / 'client'
        if not (frontend / 'index.html').is_file():
            raise RuntimeError('Build the frontend with VITE_API_BASE_URL=/ before enabling MER_SERVE_FRONTEND.')
        # Mount last so API endpoints take precedence over static routes.
        app.mount('/', StaticFiles(directory=frontend, html=True), name='frontend')

    return app


app = create_app()