81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
# llmgateway/routers/stt_proxy.py
|
|
import io
|
|
import logging
|
|
|
|
import httpx
|
|
from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile
|
|
from fastapi.responses import JSONResponse, StreamingResponse
|
|
from utils.checking_keys import create_key
|
|
from utils.logging_utils import EndpointLogger
|
|
from utils.minio_utils import upload_file_to_minio_v2
|
|
|
|
router = APIRouter(tags=["YOLO Gateway"])
|
|
|
|
YOLO_BASE_URL = "http://yolo_gateway:8891" # docker-compose 내 서비스명 기반
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.post("/detect_view")
|
|
async def proxy_audio(
|
|
request_info: Request,
|
|
image_file: UploadFile = File(...),
|
|
endpoint_logger: EndpointLogger = Depends(EndpointLogger),
|
|
):
|
|
request_id = create_key()
|
|
bucket_name = "yolo-gateway"
|
|
object_name = f"{request_id}/{image_file.filename}"
|
|
|
|
try:
|
|
presigned_url = upload_file_to_minio_v2(
|
|
file=image_file,
|
|
bucket_name=bucket_name,
|
|
object_name=object_name,
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"MinIO upload failed: {e}")
|
|
raise HTTPException(status_code=500, detail="File upload to storage failed.")
|
|
|
|
endpoint_logger.log(
|
|
model="yolo11x", input_filename=image_file.filename, context_length=0
|
|
)
|
|
|
|
try:
|
|
async with httpx.AsyncClient() as client:
|
|
payload = {
|
|
"request_id": request_id,
|
|
"file_url": presigned_url,
|
|
}
|
|
response = await client.post(f"{YOLO_BASE_URL}/detect", json=payload)
|
|
return JSONResponse(content=response.json(), status_code=response.status_code)
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"YOLO API 호출 실패: {str(e)}")
|
|
|
|
|
|
# YOLO 서버의 이미지 프록시
|
|
@router.get("/detect_view/images/{request_id}")
|
|
async def proxy_get_image(request_id: str):
|
|
try:
|
|
async with httpx.AsyncClient() as client:
|
|
yolo_url = f"{YOLO_BASE_URL}/images/{request_id}"
|
|
response = await client.get(yolo_url)
|
|
response.raise_for_status()
|
|
except httpx.HTTPError as e:
|
|
raise HTTPException(status_code=500, detail=f"YOLO 이미지 요청 실패: {str(e)}")
|
|
|
|
return StreamingResponse(io.BytesIO(response.content), media_type="image/jpeg")
|
|
|
|
|
|
# YOLO 서버의 JSON 결과 프록시
|
|
@router.get("/detect_view/results/{request_id}")
|
|
async def proxy_get_results(request_id: str):
|
|
try:
|
|
async with httpx.AsyncClient() as client:
|
|
yolo_url = f"{YOLO_BASE_URL}/results/{request_id}"
|
|
response = await client.get(yolo_url)
|
|
response.raise_for_status()
|
|
except httpx.HTTPError as e:
|
|
raise HTTPException(status_code=500, detail=f"YOLO 결과 요청 실패: {str(e)}")
|
|
|
|
return JSONResponse(content=response.json(), status_code=response.status_code)
|