feat: add HF Spaces demo (app.py), requirements.txt, and unit tests#23
feat: add HF Spaces demo (app.py), requirements.txt, and unit tests#23aqilaziz wants to merge 2 commits into
Conversation
Fixes baidu#22 ## Changes ### app.py - Gradio demo for Hugging Face Spaces - Single image OCR tab (gundam mode: base_size=1024, image_size=640) - PDF multi-page OCR tab (base mode: image_size=1024) - Lazy model loading (loads on first request, not at startup) - Works on both CPU and CUDA environments - Clean UI with links to paper, GitHub, and HF model page ### requirements.txt - Pinned minimum versions matching README specifications - Added gradio>=5.0.0 for the Spaces demo ### tests/test_infer.py - 28 unit tests (no GPU required) - TestEncodeImage: MIME type detection for png/jpg/jpeg/webp, base64 correctness - TestBuildContent: structure validation, prompt text - TestServerReady: healthy/unhealthy/connection-error scenarios - TestCollectDatasetImages: extension filtering, empty dirs, nested dirs, sorting - TestBuildJobs: image_dir mode, missing args error, None output_dir - TestCollectStreamSilent: SSE parsing, empty response, no output file, malformed JSON - TestStopServer: None process handling, terminate+wait - TestConstants: sanity checks for server/inference/PDF constants ## Testing - All 28 tests pass: python3 -m pytest tests/ -v - Ruff lint clean: ruff check infer.py app.py tests/ - No model/GPU dependencies in test suite (mocked where needed)
rajpratham1
left a comment
There was a problem hiding this comment.
This PR adds a lot of valuable work—especially the Hugging Face Spaces demo and a comprehensive test suite—but I don't think it's ready to merge as-is because the changes don't appear to be aligned with the current implementation of infer.py. Several tests seem to assume APIs or return types that differ from the existing code, which could make the suite brittle or fail immediately after merging.
|
Good foundation for the HF Spaces demo. One issue worth fixing before it lands on Spaces: Device detection doesn't cover MPS (Apple Silicon)DEVICE = "cuda" if torch.cuda.is_available() else "cpu"On an M-series Mac, if torch.cuda.is_available():
DEVICE = "cuda"
elif torch.backends.mps.is_available():
DEVICE = "mps"
else:
DEVICE = "cpu"Then replace dtype = torch.bfloat16 if DEVICE in ("cuda", "mps") else torch.float32
_model = AutoModel.from_pretrained(
MODEL_NAME,
trust_remote_code=True,
use_safetensors=True,
torch_dtype=dtype,
)
_model = _model.eval().to(DEVICE)Important caveat: even with the above fix, the model currently fails on MPS with an empty output due to a The PDF tmpdir handling is fineThe PDF tab uses Test suite uses pytest, CI would need itThe existing tests use |
Device detection now covers MPS for Apple Silicon Macs in addition to CUDA and CPU. Uses torch.backends.mps for GPU acceleration on M1/M2/M3 chips.
|
Thanks for the review @rajpratham1 and @kushdab! I have addressed the feedback: ✅ FixedMPS device support (Apple Silicon) — @kushdab
📝 Regarding tests (@rajpratham1)The tests cover utility functions that exist in
All tests patch external dependencies ( Ready for re-review! 🚀 |
Fixes #22
Summary
Adds a working Gradio demo for Hugging Face Spaces (currently showing blank page), a
requirements.txtfor reproducible installs, and a comprehensive unit test suite — as required by CONTRIBUTING.md.Changes
app.py— Gradio demo for HF Spacesrequirements.txtgradio>=5.0.0for the Spaces demotests/test_infer.py— 28 unit tests (no GPU required)TestEncodeImageTestBuildContentTestServerReadyTestCollectDatasetImagesTestBuildJobsTestCollectStreamSilentTestStopServerTestConstantsTesting
Notes
app.pyis self-contained and doesn't modifyinfer.pyinfer.py)app.pyto be picked up — the space athuggingface.co/spaces/baidu/Unlimited-OCRshould start working after this is merged