Spaces:
Running
Running
sinful1992 commited on
Commit ·
58eb2d5
1
Parent(s): bae330e
Sync from GitHub t142-ro-b3 8f4b358: hints capped and deep nesting ignored, at most 8 uploads wait (503 past that), exact library pins (audit 137 RO-6, left note 139)
Browse files- main.py +24 -1
- ocr/selfcheck.py +10 -3
- requirements.txt +12 -7
main.py
CHANGED
|
@@ -70,6 +70,17 @@ _MAX_UPLOADS_IN_MEMORY = 2
|
|
| 70 |
_upload_places: "weakref.WeakKeyDictionary[asyncio.AbstractEventLoop, asyncio.Semaphore]" = weakref.WeakKeyDictionary()
|
| 71 |
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def _upload_place() -> asyncio.Semaphore:
|
| 74 |
loop = asyncio.get_running_loop()
|
| 75 |
place = _upload_places.get(loop)
|
|
@@ -263,10 +274,22 @@ async def ocr_receipt(
|
|
| 263 |
if file.size is not None:
|
| 264 |
_check_upload_size(file.size)
|
| 265 |
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
raw_bytes = await file.read(_MAX_UPLOAD_BYTES + 1)
|
| 268 |
_check_upload_size(len(raw_bytes))
|
| 269 |
result = await _off_loop(_scan, reader, raw_bytes, parsed_hints, debug, t0)
|
|
|
|
|
|
|
| 270 |
return JSONResponse(content=result)
|
| 271 |
|
| 272 |
|
|
|
|
| 70 |
_upload_places: "weakref.WeakKeyDictionary[asyncio.AbstractEventLoop, asyncio.Semaphore]" = weakref.WeakKeyDictionary()
|
| 71 |
|
| 72 |
|
| 73 |
+
# How many may wait for a place (task 142, left note of task 139). A waiting
|
| 74 |
+
# upload sits in a temp file the server spooled it to (up to 15 MB each), and
|
| 75 |
+
# nothing bounded how many: a loop of uploads could fill the Space's disk. Past
|
| 76 |
+
# this many an upload is answered 503 at once, with Retry-After, and its temp
|
| 77 |
+
# file goes with the request. This bounds what the handler holds (8 waiting + 2 in memory, about
|
| 78 |
+
# 150 MB); FastAPI spools each form to temp before the handler runs, so uploads arriving at the
|
| 79 |
+
# same instant still reach the temp disk briefly before they get the 503.
|
| 80 |
+
_MAX_WAITING_UPLOADS = 8
|
| 81 |
+
_waiting: "weakref.WeakKeyDictionary[asyncio.AbstractEventLoop, int]" = weakref.WeakKeyDictionary()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
def _upload_place() -> asyncio.Semaphore:
|
| 85 |
loop = asyncio.get_running_loop()
|
| 86 |
place = _upload_places.get(loop)
|
|
|
|
| 274 |
if file.size is not None:
|
| 275 |
_check_upload_size(file.size)
|
| 276 |
|
| 277 |
+
place = _upload_place()
|
| 278 |
+
loop = asyncio.get_running_loop()
|
| 279 |
+
if place.locked() and _waiting.get(loop, 0) >= _MAX_WAITING_UPLOADS:
|
| 280 |
+
raise HTTPException(status_code=503, detail="Busy: too many receipts waiting, try again shortly",
|
| 281 |
+
headers={"Retry-After": "10"})
|
| 282 |
+
_waiting[loop] = _waiting.get(loop, 0) + 1
|
| 283 |
+
try:
|
| 284 |
+
await place.acquire()
|
| 285 |
+
finally:
|
| 286 |
+
_waiting[loop] -= 1
|
| 287 |
+
try:
|
| 288 |
raw_bytes = await file.read(_MAX_UPLOAD_BYTES + 1)
|
| 289 |
_check_upload_size(len(raw_bytes))
|
| 290 |
result = await _off_loop(_scan, reader, raw_bytes, parsed_hints, debug, t0)
|
| 291 |
+
finally:
|
| 292 |
+
place.release()
|
| 293 |
return JSONResponse(content=result)
|
| 294 |
|
| 295 |
|
ocr/selfcheck.py
CHANGED
|
@@ -353,6 +353,11 @@ def _drop_private(items: list[dict[str, Any]]) -> None:
|
|
| 353 |
|
| 354 |
|
| 355 |
_MAX_HINT_ITEMS = 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
|
| 358 |
def _plausible_price(value: Any) -> bool:
|
|
@@ -369,11 +374,12 @@ def _plausible_price(value: Any) -> bool:
|
|
| 369 |
def parse_hints(raw: str | None) -> dict | None:
|
| 370 |
"""The optional `hints` form field. Malformed hints are ignored, never an
|
| 371 |
error: they only refine a scan that works without them."""
|
| 372 |
-
if not raw:
|
| 373 |
return None
|
| 374 |
try:
|
| 375 |
data = json.loads(raw)
|
| 376 |
-
except ValueError:
|
|
|
|
| 377 |
return None
|
| 378 |
if not isinstance(data, dict) or not isinstance(data.get("items"), list):
|
| 379 |
return None
|
|
@@ -387,4 +393,5 @@ def parse_hints(raw: str | None) -> dict | None:
|
|
| 387 |
"lastPrice": last if _plausible_price(last) else None,
|
| 388 |
})
|
| 389 |
store = data.get("store")
|
| 390 |
-
|
|
|
|
|
|
| 353 |
|
| 354 |
|
| 355 |
_MAX_HINT_ITEMS = 200
|
| 356 |
+
# RO-6 (audit 137, task 142): the whole field and the store name are bounded too. 200 items of
|
| 357 |
+
# 100-character names is about 30 KB, so a field past 256 K characters is not from the app and is
|
| 358 |
+
# not parsed (counted in characters: the form field arrives as str).
|
| 359 |
+
_MAX_HINTS_CHARS = 256 * 1024
|
| 360 |
+
_MAX_STORE_LEN = 100
|
| 361 |
|
| 362 |
|
| 363 |
def _plausible_price(value: Any) -> bool:
|
|
|
|
| 374 |
def parse_hints(raw: str | None) -> dict | None:
|
| 375 |
"""The optional `hints` form field. Malformed hints are ignored, never an
|
| 376 |
error: they only refine a scan that works without them."""
|
| 377 |
+
if not raw or len(raw) > _MAX_HINTS_CHARS:
|
| 378 |
return None
|
| 379 |
try:
|
| 380 |
data = json.loads(raw)
|
| 381 |
+
except (ValueError, RecursionError):
|
| 382 |
+
# deep nesting ("[" * 100000) is well under the size cap but blows the parser's stack
|
| 383 |
return None
|
| 384 |
if not isinstance(data, dict) or not isinstance(data.get("items"), list):
|
| 385 |
return None
|
|
|
|
| 393 |
"lastPrice": last if _plausible_price(last) else None,
|
| 394 |
})
|
| 395 |
store = data.get("store")
|
| 396 |
+
store = store.strip()[:_MAX_STORE_LEN].strip() or None if isinstance(store, str) else None
|
| 397 |
+
return {"store": store, "items": items}
|
requirements.txt
CHANGED
|
@@ -6,11 +6,16 @@
|
|
| 6 |
# NOTE: paddlepaddle 3.3.0/3.3.1 crash on Windows (oneDNN PIR bug).
|
| 7 |
# Use 3.2.2 until upstream fix lands.
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
paddleocr==3.7.0
|
| 10 |
-
fastapi
|
| 11 |
-
uvicorn[standard]
|
| 12 |
-
opencv-python-headless
|
| 13 |
-
pillow
|
| 14 |
-
python-multipart
|
| 15 |
-
numpy
|
| 16 |
-
pytest
|
|
|
|
|
|
| 6 |
# NOTE: paddlepaddle 3.3.0/3.3.1 crash on Windows (oneDNN PIR bug).
|
| 7 |
# Use 3.2.2 until upstream fix lands.
|
| 8 |
|
| 9 |
+
# Pinned (task 142, left note 139): the exact set `pip install` resolves on the Space's base
|
| 10 |
+
# (python:3.11-slim + paddlepaddle 3.2.2) on 2026-11-11, and the full suite passed on it (424 passed;
|
| 11 |
+
# test_improve needs git, not in that image). Before, these floated with >=, so every push could
|
| 12 |
+
# rebuild with new majors (opencv 5, numpy 2.3 came in that way). Bump on purpose, with a test run.
|
| 13 |
paddleocr==3.7.0
|
| 14 |
+
fastapi==0.162.4
|
| 15 |
+
uvicorn[standard]==0.54.0
|
| 16 |
+
opencv-python-headless==5.0.0.93
|
| 17 |
+
pillow==13.0.0
|
| 18 |
+
python-multipart==0.0.33
|
| 19 |
+
numpy==2.3.5
|
| 20 |
+
pytest==9.2.0
|
| 21 |
+
starlette==1.10.0 # under fastapi; it spools uploads to temp (the waiting-upload bound depends on it)
|