Spaces:
Sleeping
Sleeping
Commit ·
8af372d
1
Parent(s): fc41c89
Backend: Add Base64 support for mobile (/predict_mobile)
Browse files- backend/app.py +27 -1
backend/app.py
CHANGED
|
@@ -44,7 +44,33 @@ class PredictionResponse(BaseModel):
|
|
| 44 |
image_info: Dict[str, Any]
|
| 45 |
|
| 46 |
class MobileRequest(BaseModel):
|
| 47 |
-
image: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Constants
|
| 50 |
VALID_IMAGE_FORMATS = {"image/jpeg", "image/png", "image/jpg"}
|
|
|
|
| 44 |
image_info: Dict[str, Any]
|
| 45 |
|
| 46 |
class MobileRequest(BaseModel):
|
| 47 |
+
image: str
|
| 48 |
+
|
| 49 |
+
# Allow aliases in case the mobile app uses different names
|
| 50 |
+
class Config:
|
| 51 |
+
populate_by_name = True
|
| 52 |
+
json_schema_extra = {
|
| 53 |
+
"example": {
|
| 54 |
+
"image": "base64_string_here"
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
from fastapi.exceptions import RequestValidationError
|
| 59 |
+
from fastapi.responses import JSONResponse
|
| 60 |
+
from fastapi import Request
|
| 61 |
+
|
| 62 |
+
@app.exception_handler(RequestValidationError)
|
| 63 |
+
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
| 64 |
+
body = await request.body()
|
| 65 |
+
print(f"--- 422 Validation Error ---")
|
| 66 |
+
print(f"Method: {request.method} URL: {request.url}")
|
| 67 |
+
print(f"Body: {body.decode()[:500]}...") # Print first 500 chars
|
| 68 |
+
print(f"Errors: {exc.errors()}")
|
| 69 |
+
print(f"---------------------------")
|
| 70 |
+
return JSONResponse(
|
| 71 |
+
status_code=422,
|
| 72 |
+
content={"detail": exc.errors(), "body_received": body.decode()[:100]},
|
| 73 |
+
)
|
| 74 |
|
| 75 |
# Constants
|
| 76 |
VALID_IMAGE_FORMATS = {"image/jpeg", "image/png", "image/jpg"}
|