mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-10-04 01:25:18 -07:00
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:
+10
-10
@@ -1209,22 +1209,22 @@ async def trigger_model_download(request: models.ModelDownloadRequest):
|
|||||||
|
|
||||||
config = model_configs[request.model_name]
|
config = model_configs[request.model_name]
|
||||||
|
|
||||||
|
async def download_in_background():
|
||||||
|
"""Download model in background without blocking the HTTP request."""
|
||||||
try:
|
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))
|
||||||
|
|
||||||
# Start tracking download
|
# Start tracking download
|
||||||
task_manager.start_download(request.model_name)
|
task_manager.start_download(request.model_name)
|
||||||
|
|
||||||
# Trigger download by loading the model (which will download if not cached)
|
# Start download in background task (don't await)
|
||||||
# Run in background to avoid blocking
|
asyncio.create_task(download_in_background())
|
||||||
await asyncio.to_thread(config["load_func"])
|
|
||||||
|
|
||||||
# Mark download as complete
|
|
||||||
task_manager.complete_download(request.model_name)
|
|
||||||
|
|
||||||
|
# Return immediately - frontend should poll progress endpoint
|
||||||
return {"message": f"Model {request.model_name} download started"}
|
return {"message": f"Model {request.model_name} download started"}
|
||||||
except Exception as e:
|
|
||||||
# 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}")
|
||||||
|
|||||||
Reference in New Issue
Block a user