kadabengaran commited on
Commit
63863ac
·
verified ·
1 Parent(s): efeb89b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -7,7 +7,6 @@ from gradio.routes import App as GradioApp
7
  import logging
8
  from fastapi.exceptions import RequestValidationError
9
  from fastapi.responses import JSONResponse
10
- from starlette.exceptions import HTTPException as StarletteHTTPException
11
 
12
  os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
13
 
@@ -18,8 +17,8 @@ app = FastAPI()
18
  logging.basicConfig(level=logging.INFO)
19
  logger = logging.getLogger(__name__)
20
 
21
- @app.exception_handler(StarletteHTTPException)
22
- async def http_exception_handler(request: Request, exc: StarletteHTTPException):
23
  logger.error(f"HTTP error occurred: {exc.detail}")
24
  return JSONResponse(
25
  status_code=exc.status_code,
@@ -30,20 +29,33 @@ async def http_exception_handler(request: Request, exc: StarletteHTTPException):
30
  async def validation_exception_handler(request: Request, exc: RequestValidationError):
31
  body = await request.body()
32
  logger.error(f"Validation error: {exc.errors()}")
33
- logger.error(f"Request body: {body.decode()}")
 
 
 
34
  return JSONResponse(
35
  status_code=400,
36
  content={"detail": exc.errors()},
37
- )
38
-
39
  @app.middleware("http")
40
  async def log_request_body(request: Request, call_next):
41
- body = await request.body()
42
- logger.info(f"Incoming request: {request.method} {request.url}")
43
- logger.info(f"Request body: {body.decode()}")
 
 
 
 
 
 
 
 
 
 
44
  response = await call_next(request)
45
  return response
46
-
47
  # Gradio Interface Function
48
  def face_verification_uii(img1, img2, dist="cosine", model="Facenet", detector="ssd"):
49
  """
@@ -119,6 +131,7 @@ async def face_verification(
119
  }
120
 
121
  except Exception as e:
 
122
  raise HTTPException(status_code=500, detail=str(e))
123
 
124
 
 
7
  import logging
8
  from fastapi.exceptions import RequestValidationError
9
  from fastapi.responses import JSONResponse
 
10
 
11
  os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
12
 
 
17
  logging.basicConfig(level=logging.INFO)
18
  logger = logging.getLogger(__name__)
19
 
20
+ @app.exception_handler(HTTPException)
21
+ async def http_exception_handler(request: Request, exc: HTTPException):
22
  logger.error(f"HTTP error occurred: {exc.detail}")
23
  return JSONResponse(
24
  status_code=exc.status_code,
 
29
  async def validation_exception_handler(request: Request, exc: RequestValidationError):
30
  body = await request.body()
31
  logger.error(f"Validation error: {exc.errors()}")
32
+ try:
33
+ logger.error(f"Request body: {body.decode('utf-8')}")
34
+ except UnicodeDecodeError:
35
+ logger.warning("Request body contains non-text data and could not be decoded.")
36
  return JSONResponse(
37
  status_code=400,
38
  content={"detail": exc.errors()},
39
+ )
40
+
41
  @app.middleware("http")
42
  async def log_request_body(request: Request, call_next):
43
+ # Check the Content-Type of the request
44
+ content_type = request.headers.get('Content-Type', '')
45
+
46
+ if 'multipart/form-data' in content_type:
47
+ # For binary data, log a placeholder message
48
+ logger.info(f"Incoming request: {request.method} {request.url}")
49
+ logger.info("Request body contains binary data and is not logged.")
50
+ else:
51
+ # For non-binary data, log the actual body
52
+ body = await request.body()
53
+ logger.info(f"Incoming request: {request.method} {request.url}")
54
+ logger.info(f"Request body: {body.decode('utf-8')}")
55
+
56
  response = await call_next(request)
57
  return response
58
+
59
  # Gradio Interface Function
60
  def face_verification_uii(img1, img2, dist="cosine", model="Facenet", detector="ssd"):
61
  """
 
131
  }
132
 
133
  except Exception as e:
134
+ logger.error(f"An error occurred during face verification: {str(e)}")
135
  raise HTTPException(status_code=500, detail=str(e))
136
 
137