fix(server): load pipeline once at startup and return XML file directly#44
Open
haoyu-haoyu wants to merge 1 commit intoBIT-DataLab:mainfrom
Open
Conversation
Three issues fixed in server_pa.py:
1. **Model reloaded on every request** — Pipeline() was instantiated
inside the /convert handler, meaning SAM3 weights were loaded into
GPU memory on every upload. Now a singleton Pipeline is created once
during the FastAPI lifespan startup event.
2. **API returned a local filesystem path instead of the file** — The
response was {"output_path": "/server/path/..."}, which is useless
to API callers. Now /convert returns the actual .drawio XML as a
FileResponse download.
3. **No upload size limit** — Arbitrarily large files could be uploaded,
risking OOM crashes on the GPU. Added a 20 MB cap with a clear 413
error.
Other improvements:
- Proper logging via Python logging module
- Structured lifespan context manager (FastAPI best practice)
- Cleaner error handling and temp file cleanup
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three critical issues in
server_pa.pyfixed in a single focused PR:1. Model reloaded on every request (performance)
Pipeline(config)was instantiated inside the/converthandler, causing SAM3 model weights to be loaded into GPU memory on every single upload. For a multi-GB model this means:Fix: Singleton
Pipelinecreated once during FastAPIlifespanstartup. All requests share the same instance.2. API returned local path instead of file (usability)
The response was
{"output_path": "/server/local/path/xxx.drawio"}— a local filesystem path that API callers cannot use. This makes the entire API non-functional for any remote client.Fix:
/convertnow returns the actual.drawioXML file as aFileResponsedownload with properContent-Type: application/xmlandContent-Dispositionheaders.3. No upload size limit (stability)
No file size validation meant arbitrarily large uploads could crash the server with OOM errors.
Fix: Added a 20 MB cap (
MAX_UPLOAD_BYTES) with a clear HTTP 413 response.Other improvements
loggingmodule instead of bareprint()lifespancontext manager (modern best practice, replaces deprecatedon_event)Before / After
Test plan
python server_pa.pystarts and logs "Pipeline ready"curl -F "file=@test.png" http://localhost:8000/convert -o result.drawioreturns valid XMLGET /healthreturns{"status": "ok"}