2024-09-25 21:24:30 +09:00
|
|
|
from fastapi import FastAPI, Request
|
|
|
|
from fastapi.exception_handlers import http_exception_handler, request_validation_exception_handler
|
|
|
|
from fastapi.exceptions import RequestValidationError
|
|
|
|
from starlette.exceptions import HTTPException
|
2024-09-03 13:52:17 +09:00
|
|
|
from api.yolo.detection import router as yolo_detection_router
|
2024-09-05 11:02:52 +09:00
|
|
|
from api.yolo.segmentation import router as yolo_segmentation_router
|
2024-09-26 22:14:16 +09:00
|
|
|
from api.yolo.classfication import router as yolo_classification_router
|
2024-09-18 00:13:01 +09:00
|
|
|
from api.yolo.model import router as yolo_model_router
|
2024-09-25 21:24:30 +09:00
|
|
|
from utils.slackMessage import send_slack_message
|
2024-10-03 00:13:45 +09:00
|
|
|
import time, torch, gc
|
2024-09-02 13:50:43 +09:00
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
2024-09-03 13:52:17 +09:00
|
|
|
# 각 기능별 라우터를 애플리케이션에 등록
|
2024-09-18 00:02:02 +09:00
|
|
|
app.include_router(yolo_detection_router, prefix="/api/detection", tags=["Detection"])
|
|
|
|
app.include_router(yolo_segmentation_router, prefix="/api/segmentation", tags=["Segmentation"])
|
2024-09-26 22:14:16 +09:00
|
|
|
app.include_router(yolo_classification_router, prefix="/api/classification", tags=["Classification"])
|
2024-09-18 00:13:01 +09:00
|
|
|
app.include_router(yolo_model_router, prefix="/api/model", tags=["Model"])
|
2024-09-03 13:52:17 +09:00
|
|
|
|
2024-09-25 21:24:30 +09:00
|
|
|
|
2024-10-03 00:13:45 +09:00
|
|
|
|
|
|
|
@app.middleware("http")
|
|
|
|
async def resource_cleaner_middleware(request: Request, call_next):
|
|
|
|
start_time = time.time()
|
|
|
|
try:
|
|
|
|
response = await call_next(request)
|
|
|
|
except Exception as exc:
|
|
|
|
raise exc
|
|
|
|
finally:
|
|
|
|
process_time = time.time() - start_time
|
2024-10-04 14:24:47 +09:00
|
|
|
if request.method != "GET":
|
|
|
|
send_slack_message(f"처리 시간: {process_time}초")
|
2024-10-03 00:13:45 +09:00
|
|
|
gc.collect()
|
|
|
|
torch.cuda.empty_cache()
|
|
|
|
return response
|
|
|
|
|
2024-09-25 21:24:30 +09:00
|
|
|
# 예외 처리기
|
|
|
|
@app.exception_handler(HTTPException)
|
|
|
|
async def custom_http_exception_handler(request:Request, exc):
|
|
|
|
body = await request.json()
|
|
|
|
send_slack_message(f"프로젝트 ID: {body['project_id']} - 실패! 에러: {str(exc)}", status="error")
|
|
|
|
return await http_exception_handler(request, exc)
|
|
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
|
|
async def validation_exception_handler(request:Request, exc):
|
|
|
|
send_slack_message(f"{request.url} - 요청 실패! 에러: {str(exc)}", status="error")
|
|
|
|
return await request_validation_exception_handler(request, exc)
|
|
|
|
|
2024-09-25 15:24:14 +09:00
|
|
|
# # 애플리케이션 실행
|
|
|
|
# if __name__ == "__main__":
|
|
|
|
# import uvicorn
|
|
|
|
# uvicorn.run("main:app", reload=True)
|