Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,6 +5,8 @@ import os
|
|
| 5 |
import sys
|
| 6 |
import logging
|
| 7 |
import tempfile
|
|
|
|
|
|
|
| 8 |
from fastapi import FastAPI, Request, HTTPException, Query, Body
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
from fastapi.responses import JSONResponse
|
|
@@ -13,9 +15,6 @@ import json
|
|
| 13 |
import base64
|
| 14 |
from io import BytesIO
|
| 15 |
from PIL import Image
|
| 16 |
-
import time
|
| 17 |
-
import traceback
|
| 18 |
-
from db_connector import fetch_found_items, count_found_items
|
| 19 |
|
| 20 |
# ์บ์ ๋๋ ํ ๋ฆฌ ์ค์ ๋ฐ ์ต์ ํ
|
| 21 |
CACHE_DIRS = {
|
|
@@ -25,8 +24,17 @@ CACHE_DIRS = {
|
|
| 25 |
'UPLOADS_DIR': '/tmp/uploads'
|
| 26 |
}
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
|
|
|
| 30 |
os.environ.setdefault('DB_HOST', 'localhost')
|
| 31 |
os.environ.setdefault('DB_PORT', '3306')
|
| 32 |
os.environ.setdefault('DB_USER', 'username') # ์ค์ ์ฌ์ฉ์ ๋ณ๊ฒฝ ํ์
|
|
@@ -36,48 +44,6 @@ os.environ.setdefault('DB_NAME', 'foundlost')
|
|
| 36 |
# ์ ํ๋ฆฌ์ผ์ด์
ํ๊ฒฝ ์ค์ (development, production, test)
|
| 37 |
os.environ.setdefault('APP_ENV', 'development')
|
| 38 |
|
| 39 |
-
# ์ ํ๋ฆฌ์ผ์ด์
์์ ์ด๋ฒคํธ ์์
|
| 40 |
-
@app.on_event("startup")
|
| 41 |
-
async def startup_event():
|
| 42 |
-
"""
|
| 43 |
-
์ ํ๋ฆฌ์ผ์ด์
์์ ์ ์คํ๋๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 44 |
-
"""
|
| 45 |
-
logger.info("์ ํ๋ฆฌ์ผ์ด์
์์ ์ค...")
|
| 46 |
-
try:
|
| 47 |
-
# ๋ชจ๋ธ ์ฌ์ ๋ค์ด๋ก๋ (๋น๋๊ธฐ์ ์ผ๋ก)
|
| 48 |
-
from models.clip_model import preload_clip_model
|
| 49 |
-
preload_clip_model()
|
| 50 |
-
logger.info("๋ชจ๋ธ ์ฌ์ ๋ค์ด๋ก๋ ์๋ฃ")
|
| 51 |
-
|
| 52 |
-
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํ
์คํธ
|
| 53 |
-
if os.getenv('APP_ENV') != 'test':
|
| 54 |
-
try:
|
| 55 |
-
from db_connector import get_db_connection
|
| 56 |
-
with get_db_connection() as connection:
|
| 57 |
-
with connection.cursor() as cursor:
|
| 58 |
-
cursor.execute("SELECT 1")
|
| 59 |
-
result = cursor.fetchone()
|
| 60 |
-
if result:
|
| 61 |
-
logger.info("โ
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํ
์คํธ ์ฑ๊ณต")
|
| 62 |
-
else:
|
| 63 |
-
logger.warning("โ ๏ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํ
์คํธ ๊ฒฐ๊ณผ ์์")
|
| 64 |
-
except Exception as db_error:
|
| 65 |
-
logger.error(f"โ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํ
์คํธ ์คํจ: {str(db_error)}")
|
| 66 |
-
logger.error(traceback.format_exc())
|
| 67 |
-
|
| 68 |
-
except Exception as e:
|
| 69 |
-
logger.error(f"์์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 70 |
-
logger.error(traceback.format_exc())
|
| 71 |
-
|
| 72 |
-
# ํ๊ฒฝ๋ณ์ ์ค์
|
| 73 |
-
for key, path in CACHE_DIRS.items():
|
| 74 |
-
os.environ[key] = path
|
| 75 |
-
os.makedirs(path, exist_ok=True)
|
| 76 |
-
|
| 77 |
-
# ์ถ๊ฐ ํ๊ฒฝ๋ณ์ ์ต์ ํ
|
| 78 |
-
os.environ['HF_HUB_DISABLE_TELEMETRY'] = '1'
|
| 79 |
-
os.environ['TRANSFORMERS_VERBOSITY'] = 'error'
|
| 80 |
-
|
| 81 |
# ๋ก๊น
์ค์ ๊ฐ์
|
| 82 |
logging.basicConfig(
|
| 83 |
level=logging.INFO,
|
|
@@ -166,53 +132,6 @@ def get_clip_model(force_reload=False):
|
|
| 166 |
# ์คํจ ์ None ๋ฐํ
|
| 167 |
return None
|
| 168 |
return clip_model
|
| 169 |
-
|
| 170 |
-
# FastAPI ์ ํ๋ฆฌ์ผ์ด์
์์ฑ
|
| 171 |
-
app = FastAPI(
|
| 172 |
-
title="์ต๋๋ฌผ ์ ์ฌ๋ ๊ฒ์ API",
|
| 173 |
-
description="ํ๊ตญ์ด CLIP ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ๊ฒ์๊ธ๊ณผ ์ต๋๋ฌผ ๊ฐ์ ์ ์ฌ๋๋ฅผ ๊ณ์ฐํ๋ API",
|
| 174 |
-
version="1.0.0"
|
| 175 |
-
)
|
| 176 |
-
|
| 177 |
-
# CORS ๋ฏธ๋ค์จ์ด ์ค์
|
| 178 |
-
app.add_middleware(
|
| 179 |
-
CORSMiddleware,
|
| 180 |
-
allow_origins=["*"],
|
| 181 |
-
allow_credentials=True,
|
| 182 |
-
allow_methods=["*"],
|
| 183 |
-
allow_headers=["*"],
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
# ์ ํ๋ฆฌ์ผ์ด์
์์ ์ด๋ฒคํธ
|
| 187 |
-
@app.on_event("startup")
|
| 188 |
-
async def startup_event():
|
| 189 |
-
"""
|
| 190 |
-
์ ํ๋ฆฌ์ผ์ด์
์์ ์ ์คํ๋๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 191 |
-
"""
|
| 192 |
-
logger.info("์ ํ๋ฆฌ์ผ์ด์
์์ ์ค...")
|
| 193 |
-
try:
|
| 194 |
-
# ๋ชจ๋ธ ์ฌ์ ๋ค์ด๋ก๋ (๋น๋๊ธฐ์ ์ผ๋ก)
|
| 195 |
-
from models.clip_model import preload_clip_model
|
| 196 |
-
preload_clip_model()
|
| 197 |
-
logger.info("๋ชจ๋ธ ์ฌ์ ๋ค์ด๋ก๋ ์๋ฃ")
|
| 198 |
-
except Exception as e:
|
| 199 |
-
logger.error(f"์์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 200 |
-
logger.error(traceback.format_exc())
|
| 201 |
-
|
| 202 |
-
# ์ ์ญ ์์ธ ์ฒ๋ฆฌ
|
| 203 |
-
@app.exception_handler(Exception)
|
| 204 |
-
async def global_exception_handler(request: Request, exc: Exception):
|
| 205 |
-
"""
|
| 206 |
-
์ ์ญ ์์ธ ์ฒ๋ฆฌ๊ธฐ
|
| 207 |
-
"""
|
| 208 |
-
logger.error(f"์์ฒญ ์ฒ๋ฆฌ ์ค ์์ธ ๋ฐ์: {str(exc)}")
|
| 209 |
-
return JSONResponse(
|
| 210 |
-
status_code=500,
|
| 211 |
-
content={"success": False, "message": f"์๋ฒ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(exc)}"}
|
| 212 |
-
)
|
| 213 |
-
|
| 214 |
-
# ์ ํธ๋ฆฌํฐ ๋ชจ๋ ์ํฌํธ
|
| 215 |
-
from utils.similarity import calculate_similarity, find_similar_items, CATEGORY_WEIGHT, ITEM_NAME_WEIGHT, COLOR_WEIGHT, CONTENT_WEIGHT
|
| 216 |
|
| 217 |
# ๋ด๋ถ์ ์ผ๋ก ์ต๋๋ฌผ ๋ชฉ๋ก์ ๊ฐ์ ธ์ค๋ ํจ์
|
| 218 |
async def fetch_found_items(limit=100, offset=0):
|
|
@@ -272,6 +191,73 @@ async def fetch_found_items(limit=100, offset=0):
|
|
| 272 |
|
| 273 |
# ์ค๋ฅ ๋ฐ์ ์ ๋น ๋ชฉ๋ก ๋ฐํ
|
| 274 |
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
# API ์๋ํฌ์ธํธ ์ ์ - Spring Boot์ ๋ง๊ฒ ์์
|
| 277 |
@app.post("/api/matching/find-similar", response_model=MatchingResponse)
|
|
@@ -463,7 +449,7 @@ async def find_similar_items_api(
|
|
| 463 |
}
|
| 464 |
|
| 465 |
return JSONResponse(status_code=500, content=error_response)
|
| 466 |
-
|
| 467 |
@app.get("/api/matching/test")
|
| 468 |
async def test_endpoint():
|
| 469 |
"""
|
|
|
|
| 5 |
import sys
|
| 6 |
import logging
|
| 7 |
import tempfile
|
| 8 |
+
import traceback
|
| 9 |
+
import time
|
| 10 |
from fastapi import FastAPI, Request, HTTPException, Query, Body
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
from fastapi.responses import JSONResponse
|
|
|
|
| 15 |
import base64
|
| 16 |
from io import BytesIO
|
| 17 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# ์บ์ ๋๋ ํ ๋ฆฌ ์ค์ ๋ฐ ์ต์ ํ
|
| 20 |
CACHE_DIRS = {
|
|
|
|
| 24 |
'UPLOADS_DIR': '/tmp/uploads'
|
| 25 |
}
|
| 26 |
|
| 27 |
+
# ํ๊ฒฝ๋ณ์ ์ค์
|
| 28 |
+
for key, path in CACHE_DIRS.items():
|
| 29 |
+
os.environ[key] = path
|
| 30 |
+
os.makedirs(path, exist_ok=True)
|
| 31 |
+
|
| 32 |
+
# ์ถ๊ฐ ํ๊ฒฝ๋ณ์ ์ต์ ํ
|
| 33 |
+
os.environ['HF_HUB_DISABLE_TELEMETRY'] = '1'
|
| 34 |
+
os.environ['TRANSFORMERS_VERBOSITY'] = 'error'
|
| 35 |
|
| 36 |
+
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ด๋ จ ํ๊ฒฝ ๋ณ์ (๊ธฐ๋ณธ๊ฐ ์ค์ )
|
| 37 |
+
# ์ค์ ํ๊ฒฝ์์๋ .env ํ์ผ์ด๋ ํ๊ฒฝ ๋ณ์๋ก ์ค์ ํด์ผ ํจ
|
| 38 |
os.environ.setdefault('DB_HOST', 'localhost')
|
| 39 |
os.environ.setdefault('DB_PORT', '3306')
|
| 40 |
os.environ.setdefault('DB_USER', 'username') # ์ค์ ์ฌ์ฉ์ ๋ณ๊ฒฝ ํ์
|
|
|
|
| 44 |
# ์ ํ๋ฆฌ์ผ์ด์
ํ๊ฒฝ ์ค์ (development, production, test)
|
| 45 |
os.environ.setdefault('APP_ENV', 'development')
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# ๋ก๊น
์ค์ ๊ฐ์
|
| 48 |
logging.basicConfig(
|
| 49 |
level=logging.INFO,
|
|
|
|
| 132 |
# ์คํจ ์ None ๋ฐํ
|
| 133 |
return None
|
| 134 |
return clip_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# ๋ด๋ถ์ ์ผ๋ก ์ต๋๋ฌผ ๋ชฉ๋ก์ ๊ฐ์ ธ์ค๋ ํจ์
|
| 137 |
async def fetch_found_items(limit=100, offset=0):
|
|
|
|
| 191 |
|
| 192 |
# ์ค๋ฅ ๋ฐ์ ์ ๋น ๋ชฉ๋ก ๋ฐํ
|
| 193 |
return []
|
| 194 |
+
|
| 195 |
+
# FastAPI ์ ํ๋ฆฌ์ผ์ด์
์์ฑ
|
| 196 |
+
app = FastAPI(
|
| 197 |
+
title="์ต๋๋ฌผ ์ ์ฌ๋ ๊ฒ์ API",
|
| 198 |
+
description="ํ๊ตญ์ด CLIP ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ๊ฒ์๊ธ๊ณผ ์ต๋๋ฌผ ๊ฐ์ ์ ์ฌ๋๋ฅผ ๊ณ์ฐํ๋ API",
|
| 199 |
+
version="1.0.0"
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
# CORS ๋ฏธ๋ค์จ์ด ์ค์
|
| 203 |
+
app.add_middleware(
|
| 204 |
+
CORSMiddleware,
|
| 205 |
+
allow_origins=["*"],
|
| 206 |
+
allow_credentials=True,
|
| 207 |
+
allow_methods=["*"],
|
| 208 |
+
allow_headers=["*"],
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
# ์ ํ๋ฆฌ์ผ์ด์
์์ ์ด๋ฒคํธ
|
| 212 |
+
@app.on_event("startup")
|
| 213 |
+
async def startup_event():
|
| 214 |
+
"""
|
| 215 |
+
์ ํ๋ฆฌ์ผ์ด์
์์ ์ ์คํ๋๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 216 |
+
"""
|
| 217 |
+
logger.info("์ ํ๋ฆฌ์ผ์ด์
์์ ์ค...")
|
| 218 |
+
try:
|
| 219 |
+
# ๋ชจ๋ธ ์ฌ์ ๋ค์ด๋ก๋ (๋น๋๊ธฐ์ ์ผ๋ก)
|
| 220 |
+
from models.clip_model import preload_clip_model
|
| 221 |
+
preload_clip_model()
|
| 222 |
+
logger.info("๋ชจ๋ธ ์ฌ์ ๋ค์ด๋ก๋ ์๋ฃ")
|
| 223 |
+
|
| 224 |
+
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํ
์คํธ
|
| 225 |
+
if os.getenv('APP_ENV') != 'test':
|
| 226 |
+
try:
|
| 227 |
+
from db_connector import get_db_connection
|
| 228 |
+
with get_db_connection() as connection:
|
| 229 |
+
with connection.cursor() as cursor:
|
| 230 |
+
cursor.execute("SELECT 1")
|
| 231 |
+
result = cursor.fetchone()
|
| 232 |
+
if result:
|
| 233 |
+
logger.info("โ
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํ
์คํธ ์ฑ๊ณต")
|
| 234 |
+
else:
|
| 235 |
+
logger.warning("โ ๏ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํ
์คํธ ๊ฒฐ๊ณผ ์์")
|
| 236 |
+
except Exception as db_error:
|
| 237 |
+
logger.error(f"โ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํ
์คํธ ์คํจ: {str(db_error)}")
|
| 238 |
+
logger.error(traceback.format_exc())
|
| 239 |
+
|
| 240 |
+
except Exception as e:
|
| 241 |
+
logger.error(f"์์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 242 |
+
logger.error(traceback.format_exc())
|
| 243 |
+
|
| 244 |
+
# ์ ์ญ ์์ธ ์ฒ๋ฆฌ
|
| 245 |
+
@app.exception_handler(Exception)
|
| 246 |
+
async def global_exception_handler(request: Request, exc: Exception):
|
| 247 |
+
"""
|
| 248 |
+
์ ์ญ ์์ธ ์ฒ๋ฆฌ๊ธฐ
|
| 249 |
+
"""
|
| 250 |
+
logger.error(f"์์ฒญ ์ฒ๋ฆฌ ์ค ์์ธ ๋ฐ์: {str(exc)}")
|
| 251 |
+
return JSONResponse(
|
| 252 |
+
status_code=500,
|
| 253 |
+
content={"success": False, "message": f"์๋ฒ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(exc)}"}
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
# ์ ํธ๋ฆฌํฐ ๋ชจ๋ ์ํฌํธ
|
| 257 |
+
from utils.similarity import calculate_similarity, find_similar_items, CATEGORY_WEIGHT, ITEM_NAME_WEIGHT, COLOR_WEIGHT, CONTENT_WEIGHT
|
| 258 |
+
|
| 259 |
+
# ์ด ๋ฐ์ดํฐ ๊ฐ์ ์กฐํ ํจ์ ์ํฌํธ
|
| 260 |
+
from db_connector import count_found_items
|
| 261 |
|
| 262 |
# API ์๋ํฌ์ธํธ ์ ์ - Spring Boot์ ๋ง๊ฒ ์์
|
| 263 |
@app.post("/api/matching/find-similar", response_model=MatchingResponse)
|
|
|
|
| 449 |
}
|
| 450 |
|
| 451 |
return JSONResponse(status_code=500, content=error_response)
|
| 452 |
+
|
| 453 |
@app.get("/api/matching/test")
|
| 454 |
async def test_endpoint():
|
| 455 |
"""
|