Spaces:
Sleeping
Sleeping
feat: improve demo upload flow and model selection
Browse files新增 demo 頁面的模型切換功能,支援在同一張圖片上切換不同模型重新推論,
並調整預覽圖片的縮放與顯示尺寸,避免被外框裁切。
改寫 multipart 表單解析流程,支援重用既有預覽圖並放寬上傳大小限制,
同時修正手動解析表單時無法正確辨識上傳檔案的問題。
7 unmodified lines8910111213102 unmodified lines1161171181191201212 unmodified lines1241251261271281291301319 unmodified lines1411421431441451461471487 unmodified linesfrom fastapi.responses import HTMLResponsefrom fastapi.templating import Jinja2Templatesfrom PIL import Image, UnidentifiedImageErrorfrom model_service import MODEL_CONFIGS, get_model_config, get_model_service102 unmodified linesreturn result, dataasync def _parse_demo_form(request: Request) -> tuple[UploadFile | None, str, str | None]:try:form = await request.form(max_part_size=MAX_UPLOAD_SIZE)2 unmodified linesexcept Exception as exc:raise HTTPException(status_code=400, detail=str(exc)) from excfile_value = form.get("file")file = file_value if isinstance(file_value, UploadFile) else Nonemodel_name = str(form.get("model_name") or ACTIVE_MODEL_CONFIG.name)existing_image_data_url = form.get("existing_image_data_url")if existing_image_data_url is not None:9 unmodified linesexcept Exception as exc:raise HTTPException(status_code=400, detail=str(exc)) from excfile_value = form.get("file")file = file_value if isinstance(file_value, UploadFile) else Nonemodel_name = str(form.get("model_name") or ACTIVE_MODEL_CONFIG.name)return file, model_name7 unmodified lines891011121314102 unmodified lines1171181191201211221231241251261271282 unmodified lines1311321331341351361379 unmodified lines1471481491501511521537 unmodified linesfrom fastapi.responses import HTMLResponsefrom fastapi.templating import Jinja2Templatesfrom PIL import Image, UnidentifiedImageErrorfrom starlette.datastructures import UploadFile as StarletteUploadFilefrom model_service import MODEL_CONFIGS, get_model_config, get_model_service102 unmodified linesreturn result, datadef _coerce_upload_file(value: object) -> UploadFile | None:if isinstance(value, (UploadFile, StarletteUploadFile)):return valuereturn Noneasync def _parse_demo_form(request: Request) -> tuple[UploadFile | None, str, str | None]:try:form = await request.form(max_part_size=MAX_UPLOAD_SIZE)2 unmodified linesexcept Exception as exc:raise HTTPException(status_code=400, detail=str(exc)) from excfile = _coerce_upload_file(form.get("file"))model_name = str(form.get("model_name") or ACTIVE_MODEL_CONFIG.name)existing_image_data_url = form.get("existing_image_data_url")if existing_image_data_url is not None:9 unmodified linesexcept Exception as exc:raise HTTPException(status_code=400, detail=str(exc)) from excfile = _coerce_upload_file(form.get("file"))model_name = str(form.get("model_name") or ACTIVE_MODEL_CONFIG.name)return file, model_name
app.py+9-4