Repository navigation
Conversation
The DeepSeek-OCR helpers and the DeepSeek-VL2 grounding helper parse box coordinates (and, in the OCR helpers, the geometry output) from the model's text with eval(). That text depends on the input image or document, so it should be treated as untrusted. Only Python literals are needed, and ast.literal_eval() parses them without evaluating expressions, as the unlimited-ocr helpers already do. Valid coordinates parse to the same values as before. Text that is not a literal now raises ValueError and goes through the existing error handling. Assisted-by: Claude <[email protected]>
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the DeepSeek output-parsing hardening changes.
No open issues 🎉
What changed in this PR
This PR hardens DeepSeek OCR and VL2 output parsing by replacing eval() with ast.literal_eval(), preventing model-generated text from executing arbitrary Python expressions.
Changes:
- Adds
astimports and literal parsing for OCR coordinates and geometry output. - Updates VL2 grounding-box parsing in both the Gradio helper and notebook cell.
| File | Description |
|---|---|
notebooks/deepseek-vl2/gradio_helper.py |
Uses literal parsing for grounding boxes. |
notebooks/deepseek-vl2/deepseek-vl2.ipynb |
Applies the same grounding-box change in the notebook. |
notebooks/deepseek-ocr/ov_deepseek_ocr2_helper.py |
Uses literal parsing for OCR2 coordinates and geometry. |
notebooks/deepseek-ocr/ov_deepseek_ocr_helper.py |
Uses literal parsing for OCR coordinates and geometry. |
notebooks/deepseek-ocr/gradio_helper.py |
Uses literal parsing for bounding-box coordinates. |
Review effort: Balanced
This branch has not been deployed
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.
What
Replace
eval()withast.literal_eval()where the notebooks parse text produced by the model, and addimport ast:notebooks/deepseek-ocr/ov_deepseek_ocr_helper.py,ov_deepseek_ocr2_helper.py:<|det|>coordinates inextract_coordinates_and_label, and the geometry (Line) outputnotebooks/deepseek-ocr/gradio_helper.py: coordinates indraw_bounding_boxesnotebooks/deepseek-vl2/gradio_helper.pyand the sameparse_ref_bboxcell indeepseek-vl2.ipynb: grounding boxesNo other changes; lines stay within the 160-column Black limit.
Why
The model's output depends on the input image or document, so it is untrusted input. These spots only need lists, tuples and numbers, which
ast.literal_evalparses without evaluating expressions. This matches whatnotebooks/unlimited-ocralready does for the same<|det|>format. Text that isn't a literal now raisesValueErrorand takes the existing error path: theexceptinextract_coordinates_and_label/parse_ref_bbox, or an exception to the caller in the Gradiodraw_bounding_boxes, as malformed coordinates already do.Testing
No model or GPU needed. I loaded the affected functions from the original and the patched files (and from the notebook cell) into a small script (Python 3.12, Pillow 12.3) and compared them:
[[10, 20, 30, 40]],[10, 20, 30, 40], several boxes, floats/negatives, tuples and non-list literals return the same values as before.draw_bounding_boxesandparse_ref_bboxproduce byte-identical images (and the same crops).I did not run the notebooks end to end.
Prepared with help from Claude (AI), including the local test described above.