- Fix ambiguous truth value error when masks is a numpy array
- Add proper type checking for numpy arrays vs lists
- Handle edge cases (scalar arrays, empty arrays, multi-dimensional arrays)
- Improve error handling and logging
Refactor codebase: Add modular structure, logging, validation, and comprehensive improvements
- Add config.py for centralized configuration management
- Add logger_config.py replacing all print() statements with proper logging
- Add models.py for modular model loading and inference
- Add dicom_utils.py for DICOM processing utilities
- Add validators.py for comprehensive input validation and security
- Add cache_manager.py for LRU cache with TTL support
- Add utils.py for common utility functions
- Add segmentation.py for core segmentation functions
- Refactor app.py to use new modular components
- Fix all bare except clauses with specific exception handling
- Add type hints throughout codebase
- Add comprehensive test suite (tests/)
- Update requirements.txt with cachetools dependency
- Fix demo_dicom_path undefined variable issue
Merge pull request #8 from matheus-rech/copilot/fix-no-masks-in-results
Matheus Rech
Unverified signature
0f2db80
Lower SAM3 detection thresholds for medical imaging
Add valid ranges to threshold parameter documentation
Merge pull request #7 from matheus-rech/copilot/fix-cuda-initialization-error
Matheus Rech
Unverified signature
e8d7082
Fix CUDA initialization in main process for HF Spaces Stateless GPU
Polish: clarify sequential execution guarantee and fix spacing
copilot-swe-agent[bot]+1: matheus-rech
1763823
- Add detailed comment explaining @spaces.GPU sequential execution
- Clarify that requests are queued, preventing concurrent access
- Fix extra space in warning emoji
- Make it clear this design is intentional and safe
Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>
CRITICAL FIX: Convert all tensors to numpy arrays before returning
copilot-swe-agent[bot]+1: matheus-rech
52a0ce4
The issue was that even CPU tensors can trigger CUDA init during unpickling
in the main process. The solution is to convert ALL PyTorch tensors (CPU or CUDA)
to numpy arrays before returning from the @spaces.GPU function.
Key changes:
- Renamed move_to_cpu() to to_serializable()
- Convert tensors to numpy arrays instead of just moving to CPU
- Handle all data types (tensors, dicts, lists, tuples, primitives)
- Added logging to track conversion process
- Numpy arrays are safely serializable without triggering CUDA init
This ensures NO PyTorch tensor objects cross the process boundary.
Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>
Add extensive debug logging to diagnose CUDA tensor issue
copilot-swe-agent[bot]+1: matheus-rech
7ba3f6a
Adding detailed logging to:
- Track device movement
- Show result structure and types
- Log each tensor being moved to CPU
- Identify any tensors that might be missed
This will help identify exactly what's causing the CUDA init error.
Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>
Use recursive function to move ALL tensors to CPU
copilot-swe-agent[bot]+1: matheus-rech
4518467
The previous fix only moved specific known fields (masks, scores) to CPU,
but post_process_instance_segmentation may return other fields with tensors.
Now using a recursive move_to_cpu() function that handles:
- Tensors (direct .cpu() call)
- Dicts (recursively process all values)
- Lists (recursively process all items)
- Tuples (recursively process all items)
- Other types (pass through unchanged)
This ensures NO CUDA tensors can slip through when crossing process boundary.
Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>
CRITICAL: Move all result tensors to CPU before returning from GPU function
copilot-swe-agent[bot]+1: matheus-rech
6e1f61b
This is the actual fix for the CUDA initialization error. The previous changes
moved the model to GPU correctly, but the results still contained CUDA tensors.
When these tensors crossed the process boundary (GPU worker -> main process),
PyTorch tried to reconstruct them, triggering CUDA init in the main process.
Now all masks and scores are explicitly moved to CPU before returning.
Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>
Final improvements: consistent dtype handling and safer error cleanup
copilot-swe-agent[bot]+1: matheus-rech
1396743
- Use single conditional dtype for consistent behavior across CPU/GPU
- Check model is not None before cleanup to avoid masking bugs
- Catch only RuntimeError for device-related exceptions
- Remove unnecessary AttributeError catch
Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>
Address code review: use single .to() call and improve test precision
copilot-swe-agent[bot]+1: matheus-rech
833b39a
- Use single .to(device=..., dtype=...) call instead of chaining
- Correct comment about .to() behavior for nn.Module
- Fix test to check for 'demo.launch(' instead of just 'demo.launch'
Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>
- Remove confusing model_on_device variable (it was just a reference)
- Clarify that .to() is in-place and safe with @spaces.GPU decorator
- Replace bare except with specific exception types
- Add comment explaining sequential execution guarantee
Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>
Update test_app.py to use relative paths and fix demo.launch check
Fix CUDA initialization for HF Spaces Stateless GPU
copilot-swe-agent[bot]+1: matheus-rech
817b249
- Load model on CPU in main process (avoid CUDA init)
- Move model to GPU only inside @spaces.GPU decorated function
- Move model back to CPU after inference to free GPU memory
- This ensures CUDA is only initialized inside GPU-decorated functions
Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>
Initial plan
copilot-swe-agent[bot]
d7db680
Merge pull request #6 from matheus-rech/copilot/fix-hf-token-issue
Matheus Rech
Unverified signature
f824bc6
Fix interactive password prompt when HF_TOKEN not set
Refactor: move huggingface_hub import to module level