Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- backend/main.py +13 -0
- backend/services/gemini_service.py +10 -0
backend/main.py
CHANGED
|
@@ -338,6 +338,19 @@ class TicketRequest(BaseModel):
|
|
| 338 |
confidence_threshold: float = 0.20
|
| 339 |
duplicate_sensitivity: float = 0.85
|
| 340 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
class TicketSaveRequest(BaseModel):
|
| 342 |
user_id: str
|
| 343 |
subject: str
|
|
|
|
| 338 |
confidence_threshold: float = 0.20
|
| 339 |
duplicate_sensitivity: float = 0.85
|
| 340 |
|
| 341 |
+
def __init__(self, **data):
|
| 342 |
+
super().__init__(**data)
|
| 343 |
+
# Validate image size to prevent memory exhaustion DoS
|
| 344 |
+
if self.image_base64:
|
| 345 |
+
# base64 expands binary by ~33%, so 10MB binary ≈ 13.3MB base64
|
| 346 |
+
max_base64_len = 14_000_000 # ~10MB original image
|
| 347 |
+
if len(self.image_base64) > max_base64_len:
|
| 348 |
+
from fastapi import HTTPException
|
| 349 |
+
raise HTTPException(
|
| 350 |
+
status_code=413,
|
| 351 |
+
detail="Image too large. Maximum size is 10MB."
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
class TicketSaveRequest(BaseModel):
|
| 355 |
user_id: str
|
| 356 |
subject: str
|
backend/services/gemini_service.py
CHANGED
|
@@ -54,6 +54,16 @@ class GeminiService:
|
|
| 54 |
# Decode base64 image (actually the new SDK handles base64 easily if we just pass bytes,
|
| 55 |
# but we can also use PIL if we need to process it)
|
| 56 |
image_bytes = base64.b64decode(image_base64)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
img = Image.open(io.BytesIO(image_bytes))
|
| 58 |
|
| 59 |
prompt = (
|
|
|
|
| 54 |
# Decode base64 image (actually the new SDK handles base64 easily if we just pass bytes,
|
| 55 |
# but we can also use PIL if we need to process it)
|
| 56 |
image_bytes = base64.b64decode(image_base64)
|
| 57 |
+
|
| 58 |
+
# Defense-in-depth: reject oversized images to prevent memory exhaustion
|
| 59 |
+
max_size_bytes = 10 * 1024 * 1024 # 10MB
|
| 60 |
+
if len(image_bytes) > max_size_bytes:
|
| 61 |
+
return {
|
| 62 |
+
"image_description": "[Image Too Large] Image exceeds 10MB limit.",
|
| 63 |
+
"ocr_text": "",
|
| 64 |
+
"detected_problem": ""
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
img = Image.open(io.BytesIO(image_bytes))
|
| 68 |
|
| 69 |
prompt = (
|