nishanth-saka commited on
Commit
9790229
·
verified ·
1 Parent(s): 83e2352

404 Handling

Browse files
Files changed (1) hide show
  1. app.py +43 -4
app.py CHANGED
@@ -10,6 +10,7 @@ from PIL import Image, UnidentifiedImageError
10
  from io import BytesIO
11
  import base64
12
  import traceback
 
13
 
14
  # ===============================
15
  # SIMPLE DPT MODEL (DEPTH ESTIMATION)
@@ -141,9 +142,6 @@ def _process_saree_core(base_image: Image.Image, pattern_image: Image.Image):
141
  # WRAPPER: ACCEPT BYTES OR BASE64
142
  # ===============================
143
  def process_saree(data):
144
- """
145
- Accepts [base_blob, pattern_blob] as bytes OR base64 strings
146
- """
147
  if not isinstance(data, (list, tuple)) or len(data) != 2:
148
  raise HTTPException(status_code=422, detail="Expected an array with two elements: [base_blob, pattern_blob]")
149
 
@@ -161,7 +159,6 @@ def process_saree(data):
161
 
162
  except (base64.binascii.Error, UnidentifiedImageError) as e:
163
  raise HTTPException(status_code=422, detail=f"Invalid image data: {str(e)}")
164
-
165
  except Exception as e:
166
  raise HTTPException(status_code=400, detail=f"Error reading input images: {str(e)}")
167
 
@@ -211,6 +208,48 @@ async def predict_saree(request: Request):
211
  content={"error": "Processing Error", "details": str(e), "trace": tb}
212
  )
213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  # Run (Hugging Face will call uvicorn automatically)
215
  if __name__ == "__main__":
216
  import uvicorn
 
10
  from io import BytesIO
11
  import base64
12
  import traceback
13
+ from starlette.exceptions import HTTPException as StarletteHTTPException
14
 
15
  # ===============================
16
  # SIMPLE DPT MODEL (DEPTH ESTIMATION)
 
142
  # WRAPPER: ACCEPT BYTES OR BASE64
143
  # ===============================
144
  def process_saree(data):
 
 
 
145
  if not isinstance(data, (list, tuple)) or len(data) != 2:
146
  raise HTTPException(status_code=422, detail="Expected an array with two elements: [base_blob, pattern_blob]")
147
 
 
159
 
160
  except (base64.binascii.Error, UnidentifiedImageError) as e:
161
  raise HTTPException(status_code=422, detail=f"Invalid image data: {str(e)}")
 
162
  except Exception as e:
163
  raise HTTPException(status_code=400, detail=f"Error reading input images: {str(e)}")
164
 
 
208
  content={"error": "Processing Error", "details": str(e), "trace": tb}
209
  )
210
 
211
+ # Alias for backward compatibility
212
+ @app.post("/api/predict/")
213
+ async def alias_predict(request: Request):
214
+ return await predict_saree(request)
215
+
216
+ # ===============================
217
+ # GLOBAL ERROR HANDLERS
218
+ # ===============================
219
+ @app.exception_handler(StarletteHTTPException)
220
+ async def http_exception_handler(request: Request, exc: StarletteHTTPException):
221
+ if exc.status_code == 404:
222
+ return JSONResponse(
223
+ status_code=404,
224
+ content={
225
+ "error": "Endpoint Not Found",
226
+ "details": f"The requested URL {request.url.path} does not exist. "
227
+ "Valid endpoints: /predict-saree or /api/predict/."
228
+ }
229
+ )
230
+ elif exc.status_code == 405:
231
+ return JSONResponse(
232
+ status_code=405,
233
+ content={
234
+ "error": "Method Not Allowed",
235
+ "details": f"Method {request.method} not allowed on {request.url.path}"
236
+ }
237
+ )
238
+ return JSONResponse(
239
+ status_code=exc.status_code,
240
+ content={"error": exc.detail or "HTTP Error"}
241
+ )
242
+
243
+ @app.exception_handler(Exception)
244
+ async def unhandled_exception_handler(request: Request, exc: Exception):
245
+ return JSONResponse(
246
+ status_code=500,
247
+ content={
248
+ "error": "Internal Server Error",
249
+ "details": str(exc)
250
+ }
251
+ )
252
+
253
  # Run (Hugging Face will call uvicorn automatically)
254
  if __name__ == "__main__":
255
  import uvicorn