Refactor model download handling to use background tasks

- Moved model download logic into a separate asynchronous function to allow non-blocking HTTP responses.
- Improved error handling by tracking download status and reporting errors without interrupting the main request flow.
- The frontend is now expected to poll the progress endpoint for download status updates.
This commit is contained in:
Jamie Pine
2026-01-29 02:42:02 -08:00
parent e23118f610
commit 8d730621bc
+13 -13
View File
@@ -1209,22 +1209,22 @@ async def trigger_model_download(request: models.ModelDownloadRequest):
config = model_configs[request.model_name] config = model_configs[request.model_name]
try: async def download_in_background():
# Start tracking download """Download model in background without blocking the HTTP request."""
task_manager.start_download(request.model_name) try:
await asyncio.to_thread(config["load_func"])
task_manager.complete_download(request.model_name)
except Exception as e:
task_manager.error_download(request.model_name, str(e))
# Trigger download by loading the model (which will download if not cached) # Start tracking download
# Run in background to avoid blocking task_manager.start_download(request.model_name)
await asyncio.to_thread(config["load_func"])
# Mark download as complete # Start download in background task (don't await)
task_manager.complete_download(request.model_name) asyncio.create_task(download_in_background())
return {"message": f"Model {request.model_name} download started"} # Return immediately - frontend should poll progress endpoint
except Exception as e: return {"message": f"Model {request.model_name} download started"}
# Mark download as failed
task_manager.error_download(request.model_name, str(e))
raise HTTPException(status_code=500, detail=str(e))
@app.delete("/models/{model_name}") @app.delete("/models/{model_name}")