⚡ Bolt: Optimize detection endpoints with image preprocessing#411
⚡ Bolt: Optimize detection endpoints with image preprocessing#411RohanExploit wants to merge 4 commits intomainfrom
Conversation
Refactors `detect_traffic_sign_endpoint` and `detect_abandoned_vehicle_endpoint` to use `process_uploaded_image`. This adds validation (security), resizing (performance), and EXIF stripping (privacy) before sending images to the CLIP service. Reduces payload size significantly for high-resolution uploads and prevents processing of invalid files.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
✅ Deploy Preview for fixmybharat canceled.
|
🙏 Thank you for your contribution, @RohanExploit!PR Details:
Quality Checklist:
Review Process:
Note: The maintainers will monitor code quality and ensure the overall project flow isn't broken. |
📝 WalkthroughWalkthroughAdded documentation on CLIP payload optimization strategies and refactored detection endpoints to use a centralized image processing helper function instead of direct file reads, removing explicit error handling in favor of delegated validation and optimization. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR optimizes two detection endpoints (/api/detect-traffic-sign and /api/detect-abandoned-vehicle) by integrating the process_uploaded_image utility function. The optimization reduces payload sizes sent to the CLIP inference service by resizing large images to a maximum of 1024px, adds security validation (MIME type checking and file size limits), and strips EXIF metadata for privacy. This change aligns these endpoints with 17 other detection endpoints in the codebase that already use the same optimization pattern.
Changes:
- Replaced raw image byte reading with
process_uploaded_image()call in traffic sign detection endpoint - Replaced raw image byte reading with
process_uploaded_image()call in abandoned vehicle detection endpoint - Added bolt.md documentation entry capturing the CLIP payload optimization learning
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| backend/routers/detection.py | Integrated process_uploaded_image into two CLIP-based detection endpoints to optimize image processing |
| .jules/bolt.md | Documented the CLIP payload optimization learning for future reference |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
On Render (and minimal Linux containers), `python-magic` might raise `OSError` (instead of just `ImportError`) if the system library `libmagic` is missing. This prevents the backend from starting. This fix broadens the exception handler to catch `Exception` during `import magic`, ensuring the app can start even without `libmagic` installed (falling back to PIL validation).
There was a problem hiding this comment.
1 issue found across 1 file (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="backend/utils.py">
<violation number="1" location="backend/utils.py:24">
P2: Overly broad exception handler: `(ImportError, Exception)` is equivalent to just `Exception`, which silently swallows all errors during `import magic`. The comment says the goal is to catch `OSError` for a missing `libmagic` — use that specific exception instead of the catch-all `Exception`.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
backend/utils.py
Outdated
| import magic | ||
| HAS_MAGIC = True | ||
| except ImportError: | ||
| except (ImportError, Exception): # Catch OSError (libmagic missing) and other startup errors |
There was a problem hiding this comment.
P2: Overly broad exception handler: (ImportError, Exception) is equivalent to just Exception, which silently swallows all errors during import magic. The comment says the goal is to catch OSError for a missing libmagic — use that specific exception instead of the catch-all Exception.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend/utils.py, line 24:
<comment>Overly broad exception handler: `(ImportError, Exception)` is equivalent to just `Exception`, which silently swallows all errors during `import magic`. The comment says the goal is to catch `OSError` for a missing `libmagic` — use that specific exception instead of the catch-all `Exception`.</comment>
<file context>
@@ -21,7 +21,7 @@
import magic
HAS_MAGIC = True
-except ImportError:
+except (ImportError, Exception): # Catch OSError (libmagic missing) and other startup errors
HAS_MAGIC = False
</file context>
| except (ImportError, Exception): # Catch OSError (libmagic missing) and other startup errors | |
| except (ImportError, OSError): # Catch ImportError (no python-magic) or OSError (libmagic missing) |
Wraps `genai.configure()` in a try-except block to prevent the application from crashing at startup if `GEMINI_API_KEY` is invalid or missing. This ensures the backend can start successfully even if AI services are misconfigured, allowing other features to work. Logs the error for debugging purposes.
🔍 Quality Reminder |
Ensures the application starts and functions gracefully even if Gemini AI configuration fails (e.g., due to invalid API keys or network issues). - Wraps `genai.configure()` in try-except blocks. - Adds `_GEMINI_CONFIGURED` flags to track status. - Implements fallback responses for action plans, chat, and MLA summaries when AI is unavailable. - Prevents startup crashes on Render/production environments.
There was a problem hiding this comment.
2 issues found across 4 files (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="backend/routers/detection.py">
<violation number="1">
P1: Regression: This change **removes** `process_uploaded_image` instead of adding it, despite the PR claiming the opposite. Raw `image.read()` bypasses file-type validation, image resizing (potentially sending 10MB+ payloads to CLIP), and EXIF stripping (privacy leak). This is inconsistent with every other CLIP-based endpoint in the file, which all use `process_uploaded_image`.</violation>
</file>
<file name="backend/utils.py">
<violation number="1">
P1: This narrows the exception handler from catching all `Exception` subclasses to only `ImportError`, which can cause application startup crashes. While `ImportError` covers the most common case (libmagic not found), the `import magic` statement can also raise `OSError` on some platforms when `ctypes` fails to load the shared library (e.g., corrupted or ABI-incompatible `libmagic`). The original comment explicitly noted this: *"Catch OSError (libmagic missing) and other startup errors"*. Consider catching both `ImportError` and `OSError`, or at minimum `Exception` with a logged warning.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
⚡ Bolt Optimization:
process_uploaded_imageintodetect_traffic_sign_endpointanddetect_abandoned_vehicle_endpoint.backend/tests/repro_issue.py(created and deleted during process) which confirmed correct invocation ofprocess_uploaded_imageanddetect_..._clip.PR created automatically by Jules for task 504493676098731294 started by @RohanExploit
Summary by cubic
Optimized image preprocessing in traffic sign and abandoned vehicle detection to shrink CLIP payloads and add validation and privacy safeguards. Added robust Gemini failover with offline fallbacks for action plans, chat, and MLA summaries; startup no longer crashes without libmagic or a valid API key.
Refactors
Bug Fixes
Written for commit ebbc003. Summary will update on new commits.
Summary by CodeRabbit
Documentation
Refactor