Compare commits

...
Author SHA1 Message Date
jaehwang ba26284451 Merge branch 'main' into scraper-poc 2026-01-13 15:50:58 +09:00
Dohyun Lim 3f75b6d61d add facilities from result of crawling 2026-01-12 16:50:16 +09:00
jaehwang 2e1ccebe43 테스트 케이스 추가 및 1차 시도 실패시 2차 시도 2026-01-12 14:55:48 +09:00
Dohyun Lim b84c07c325 update toml 2026-01-12 14:29:50 +09:00
Dohyun Lim 94aae50564 update crawler for short url 2026-01-12 13:46:28 +09:00
jaehwang b7edba8c80 Playwright 모듈 PoC 추가 2026-01-12 11:01:03 +09:00
Dohyun Lim 2b777f5314 remove .gitkeep 2026-01-09 10:31:30 +09:00
Dohyun Lim 1199eca649 upgrade marketing template 2026-01-09 10:30:03 +09:00
Dohyun Lim 073777081e add logs for tracing processing task 2026-01-08 14:05:44 +09:00
jaehwang 56d4c690bf Merge branch 'main' into scraper-poc 2026-01-02 10:18:02 +09:00
bluebamus efddee217a 개선된 pool 관리 2025-12-30 01:01:05 +09:00
bluebamus 8671a45d96 bug fix for 다중 쿼리 2025-12-30 00:01:20 +09:00
bluebamus 5c99610e00 세션 및 비동기 처리 개선 2025-12-29 23:46:21 +09:00
bluebamus 153b9f0ca4 비동기 적용 2025-12-29 16:48:01 +09:00
bluebamus 95d90dcb50 영상 생성시 이미지 url 전송 -> task_id로 직접 검색으로 변경 2025-12-29 12:15:46 +09:00
bluebamus c6d9edbb42 이미지 업로드 때 task_id 생성으로 변경, 가사 생성시 task_id 받아오는 것으로 변경 2025-12-29 11:01:36 +09:00
bluebamus f81d158f0f add docs 2025-12-28 17:54:26 +09:00
bluebamus c6a2fa6808 비디오 영상 생성 요청시, 가사 전달 하는 항목 삭제, task_id로 직접 검색 2025-12-27 13:44:59 +09:00
bluebamus 47da24a12e 비디오 영상 생성 요청 버그 픽스 2025-12-26 18:56:55 +09:00
bluebamus d4bce083ab buf fix 2025-12-26 18:45:07 +09:00
bluebamus 5dddbaeda2 duration 버그 픽스 2025-12-26 18:21:22 +09:00
jaehwang 3432d5189b 블롭스토리지 class 추가 2025-12-26 15:30:51 +09:00
jaehwang 1516f2807c 스크래퍼 업데이트: 단축 url 처리, 시설 정보 추출 2025-12-26 11:10:14 +09:00
71 changed files with 10807 additions and 866 deletions

No files matched your search

Vendored
BIN
View File
Binary file not shown.
+3
View File
@@ -27,3 +27,6 @@ build/
*.mp3 *.mp3
*.mp4 *.mp4
media/ media/
*.ipynb_checkpoint*
BIN
View File
Binary file not shown.
+11 -3
View File
@@ -33,10 +33,18 @@ async def lifespan(app: FastAPI):
# Shutdown - 애플리케이션 종료 시 # Shutdown - 애플리케이션 종료 시
print("Shutting down...") print("Shutting down...")
from app.database.session import engine
await engine.dispose() # 공유 HTTP 클라이언트 종료
print("Database engine disposed") from app.utils.creatomate import close_shared_client
from app.utils.upload_blob_as_request import close_shared_blob_client
await close_shared_client()
await close_shared_blob_client()
# 데이터베이스 엔진 종료
from app.database.session import dispose_engine
await dispose_engine()
# FastAPI 앱 생성 (lifespan 적용) # FastAPI 앱 생성 (lifespan 적용)
+199
View File
@@ -1,5 +1,16 @@
import logging
import traceback
from functools import wraps
from typing import Any, Callable, TypeVar
from fastapi import FastAPI, HTTPException, Request, Response, status from fastapi import FastAPI, HTTPException, Request, Response, status
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from sqlalchemy.exc import SQLAlchemyError
# 로거 설정
logger = logging.getLogger(__name__)
T = TypeVar("T")
class FastShipError(Exception): class FastShipError(Exception):
@@ -61,6 +72,194 @@ class DeliveryPartnerCapacityExceeded(FastShipError):
status = status.HTTP_406_NOT_ACCEPTABLE status = status.HTTP_406_NOT_ACCEPTABLE
# =============================================================================
# 데이터베이스 관련 예외
# =============================================================================
class DatabaseError(FastShipError):
"""Database operation failed"""
status = status.HTTP_503_SERVICE_UNAVAILABLE
class DatabaseConnectionError(DatabaseError):
"""Database connection failed"""
status = status.HTTP_503_SERVICE_UNAVAILABLE
class DatabaseTimeoutError(DatabaseError):
"""Database operation timed out"""
status = status.HTTP_504_GATEWAY_TIMEOUT
# =============================================================================
# 외부 서비스 관련 예외
# =============================================================================
class ExternalServiceError(FastShipError):
"""External service call failed"""
status = status.HTTP_502_BAD_GATEWAY
class GPTServiceError(ExternalServiceError):
"""GPT API call failed"""
status = status.HTTP_502_BAD_GATEWAY
class CrawlingError(ExternalServiceError):
"""Web crawling failed"""
status = status.HTTP_502_BAD_GATEWAY
class BlobStorageError(ExternalServiceError):
"""Azure Blob Storage operation failed"""
status = status.HTTP_502_BAD_GATEWAY
class CreatomateError(ExternalServiceError):
"""Creatomate API call failed"""
status = status.HTTP_502_BAD_GATEWAY
# =============================================================================
# 예외 처리 데코레이터
# =============================================================================
def handle_db_exceptions(
error_message: str = "데이터베이스 작업 중 오류가 발생했습니다.",
):
"""데이터베이스 예외를 처리하는 데코레이터.
Args:
error_message: 오류 발생 시 반환할 메시지
Example:
@handle_db_exceptions("사용자 조회 중 오류 발생")
async def get_user(user_id: int):
...
"""
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
@wraps(func)
async def wrapper(*args: Any, **kwargs: Any) -> Any:
try:
return await func(*args, **kwargs)
except HTTPException:
# HTTPException은 그대로 raise
raise
except SQLAlchemyError as e:
logger.error(f"[DB Error] {func.__name__}: {e}")
logger.error(traceback.format_exc())
print(f"[DB Error] {func.__name__}: {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=error_message,
)
except Exception as e:
logger.error(f"[Unexpected Error] {func.__name__}: {e}")
logger.error(traceback.format_exc())
print(f"[Unexpected Error] {func.__name__}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="서비스 처리 중 예기치 않은 오류가 발생했습니다.",
)
return wrapper
return decorator
def handle_external_service_exceptions(
service_name: str = "외부 서비스",
error_message: str | None = None,
):
"""외부 서비스 호출 예외를 처리하는 데코레이터.
Args:
service_name: 서비스 이름 (로그용)
error_message: 오류 발생 시 반환할 메시지
Example:
@handle_external_service_exceptions("GPT")
async def call_gpt():
...
"""
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
@wraps(func)
async def wrapper(*args: Any, **kwargs: Any) -> Any:
try:
return await func(*args, **kwargs)
except HTTPException:
raise
except Exception as e:
msg = error_message or f"{service_name} 서비스 호출 중 오류가 발생했습니다."
logger.error(f"[{service_name} Error] {func.__name__}: {e}")
logger.error(traceback.format_exc())
print(f"[{service_name} Error] {func.__name__}: {e}")
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
detail=msg,
)
return wrapper
return decorator
def handle_api_exceptions(
error_message: str = "요청 처리 중 오류가 발생했습니다.",
):
"""API 엔드포인트 예외를 처리하는 데코레이터.
Args:
error_message: 오류 발생 시 반환할 메시지
Example:
@handle_api_exceptions("가사 생성 중 오류 발생")
async def generate_lyric():
...
"""
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
@wraps(func)
async def wrapper(*args: Any, **kwargs: Any) -> Any:
try:
return await func(*args, **kwargs)
except HTTPException:
raise
except SQLAlchemyError as e:
logger.error(f"[API DB Error] {func.__name__}: {e}")
logger.error(traceback.format_exc())
print(f"[API DB Error] {func.__name__}: {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="데이터베이스 연결에 문제가 발생했습니다.",
)
except Exception as e:
logger.error(f"[API Error] {func.__name__}: {e}")
logger.error(traceback.format_exc())
print(f"[API Error] {func.__name__}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=error_message,
)
return wrapper
return decorator
def _get_handler(status: int, detail: str): def _get_handler(status: int, detail: str):
# Define # Define
def handler(request: Request, exception: Exception) -> Response: def handler(request: Request, exception: Exception) -> Response:
+104 -70
View File
@@ -1,9 +1,8 @@
from contextlib import asynccontextmanager import time
from typing import AsyncGenerator from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.pool import NullPool
from config import db_settings from config import db_settings
@@ -12,24 +11,25 @@ class Base(DeclarativeBase):
pass pass
# 데이터베이스 엔진 생성 # =============================================================================
# 메인 엔진 (FastAPI 요청용)
# =============================================================================
engine = create_async_engine( engine = create_async_engine(
url=db_settings.MYSQL_URL, url=db_settings.MYSQL_URL,
echo=False, echo=False,
pool_size=10, pool_size=20, # 기본 풀 크기: 20
max_overflow=10, max_overflow=20, # 추가 연결: 20 (총 최대 40)
pool_timeout=5, pool_timeout=30, # 풀에서 연결 대기 시간 (초)
pool_recycle=3600, pool_recycle=280, # MySQL wait_timeout(기본 28800s, 클라우드는 보통 300s) 보다 짧게 설정
pool_pre_ping=True, pool_pre_ping=True, # 연결 유효성 검사 (죽은 연결 자동 재연결)
pool_reset_on_return="rollback", pool_reset_on_return="rollback", # 반환 시 롤백으로 초기화
connect_args={ connect_args={
"connect_timeout": 3, "connect_timeout": 10, # DB 연결 타임아웃
"charset": "utf8mb4", "charset": "utf8mb4",
# "allow_public_key_retrieval": True,
}, },
) )
# Async sessionmaker 생성 # 메인 세션 팩토리 (FastAPI DI용)
AsyncSessionLocal = async_sessionmaker( AsyncSessionLocal = async_sessionmaker(
bind=engine, bind=engine,
class_=AsyncSession, class_=AsyncSession,
@@ -38,6 +38,33 @@ AsyncSessionLocal = async_sessionmaker(
) )
# =============================================================================
# 백그라운드 태스크 전용 엔진 (메인 풀과 분리)
# =============================================================================
background_engine = create_async_engine(
url=db_settings.MYSQL_URL,
echo=False,
pool_size=10, # 백그라운드용 풀 크기: 10
max_overflow=10, # 추가 연결: 10 (총 최대 20)
pool_timeout=60, # 백그라운드는 대기 시간 여유있게
pool_recycle=280, # MySQL wait_timeout 보다 짧게 설정
pool_pre_ping=True, # 연결 유효성 검사 (죽은 연결 자동 재연결)
pool_reset_on_return="rollback",
connect_args={
"connect_timeout": 10,
"charset": "utf8mb4",
},
)
# 백그라운드 세션 팩토리
BackgroundSessionLocal = async_sessionmaker(
bind=background_engine,
class_=AsyncSession,
expire_on_commit=False,
autoflush=False,
)
async def create_db_tables(): async def create_db_tables():
import asyncio import asyncio
@@ -56,72 +83,79 @@ async def create_db_tables():
# FastAPI 의존성용 세션 제너레이터 # FastAPI 의존성용 세션 제너레이터
async def get_session() -> AsyncGenerator[AsyncSession, None]: async def get_session() -> AsyncGenerator[AsyncSession, None]:
start_time = time.perf_counter()
pool = engine.pool
# 커넥션 풀 상태 로깅 (디버깅용)
print(
f"[get_session] ACQUIRE - pool_size: {pool.size()}, "
f"in: {pool.checkedin()}, out: {pool.checkedout()}, "
f"overflow: {pool.overflow()}"
)
async with AsyncSessionLocal() as session: async with AsyncSessionLocal() as session:
acquire_time = time.perf_counter()
print(
f"[get_session] Session acquired in "
f"{(acquire_time - start_time)*1000:.1f}ms"
)
try: try:
yield session yield session
# print("Session commited")
# await session.commit()
except Exception as e: except Exception as e:
await session.rollback() await session.rollback()
print(f"Session rollback due to: {e}") print(
f"[get_session] ROLLBACK - error: {type(e).__name__}: {e}, "
f"duration: {(time.perf_counter() - start_time)*1000:.1f}ms"
)
raise e raise e
# async with 종료 시 session.close()가 자동 호출됨 finally:
total_time = time.perf_counter() - start_time
print(
f"[get_session] RELEASE - duration: {total_time*1000:.1f}ms, "
f"pool_out: {pool.checkedout()}"
)
# 백그라운드 태스크용 세션 제너레이터
async def get_background_session() -> AsyncGenerator[AsyncSession, None]:
start_time = time.perf_counter()
pool = background_engine.pool
print(
f"[get_background_session] ACQUIRE - pool_size: {pool.size()}, "
f"in: {pool.checkedin()}, out: {pool.checkedout()}, "
f"overflow: {pool.overflow()}"
)
async with BackgroundSessionLocal() as session:
acquire_time = time.perf_counter()
print(
f"[get_background_session] Session acquired in "
f"{(acquire_time - start_time)*1000:.1f}ms"
)
try:
yield session
except Exception as e:
await session.rollback()
print(
f"[get_background_session] ROLLBACK - "
f"error: {type(e).__name__}: {e}, "
f"duration: {(time.perf_counter() - start_time)*1000:.1f}ms"
)
raise e
finally:
total_time = time.perf_counter() - start_time
print(
f"[get_background_session] RELEASE - "
f"duration: {total_time*1000:.1f}ms, "
f"pool_out: {pool.checkedout()}"
)
# 앱 종료 시 엔진 리소스 정리 함수 # 앱 종료 시 엔진 리소스 정리 함수
async def dispose_engine() -> None: async def dispose_engine() -> None:
print("[dispose_engine] Disposing database engines...")
await engine.dispose() await engine.dispose()
print("Database engine disposed") print("[dispose_engine] Main engine disposed")
await background_engine.dispose()
print("[dispose_engine] Background engine disposed - ALL DONE")
# =============================================================================
# 백그라운드 태스크용 세션 (별도 이벤트 루프에서 사용)
# =============================================================================
@asynccontextmanager
async def get_worker_session() -> AsyncGenerator[AsyncSession, None]:
"""백그라운드 태스크용 세션 컨텍스트 매니저
asyncio.run()으로 새 이벤트 루프를 생성하는 백그라운드 태스크에서 사용합니다.
NullPool을 사용하여 연결 풀링을 비활성화하고, 이벤트 루프 충돌을 방지합니다.
get_session()과의 차이점:
- get_session(): FastAPI DI용, 메인 이벤트 루프의 연결 풀 사용
- get_worker_session(): 백그라운드 태스크용, NullPool로 매번 새 연결 생성
Usage:
async with get_worker_session() as session:
result = await session.execute(select(Model))
await session.commit()
Note:
- 매 호출마다 엔진을 생성하고 dispose하므로 오버헤드가 있음
- 빈번한 호출이 필요한 경우 방법 1(모듈 레벨 엔진)을 고려
"""
worker_engine = create_async_engine(
url=db_settings.MYSQL_URL,
poolclass=NullPool,
connect_args={
"connect_timeout": 3,
"charset": "utf8mb4",
},
)
session_factory = async_sessionmaker(
bind=worker_engine,
class_=AsyncSession,
expire_on_commit=False,
autoflush=False,
)
async with session_factory() as session:
try:
yield session
except Exception as e:
await session.rollback()
print(f"Worker session rollback due to: {e}")
raise e
finally:
await session.close()
await worker_engine.dispose()
BIN
View File
Binary file not shown.
+244 -56
View File
@@ -1,4 +1,6 @@
import json import json
import logging
import traceback
from datetime import date from datetime import date
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
@@ -6,9 +8,10 @@ from urllib.parse import unquote, urlparse
import aiofiles import aiofiles
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile, status from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile, status
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.database.session import get_session from app.database.session import get_session, AsyncSessionLocal
from app.home.models import Image from app.home.models import Image
from app.home.schemas.home_schema import ( from app.home.schemas.home_schema import (
CrawlingRequest, CrawlingRequest,
@@ -23,7 +26,10 @@ from app.home.schemas.home_schema import (
from app.utils.upload_blob_as_request import AzureBlobUploader from app.utils.upload_blob_as_request import AzureBlobUploader
from app.utils.chatgpt_prompt import ChatgptService from app.utils.chatgpt_prompt import ChatgptService
from app.utils.common import generate_task_id from app.utils.common import generate_task_id
from app.utils.nvMapScraper import NvMapScraper from app.utils.nvMapScraper import NvMapScraper, GraphQLException
# 로거 설정
logger = logging.getLogger(__name__)
MEDIA_ROOT = Path("media") MEDIA_ROOT = Path("media")
@@ -91,15 +97,56 @@ def _extract_region_from_address(road_address: str | None) -> str:
"description": "잘못된 URL", "description": "잘못된 URL",
"model": ErrorResponse, "model": ErrorResponse,
}, },
502: {
"description": "크롤링 실패",
"model": ErrorResponse,
},
}, },
tags=["crawling"], tags=["crawling"],
) )
async def crawling(request_body: CrawlingRequest): async def crawling(request_body: CrawlingRequest):
"""네이버 지도 장소 크롤링""" """네이버 지도 장소 크롤링"""
import time
request_start = time.perf_counter()
logger.info(f"[crawling] START - url: {request_body.url[:80]}...")
print(f"[crawling] ========== START ==========")
print(f"[crawling] URL: {request_body.url[:80]}...")
# ========== Step 1: 네이버 지도 크롤링 ==========
step1_start = time.perf_counter()
print(f"[crawling] Step 1: 네이버 지도 크롤링 시작...")
try:
scraper = NvMapScraper(request_body.url) scraper = NvMapScraper(request_body.url)
await scraper.scrap() await scraper.scrap()
except GraphQLException as e:
step1_elapsed = (time.perf_counter() - step1_start) * 1000
logger.error(f"[crawling] Step 1 FAILED - GraphQL 크롤링 실패: {e} ({step1_elapsed:.1f}ms)")
print(f"[crawling] Step 1 FAILED - {e} ({step1_elapsed:.1f}ms)")
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
detail=f"네이버 지도 크롤링에 실패했습니다: {e}",
)
except Exception as e:
step1_elapsed = (time.perf_counter() - step1_start) * 1000
logger.error(f"[crawling] Step 1 FAILED - 크롤링 중 예기치 않은 오류: {e} ({step1_elapsed:.1f}ms)")
print(f"[crawling] Step 1 FAILED - {e} ({step1_elapsed:.1f}ms)")
traceback.print_exc()
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
detail="네이버 지도 크롤링 중 오류가 발생했습니다.",
)
step1_elapsed = (time.perf_counter() - step1_start) * 1000
image_count = len(scraper.image_link_list) if scraper.image_link_list else 0
logger.info(f"[crawling] Step 1 완료 - 이미지 {image_count}개 ({step1_elapsed:.1f}ms)")
print(f"[crawling] Step 1 완료 - 이미지 {image_count}개 ({step1_elapsed:.1f}ms)")
# ========== Step 2: 정보 가공 ==========
step2_start = time.perf_counter()
print(f"[crawling] Step 2: 정보 가공 시작...")
# 가공된 정보 생성
processed_info = None processed_info = None
marketing_analysis = None marketing_analysis = None
@@ -114,16 +161,76 @@ async def crawling(request_body: CrawlingRequest):
detail_region_info=road_address or "", detail_region_info=road_address or "",
) )
# ChatGPT를 이용한 마케팅 분석 step2_elapsed = (time.perf_counter() - step2_start) * 1000
logger.info(f"[crawling] Step 2 완료 - {customer_name}, {region} ({step2_elapsed:.1f}ms)")
print(f"[crawling] Step 2 완료 - {customer_name}, {region} ({step2_elapsed:.1f}ms)")
# ========== Step 3: ChatGPT 마케팅 분석 ==========
step3_start = time.perf_counter()
print(f"[crawling] Step 3: ChatGPT 마케팅 분석 시작...")
try:
# Step 3-1: ChatGPT 서비스 초기화
step3_1_start = time.perf_counter()
chatgpt_service = ChatgptService( chatgpt_service = ChatgptService(
customer_name=customer_name, customer_name=customer_name,
region=region, region=region,
detail_region_info=road_address or "", detail_region_info=road_address or "",
) )
step3_1_elapsed = (time.perf_counter() - step3_1_start) * 1000
print(f"[crawling] Step 3-1: 서비스 초기화 완료 ({step3_1_elapsed:.1f}ms)")
# Step 3-2: 프롬프트 생성
step3_2_start = time.perf_counter()
prompt = chatgpt_service.build_market_analysis_prompt() prompt = chatgpt_service.build_market_analysis_prompt()
step3_2_elapsed = (time.perf_counter() - step3_2_start) * 1000
print(f"[crawling] Step 3-2: 프롬프트 생성 완료 - {len(prompt)}자 ({step3_2_elapsed:.1f}ms)")
# Step 3-3: GPT API 호출
step3_3_start = time.perf_counter()
raw_response = await chatgpt_service.generate(prompt) raw_response = await chatgpt_service.generate(prompt)
parsed = await chatgpt_service.parse_marketing_analysis(raw_response) step3_3_elapsed = (time.perf_counter() - step3_3_start) * 1000
logger.info(f"[crawling] Step 3-3: GPT API 호출 완료 - 응답 {len(raw_response)}자 ({step3_3_elapsed:.1f}ms)")
print(f"[crawling] Step 3-3: GPT API 호출 완료 - 응답 {len(raw_response)}자 ({step3_3_elapsed:.1f}ms)")
# Step 3-4: 응답 파싱 (크롤링에서 가져온 facility_info 전달)
step3_4_start = time.perf_counter()
print(f"[crawling] Step 3-4: 응답 파싱 시작 - facility_info: {scraper.facility_info}")
parsed = await chatgpt_service.parse_marketing_analysis(
raw_response, facility_info=scraper.facility_info
)
marketing_analysis = MarketingAnalysis(**parsed) marketing_analysis = MarketingAnalysis(**parsed)
step3_4_elapsed = (time.perf_counter() - step3_4_start) * 1000
print(f"[crawling] Step 3-4: 응답 파싱 완료 ({step3_4_elapsed:.1f}ms)")
step3_elapsed = (time.perf_counter() - step3_start) * 1000
logger.info(f"[crawling] Step 3 완료 - 마케팅 분석 성공 ({step3_elapsed:.1f}ms)")
print(f"[crawling] Step 3 완료 - 마케팅 분석 성공 ({step3_elapsed:.1f}ms)")
except Exception as e:
step3_elapsed = (time.perf_counter() - step3_start) * 1000
logger.error(f"[crawling] Step 3 FAILED - GPT 마케팅 분석 중 오류: {e} ({step3_elapsed:.1f}ms)")
print(f"[crawling] Step 3 FAILED - {e} ({step3_elapsed:.1f}ms)")
traceback.print_exc()
# GPT 실패 시에도 크롤링 결과는 반환
marketing_analysis = None
else:
step2_elapsed = (time.perf_counter() - step2_start) * 1000
logger.warning(f"[crawling] Step 2 - base_info 없음 ({step2_elapsed:.1f}ms)")
print(f"[crawling] Step 2 - base_info 없음, 마케팅 분석 스킵 ({step2_elapsed:.1f}ms)")
# ========== 완료 ==========
total_elapsed = (time.perf_counter() - request_start) * 1000
logger.info(f"[crawling] COMPLETE - 총 소요시간: {total_elapsed:.1f}ms")
print(f"[crawling] ========== COMPLETE ==========")
print(f"[crawling] 총 소요시간: {total_elapsed:.1f}ms")
print(f"[crawling] - Step 1 (크롤링): {step1_elapsed:.1f}ms")
if scraper.base_info:
print(f"[crawling] - Step 2 (정보가공): {step2_elapsed:.1f}ms")
if 'step3_elapsed' in locals():
print(f"[crawling] - Step 3 (GPT 분석): {step3_elapsed:.1f}ms")
if 'step3_3_elapsed' in locals():
print(f"[crawling] - GPT API 호출: {step3_3_elapsed:.1f}ms")
return { return {
"image_list": scraper.image_link_list, "image_list": scraper.image_link_list,
@@ -179,11 +286,11 @@ IMAGES_JSON_EXAMPLE = """[
@router.post( @router.post(
"/image/upload/server/{task_id}", "/image/upload/server",
include_in_schema=False, include_in_schema=False,
summary="이미지 업로드 (로컬 서버)", summary="이미지 업로드 (로컬 서버)",
description=""" description="""
task_id에 연결된 이미지를 로컬 서버(media 폴더)에 업로드합니다. 이미지를 로컬 서버(media 폴더)에 업로드하고 새로운 task_id를 생성합니다.
## 요청 방식 ## 요청 방식
multipart/form-data 형식으로 전송합니다. multipart/form-data 형식으로 전송합니다.
@@ -258,6 +365,10 @@ print(response.json())
## 저장 경로 ## 저장 경로
- 바이너리 파일: /media/image/{날짜}/{uuid7}/{파일명} - 바이너리 파일: /media/image/{날짜}/{uuid7}/{파일명}
- URL 이미지: 외부 URL 그대로 Image 테이블에 저장 - URL 이미지: 외부 URL 그대로 Image 테이블에 저장
## 반환 정보
- **task_id**: 새로 생성된 작업 고유 식별자
- **image_urls**: Image 테이블에 저장된 현재 task_id의 이미지 URL 목록
""", """,
response_model=ImageUploadResponse, response_model=ImageUploadResponse,
responses={ responses={
@@ -267,11 +378,10 @@ print(response.json())
tags=["image"], tags=["image"],
) )
async def upload_images( async def upload_images(
task_id: str,
images_json: Optional[str] = Form( images_json: Optional[str] = Form(
default=None, default=None,
description="외부 이미지 URL 목록 (JSON 문자열)", description="외부 이미지 URL 목록 (JSON 문자열)",
example=IMAGES_JSON_EXAMPLE, examples=[IMAGES_JSON_EXAMPLE],
), ),
files: Optional[list[UploadFile]] = File( files: Optional[list[UploadFile]] = File(
default=None, description="이미지 바이너리 파일 목록" default=None, description="이미지 바이너리 파일 목록"
@@ -279,6 +389,9 @@ async def upload_images(
session: AsyncSession = Depends(get_session), session: AsyncSession = Depends(get_session),
) -> ImageUploadResponse: ) -> ImageUploadResponse:
"""이미지 업로드 (URL + 바이너리 파일)""" """이미지 업로드 (URL + 바이너리 파일)"""
# task_id 생성
task_id = await generate_task_id()
# 1. 진입 검증: images_json 또는 files 중 하나는 반드시 있어야 함 # 1. 진입 검증: images_json 또는 files 중 하나는 반드시 있어야 함
has_images_json = images_json is not None and images_json.strip() != "" has_images_json = images_json is not None and images_json.strip() != ""
has_files = files is not None and len(files) > 0 has_files = files is not None and len(files) > 0
@@ -405,6 +518,9 @@ async def upload_images(
saved_count = len(result_images) saved_count = len(result_images)
await session.commit() await session.commit()
# Image 테이블에서 현재 task_id의 이미지 URL 목록 조회
image_urls = [img.img_url for img in result_images]
return ImageUploadResponse( return ImageUploadResponse(
task_id=task_id, task_id=task_id,
total_count=len(result_images), total_count=len(result_images),
@@ -412,14 +528,15 @@ async def upload_images(
file_count=len(valid_files), file_count=len(valid_files),
saved_count=saved_count, saved_count=saved_count,
images=result_images, images=result_images,
image_urls=image_urls,
) )
@router.post( @router.post(
"/image/upload/blob/{task_id}", "/image/upload/blob",
summary="이미지 업로드 (Azure Blob Storage)", summary="이미지 업로드 (Azure Blob Storage)",
description=""" description="""
task_id에 연결된 이미지를 Azure Blob Storage에 업로드합니다. 이미지를 Azure Blob Storage에 업로드하고 새로운 task_id를 생성합니다.
바이너리 파일은 로컬 서버에 저장하지 않고 Azure Blob에 직접 업로드됩니다. 바이너리 파일은 로컬 서버에 저장하지 않고 Azure Blob에 직접 업로드됩니다.
## 요청 방식 ## 요청 방식
@@ -447,24 +564,25 @@ jpg, jpeg, png, webp, heic, heif
### cURL로 테스트 ### cURL로 테스트
```bash ```bash
# 바이너리 파일만 업로드 # 바이너리 파일만 업로드
curl -X POST "http://localhost:8000/image/upload/blob/test-task-001" \\ curl -X POST "http://localhost:8000/image/upload/blob" \\
-F "files=@/path/to/image1.jpg" \\ -F "files=@/path/to/image1.jpg" \\
-F "files=@/path/to/image2.png" -F "files=@/path/to/image2.png"
# URL + 바이너리 파일 동시 업로드 # URL + 바이너리 파일 동시 업로드
curl -X POST "http://localhost:8000/image/upload/blob/test-task-001" \\ curl -X POST "http://localhost:8000/image/upload/blob" \\
-F 'images_json=[{"url":"https://example.com/image.jpg"}]' \\ -F 'images_json=[{"url":"https://example.com/image.jpg"}]' \\
-F "files=@/path/to/local_image.jpg" -F "files=@/path/to/local_image.jpg"
``` ```
## 반환 정보 ## 반환 정보
- **task_id**: 작업 고유 식별자 - **task_id**: 새로 생성된 작업 고유 식별자
- **total_count**: 총 업로드된 이미지 개수 - **total_count**: 총 업로드된 이미지 개수
- **url_count**: URL로 등록된 이미지 개수 (Image 테이블에 외부 URL 그대로 저장) - **url_count**: URL로 등록된 이미지 개수 (Image 테이블에 외부 URL 그대로 저장)
- **file_count**: 파일로 업로드된 이미지 개수 (Azure Blob Storage에 저장) - **file_count**: 파일로 업로드된 이미지 개수 (Azure Blob Storage에 저장)
- **saved_count**: Image 테이블에 저장된 row 수 - **saved_count**: Image 테이블에 저장된 row 수
- **images**: 업로드된 이미지 목록 - **images**: 업로드된 이미지 목록
- **source**: "url" (외부 URL) 또는 "blob" (Azure Blob Storage) - **source**: "url" (외부 URL) 또는 "blob" (Azure Blob Storage)
- **image_urls**: Image 테이블에 저장된 현재 task_id의 이미지 URL 목록
## 저장 경로 ## 저장 경로
- 바이너리 파일: Azure Blob Storage ({BASE_URL}/{task_id}/image/{파일명}) - 바이너리 파일: Azure Blob Storage ({BASE_URL}/{task_id}/image/{파일명})
@@ -478,19 +596,30 @@ curl -X POST "http://localhost:8000/image/upload/blob/test-task-001" \\
tags=["image"], tags=["image"],
) )
async def upload_images_blob( async def upload_images_blob(
task_id: str,
images_json: Optional[str] = Form( images_json: Optional[str] = Form(
default=None, default=None,
description="외부 이미지 URL 목록 (JSON 문자열)", description="외부 이미지 URL 목록 (JSON 문자열)",
example=IMAGES_JSON_EXAMPLE, examples=[IMAGES_JSON_EXAMPLE],
), ),
files: Optional[list[UploadFile]] = File( files: Optional[list[UploadFile]] = File(
default=None, description="이미지 바이너리 파일 목록" default=None, description="이미지 바이너리 파일 목록"
), ),
session: AsyncSession = Depends(get_session),
) -> ImageUploadResponse: ) -> ImageUploadResponse:
"""이미지 업로드 (URL + Azure Blob Storage)""" """이미지 업로드 (URL + Azure Blob Storage)
# 1. 진입 검증
3단계로 분리하여 세션 점유 시간 최소화:
- Stage 1: 입력 검증 및 파일 데이터 준비 (세션 없음)
- Stage 2: Azure Blob 업로드 (세션 없음)
- Stage 3: DB 저장 (새 세션으로 빠르게 처리)
"""
import time
request_start = time.perf_counter()
# task_id 생성
task_id = await generate_task_id()
print(f"[upload_images_blob] START - task_id: {task_id}")
# ========== Stage 1: 입력 검증 및 파일 데이터 준비 (세션 없음) ==========
has_images_json = images_json is not None and images_json.strip() != "" has_images_json = images_json is not None and images_json.strip() != ""
has_files = files is not None and len(files) > 0 has_files = files is not None and len(files) > 0
@@ -500,9 +629,9 @@ async def upload_images_blob(
detail="images_json 또는 files 중 하나는 반드시 제공해야 합니다.", detail="images_json 또는 files 중 하나는 반드시 제공해야 합니다.",
) )
# 2. images_json 파싱 # images_json 파싱
url_images: list[ImageUrlItem] = [] url_images: list[ImageUrlItem] = []
if has_images_json: if has_images_json and images_json:
try: try:
parsed = json.loads(images_json) parsed = json.loads(images_json)
if isinstance(parsed, list): if isinstance(parsed, list):
@@ -513,8 +642,8 @@ async def upload_images_blob(
detail=f"images_json 파싱 오류: {str(e)}", detail=f"images_json 파싱 오류: {str(e)}",
) )
# 3. 유효한 파일만 필터링 # 유효한 파일만 필터링 및 파일 내용 미리 읽기
valid_files: list[UploadFile] = [] valid_files_data: list[tuple[str, str, bytes]] = [] # (original_name, ext, content)
skipped_files: list[str] = [] skipped_files: list[str] = []
if has_files and files: if has_files and files:
for f in files: for f in files:
@@ -523,22 +652,78 @@ async def upload_images_blob(
is_real_file = f.filename and f.filename != "filename" is_real_file = f.filename and f.filename != "filename"
if f and is_real_file and is_valid_ext and is_not_empty: if f and is_real_file and is_valid_ext and is_not_empty:
valid_files.append(f) # 파일 내용을 미리 읽어둠
content = await f.read()
ext = _get_file_extension(f.filename) # type: ignore[arg-type]
valid_files_data.append((f.filename or "image", ext, content))
else: else:
skipped_files.append(f.filename or "unknown") skipped_files.append(f.filename or "unknown")
if not url_images and not valid_files: if not url_images and not valid_files_data:
detail = (
f"유효한 이미지가 없습니다. "
f"지원 확장자: {', '.join(ALLOWED_IMAGE_EXTENSIONS)}. "
f"건너뛴 파일: {skipped_files}"
)
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
detail=f"유효한 이미지가 없습니다. 지원 확장자: {', '.join(ALLOWED_IMAGE_EXTENSIONS)}. 건너뛴 파일: {skipped_files}", detail=detail,
) )
stage1_time = time.perf_counter()
print(f"[upload_images_blob] Stage 1 done - urls: {len(url_images)}, "
f"files: {len(valid_files_data)}, "
f"elapsed: {(stage1_time - request_start)*1000:.1f}ms")
# ========== Stage 2: Azure Blob 업로드 (세션 없음) ==========
# 업로드 결과를 저장할 리스트 (나중에 DB에 저장)
blob_upload_results: list[tuple[str, str]] = [] # (img_name, blob_url)
img_order = len(url_images) # URL 이미지 다음 순서부터 시작
if valid_files_data:
uploader = AzureBlobUploader(task_id=task_id)
total_files = len(valid_files_data)
for idx, (original_name, ext, file_content) in enumerate(valid_files_data):
name_without_ext = (
original_name.rsplit(".", 1)[0]
if "." in original_name
else original_name
)
filename = f"{name_without_ext}_{img_order:03d}{ext}"
print(f"[upload_images_blob] Uploading file {idx+1}/{total_files}: "
f"{filename} ({len(file_content)} bytes)")
# Azure Blob Storage에 직접 업로드
upload_success = await uploader.upload_image_bytes(file_content, filename)
if upload_success:
blob_url = uploader.public_url
blob_upload_results.append((original_name, blob_url))
img_order += 1
print(f"[upload_images_blob] File {idx+1}/{total_files} SUCCESS")
else:
skipped_files.append(filename)
print(f"[upload_images_blob] File {idx+1}/{total_files} FAILED")
stage2_time = time.perf_counter()
print(f"[upload_images_blob] Stage 2 done - blob uploads: "
f"{len(blob_upload_results)}, skipped: {len(skipped_files)}, "
f"elapsed: {(stage2_time - stage1_time)*1000:.1f}ms")
# ========== Stage 3: DB 저장 (새 세션으로 빠르게 처리) ==========
print("[upload_images_blob] Stage 3 starting - DB save...")
result_images: list[ImageUploadResultItem] = [] result_images: list[ImageUploadResultItem] = []
img_order = 0 img_order = 0
# 1. URL 이미지 저장 try:
async with AsyncSessionLocal() as session:
# URL 이미지 저장
for url_item in url_images: for url_item in url_images:
img_name = url_item.name or _extract_image_name(url_item.url, img_order) img_name = (
url_item.name or _extract_image_name(url_item.url, img_order)
)
image = Image( image = Image(
task_id=task_id, task_id=task_id,
@@ -560,30 +745,8 @@ async def upload_images_blob(
) )
img_order += 1 img_order += 1
# 2. 바이너리 파일을 Azure Blob Storage에 직접 업로드 (media 저장 없음) # Blob 업로드 결과 저장
if valid_files: for img_name, blob_url in blob_upload_results:
uploader = AzureBlobUploader(task_id=task_id)
for file in valid_files:
original_name = file.filename or "image"
ext = _get_file_extension(file.filename) # type: ignore[arg-type]
name_without_ext = (
original_name.rsplit(".", 1)[0]
if "." in original_name
else original_name
)
filename = f"{name_without_ext}_{img_order:03d}{ext}"
# 파일 내용 읽기
file_content = await file.read()
# Azure Blob Storage에 직접 업로드
upload_success = await uploader.upload_image_bytes(file_content, filename)
if upload_success:
blob_url = uploader.public_url
img_name = file.filename or filename
image = Image( image = Image(
task_id=task_id, task_id=task_id,
img_name=img_name, img_name=img_name,
@@ -603,17 +766,42 @@ async def upload_images_blob(
) )
) )
img_order += 1 img_order += 1
else:
skipped_files.append(filename) await session.commit()
stage3_time = time.perf_counter()
print(f"[upload_images_blob] Stage 3 done - "
f"saved: {len(result_images)}, "
f"elapsed: {(stage3_time - stage2_time)*1000:.1f}ms")
except SQLAlchemyError as e:
logger.error(f"[upload_images_blob] DB Error - task_id: {task_id}, error: {e}")
traceback.print_exc()
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="이미지 저장 중 데이터베이스 오류가 발생했습니다.",
)
except Exception as e:
logger.error(f"[upload_images_blob] Stage 3 EXCEPTION - "
f"task_id: {task_id}, error: {type(e).__name__}: {e}")
traceback.print_exc()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="이미지 업로드 중 오류가 발생했습니다.",
)
saved_count = len(result_images) saved_count = len(result_images)
await session.commit() image_urls = [img.img_url for img in result_images]
total_time = time.perf_counter() - request_start
print(f"[upload_images_blob] SUCCESS - task_id: {task_id}, "
f"total: {saved_count}, total_time: {total_time*1000:.1f}ms")
return ImageUploadResponse( return ImageUploadResponse(
task_id=task_id, task_id=task_id,
total_count=len(result_images), total_count=len(result_images),
url_count=len(url_images), url_count=len(url_images),
file_count=len(valid_files) - len(skipped_files), file_count=len(blob_upload_results),
saved_count=saved_count, saved_count=saved_count,
images=result_images, images=result_images,
image_urls=image_urls,
) )
+42 -1
View File
@@ -211,9 +211,50 @@ class ImageUploadResultItem(BaseModel):
class ImageUploadResponse(BaseModel): class ImageUploadResponse(BaseModel):
"""이미지 업로드 응답 스키마""" """이미지 업로드 응답 스키마"""
task_id: str = Field(..., description="작업 고유 식별자") model_config = ConfigDict(
json_schema_extra={
"example": {
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"total_count": 3,
"url_count": 2,
"file_count": 1,
"saved_count": 3,
"images": [
{
"id": 1,
"img_name": "외관",
"img_url": "https://example.com/images/image_001.jpg",
"img_order": 0,
"source": "url",
},
{
"id": 2,
"img_name": "내부",
"img_url": "https://example.com/images/image_002.jpg",
"img_order": 1,
"source": "url",
},
{
"id": 3,
"img_name": "uploaded_image.jpg",
"img_url": "/media/image/2024-01-15/0694b716-dbff-7219-8000-d08cb5fce431/uploaded_image_002.jpg",
"img_order": 2,
"source": "file",
},
],
"image_urls": [
"https://example.com/images/image_001.jpg",
"https://example.com/images/image_002.jpg",
"/media/image/2024-01-15/0694b716-dbff-7219-8000-d08cb5fce431/uploaded_image_002.jpg",
],
}
}
)
task_id: str = Field(..., description="작업 고유 식별자 (새로 생성된 UUID7)")
total_count: int = Field(..., description="총 업로드된 이미지 개수") total_count: int = Field(..., description="총 업로드된 이미지 개수")
url_count: int = Field(..., description="URL로 등록된 이미지 개수") url_count: int = Field(..., description="URL로 등록된 이미지 개수")
file_count: int = Field(..., description="파일로 업로드된 이미지 개수") file_count: int = Field(..., description="파일로 업로드된 이미지 개수")
saved_count: int = Field(..., description="Image 테이블에 저장된 row 수") saved_count: int = Field(..., description="Image 테이블에 저장된 row 수")
images: list[ImageUploadResultItem] = Field(..., description="업로드된 이미지 목록") images: list[ImageUploadResultItem] = Field(..., description="업로드된 이미지 목록")
image_urls: list[str] = Field(..., description="Image 테이블에 저장된 현재 task_id의 이미지 URL 목록")
BIN
View File
Binary file not shown.
+84 -81
View File
@@ -25,9 +25,7 @@ Lyric API Router
from app.utils.pagination import PaginatedResponse, get_paginated from app.utils.pagination import PaginatedResponse, get_paginated
""" """
from typing import Optional from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query, status
from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -41,8 +39,8 @@ from app.lyric.schemas.lyric import (
LyricListItem, LyricListItem,
LyricStatusResponse, LyricStatusResponse,
) )
from app.lyric.worker.lyric_task import generate_lyric_background
from app.utils.chatgpt_prompt import ChatgptService from app.utils.chatgpt_prompt import ChatgptService
from app.utils.common import generate_task_id
from app.utils.pagination import PaginatedResponse, get_paginated from app.utils.pagination import PaginatedResponse, get_paginated
router = APIRouter(prefix="/lyric", tags=["lyric"]) router = APIRouter(prefix="/lyric", tags=["lyric"])
@@ -77,7 +75,12 @@ async def get_lyric_status_by_task_id(
# 완료 처리 # 완료 처리
""" """
print(f"[get_lyric_status_by_task_id] START - task_id: {task_id}") print(f"[get_lyric_status_by_task_id] START - task_id: {task_id}")
result = await session.execute(select(Lyric).where(Lyric.task_id == task_id)) result = await session.execute(
select(Lyric)
.where(Lyric.task_id == task_id)
.order_by(Lyric.created_at.desc())
.limit(1)
)
lyric = result.scalar_one_or_none() lyric = result.scalar_one_or_none()
if not lyric: if not lyric:
@@ -125,7 +128,12 @@ async def get_lyric_by_task_id(
lyric = await get_lyric_by_task_id(session, task_id) lyric = await get_lyric_by_task_id(session, task_id)
""" """
print(f"[get_lyric_by_task_id] START - task_id: {task_id}") print(f"[get_lyric_by_task_id] START - task_id: {task_id}")
result = await session.execute(select(Lyric).where(Lyric.task_id == task_id)) result = await session.execute(
select(Lyric)
.where(Lyric.task_id == task_id)
.order_by(Lyric.created_at.desc())
.limit(1)
)
lyric = result.scalar_one_or_none() lyric = result.scalar_one_or_none()
if not lyric: if not lyric:
@@ -157,29 +165,31 @@ async def get_lyric_by_task_id(
summary="가사 생성", summary="가사 생성",
description=""" description="""
고객 정보를 기반으로 ChatGPT를 이용하여 가사를 생성합니다. 고객 정보를 기반으로 ChatGPT를 이용하여 가사를 생성합니다.
백그라운드에서 비동기로 처리되며, 즉시 task_id를 반환합니다.
## 요청 필드 ## 요청 필드
- **task_id**: 작업 고유 식별자 (이미지 업로드 시 생성된 task_id, 필수)
- **customer_name**: 고객명/가게명 (필수) - **customer_name**: 고객명/가게명 (필수)
- **region**: 지역명 (필수) - **region**: 지역명 (필수)
- **detail_region_info**: 상세 지역 정보 (선택) - **detail_region_info**: 상세 지역 정보 (선택)
- **language**: 가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese) - **language**: 가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)
## 반환 정보 ## 반환 정보
- **success**: 생성 성공 여부 - **success**: 요청 접수 성공 여부
- **task_id**: 작업 고유 식별자 - **task_id**: 작업 고유 식별자
- **lyric**: 생성된 가사 (성공 시) - **lyric**: null (백그라운드 처리 중)
- **language**: 가사 언어 - **language**: 가사 언어
- **error_message**: 에러 메시지 (실패 시) - **error_message**: 에러 메시지 (요청 접수 실패 시)
## 실패 조건 ## 상태 확인
- ChatGPT API 오류 - GET /lyric/status/{task_id} 로 처리 상태 확인
- ChatGPT 거부 응답 (I'm sorry, I cannot 등) - GET /lyric/{task_id} 로 생성된 가사 조회
- 응답에 ERROR: 포함
## 사용 예시 ## 사용 예시
``` ```
POST /lyric/generate POST /lyric/generate
{ {
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"customer_name": "스테이 머뭄", "customer_name": "스테이 머뭄",
"region": "군산", "region": "군산",
"detail_region_info": "군산 신흥동 말랭이 마을", "detail_region_info": "군산 신흥동 말랭이 마을",
@@ -187,46 +197,46 @@ POST /lyric/generate
} }
``` ```
## 응답 예시 (성공) ## 응답 예시
```json ```json
{ {
"success": true, "success": true,
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431", "task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"lyric": "인스타 감성의 스테이 머뭄...",
"language": "Korean",
"error_message": null
}
```
## 응답 예시 (실패)
```json
{
"success": false,
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"lyric": null, "lyric": null,
"language": "Korean", "language": "Korean",
"error_message": "I'm sorry, I can't comply with that request." "error_message": null
} }
``` ```
""", """,
response_model=GenerateLyricResponse, response_model=GenerateLyricResponse,
responses={ responses={
200: {"description": "가사 생성 성공 또는 실패 (success 필드로 구분)"}, 200: {"description": "가사 생성 요청 접수 성공"},
500: {"description": "서버 내부 오류"}, 500: {"description": "서버 내부 오류"},
}, },
) )
async def generate_lyric( async def generate_lyric(
request_body: GenerateLyricRequest, request_body: GenerateLyricRequest,
background_tasks: BackgroundTasks,
session: AsyncSession = Depends(get_session), session: AsyncSession = Depends(get_session),
) -> GenerateLyricResponse: ) -> GenerateLyricResponse:
"""고객 정보를 기반으로 가사를 생성합니다.""" """고객 정보를 기반으로 가사를 생성합니다. (백그라운드 처리)"""
task_id = await generate_task_id(session=session, table_name=Project) import time
request_start = time.perf_counter()
task_id = request_body.task_id
print(f"[generate_lyric] ========== START ==========")
print( print(
f"[generate_lyric] START - task_id: {task_id}, customer_name: {request_body.customer_name}, region: {request_body.region}" f"[generate_lyric] task_id: {task_id}, "
f"customer_name: {request_body.customer_name}, "
f"region: {request_body.region}"
) )
try: try:
# 1. ChatGPT 서비스 초기화 및 프롬프트 생성 # ========== Step 1: ChatGPT 서비스 초기화 및 프롬프트 생성 ==========
step1_start = time.perf_counter()
print(f"[generate_lyric] Step 1: 서비스 초기화 및 프롬프트 생성...")
service = ChatgptService( service = ChatgptService(
customer_name=request_body.customer_name, customer_name=request_body.customer_name,
region=request_body.region, region=request_body.region,
@@ -235,7 +245,13 @@ async def generate_lyric(
) )
prompt = service.build_lyrics_prompt() prompt = service.build_lyrics_prompt()
# 2. Project 테이블에 데이터 저장 step1_elapsed = (time.perf_counter() - step1_start) * 1000
print(f"[generate_lyric] Step 1 완료 - 프롬프트 {len(prompt)}자 ({step1_elapsed:.1f}ms)")
# ========== Step 2: Project 테이블에 데이터 저장 ==========
step2_start = time.perf_counter()
print(f"[generate_lyric] Step 2: Project 저장...")
project = Project( project = Project(
store_name=request_body.customer_name, store_name=request_body.customer_name,
region=request_body.region, region=request_body.region,
@@ -245,12 +261,15 @@ async def generate_lyric(
) )
session.add(project) session.add(project)
await session.commit() await session.commit()
await session.refresh(project) # commit 후 project.id 동기화 await session.refresh(project)
print(
f"[generate_lyric] Project saved - project_id: {project.id}, task_id: {task_id}" step2_elapsed = (time.perf_counter() - step2_start) * 1000
) print(f"[generate_lyric] Step 2 완료 - project_id: {project.id} ({step2_elapsed:.1f}ms)")
# ========== Step 3: Lyric 테이블에 데이터 저장 ==========
step3_start = time.perf_counter()
print(f"[generate_lyric] Step 3: Lyric 저장 (processing)...")
# 3. Lyric 테이블에 데이터 저장 (status: processing)
lyric = Lyric( lyric = Lyric(
project_id=project.id, project_id=project.id,
task_id=task_id, task_id=task_id,
@@ -260,64 +279,48 @@ async def generate_lyric(
language=request_body.language, language=request_body.language,
) )
session.add(lyric) session.add(lyric)
await (
session.commit()
) # processing 상태를 확실히 저장 (다른 트랜잭션에서 조회 가능)
await session.refresh(lyric) # commit 후 객체 상태 동기화
print(
f"[generate_lyric] Lyric saved (processing) - lyric_id: {lyric.id}, task_id: {task_id}"
)
# 4. ChatGPT를 통해 가사 생성
print(f"[generate_lyric] ChatGPT generation started - task_id: {task_id}")
result = await service.generate(prompt=prompt)
print(f"[generate_lyric] ChatGPT generation completed - task_id: {task_id}")
# 5. 실패 응답 검사 (ERROR 또는 ChatGPT 거부 응답)
failure_patterns = [
"ERROR:",
"I'm sorry",
"I cannot",
"I can't",
"I apologize",
"I'm unable",
"I am unable",
"I'm not able",
"I am not able",
]
is_failure = any(
pattern.lower() in result.lower() for pattern in failure_patterns
)
if is_failure:
print(f"[generate_lyric] FAILED - task_id: {task_id}, error: {result}")
lyric.status = "failed"
lyric.lyric_result = result
await session.commit() await session.commit()
await session.refresh(lyric)
return GenerateLyricResponse( step3_elapsed = (time.perf_counter() - step3_start) * 1000
success=False, print(f"[generate_lyric] Step 3 완료 - lyric_id: {lyric.id} ({step3_elapsed:.1f}ms)")
# ========== Step 4: 백그라운드 태스크 스케줄링 ==========
step4_start = time.perf_counter()
print(f"[generate_lyric] Step 4: 백그라운드 태스크 스케줄링...")
background_tasks.add_task(
generate_lyric_background,
task_id=task_id, task_id=task_id,
lyric=None, prompt=prompt,
language=request_body.language, language=request_body.language,
error_message=result,
) )
# 6. 성공 시 Lyric 테이블 업데이트 (status: completed) step4_elapsed = (time.perf_counter() - step4_start) * 1000
lyric.status = "completed" print(f"[generate_lyric] Step 4 완료 ({step4_elapsed:.1f}ms)")
lyric.lyric_result = result
await session.commit()
print(f"[generate_lyric] SUCCESS - task_id: {task_id}") # ========== 완료 ==========
total_elapsed = (time.perf_counter() - request_start) * 1000
print(f"[generate_lyric] ========== COMPLETE ==========")
print(f"[generate_lyric] API 응답 소요시간: {total_elapsed:.1f}ms")
print(f"[generate_lyric] - Step 1 (프롬프트 생성): {step1_elapsed:.1f}ms")
print(f"[generate_lyric] - Step 2 (Project 저장): {step2_elapsed:.1f}ms")
print(f"[generate_lyric] - Step 3 (Lyric 저장): {step3_elapsed:.1f}ms")
print(f"[generate_lyric] - Step 4 (태스크 스케줄링): {step4_elapsed:.1f}ms")
print(f"[generate_lyric] (GPT API 호출은 백그라운드에서 별도 진행)")
# 5. 즉시 응답 반환
return GenerateLyricResponse( return GenerateLyricResponse(
success=True, success=True,
task_id=task_id, task_id=task_id,
lyric=result, lyric=None,
language=request_body.language, language=request_body.language,
error_message=None, error_message=None,
) )
except Exception as e: except Exception as e:
print(f"[generate_lyric] EXCEPTION - task_id: {task_id}, error: {e}") elapsed = (time.perf_counter() - request_start) * 1000
print(f"[generate_lyric] EXCEPTION - task_id: {task_id}, error: {e} ({elapsed:.1f}ms)")
await session.rollback() await session.rollback()
return GenerateLyricResponse( return GenerateLyricResponse(
success=False, success=False,
+53 -36
View File
@@ -25,7 +25,7 @@ Lyric API Schemas
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import BaseModel, ConfigDict, Field
class GenerateLyricRequest(BaseModel): class GenerateLyricRequest(BaseModel):
@@ -37,6 +37,7 @@ class GenerateLyricRequest(BaseModel):
Example Request: Example Request:
{ {
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"customer_name": "스테이 머뭄", "customer_name": "스테이 머뭄",
"region": "군산", "region": "군산",
"detail_region_info": "군산 신흥동 말랭이 마을", "detail_region_info": "군산 신흥동 말랭이 마을",
@@ -44,17 +45,21 @@ class GenerateLyricRequest(BaseModel):
} }
""" """
model_config = { model_config = ConfigDict(
"json_schema_extra": { json_schema_extra={
"example": { "example": {
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"customer_name": "스테이 머뭄", "customer_name": "스테이 머뭄",
"region": "군산", "region": "군산",
"detail_region_info": "군산 신흥동 말랭이 마을", "detail_region_info": "군산 신흥동 말랭이 마을",
"language": "Korean", "language": "Korean",
} }
} }
} )
task_id: str = Field(
..., description="작업 고유 식별자 (이미지 업로드 시 생성된 task_id)"
)
customer_name: str = Field(..., description="고객명/가게명") customer_name: str = Field(..., description="고객명/가게명")
region: str = Field(..., description="지역명") region: str = Field(..., description="지역명")
detail_region_info: Optional[str] = Field(None, description="상세 지역 정보") detail_region_info: Optional[str] = Field(None, description="상세 지역 정보")
@@ -76,26 +81,20 @@ class GenerateLyricResponse(BaseModel):
- ChatGPT API 오류 - ChatGPT API 오류
- ChatGPT 거부 응답 (I'm sorry, I cannot, I can't, I apologize 등) - ChatGPT 거부 응답 (I'm sorry, I cannot, I can't, I apologize 등)
- 응답에 ERROR: 포함 - 응답에 ERROR: 포함
Example Response (Success):
{
"success": true,
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
"lyric": "인스타 감성의 스테이 머뭄...",
"language": "Korean",
"error_message": null
}
Example Response (Failure):
{
"success": false,
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
"lyric": null,
"language": "Korean",
"error_message": "I'm sorry, I can't comply with that request."
}
""" """
model_config = ConfigDict(
json_schema_extra={
"example": {
"success": True,
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"lyric": "인스타 감성의 스테이 머뭄\n군산 신흥동 말랭이 마을에서\n여유로운 하루를 보내며\n추억을 만들어가요",
"language": "Korean",
"error_message": None,
}
}
)
success: bool = Field(..., description="생성 성공 여부") success: bool = Field(..., description="생성 성공 여부")
task_id: Optional[str] = Field(None, description="작업 고유 식별자 (uuid7)") task_id: Optional[str] = Field(None, description="작업 고유 식별자 (uuid7)")
lyric: Optional[str] = Field(None, description="생성된 가사 (성공 시)") lyric: Optional[str] = Field(None, description="생성된 가사 (성공 시)")
@@ -109,15 +108,18 @@ class LyricStatusResponse(BaseModel):
Usage: Usage:
GET /lyric/status/{task_id} GET /lyric/status/{task_id}
Returns the current processing status of a lyric generation task. Returns the current processing status of a lyric generation task.
Example Response:
{
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
"status": "completed",
"message": "가사 생성이 완료되었습니다."
}
""" """
model_config = ConfigDict(
json_schema_extra={
"example": {
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"status": "completed",
"message": "가사 생성이 완료되었습니다.",
}
}
)
task_id: str = Field(..., description="작업 고유 식별자") task_id: str = Field(..., description="작업 고유 식별자")
status: str = Field(..., description="처리 상태 (processing, completed, failed)") status: str = Field(..., description="처리 상태 (processing, completed, failed)")
message: str = Field(..., description="상태 메시지") message: str = Field(..., description="상태 메시지")
@@ -129,18 +131,21 @@ class LyricDetailResponse(BaseModel):
Usage: Usage:
GET /lyric/{task_id} GET /lyric/{task_id}
Returns the generated lyric content for a specific task. Returns the generated lyric content for a specific task.
"""
Example Response: model_config = ConfigDict(
{ json_schema_extra={
"example": {
"id": 1, "id": 1,
"task_id": "019123ab-cdef-7890-abcd-ef1234567890", "task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"project_id": 1, "project_id": 1,
"status": "completed", "status": "completed",
"lyric_prompt": "...", "lyric_prompt": "고객명: 스테이 머뭄, 지역: 군산...",
"lyric_result": "생성된 가사...", "lyric_result": "인스타 감성의 스테이 머뭄\n군산 신흥동 말랭이 마을에서\n여유로운 하루를 보내며\n추억을 만들어가요",
"created_at": "2024-01-01T12:00:00" "created_at": "2024-01-15T12:00:00",
} }
""" }
)
id: int = Field(..., description="가사 ID") id: int = Field(..., description="가사 ID")
task_id: str = Field(..., description="작업 고유 식별자") task_id: str = Field(..., description="작업 고유 식별자")
@@ -158,6 +163,18 @@ class LyricListItem(BaseModel):
Used as individual items in paginated lyric list responses. Used as individual items in paginated lyric list responses.
""" """
model_config = ConfigDict(
json_schema_extra={
"example": {
"id": 1,
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"status": "completed",
"lyric_result": "인스타 감성의 스테이 머뭄\n군산 신흥동 말랭이 마을에서...",
"created_at": "2024-01-15T12:00:00",
}
}
)
id: int = Field(..., description="가사 ID") id: int = Field(..., description="가사 ID")
task_id: str = Field(..., description="작업 고유 식별자") task_id: str = Field(..., description="작업 고유 식별자")
status: str = Field(..., description="처리 상태") status: str = Field(..., description="처리 상태")
+2 -2
View File
@@ -52,7 +52,7 @@ class SongFormData:
attributes: Dict[str, str] = field(default_factory=dict) attributes: Dict[str, str] = field(default_factory=dict)
attributes_str: str = "" attributes_str: str = ""
lyrics_ids: List[int] = field(default_factory=list) lyrics_ids: List[int] = field(default_factory=list)
llm_model: str = "gpt-4o" llm_model: str = "gpt-5-mini"
@classmethod @classmethod
async def from_form(cls, request: Request): async def from_form(cls, request: Request):
@@ -86,6 +86,6 @@ class SongFormData:
attributes=attributes, attributes=attributes,
attributes_str=attributes_str, attributes_str=attributes_str,
lyrics_ids=lyrics_ids, lyrics_ids=lyrics_ids,
llm_model=form_data.get("llm_model", "gpt-4o"), llm_model=form_data.get("llm_model", "gpt-5-mini"),
prompts=form_data.get("prompts", ""), prompts=form_data.get("prompts", ""),
) )
+1 -1
View File
@@ -783,7 +783,7 @@ async def make_automation(request: Request, conn: Connection):
else "", else "",
"attr_value": ", ".join(selected_values) if selected_values else "", "attr_value": ", ".join(selected_values) if selected_values else "",
"ai": "ChatGPT", "ai": "ChatGPT",
"ai_model": "gpt-4o", "ai_model": "gpt-5-mini",
"genre": "후크송", "genre": "후크송",
"sample_song": combined_sample_song or "없음", "sample_song": combined_sample_song or "없음",
"result_song": final_lyrics, "result_song": final_lyrics,
+146
View File
@@ -0,0 +1,146 @@
"""
Lyric Background Tasks
가사 생성 관련 백그라운드 태스크를 정의합니다.
"""
import logging
import traceback
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from app.database.session import BackgroundSessionLocal
from app.lyric.models import Lyric
from app.utils.chatgpt_prompt import ChatgptService
# 로거 설정
logger = logging.getLogger(__name__)
async def _update_lyric_status(
task_id: str,
status: str,
result: str | None = None,
) -> bool:
"""Lyric 테이블의 상태를 업데이트합니다.
Args:
task_id: 프로젝트 task_id
status: 변경할 상태 ("processing", "completed", "failed")
result: 가사 결과 또는 에러 메시지
Returns:
bool: 업데이트 성공 여부
"""
try:
async with BackgroundSessionLocal() as session:
query_result = await session.execute(
select(Lyric)
.where(Lyric.task_id == task_id)
.order_by(Lyric.created_at.desc())
.limit(1)
)
lyric = query_result.scalar_one_or_none()
if lyric:
lyric.status = status
if result is not None:
lyric.lyric_result = result
await session.commit()
logger.info(f"[Lyric] Status updated - task_id: {task_id}, status: {status}")
print(f"[Lyric] Status updated - task_id: {task_id}, status: {status}")
return True
else:
logger.warning(f"[Lyric] NOT FOUND in DB - task_id: {task_id}")
print(f"[Lyric] NOT FOUND in DB - task_id: {task_id}")
return False
except SQLAlchemyError as e:
logger.error(f"[Lyric] DB Error while updating status - task_id: {task_id}, error: {e}")
print(f"[Lyric] DB Error while updating status - task_id: {task_id}, error: {e}")
return False
except Exception as e:
logger.error(f"[Lyric] Unexpected error while updating status - task_id: {task_id}, error: {e}")
print(f"[Lyric] Unexpected error while updating status - task_id: {task_id}, error: {e}")
return False
async def generate_lyric_background(
task_id: str,
prompt: str,
language: str,
) -> None:
"""백그라운드에서 ChatGPT를 통해 가사를 생성하고 Lyric 테이블을 업데이트합니다.
Args:
task_id: 프로젝트 task_id
prompt: ChatGPT에 전달할 프롬프트
language: 가사 언어
"""
import time
task_start = time.perf_counter()
logger.info(f"[generate_lyric_background] START - task_id: {task_id}")
print(f"[generate_lyric_background] ========== START ==========")
print(f"[generate_lyric_background] task_id: {task_id}")
print(f"[generate_lyric_background] language: {language}")
print(f"[generate_lyric_background] prompt length: {len(prompt)}자")
try:
# ========== Step 1: ChatGPT 서비스 초기화 ==========
step1_start = time.perf_counter()
print(f"[generate_lyric_background] Step 1: ChatGPT 서비스 초기화...")
service = ChatgptService(
customer_name="", # 프롬프트가 이미 생성되었으므로 빈 값
region="",
detail_region_info="",
language=language,
)
step1_elapsed = (time.perf_counter() - step1_start) * 1000
print(f"[generate_lyric_background] Step 1 완료 ({step1_elapsed:.1f}ms)")
# ========== Step 2: ChatGPT API 호출 (가사 생성) ==========
step2_start = time.perf_counter()
logger.info(f"[generate_lyric_background] Step 2: ChatGPT API 호출 시작 - task_id: {task_id}")
print(f"[generate_lyric_background] Step 2: ChatGPT API 호출 시작...")
result = await service.generate(prompt=prompt)
step2_elapsed = (time.perf_counter() - step2_start) * 1000
logger.info(f"[generate_lyric_background] Step 2 완료 - 응답 {len(result)}자 ({step2_elapsed:.1f}ms)")
print(f"[generate_lyric_background] Step 2 완료 - 응답 {len(result)}자 ({step2_elapsed:.1f}ms)")
# ========== Step 3: DB 상태 업데이트 ==========
step3_start = time.perf_counter()
print(f"[generate_lyric_background] Step 3: DB 상태 업데이트...")
await _update_lyric_status(task_id, "completed", result)
step3_elapsed = (time.perf_counter() - step3_start) * 1000
print(f"[generate_lyric_background] Step 3 완료 ({step3_elapsed:.1f}ms)")
# ========== 완료 ==========
total_elapsed = (time.perf_counter() - task_start) * 1000
logger.info(f"[generate_lyric_background] SUCCESS - task_id: {task_id}, 총 소요시간: {total_elapsed:.1f}ms")
print(f"[generate_lyric_background] ========== SUCCESS ==========")
print(f"[generate_lyric_background] 총 소요시간: {total_elapsed:.1f}ms")
print(f"[generate_lyric_background] - Step 1 (서비스 초기화): {step1_elapsed:.1f}ms")
print(f"[generate_lyric_background] - Step 2 (GPT API 호출): {step2_elapsed:.1f}ms")
print(f"[generate_lyric_background] - Step 3 (DB 업데이트): {step3_elapsed:.1f}ms")
except SQLAlchemyError as e:
elapsed = (time.perf_counter() - task_start) * 1000
logger.error(f"[generate_lyric_background] DB ERROR - task_id: {task_id}, error: {e} ({elapsed:.1f}ms)")
print(f"[generate_lyric_background] DB ERROR - {e} ({elapsed:.1f}ms)")
traceback.print_exc()
await _update_lyric_status(task_id, "failed", f"Database Error: {str(e)}")
except Exception as e:
elapsed = (time.perf_counter() - task_start) * 1000
logger.error(f"[generate_lyric_background] EXCEPTION - task_id: {task_id}, error: {e} ({elapsed:.1f}ms)")
print(f"[generate_lyric_background] EXCEPTION - {e} ({elapsed:.1f}ms)")
traceback.print_exc()
await _update_lyric_status(task_id, "failed", f"Error: {str(e)}")
BIN
View File
Binary file not shown.
+182 -69
View File
@@ -13,7 +13,7 @@ Song API Router
app.include_router(router, prefix="/api/v1") app.include_router(router, prefix="/api/v1")
""" """
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import func, select from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -32,7 +32,6 @@ from app.song.schemas.song_schema import (
PollingSongResponse, PollingSongResponse,
SongListItem, SongListItem,
) )
from app.song.worker.song_task import download_and_upload_song_by_suno_task_id
from app.utils.pagination import PaginatedResponse from app.utils.pagination import PaginatedResponse
from app.utils.suno import SunoService from app.utils.suno import SunoService
@@ -85,20 +84,42 @@ POST /song/generate/019123ab-cdef-7890-abcd-ef1234567890
async def generate_song( async def generate_song(
task_id: str, task_id: str,
request_body: GenerateSongRequest, request_body: GenerateSongRequest,
session: AsyncSession = Depends(get_session),
) -> GenerateSongResponse: ) -> GenerateSongResponse:
"""가사와 장르를 기반으로 Suno API를 통해 노래를 생성합니다. """가사와 장르를 기반으로 Suno API를 통해 노래를 생성합니다.
1. task_id로 Project와 Lyric 조회 1. task_id로 Project와 Lyric 조회
2. Song 테이블에 초기 데이터 저장 (status: processing) 2. Song 테이블에 초기 데이터 저장 (status: processing)
3. Suno API 호출 3. Suno API 호출 (세션 닫힌 상태)
4. suno_task_id 업데이트 후 응답 반환 4. suno_task_id 업데이트 후 응답 반환
Note: 이 함수는 Depends(get_session)을 사용하지 않고 명시적으로 세션을 관리합니다.
외부 API 호출 중 DB 커넥션이 유지되지 않도록 하여 커넥션 타임아웃 문제를 방지합니다.
""" """
print(f"[generate_song] START - task_id: {task_id}, genre: {request_body.genre}, language: {request_body.language}") import time
from app.database.session import AsyncSessionLocal
request_start = time.perf_counter()
print(
f"[generate_song] START - task_id: {task_id}, "
f"genre: {request_body.genre}, language: {request_body.language}"
)
# 외부 API 호출 전에 필요한 데이터를 저장할 변수들
project_id: int | None = None
lyric_id: int | None = None
song_id: int | None = None
# ==========================================================================
# 1단계: DB 조회 및 초기 데이터 저장 (세션을 명시적으로 열고 닫음)
# ==========================================================================
try: try:
# 1. task_id로 Project 조회 async with AsyncSessionLocal() as session:
# Project 조회 (중복 시 최신 것 선택)
project_result = await session.execute( project_result = await session.execute(
select(Project).where(Project.task_id == task_id) select(Project)
.where(Project.task_id == task_id)
.order_by(Project.created_at.desc())
.limit(1)
) )
project = project_result.scalar_one_or_none() project = project_result.scalar_one_or_none()
@@ -108,11 +129,14 @@ async def generate_song(
status_code=404, status_code=404,
detail=f"task_id '{task_id}'에 해당하는 Project를 찾을 수 없습니다.", detail=f"task_id '{task_id}'에 해당하는 Project를 찾을 수 없습니다.",
) )
print(f"[generate_song] Project found - project_id: {project.id}, task_id: {task_id}") project_id = project.id
# 2. task_id로 Lyric 조회 # Lyric 조회 (중복 시 최신 것 선택)
lyric_result = await session.execute( lyric_result = await session.execute(
select(Lyric).where(Lyric.task_id == task_id) select(Lyric)
.where(Lyric.task_id == task_id)
.order_by(Lyric.created_at.desc())
.limit(1)
) )
lyric = lyric_result.scalar_one_or_none() lyric = lyric_result.scalar_one_or_none()
@@ -122,16 +146,23 @@ async def generate_song(
status_code=404, status_code=404,
detail=f"task_id '{task_id}'에 해당하는 Lyric을 찾을 수 없습니다.", detail=f"task_id '{task_id}'에 해당하는 Lyric을 찾을 수 없습니다.",
) )
print(f"[generate_song] Lyric found - lyric_id: {lyric.id}, task_id: {task_id}") lyric_id = lyric.id
# 3. Song 테이블에 초기 데이터 저장 query_time = time.perf_counter()
print(
f"[generate_song] Queries completed - task_id: {task_id}, "
f"project_id: {project_id}, lyric_id: {lyric_id}, "
f"elapsed: {(query_time - request_start)*1000:.1f}ms"
)
# Song 테이블에 초기 데이터 저장
song_prompt = ( song_prompt = (
f"[Lyrics]\n{request_body.lyrics}\n\n[Genre]\n{request_body.genre}" f"[Lyrics]\n{request_body.lyrics}\n\n[Genre]\n{request_body.genre}"
) )
song = Song( song = Song(
project_id=project.id, project_id=project_id,
lyric_id=lyric.id, lyric_id=lyric_id,
task_id=task_id, task_id=task_id,
suno_task_id=None, suno_task_id=None,
status="processing", status="processing",
@@ -139,21 +170,103 @@ async def generate_song(
language=request_body.language, language=request_body.language,
) )
session.add(song) session.add(song)
await session.flush() # ID 생성을 위해 flush await session.commit()
print(f"[generate_song] Song saved (processing) - task_id: {task_id}") song_id = song.id
# 4. Suno API 호출 stage1_time = time.perf_counter()
print(f"[generate_song] Suno API generation started - task_id: {task_id}") print(
f"[generate_song] Stage 1 DONE - Song saved - "
f"task_id: {task_id}, song_id: {song_id}, "
f"elapsed: {(stage1_time - request_start)*1000:.1f}ms"
)
# 세션이 여기서 자동으로 닫힘
except HTTPException:
raise
except Exception as e:
print(
f"[generate_song] Stage 1 EXCEPTION - "
f"task_id: {task_id}, error: {type(e).__name__}: {e}"
)
return GenerateSongResponse(
success=False,
task_id=task_id,
suno_task_id=None,
message="노래 생성 요청에 실패했습니다.",
error_message=str(e),
)
# ==========================================================================
# 2단계: 외부 API 호출 (세션 사용 안함 - 커넥션 풀 점유 없음)
# ==========================================================================
stage2_start = time.perf_counter()
suno_task_id: str | None = None
try:
print(f"[generate_song] Stage 2 START - Suno API - task_id: {task_id}")
suno_service = SunoService() suno_service = SunoService()
suno_task_id = await suno_service.generate( suno_task_id = await suno_service.generate(
prompt=request_body.lyrics, prompt=request_body.lyrics,
genre=request_body.genre, genre=request_body.genre,
) )
# 5. suno_task_id 업데이트 stage2_time = time.perf_counter()
song.suno_task_id = suno_task_id print(
await session.commit() f"[generate_song] Stage 2 DONE - task_id: {task_id}, "
print(f"[generate_song] SUCCESS - task_id: {task_id}, suno_task_id: {suno_task_id}") f"suno_task_id: {suno_task_id}, "
f"elapsed: {(stage2_time - stage2_start)*1000:.1f}ms"
)
except Exception as e:
print(
f"[generate_song] Stage 2 EXCEPTION - Suno API failed - "
f"task_id: {task_id}, error: {type(e).__name__}: {e}"
)
# 외부 API 실패 시 Song 상태를 failed로 업데이트
async with AsyncSessionLocal() as update_session:
song_result = await update_session.execute(
select(Song).where(Song.id == song_id)
)
song_to_update = song_result.scalar_one_or_none()
if song_to_update:
song_to_update.status = "failed"
await update_session.commit()
return GenerateSongResponse(
success=False,
task_id=task_id,
suno_task_id=None,
message="노래 생성 요청에 실패했습니다.",
error_message=str(e),
)
# ==========================================================================
# 3단계: suno_task_id 업데이트 (새 세션으로 빠르게 처리)
# ==========================================================================
stage3_start = time.perf_counter()
print(f"[generate_song] Stage 3 START - DB update - task_id: {task_id}")
try:
async with AsyncSessionLocal() as update_session:
song_result = await update_session.execute(
select(Song).where(Song.id == song_id)
)
song_to_update = song_result.scalar_one_or_none()
if song_to_update:
song_to_update.suno_task_id = suno_task_id
await update_session.commit()
stage3_time = time.perf_counter()
total_time = stage3_time - request_start
print(
f"[generate_song] Stage 3 DONE - task_id: {task_id}, "
f"elapsed: {(stage3_time - stage3_start)*1000:.1f}ms"
)
print(
f"[generate_song] SUCCESS - task_id: {task_id}, "
f"suno_task_id: {suno_task_id}, "
f"total_time: {total_time*1000:.1f}ms"
)
return GenerateSongResponse( return GenerateSongResponse(
success=True, success=True,
@@ -163,16 +276,16 @@ async def generate_song(
error_message=None, error_message=None,
) )
except HTTPException:
raise
except Exception as e: except Exception as e:
print(f"[generate_song] EXCEPTION - task_id: {task_id}, error: {e}") print(
await session.rollback() f"[generate_song] Stage 3 EXCEPTION - "
f"task_id: {task_id}, error: {type(e).__name__}: {e}"
)
return GenerateSongResponse( return GenerateSongResponse(
success=False, success=False,
task_id=task_id, task_id=task_id,
suno_task_id=None, suno_task_id=suno_task_id,
message="노래 생성 요청에 실패했습니다.", message="노래 생성은 요청되었으나 DB 업데이트에 실패했습니다.",
error_message=str(e), error_message=str(e),
) )
@@ -220,7 +333,6 @@ GET /song/status/abc123...
) )
async def get_song_status( async def get_song_status(
suno_task_id: str, suno_task_id: str,
background_tasks: BackgroundTasks,
session: AsyncSession = Depends(get_session), session: AsyncSession = Depends(get_session),
) -> PollingSongResponse: ) -> PollingSongResponse:
"""suno_task_id로 노래 생성 작업의 상태를 조회합니다. """suno_task_id로 노래 생성 작업의 상태를 조회합니다.
@@ -236,15 +348,16 @@ async def get_song_status(
parsed_response = suno_service.parse_status_response(result) parsed_response = suno_service.parse_status_response(result)
print(f"[get_song_status] Suno API response - suno_task_id: {suno_task_id}, status: {parsed_response.status}") print(f"[get_song_status] Suno API response - suno_task_id: {suno_task_id}, status: {parsed_response.status}")
# SUCCESS 상태인 경우 백그라운드 태스크 실행 # SUCCESS 상태인 경우 첫 번째 클립 정보를 DB에 직접 저장
if parsed_response.status == "SUCCESS" and parsed_response.clips: if parsed_response.status == "SUCCESS" and parsed_response.clips:
# 첫 번째 클립의 audioUrl과 duration 가져오기 # 첫 번째 클립(clips[0])의 audioUrl과 duration 사용
first_clip = parsed_response.clips[0] first_clip = parsed_response.clips[0]
audio_url = first_clip.audio_url audio_url = first_clip.audio_url
clip_duration = first_clip.duration clip_duration = first_clip.duration
print(f"[get_song_status] Using first clip - id: {first_clip.id}, audio_url: {audio_url}, duration: {clip_duration}")
if audio_url: if audio_url:
# suno_task_id로 Song 조회하여 store_name 가져오기 # suno_task_id로 Song 조회
song_result = await session.execute( song_result = await session.execute(
select(Song) select(Song)
.where(Song.suno_task_id == suno_task_id) .where(Song.suno_task_id == suno_task_id)
@@ -254,24 +367,13 @@ async def get_song_status(
song = song_result.scalar_one_or_none() song = song_result.scalar_one_or_none()
if song and song.status != "completed": if song and song.status != "completed":
# 이미 완료된 경우 백그라운드 작업 중복 실행 방지 # 첫 번째 클립의 audio_url과 duration을 직접 DB에 저장
# project_id로 Project 조회하여 store_name 가져오기 song.status = "completed"
project_result = await session.execute( song.song_result_url = audio_url
select(Project).where(Project.id == song.project_id) if clip_duration is not None:
) song.duration = clip_duration
project = project_result.scalar_one_or_none() await session.commit()
print(f"[get_song_status] Song updated - suno_task_id: {suno_task_id}, status: completed, song_result_url: {audio_url}, duration: {clip_duration}")
store_name = project.store_name if project else "song"
# 백그라운드 태스크로 MP3 다운로드 및 Blob 업로드, DB 업데이트 (suno_task_id 사용)
print(f"[get_song_status] Background task args - suno_task_id: {suno_task_id}, audio_url: {audio_url}, store_name: {store_name}, duration: {clip_duration}")
background_tasks.add_task(
download_and_upload_song_by_suno_task_id,
suno_task_id=suno_task_id,
audio_url=audio_url,
store_name=store_name,
duration=clip_duration,
)
elif song and song.status == "completed": elif song and song.status == "completed":
print(f"[get_song_status] SKIPPED - Song already completed, suno_task_id: {suno_task_id}") print(f"[get_song_status] SKIPPED - Song already completed, suno_task_id: {suno_task_id}")
@@ -452,23 +554,36 @@ async def get_songs(
try: try:
offset = (pagination.page - 1) * pagination.page_size offset = (pagination.page - 1) * pagination.page_size
# 서브쿼리: task_id별 최신 Song의 id 조회 (completed 상태만) # 서브쿼리: task_id별 최신 Song의 id 조회 (completed 상태, created_at 기준)
subquery = ( from sqlalchemy import and_
select(func.max(Song.id).label("max_id"))
# task_id별 최신 created_at 조회
latest_subquery = (
select(
Song.task_id,
func.max(Song.created_at).label("max_created_at")
)
.where(Song.status == "completed") .where(Song.status == "completed")
.group_by(Song.task_id) .group_by(Song.task_id)
.subquery() .subquery()
) )
# 전체 개수 조회 (task_id별 최신 1개만) # 전체 개수 조회 (task_id별 최신 1개만)
count_query = select(func.count()).select_from(subquery) count_query = select(func.count()).select_from(latest_subquery)
total_result = await session.execute(count_query) total_result = await session.execute(count_query)
total = total_result.scalar() or 0 total = total_result.scalar() or 0
# 데이터 조회 (completed 상태, task_id별 최신 1개만, 최신순) # 데이터 조회 (completed 상태, task_id별 created_at 기준 최신 1개만, 최신순)
query = ( query = (
select(Song) select(Song)
.where(Song.id.in_(select(subquery.c.max_id))) .join(
latest_subquery,
and_(
Song.task_id == latest_subquery.c.task_id,
Song.created_at == latest_subquery.c.max_created_at
)
)
.where(Song.status == "completed")
.order_by(Song.created_at.desc()) .order_by(Song.created_at.desc())
.offset(offset) .offset(offset)
.limit(pagination.page_size) .limit(pagination.page_size)
@@ -476,14 +591,19 @@ async def get_songs(
result = await session.execute(query) result = await session.execute(query)
songs = result.scalars().all() songs = result.scalars().all()
# Project 정보와 함께 SongListItem으로 변환 # Project 정보 일괄 조회 (N+1 문제 해결)
project_ids = [s.project_id for s in songs if s.project_id]
projects_map: dict = {}
if project_ids:
projects_result = await session.execute(
select(Project).where(Project.id.in_(project_ids))
)
projects_map = {p.id: p for p in projects_result.scalars().all()}
# SongListItem으로 변환
items = [] items = []
for song in songs: for song in songs:
# Project 조회 (song.project_id 직접 사용) project = projects_map.get(song.project_id)
project_result = await session.execute(
select(Project).where(Project.id == song.project_id)
)
project = project_result.scalar_one_or_none()
item = SongListItem( item = SongListItem(
store_name=project.store_name if project else None, store_name=project.store_name if project else None,
@@ -495,13 +615,6 @@ async def get_songs(
) )
items.append(item) items.append(item)
# 개별 아이템 로그
print(
f"[get_songs] Item - store_name: {item.store_name}, region: {item.region}, "
f"task_id: {item.task_id}, language: {item.language}, "
f"song_result_url: {item.song_result_url}, created_at: {item.created_at}"
)
response = PaginatedResponse.create( response = PaginatedResponse.create(
items=items, items=items,
total=total, total=total,
+2 -2
View File
@@ -335,7 +335,7 @@ class SongFormData:
attributes: Dict[str, str] = field(default_factory=dict) attributes: Dict[str, str] = field(default_factory=dict)
attributes_str: str = "" attributes_str: str = ""
lyrics_ids: List[int] = field(default_factory=list) lyrics_ids: List[int] = field(default_factory=list)
llm_model: str = "gpt-4o" llm_model: str = "gpt-5-mini"
@classmethod @classmethod
async def from_form(cls, request: Request): async def from_form(cls, request: Request):
@@ -369,6 +369,6 @@ class SongFormData:
attributes=attributes, attributes=attributes,
attributes_str=attributes_str, attributes_str=attributes_str,
lyrics_ids=lyrics_ids, lyrics_ids=lyrics_ids,
llm_model=form_data.get("llm_model", "gpt-4o"), llm_model=form_data.get("llm_model", "gpt-5-mini"),
prompts=form_data.get("prompts", ""), prompts=form_data.get("prompts", ""),
) )
+1 -1
View File
@@ -783,7 +783,7 @@ async def make_automation(request: Request, conn: Connection):
else "", else "",
"attr_value": ", ".join(selected_values) if selected_values else "", "attr_value": ", ".join(selected_values) if selected_values else "",
"ai": "ChatGPT", "ai": "ChatGPT",
"ai_model": "gpt-4o", "ai_model": "gpt-5-mini",
"genre": "후크송", "genre": "후크송",
"sample_song": combined_sample_song or "없음", "sample_song": combined_sample_song or "없음",
"result_song": final_lyrics, "result_song": final_lyrics,
+194 -108
View File
@@ -4,19 +4,116 @@ Song Background Tasks
노래 생성 관련 백그라운드 태스크를 정의합니다. 노래 생성 관련 백그라운드 태스크를 정의합니다.
""" """
import logging
import traceback
from datetime import date from datetime import date
from pathlib import Path from pathlib import Path
import aiofiles import aiofiles
import httpx import httpx
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from app.database.session import AsyncSessionLocal from app.database.session import BackgroundSessionLocal
from app.song.models import Song from app.song.models import Song
from app.utils.common import generate_task_id from app.utils.common import generate_task_id
from app.utils.upload_blob_as_request import AzureBlobUploader from app.utils.upload_blob_as_request import AzureBlobUploader
from config import prj_settings from config import prj_settings
# 로거 설정
logger = logging.getLogger(__name__)
# HTTP 요청 설정
REQUEST_TIMEOUT = 120.0 # 초
async def _update_song_status(
task_id: str,
status: str,
song_url: str | None = None,
suno_task_id: str | None = None,
duration: float | None = None,
) -> bool:
"""Song 테이블의 상태를 업데이트합니다.
Args:
task_id: 프로젝트 task_id
status: 변경할 상태 ("processing", "completed", "failed")
song_url: 노래 URL
suno_task_id: Suno task ID (선택)
duration: 노래 길이 (선택)
Returns:
bool: 업데이트 성공 여부
"""
try:
async with BackgroundSessionLocal() as session:
if suno_task_id:
query_result = await session.execute(
select(Song)
.where(Song.suno_task_id == suno_task_id)
.order_by(Song.created_at.desc())
.limit(1)
)
else:
query_result = await session.execute(
select(Song)
.where(Song.task_id == task_id)
.order_by(Song.created_at.desc())
.limit(1)
)
song = query_result.scalar_one_or_none()
if song:
song.status = status
if song_url is not None:
song.song_result_url = song_url
if duration is not None:
song.duration = duration
await session.commit()
logger.info(f"[Song] Status updated - task_id: {task_id}, status: {status}")
print(f"[Song] Status updated - task_id: {task_id}, status: {status}")
return True
else:
logger.warning(f"[Song] NOT FOUND in DB - task_id: {task_id}")
print(f"[Song] NOT FOUND in DB - task_id: {task_id}")
return False
except SQLAlchemyError as e:
logger.error(f"[Song] DB Error while updating status - task_id: {task_id}, error: {e}")
print(f"[Song] DB Error while updating status - task_id: {task_id}, error: {e}")
return False
except Exception as e:
logger.error(f"[Song] Unexpected error while updating status - task_id: {task_id}, error: {e}")
print(f"[Song] Unexpected error while updating status - task_id: {task_id}, error: {e}")
return False
async def _download_audio(url: str, task_id: str) -> bytes:
"""URL에서 오디오 파일을 다운로드합니다.
Args:
url: 다운로드할 URL
task_id: 로그용 task_id
Returns:
bytes: 다운로드한 파일 내용
Raises:
httpx.HTTPError: 다운로드 실패 시
"""
logger.info(f"[Download] Downloading - task_id: {task_id}")
print(f"[Download] Downloading - task_id: {task_id}")
async with httpx.AsyncClient() as client:
response = await client.get(url, timeout=REQUEST_TIMEOUT)
response.raise_for_status()
logger.info(f"[Download] SUCCESS - task_id: {task_id}, size: {len(response.content)} bytes")
print(f"[Download] SUCCESS - task_id: {task_id}, size: {len(response.content)} bytes")
return response.content
async def download_and_save_song( async def download_and_save_song(
task_id: str, task_id: str,
@@ -30,7 +127,9 @@ async def download_and_save_song(
audio_url: 다운로드할 오디오 URL audio_url: 다운로드할 오디오 URL
store_name: 저장할 파일명에 사용할 업체명 store_name: 저장할 파일명에 사용할 업체명
""" """
logger.info(f"[download_and_save_song] START - task_id: {task_id}, store_name: {store_name}")
print(f"[download_and_save_song] START - task_id: {task_id}, store_name: {store_name}") print(f"[download_and_save_song] START - task_id: {task_id}, store_name: {store_name}")
try: try:
# 저장 경로 생성: media/song/{날짜}/{uuid7}/{store_name}.mp3 # 저장 경로 생성: media/song/{날짜}/{uuid7}/{store_name}.mp3
today = date.today().strftime("%Y-%m-%d") today = date.today().strftime("%Y-%m-%d")
@@ -46,60 +145,50 @@ async def download_and_save_song(
media_dir = Path("media") / "song" / today / unique_id media_dir = Path("media") / "song" / today / unique_id
media_dir.mkdir(parents=True, exist_ok=True) media_dir.mkdir(parents=True, exist_ok=True)
file_path = media_dir / file_name file_path = media_dir / file_name
logger.info(f"[download_and_save_song] Directory created - path: {file_path}")
print(f"[download_and_save_song] Directory created - path: {file_path}") print(f"[download_and_save_song] Directory created - path: {file_path}")
# 오디오 파일 다운로드 # 오디오 파일 다운로드
logger.info(f"[download_and_save_song] Downloading audio - task_id: {task_id}, url: {audio_url}")
print(f"[download_and_save_song] Downloading audio - task_id: {task_id}, url: {audio_url}") print(f"[download_and_save_song] Downloading audio - task_id: {task_id}, url: {audio_url}")
async with httpx.AsyncClient() as client:
response = await client.get(audio_url, timeout=60.0) content = await _download_audio(audio_url, task_id)
response.raise_for_status()
async with aiofiles.open(str(file_path), "wb") as f: async with aiofiles.open(str(file_path), "wb") as f:
await f.write(response.content) await f.write(content)
logger.info(f"[download_and_save_song] File saved - task_id: {task_id}, path: {file_path}")
print(f"[download_and_save_song] File saved - task_id: {task_id}, path: {file_path}") print(f"[download_and_save_song] File saved - task_id: {task_id}, path: {file_path}")
# 프론트엔드에서 접근 가능한 URL 생성 # 프론트엔드에서 접근 가능한 URL 생성
relative_path = f"/media/song/{today}/{unique_id}/{file_name}" relative_path = f"/media/song/{today}/{unique_id}/{file_name}"
base_url = f"http://{prj_settings.PROJECT_DOMAIN}" base_url = f"http://{prj_settings.PROJECT_DOMAIN}"
file_url = f"{base_url}{relative_path}" file_url = f"{base_url}{relative_path}"
logger.info(f"[download_and_save_song] URL generated - task_id: {task_id}, url: {file_url}")
print(f"[download_and_save_song] URL generated - task_id: {task_id}, url: {file_url}") print(f"[download_and_save_song] URL generated - task_id: {task_id}, url: {file_url}")
# Song 테이블 업데이트 (새 세션 사용) # Song 테이블 업데이트
async with AsyncSessionLocal() as session: await _update_song_status(task_id, "completed", file_url)
# 여러 개 있을 경우 가장 최근 것 선택 logger.info(f"[download_and_save_song] SUCCESS - task_id: {task_id}")
result = await session.execute( print(f"[download_and_save_song] SUCCESS - task_id: {task_id}")
select(Song)
.where(Song.task_id == task_id)
.order_by(Song.created_at.desc())
.limit(1)
)
song = result.scalar_one_or_none()
if song: except httpx.HTTPError as e:
song.status = "completed" logger.error(f"[download_and_save_song] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
song.song_result_url = file_url print(f"[download_and_save_song] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
await session.commit() traceback.print_exc()
print(f"[download_and_save_song] SUCCESS - task_id: {task_id}, status: completed") await _update_song_status(task_id, "failed")
else:
print(f"[download_and_save_song] Song NOT FOUND in DB - task_id: {task_id}") except SQLAlchemyError as e:
logger.error(f"[download_and_save_song] DB ERROR - task_id: {task_id}, error: {e}")
print(f"[download_and_save_song] DB ERROR - task_id: {task_id}, error: {e}")
traceback.print_exc()
await _update_song_status(task_id, "failed")
except Exception as e: except Exception as e:
logger.error(f"[download_and_save_song] EXCEPTION - task_id: {task_id}, error: {e}")
print(f"[download_and_save_song] EXCEPTION - task_id: {task_id}, error: {e}") print(f"[download_and_save_song] EXCEPTION - task_id: {task_id}, error: {e}")
# 실패 시 Song 테이블 업데이트 traceback.print_exc()
async with AsyncSessionLocal() as session: await _update_song_status(task_id, "failed")
# 여러 개 있을 경우 가장 최근 것 선택
result = await session.execute(
select(Song)
.where(Song.task_id == task_id)
.order_by(Song.created_at.desc())
.limit(1)
)
song = result.scalar_one_or_none()
if song:
song.status = "failed"
await session.commit()
print(f"[download_and_save_song] FAILED - task_id: {task_id}, status updated to failed")
async def download_and_upload_song_to_blob( async def download_and_upload_song_to_blob(
@@ -114,6 +203,7 @@ async def download_and_upload_song_to_blob(
audio_url: 다운로드할 오디오 URL audio_url: 다운로드할 오디오 URL
store_name: 저장할 파일명에 사용할 업체명 store_name: 저장할 파일명에 사용할 업체명
""" """
logger.info(f"[download_and_upload_song_to_blob] START - task_id: {task_id}, store_name: {store_name}")
print(f"[download_and_upload_song_to_blob] START - task_id: {task_id}, store_name: {store_name}") print(f"[download_and_upload_song_to_blob] START - task_id: {task_id}, store_name: {store_name}")
temp_file_path: Path | None = None temp_file_path: Path | None = None
@@ -129,16 +219,19 @@ async def download_and_upload_song_to_blob(
temp_dir = Path("media") / "temp" / task_id temp_dir = Path("media") / "temp" / task_id
temp_dir.mkdir(parents=True, exist_ok=True) temp_dir.mkdir(parents=True, exist_ok=True)
temp_file_path = temp_dir / file_name temp_file_path = temp_dir / file_name
logger.info(f"[download_and_upload_song_to_blob] Temp directory created - path: {temp_file_path}")
print(f"[download_and_upload_song_to_blob] Temp directory created - path: {temp_file_path}") print(f"[download_and_upload_song_to_blob] Temp directory created - path: {temp_file_path}")
# 오디오 파일 다운로드 # 오디오 파일 다운로드
logger.info(f"[download_and_upload_song_to_blob] Downloading audio - task_id: {task_id}, url: {audio_url}")
print(f"[download_and_upload_song_to_blob] Downloading audio - task_id: {task_id}, url: {audio_url}") print(f"[download_and_upload_song_to_blob] Downloading audio - task_id: {task_id}, url: {audio_url}")
async with httpx.AsyncClient() as client:
response = await client.get(audio_url, timeout=60.0) content = await _download_audio(audio_url, task_id)
response.raise_for_status()
async with aiofiles.open(str(temp_file_path), "wb") as f: async with aiofiles.open(str(temp_file_path), "wb") as f:
await f.write(response.content) await f.write(content)
logger.info(f"[download_and_upload_song_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}")
print(f"[download_and_upload_song_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}") print(f"[download_and_upload_song_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}")
# Azure Blob Storage에 업로드 # Azure Blob Storage에 업로드
@@ -150,51 +243,41 @@ async def download_and_upload_song_to_blob(
# SAS 토큰이 제외된 public_url 사용 # SAS 토큰이 제외된 public_url 사용
blob_url = uploader.public_url blob_url = uploader.public_url
logger.info(f"[download_and_upload_song_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}")
print(f"[download_and_upload_song_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}") print(f"[download_and_upload_song_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}")
# Song 테이블 업데이트 (새 세션 사용) # Song 테이블 업데이트
async with AsyncSessionLocal() as session: await _update_song_status(task_id, "completed", blob_url)
# 여러 개 있을 경우 가장 최근 것 선택 logger.info(f"[download_and_upload_song_to_blob] SUCCESS - task_id: {task_id}")
result = await session.execute( print(f"[download_and_upload_song_to_blob] SUCCESS - task_id: {task_id}")
select(Song)
.where(Song.task_id == task_id)
.order_by(Song.created_at.desc())
.limit(1)
)
song = result.scalar_one_or_none()
if song: except httpx.HTTPError as e:
song.status = "completed" logger.error(f"[download_and_upload_song_to_blob] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
song.song_result_url = blob_url print(f"[download_and_upload_song_to_blob] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
await session.commit() traceback.print_exc()
print(f"[download_and_upload_song_to_blob] SUCCESS - task_id: {task_id}, status: completed") await _update_song_status(task_id, "failed")
else:
print(f"[download_and_upload_song_to_blob] Song NOT FOUND in DB - task_id: {task_id}") except SQLAlchemyError as e:
logger.error(f"[download_and_upload_song_to_blob] DB ERROR - task_id: {task_id}, error: {e}")
print(f"[download_and_upload_song_to_blob] DB ERROR - task_id: {task_id}, error: {e}")
traceback.print_exc()
await _update_song_status(task_id, "failed")
except Exception as e: except Exception as e:
logger.error(f"[download_and_upload_song_to_blob] EXCEPTION - task_id: {task_id}, error: {e}")
print(f"[download_and_upload_song_to_blob] EXCEPTION - task_id: {task_id}, error: {e}") print(f"[download_and_upload_song_to_blob] EXCEPTION - task_id: {task_id}, error: {e}")
# 실패 시 Song 테이블 업데이트 traceback.print_exc()
async with AsyncSessionLocal() as session: await _update_song_status(task_id, "failed")
result = await session.execute(
select(Song)
.where(Song.task_id == task_id)
.order_by(Song.created_at.desc())
.limit(1)
)
song = result.scalar_one_or_none()
if song:
song.status = "failed"
await session.commit()
print(f"[download_and_upload_song_to_blob] FAILED - task_id: {task_id}, status updated to failed")
finally: finally:
# 임시 파일 삭제 # 임시 파일 삭제
if temp_file_path and temp_file_path.exists(): if temp_file_path and temp_file_path.exists():
try: try:
temp_file_path.unlink() temp_file_path.unlink()
logger.info(f"[download_and_upload_song_to_blob] Temp file deleted - path: {temp_file_path}")
print(f"[download_and_upload_song_to_blob] Temp file deleted - path: {temp_file_path}") print(f"[download_and_upload_song_to_blob] Temp file deleted - path: {temp_file_path}")
except Exception as e: except Exception as e:
logger.warning(f"[download_and_upload_song_to_blob] Failed to delete temp file: {e}")
print(f"[download_and_upload_song_to_blob] Failed to delete temp file: {e}") print(f"[download_and_upload_song_to_blob] Failed to delete temp file: {e}")
# 임시 디렉토리 삭제 시도 # 임시 디렉토리 삭제 시도
@@ -220,13 +303,14 @@ async def download_and_upload_song_by_suno_task_id(
store_name: 저장할 파일명에 사용할 업체명 store_name: 저장할 파일명에 사용할 업체명
duration: 노래 재생 시간 (초) duration: 노래 재생 시간 (초)
""" """
logger.info(f"[download_and_upload_song_by_suno_task_id] START - suno_task_id: {suno_task_id}, store_name: {store_name}, duration: {duration}")
print(f"[download_and_upload_song_by_suno_task_id] START - suno_task_id: {suno_task_id}, store_name: {store_name}, duration: {duration}") print(f"[download_and_upload_song_by_suno_task_id] START - suno_task_id: {suno_task_id}, store_name: {store_name}, duration: {duration}")
temp_file_path: Path | None = None temp_file_path: Path | None = None
task_id: str | None = None task_id: str | None = None
try: try:
# suno_task_id로 Song 조회하여 task_id 가져오기 # suno_task_id로 Song 조회하여 task_id 가져오기
async with AsyncSessionLocal() as session: async with BackgroundSessionLocal() as session:
result = await session.execute( result = await session.execute(
select(Song) select(Song)
.where(Song.suno_task_id == suno_task_id) .where(Song.suno_task_id == suno_task_id)
@@ -236,10 +320,12 @@ async def download_and_upload_song_by_suno_task_id(
song = result.scalar_one_or_none() song = result.scalar_one_or_none()
if not song: if not song:
logger.warning(f"[download_and_upload_song_by_suno_task_id] Song NOT FOUND - suno_task_id: {suno_task_id}")
print(f"[download_and_upload_song_by_suno_task_id] Song NOT FOUND - suno_task_id: {suno_task_id}") print(f"[download_and_upload_song_by_suno_task_id] Song NOT FOUND - suno_task_id: {suno_task_id}")
return return
task_id = song.task_id task_id = song.task_id
logger.info(f"[download_and_upload_song_by_suno_task_id] Song found - suno_task_id: {suno_task_id}, task_id: {task_id}")
print(f"[download_and_upload_song_by_suno_task_id] Song found - suno_task_id: {suno_task_id}, task_id: {task_id}") print(f"[download_and_upload_song_by_suno_task_id] Song found - suno_task_id: {suno_task_id}, task_id: {task_id}")
# 파일명에 사용할 수 없는 문자 제거 # 파일명에 사용할 수 없는 문자 제거
@@ -253,16 +339,19 @@ async def download_and_upload_song_by_suno_task_id(
temp_dir = Path("media") / "temp" / task_id temp_dir = Path("media") / "temp" / task_id
temp_dir.mkdir(parents=True, exist_ok=True) temp_dir.mkdir(parents=True, exist_ok=True)
temp_file_path = temp_dir / file_name temp_file_path = temp_dir / file_name
logger.info(f"[download_and_upload_song_by_suno_task_id] Temp directory created - path: {temp_file_path}")
print(f"[download_and_upload_song_by_suno_task_id] Temp directory created - path: {temp_file_path}") print(f"[download_and_upload_song_by_suno_task_id] Temp directory created - path: {temp_file_path}")
# 오디오 파일 다운로드 # 오디오 파일 다운로드
logger.info(f"[download_and_upload_song_by_suno_task_id] Downloading audio - suno_task_id: {suno_task_id}, url: {audio_url}")
print(f"[download_and_upload_song_by_suno_task_id] Downloading audio - suno_task_id: {suno_task_id}, url: {audio_url}") print(f"[download_and_upload_song_by_suno_task_id] Downloading audio - suno_task_id: {suno_task_id}, url: {audio_url}")
async with httpx.AsyncClient() as client:
response = await client.get(audio_url, timeout=60.0) content = await _download_audio(audio_url, task_id)
response.raise_for_status()
async with aiofiles.open(str(temp_file_path), "wb") as f: async with aiofiles.open(str(temp_file_path), "wb") as f:
await f.write(response.content) await f.write(content)
logger.info(f"[download_and_upload_song_by_suno_task_id] File downloaded - suno_task_id: {suno_task_id}, path: {temp_file_path}")
print(f"[download_and_upload_song_by_suno_task_id] File downloaded - suno_task_id: {suno_task_id}, path: {temp_file_path}") print(f"[download_and_upload_song_by_suno_task_id] File downloaded - suno_task_id: {suno_task_id}, path: {temp_file_path}")
# Azure Blob Storage에 업로드 # Azure Blob Storage에 업로드
@@ -274,53 +363,50 @@ async def download_and_upload_song_by_suno_task_id(
# SAS 토큰이 제외된 public_url 사용 # SAS 토큰이 제외된 public_url 사용
blob_url = uploader.public_url blob_url = uploader.public_url
logger.info(f"[download_and_upload_song_by_suno_task_id] Uploaded to Blob - suno_task_id: {suno_task_id}, url: {blob_url}")
print(f"[download_and_upload_song_by_suno_task_id] Uploaded to Blob - suno_task_id: {suno_task_id}, url: {blob_url}") print(f"[download_and_upload_song_by_suno_task_id] Uploaded to Blob - suno_task_id: {suno_task_id}, url: {blob_url}")
# Song 테이블 업데이트 (새 세션 사용) # Song 테이블 업데이트
async with AsyncSessionLocal() as session: await _update_song_status(
result = await session.execute( task_id=task_id,
select(Song) status="completed",
.where(Song.suno_task_id == suno_task_id) song_url=blob_url,
.order_by(Song.created_at.desc()) suno_task_id=suno_task_id,
.limit(1) duration=duration,
) )
song = result.scalar_one_or_none() logger.info(f"[download_and_upload_song_by_suno_task_id] SUCCESS - suno_task_id: {suno_task_id}, duration: {duration}")
print(f"[download_and_upload_song_by_suno_task_id] SUCCESS - suno_task_id: {suno_task_id}, duration: {duration}")
if song: except httpx.HTTPError as e:
song.status = "completed" logger.error(f"[download_and_upload_song_by_suno_task_id] DOWNLOAD ERROR - suno_task_id: {suno_task_id}, error: {e}")
song.song_result_url = blob_url print(f"[download_and_upload_song_by_suno_task_id] DOWNLOAD ERROR - suno_task_id: {suno_task_id}, error: {e}")
if duration is not None: traceback.print_exc()
song.duration = duration if task_id:
await session.commit() await _update_song_status(task_id, "failed", suno_task_id=suno_task_id)
print(f"[download_and_upload_song_by_suno_task_id] SUCCESS - suno_task_id: {suno_task_id}, status: completed, duration: {duration}")
else: except SQLAlchemyError as e:
print(f"[download_and_upload_song_by_suno_task_id] Song NOT FOUND in DB - suno_task_id: {suno_task_id}") logger.error(f"[download_and_upload_song_by_suno_task_id] DB ERROR - suno_task_id: {suno_task_id}, error: {e}")
print(f"[download_and_upload_song_by_suno_task_id] DB ERROR - suno_task_id: {suno_task_id}, error: {e}")
traceback.print_exc()
if task_id:
await _update_song_status(task_id, "failed", suno_task_id=suno_task_id)
except Exception as e: except Exception as e:
logger.error(f"[download_and_upload_song_by_suno_task_id] EXCEPTION - suno_task_id: {suno_task_id}, error: {e}")
print(f"[download_and_upload_song_by_suno_task_id] EXCEPTION - suno_task_id: {suno_task_id}, error: {e}") print(f"[download_and_upload_song_by_suno_task_id] EXCEPTION - suno_task_id: {suno_task_id}, error: {e}")
# 실패 시 Song 테이블 업데이트 traceback.print_exc()
if task_id: if task_id:
async with AsyncSessionLocal() as session: await _update_song_status(task_id, "failed", suno_task_id=suno_task_id)
result = await session.execute(
select(Song)
.where(Song.suno_task_id == suno_task_id)
.order_by(Song.created_at.desc())
.limit(1)
)
song = result.scalar_one_or_none()
if song:
song.status = "failed"
await session.commit()
print(f"[download_and_upload_song_by_suno_task_id] FAILED - suno_task_id: {suno_task_id}, status updated to failed")
finally: finally:
# 임시 파일 삭제 # 임시 파일 삭제
if temp_file_path and temp_file_path.exists(): if temp_file_path and temp_file_path.exists():
try: try:
temp_file_path.unlink() temp_file_path.unlink()
logger.info(f"[download_and_upload_song_by_suno_task_id] Temp file deleted - path: {temp_file_path}")
print(f"[download_and_upload_song_by_suno_task_id] Temp file deleted - path: {temp_file_path}") print(f"[download_and_upload_song_by_suno_task_id] Temp file deleted - path: {temp_file_path}")
except Exception as e: except Exception as e:
logger.warning(f"[download_and_upload_song_by_suno_task_id] Failed to delete temp file: {e}")
print(f"[download_and_upload_song_by_suno_task_id] Failed to delete temp file: {e}") print(f"[download_and_upload_song_by_suno_task_id] Failed to delete temp file: {e}")
# 임시 디렉토리 삭제 시도 # 임시 디렉토리 삭제 시도
+104 -29
View File
@@ -1,10 +1,14 @@
import json import json
import logging
import re import re
from openai import AsyncOpenAI from openai import AsyncOpenAI
from config import apikey_settings from config import apikey_settings
# 로거 설정
logger = logging.getLogger(__name__)
# fmt: off # fmt: off
LYRICS_PROMPT_TEMPLATE_ORI = """ LYRICS_PROMPT_TEMPLATE_ORI = """
1.Act as a content marketing expert with domain knowledges in [pension/staying services] in Korea, Goal: plan viral content creation that lead online reservations and promotion 1.Act as a content marketing expert with domain knowledges in [pension/staying services] in Korea, Goal: plan viral content creation that lead online reservations and promotion
@@ -156,18 +160,10 @@ Provide comprehensive marketing analysis including:
- Return as JSON with key "tags" - Return as JSON with key "tags"
- **MUST be written in Korean (한국어)** - **MUST be written in Korean (한국어)**
2. Facilities
- Based on the business name and region details, identify 5 likely facilities/amenities
- Consider typical facilities for accommodations in the given region
- Examples: 바베큐장, 수영장, 주차장, 와이파이, 주방, 테라스, 정원, etc.
- Return as JSON with key "facilities"
- **MUST be written in Korean (한국어)**
[CRITICAL LANGUAGE REQUIREMENT - ABSOLUTE RULE] [CRITICAL LANGUAGE REQUIREMENT - ABSOLUTE RULE]
ALL OUTPUT MUST BE WRITTEN IN KOREAN (한국어) ALL OUTPUT MUST BE WRITTEN IN KOREAN (한국어)
- Analysis sections: Korean only - Analysis sections: Korean only
- Tags: Korean only - Tags: Korean only
- Facilities: Korean only
- This is a NON-NEGOTIABLE requirement - This is a NON-NEGOTIABLE requirement
- Any output in English or other languages is considered a FAILURE - Any output in English or other languages is considered a FAILURE
- Violation of this rule invalidates the entire response - Violation of this rule invalidates the entire response
@@ -199,8 +195,7 @@ ALL OUTPUT MUST BE WRITTEN IN KOREAN (한국어)
## JSON Data ## JSON Data
```json ```json
{{ {{
"tags": ["태그1", "태그2", "태그3", "태그4", "태그5"], "tags": ["태그1", "태그2", "태그3", "태그4", "태그5"]
"facilities": ["부대시설1", "부대시설2", "부대시설3", "부대시설4", "부대시설5"]
}} }}
``` ```
--- ---
@@ -215,6 +210,11 @@ ERROR: [Brief reason for failure in English]
class ChatgptService: class ChatgptService:
"""ChatGPT API 서비스 클래스
GPT 5.0 모델을 사용하여 마케팅 가사 및 분석을 생성합니다.
"""
def __init__( def __init__(
self, self,
customer_name: str, customer_name: str,
@@ -222,9 +222,8 @@ class ChatgptService:
detail_region_info: str = "", detail_region_info: str = "",
language: str = "Korean", language: str = "Korean",
): ):
# 최신 모델: GPT-5, GPT-5 mini, GPT-5 nano, GPT-4.1, GPT-4.1 mini, GPT-4.1 nano # 최신 모델: gpt-5-mini
# 이전 세대: GPT-4o, GPT-4o mini, GPT-4 Turbo, GPT-3.5 Turbo self.model = "gpt-5-mini"
self.model = "gpt-4o"
self.client = AsyncOpenAI(api_key=apikey_settings.CHATGPT_API_KEY) self.client = AsyncOpenAI(api_key=apikey_settings.CHATGPT_API_KEY)
self.customer_name = customer_name self.customer_name = customer_name
self.region = region self.region = region
@@ -248,19 +247,64 @@ class ChatgptService:
detail_region_info=self.detail_region_info, detail_region_info=self.detail_region_info,
) )
async def generate(self, prompt: str | None = None) -> str: async def _call_gpt_api(self, prompt: str) -> str:
"""GPT에게 프롬프트를 전달하여 결과를 반환""" """GPT API를 직접 호출합니다 (내부 메서드).
if prompt is None:
prompt = self.build_lyrics_prompt() Args:
print("Generated Prompt: ", prompt) prompt: GPT에 전달할 프롬프트
Returns:
GPT 응답 문자열
Raises:
APIError, APIConnectionError, RateLimitError: OpenAI API 오류
"""
completion = await self.client.chat.completions.create( completion = await self.client.chat.completions.create(
model=self.model, messages=[{"role": "user", "content": prompt}] model=self.model, messages=[{"role": "user", "content": prompt}]
) )
message = completion.choices[0].message.content message = completion.choices[0].message.content
return message or "" return message or ""
async def generate(
self,
prompt: str | None = None,
) -> str:
"""GPT에게 프롬프트를 전달하여 결과를 반환합니다.
Args:
prompt: GPT에 전달할 프롬프트 (None이면 기본 가사 프롬프트 사용)
Returns:
GPT 응답 문자열
Raises:
APIError, APIConnectionError, RateLimitError: OpenAI API 오류
"""
if prompt is None:
prompt = self.build_lyrics_prompt()
print(f"[ChatgptService] Generated Prompt (length: {len(prompt)})")
logger.info(f"[ChatgptService] Starting GPT request with model: {self.model}")
# GPT API 호출
response = await self._call_gpt_api(prompt)
print(f"[ChatgptService] SUCCESS - Response length: {len(response)}")
logger.info(f"[ChatgptService] SUCCESS - Response length: {len(response)}")
return response
async def summarize_marketing(self, text: str) -> str: async def summarize_marketing(self, text: str) -> str:
"""마케팅 텍스트를 항목으로 구분하여 500자로 요약 정리""" """마케팅 텍스트를 항목으로 구분하여 500자로 요약 정리.
Args:
text: 요약할 마케팅 텍스트
Returns:
요약된 텍스트
Raises:
APIError, APIConnectionError, RateLimitError: OpenAI API 오류
"""
prompt = f"""[ROLE] prompt = f"""[ROLE]
마케팅 콘텐츠 요약 전문가 마케팅 콘텐츠 요약 전문가
@@ -271,21 +315,34 @@ class ChatgptService:
위 텍스트를 분석하여 핵심 내용을 항목별로 구분하여 500자 이내로 요약해주세요. 위 텍스트를 분석하여 핵심 내용을 항목별로 구분하여 500자 이내로 요약해주세요.
[OUTPUT REQUIREMENTS] [OUTPUT REQUIREMENTS]
- 항목별로 구분하여 정리 (예: 타겟 고객, 차별점, 지역 특성 등) - 5개 항목으로 구분: 타겟 고객, 핵심 차별점, 지역 특성, 시즌별 포인트, 추천 키워드
- 각 항목은 줄바꿈으로 구분
- 총 500자 이내로 요약 - 총 500자 이내로 요약
- 핵심 정보만 간결하게 포함 - 핵심 정보만 간결하게 포함
- 한국어로 작성 - 한국어로 작성
- 특수문자 사용 금지 (괄호, 슬래시, 하이픈, 물결표 등 제외)
- 쉼표와 마침표만 사용하여 자연스러운 문장으로 작성
[OUTPUT FORMAT] [OUTPUT FORMAT - 반드시 아래 형식 준수]
--- ---
[항목별로 구분된 500자 이내 요약] 타겟 고객
[대상 고객층을 자연스러운 문장으로 설명]
핵심 차별점
[숙소의 차별화 포인트를 자연스러운 문장으로 설명]
지역 특성
[주변 관광지와 지역 특색을 자연스러운 문장으로 설명]
시즌별 포인트
[계절별 매력 포인트를 자연스러운 문장으로 설명]
추천 키워드
[마케팅에 활용할 키워드를 쉼표로 구분하여 나열]
--- ---
""" """
completion = await self.client.chat.completions.create(
model=self.model, messages=[{"role": "user", "content": prompt}] result = await self.generate(prompt=prompt)
)
message = completion.choices[0].message.content
result = message or ""
# --- 구분자 제거 # --- 구분자 제거
if result.startswith("---"): if result.startswith("---"):
@@ -295,9 +352,15 @@ class ChatgptService:
return result return result
async def parse_marketing_analysis(self, raw_response: str) -> dict: async def parse_marketing_analysis(
self, raw_response: str, facility_info: str | None = None
) -> dict:
"""ChatGPT 마케팅 분석 응답을 파싱하고 요약하여 딕셔너리로 반환 """ChatGPT 마케팅 분석 응답을 파싱하고 요약하여 딕셔너리로 반환
Args:
raw_response: ChatGPT 마케팅 분석 응답 원문
facility_info: 크롤링에서 가져온 편의시설 정보 문자열
Returns: Returns:
dict: {"report": str, "tags": list[str], "facilities": list[str]} dict: {"report": str, "tags": list[str], "facilities": list[str]}
""" """
@@ -311,7 +374,7 @@ class ChatgptService:
try: try:
json_data = json.loads(json_match.group(1)) json_data = json.loads(json_match.group(1))
tags = json_data.get("tags", []) tags = json_data.get("tags", [])
facilities = json_data.get("facilities", []) print(f"[parse_marketing_analysis] GPT 응답에서 tags 파싱 완료: {tags}")
# JSON 블록을 제외한 리포트 부분 추출 # JSON 블록을 제외한 리포트 부분 추출
report = raw_response[: json_match.start()].strip() report = raw_response[: json_match.start()].strip()
# --- 구분자 제거 # --- 구분자 제거
@@ -320,10 +383,22 @@ class ChatgptService:
if report.endswith("---"): if report.endswith("---"):
report = report[:-3].strip() report = report[:-3].strip()
except json.JSONDecodeError: except json.JSONDecodeError:
print("[parse_marketing_analysis] JSON 파싱 실패")
pass pass
# 크롤링에서 가져온 facility_info로 facilities 설정
print(f"[parse_marketing_analysis] 크롤링 facility_info 원본: {facility_info}")
if facility_info:
# 쉼표로 구분된 편의시설 문자열을 리스트로 변환
facilities = [f.strip() for f in facility_info.split(",") if f.strip()]
print(f"[parse_marketing_analysis] facility_info 파싱 결과: {facilities}")
else:
facilities = ["등록된 정보 없음"]
print("[parse_marketing_analysis] facility_info 없음 - '등록된 정보 없음' 설정")
# 리포트 내용을 500자로 요약 # 리포트 내용을 500자로 요약
if report: if report:
report = await self.summarize_marketing(report) report = await self.summarize_marketing(report)
print(f"[parse_marketing_analysis] 최종 facilities: {facilities}")
return {"report": report, "tags": tags, "facilities": facilities} return {"report": report, "tags": tags, "facilities": facilities}
+178 -58
View File
@@ -13,31 +13,89 @@ creatomate = CreatomateService()
# 또는 명시적으로 API 키 전달 # 또는 명시적으로 API 키 전달
creatomate = CreatomateService(api_key="your_api_key") creatomate = CreatomateService(api_key="your_api_key")
# 템플릿 목록 조회 # 템플릿 목록 조회 (비동기)
templates = creatomate.get_all_templates_data() templates = await creatomate.get_all_templates_data()
# 특정 템플릿 조회 # 특정 템플릿 조회 (비동기)
template = creatomate.get_one_template_data(template_id) template = await creatomate.get_one_template_data(template_id)
# 영상 렌더링 요청 # 영상 렌더링 요청 (비동기)
response = creatomate.make_creatomate_call(template_id, modifications) response = await creatomate.make_creatomate_call(template_id, modifications)
``` ```
## 성능 최적화
- 템플릿 캐싱: 템플릿 데이터는 메모리에 캐싱되어 반복 조회 시 API 호출을 줄입니다.
- HTTP 클라이언트 재사용: 모듈 레벨의 공유 클라이언트로 커넥션 풀을 재사용합니다.
- 캐시 만료: 기본 5분 후 자동 만료 (CACHE_TTL_SECONDS로 조정 가능)
""" """
import copy import copy
import logging
import time
from typing import Literal from typing import Literal
import httpx import httpx
from config import apikey_settings, creatomate_settings from config import apikey_settings, creatomate_settings
# 로거 설정
logger = logging.getLogger(__name__)
# Orientation 타입 정의 # Orientation 타입 정의
OrientationType = Literal["horizontal", "vertical"] OrientationType = Literal["horizontal", "vertical"]
# =============================================================================
# 모듈 레벨 캐시 및 HTTP 클라이언트 (싱글톤 패턴)
# =============================================================================
# 템플릿 캐시: {template_id: {"data": dict, "cached_at": float}}
_template_cache: dict[str, dict] = {}
# 캐시 TTL (초) - 기본 5분
CACHE_TTL_SECONDS = 300
# 모듈 레벨 공유 HTTP 클라이언트 (커넥션 풀 재사용)
_shared_client: httpx.AsyncClient | None = None
async def get_shared_client() -> httpx.AsyncClient:
"""공유 HTTP 클라이언트를 반환합니다. 없으면 생성합니다."""
global _shared_client
if _shared_client is None or _shared_client.is_closed:
_shared_client = httpx.AsyncClient(
timeout=httpx.Timeout(60.0, connect=10.0),
limits=httpx.Limits(max_keepalive_connections=10, max_connections=20),
)
return _shared_client
async def close_shared_client() -> None:
"""공유 HTTP 클라이언트를 닫습니다. 앱 종료 시 호출하세요."""
global _shared_client
if _shared_client is not None and not _shared_client.is_closed:
await _shared_client.aclose()
_shared_client = None
print("[CreatomateService] Shared HTTP client closed")
def clear_template_cache() -> None:
"""템플릿 캐시를 전체 삭제합니다."""
global _template_cache
_template_cache.clear()
print("[CreatomateService] Template cache cleared")
def _is_cache_valid(cached_at: float) -> bool:
"""캐시가 유효한지 확인합니다."""
return (time.time() - cached_at) < CACHE_TTL_SECONDS
class CreatomateService: class CreatomateService:
"""Creatomate API를 통한 영상 생성 서비스""" """Creatomate API를 통한 영상 생성 서비스
모든 HTTP 호출 메서드는 비동기(async)로 구현되어 있습니다.
"""
BASE_URL = "https://api.creatomate.com" BASE_URL = "https://api.creatomate.com"
@@ -71,36 +129,116 @@ class CreatomateService:
self.orientation = orientation self.orientation = orientation
# orientation에 따른 템플릿 설정 가져오기 # orientation에 따른 템플릿 설정 가져오기
config = self.TEMPLATE_CONFIG.get(orientation, self.TEMPLATE_CONFIG["vertical"]) config = self.TEMPLATE_CONFIG.get(
orientation, self.TEMPLATE_CONFIG["vertical"]
)
self.template_id = config["template_id"] self.template_id = config["template_id"]
self.target_duration = target_duration if target_duration is not None else config["duration"] self.target_duration = (
target_duration if target_duration is not None else config["duration"]
)
self.headers = { self.headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}", "Authorization": f"Bearer {self.api_key}",
} }
def get_all_templates_data(self) -> dict: async def _request(
self,
method: str,
url: str,
timeout: float = 30.0,
**kwargs,
) -> httpx.Response:
"""HTTP 요청을 수행합니다.
Args:
method: HTTP 메서드 ("GET", "POST", etc.)
url: 요청 URL
timeout: 요청 타임아웃 (초)
**kwargs: httpx 요청에 전달할 추가 인자
Returns:
httpx.Response: 응답 객체
Raises:
httpx.HTTPError: 요청 실패 시
"""
logger.info(f"[Creatomate] {method} {url}")
print(f"[Creatomate] {method} {url}")
client = await get_shared_client()
if method.upper() == "GET":
response = await client.get(
url, headers=self.headers, timeout=timeout, **kwargs
)
elif method.upper() == "POST":
response = await client.post(
url, headers=self.headers, timeout=timeout, **kwargs
)
else:
raise ValueError(f"Unsupported HTTP method: {method}")
logger.info(f"[Creatomate] Response - Status: {response.status_code}")
print(f"[Creatomate] Response - Status: {response.status_code}")
return response
async def get_all_templates_data(self) -> dict:
"""모든 템플릿 정보를 조회합니다.""" """모든 템플릿 정보를 조회합니다."""
url = f"{self.BASE_URL}/v1/templates" url = f"{self.BASE_URL}/v1/templates"
response = httpx.get(url, headers=self.headers, timeout=30.0) response = await self._request("GET", url, timeout=30.0)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
def get_one_template_data(self, template_id: str) -> dict: async def get_one_template_data(
"""특정 템플릿 ID로 템플릿 정보를 조회합니다.""" self,
template_id: str,
use_cache: bool = True,
) -> dict:
"""특정 템플릿 ID로 템플릿 정보를 조회합니다.
Args:
template_id: 조회할 템플릿 ID
use_cache: 캐시 사용 여부 (기본: True)
Returns:
템플릿 데이터 (deep copy)
"""
global _template_cache
# 캐시 확인
if use_cache and template_id in _template_cache:
cached = _template_cache[template_id]
if _is_cache_valid(cached["cached_at"]):
print(f"[CreatomateService] Cache HIT - {template_id}")
return copy.deepcopy(cached["data"])
else:
# 만료된 캐시 삭제
del _template_cache[template_id]
print(f"[CreatomateService] Cache EXPIRED - {template_id}")
# API 호출
url = f"{self.BASE_URL}/v1/templates/{template_id}" url = f"{self.BASE_URL}/v1/templates/{template_id}"
response = httpx.get(url, headers=self.headers, timeout=30.0) response = await self._request("GET", url, timeout=30.0)
response.raise_for_status() response.raise_for_status()
return response.json() data = response.json()
# 캐시 저장
_template_cache[template_id] = {
"data": data,
"cached_at": time.time(),
}
print(f"[CreatomateService] Cache MISS - {template_id} (cached)")
return copy.deepcopy(data)
# 하위 호환성을 위한 별칭 (deprecated)
async def get_one_template_data_async(self, template_id: str) -> dict: async def get_one_template_data_async(self, template_id: str) -> dict:
"""특정 템플릿 ID로 템플릿 정보를 비동기로 조회합니다.""" """특정 템플릿 ID로 템플릿 정보를 조회합니다.
url = f"{self.BASE_URL}/v1/templates/{template_id}"
async with httpx.AsyncClient() as client: Deprecated: get_one_template_data()를 사용하세요.
response = await client.get(url, headers=self.headers, timeout=30.0) """
response.raise_for_status() return await self.get_one_template_data(template_id)
return response.json()
def parse_template_component_name(self, template_source: list) -> dict: def parse_template_component_name(self, template_source: list) -> dict:
"""템플릿 정보를 파싱하여 리소스 이름을 추출합니다.""" """템플릿 정보를 파싱하여 리소스 이름을 추출합니다."""
@@ -129,7 +267,7 @@ class CreatomateService:
return result return result
def template_connect_resource_blackbox( async def template_connect_resource_blackbox(
self, self,
template_id: str, template_id: str,
image_url_list: list[str], image_url_list: list[str],
@@ -143,7 +281,7 @@ class CreatomateService:
- 가사는 개행마다 한 텍스트 삽입 - 가사는 개행마다 한 텍스트 삽입
- Template에 audio-music 항목이 있어야 함 - Template에 audio-music 항목이 있어야 함
""" """
template_data = self.get_one_template_data(template_id) template_data = await self.get_one_template_data(template_id)
template_component_data = self.parse_template_component_name( template_component_data = self.parse_template_component_name(
template_data["source"]["elements"] template_data["source"]["elements"]
) )
@@ -223,7 +361,9 @@ class CreatomateService:
return elements return elements
def make_creatomate_call(self, template_id: str, modifications: dict): async def make_creatomate_call(
self, template_id: str, modifications: dict
) -> dict:
"""Creatomate에 렌더링 요청을 보냅니다. """Creatomate에 렌더링 요청을 보냅니다.
Note: Note:
@@ -234,34 +374,30 @@ class CreatomateService:
"template_id": template_id, "template_id": template_id,
"modifications": modifications, "modifications": modifications,
} }
response = httpx.post(url, json=data, headers=self.headers, timeout=60.0) response = await self._request("POST", url, timeout=60.0, json=data)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
def make_creatomate_custom_call(self, source: dict): async def make_creatomate_custom_call(self, source: dict) -> dict:
"""템플릿 없이 Creatomate에 커스텀 렌더링 요청을 보냅니다. """템플릿 없이 Creatomate에 커스텀 렌더링 요청을 보냅니다.
Note: Note:
response에 요청 정보가 있으니 폴링 필요 response에 요청 정보가 있으니 폴링 필요
""" """
url = f"{self.BASE_URL}/v2/renders" url = f"{self.BASE_URL}/v2/renders"
response = httpx.post(url, json=source, headers=self.headers, timeout=60.0) response = await self._request("POST", url, timeout=60.0, json=source)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
async def make_creatomate_custom_call_async(self, source: dict): # 하위 호환성을 위한 별칭 (deprecated)
"""템플릿 없이 Creatomate에 비동기로 커스텀 렌더링 요청을 보냅니다. async def make_creatomate_custom_call_async(self, source: dict) -> dict:
"""템플릿 없이 Creatomate에 커스텀 렌더링 요청을 보냅니다.
Note: Deprecated: make_creatomate_custom_call()을 사용하세요.
response에 요청 정보가 있으니 폴링 필요
""" """
url = f"{self.BASE_URL}/v2/renders" return await self.make_creatomate_custom_call(source)
async with httpx.AsyncClient() as client:
response = await client.post(url, json=source, headers=self.headers, timeout=60.0)
response.raise_for_status()
return response.json()
def get_render_status(self, render_id: str) -> dict: async def get_render_status(self, render_id: str) -> dict:
"""렌더링 작업의 상태를 조회합니다. """렌더링 작업의 상태를 조회합니다.
Args: Args:
@@ -280,33 +416,17 @@ class CreatomateService:
- failed: 실패 - failed: 실패
""" """
url = f"{self.BASE_URL}/v1/renders/{render_id}" url = f"{self.BASE_URL}/v1/renders/{render_id}"
response = httpx.get(url, headers=self.headers, timeout=30.0) response = await self._request("GET", url, timeout=30.0)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
# 하위 호환성을 위한 별칭 (deprecated)
async def get_render_status_async(self, render_id: str) -> dict: async def get_render_status_async(self, render_id: str) -> dict:
"""렌더링 작업의 상태를 비동기로 조회합니다. """렌더링 작업의 상태를 조회합니다.
Args: Deprecated: get_render_status()를 사용하세요.
render_id: Creatomate 렌더 ID
Returns:
렌더링 상태 정보
Note:
상태 값:
- planned: 예약됨
- waiting: 대기 중
- transcribing: 트랜스크립션 중
- rendering: 렌더링 중
- succeeded: 성공
- failed: 실패
""" """
url = f"{self.BASE_URL}/v1/renders/{render_id}" return await self.get_render_status(render_id)
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=self.headers, timeout=30.0)
response.raise_for_status()
return response.json()
def calc_scene_duration(self, template: dict) -> float: def calc_scene_duration(self, template: dict) -> float:
"""템플릿의 전체 장면 duration을 계산합니다.""" """템플릿의 전체 장면 duration을 계산합니다."""
+113
View File
@@ -0,0 +1,113 @@
import asyncio
from playwright.async_api import async_playwright
from urllib import parse
class nvMapPwScraper():
# cls vars
is_ready = False
_playwright = None
_browser = None
_context = None
_win_width = 1280
_win_height = 720
_max_retry = 30 # place id timeout threshold seconds
# instance var
page = None
@classmethod
def default_context_builder(cls):
context_builder_dict = {}
context_builder_dict['viewport'] = {
'width' : cls._win_width,
'height' : cls._win_height
}
context_builder_dict['screen'] = {
'width' : cls._win_width,
'height' : cls._win_height
}
context_builder_dict['user_agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
context_builder_dict['locale'] = 'ko-KR'
context_builder_dict['timezone_id']='Asia/Seoul'
return context_builder_dict
@classmethod
async def initiate_scraper(cls):
if not cls._playwright:
cls._playwright = await async_playwright().start()
if not cls._browser:
cls._browser = await cls._playwright.chromium.launch(headless=True)
if not cls._context:
cls._context = await cls._browser.new_context(**cls.default_context_builder())
cls.is_ready = True
def __init__(self):
if not self.is_ready:
raise Exception("nvMapScraper is not initiated")
async def __aenter__(self):
await self.create_page()
return self
async def __aexit__(self, exc_type, exc, tb):
await self.page.close()
async def create_page(self):
self.page = await self._context.new_page()
await self.page.add_init_script(
'''const defaultGetter = Object.getOwnPropertyDescriptor(
Navigator.prototype,
"webdriver"
).get;
defaultGetter.apply(navigator);
defaultGetter.toString();
Object.defineProperty(Navigator.prototype, "webdriver", {
set: undefined,
enumerable: true,
configurable: true,
get: new Proxy(defaultGetter, {
apply: (target, thisArg, args) => {
Reflect.apply(target, thisArg, args);
return false;
},
}),
});
const patchedGetter = Object.getOwnPropertyDescriptor(
Navigator.prototype,
"webdriver"
).get;
patchedGetter.apply(navigator);
patchedGetter.toString();''')
await self.page.set_extra_http_headers({
'sec-ch-ua': '\"Not?A_Brand\";v=\"99\", \"Chromium\";v=\"130\"'
})
await self.page.goto("http://google.com")
async def goto_url(self, url, wait_until="domcontentloaded", timeout=20000):
page = self.page
await page.goto(url, wait_until=wait_until, timeout=timeout)
async def get_place_id_url(self, selected):
title = selected['title'].replace("<b>", "").replace("</b>", "")
address = selected.get('roadAddress', selected['address']).replace("<b>", "").replace("</b>", "")
encoded_query = parse.quote(f"{address} {title}")
url = f"https://map.naver.com/p/search/{encoded_query}"
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
if "/place/" in self.page.url:
return self.page.url
url = self.page.url.replace("?","?isCorrectAnswer=true&")
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
if "/place/" in self.page.url:
return self.page.url
if (count == self._max_retry / 2):
raise Exception("Failed to identify place id. loading timeout")
else:
raise Exception("Failed to identify place id. item is ambiguous")
+95 -6
View File
@@ -1,17 +1,35 @@
import asyncio
import json import json
import logging
import re import re
import aiohttp import aiohttp
import bs4
from config import crawler_settings from config import crawler_settings
# 로거 설정
logger = logging.getLogger(__name__)
class GraphQLException(Exception): class GraphQLException(Exception):
"""GraphQL 요청 실패 시 발생하는 예외"""
pass
class CrawlingTimeoutException(Exception):
"""크롤링 타임아웃 시 발생하는 예외"""
pass pass
class NvMapScraper: class NvMapScraper:
"""네이버 지도 GraphQL API 스크래퍼
네이버 지도에서 숙소/장소 정보를 크롤링합니다.
"""
GRAPHQL_URL: str = "https://pcmap-api.place.naver.com/graphql" GRAPHQL_URL: str = "https://pcmap-api.place.naver.com/graphql"
REQUEST_TIMEOUT = 120 # 초
OVERVIEW_QUERY: str = """ OVERVIEW_QUERY: str = """
query getAccommodation($id: String!, $deviceType: String) { query getAccommodation($id: String!, $deviceType: String) {
@@ -49,6 +67,7 @@ query getAccommodation($id: String!, $deviceType: String) {
self.rawdata: dict | None = None self.rawdata: dict | None = None
self.image_link_list: list[str] | None = None self.image_link_list: list[str] | None = None
self.base_info: dict | None = None self.base_info: dict | None = None
self.facility_info: str | None = None
def _get_request_headers(self) -> dict: def _get_request_headers(self) -> dict:
headers = self.DEFAULT_HEADERS.copy() headers = self.DEFAULT_HEADERS.copy()
@@ -56,8 +75,19 @@ query getAccommodation($id: String!, $deviceType: String) {
headers["Cookie"] = self.cookies headers["Cookie"] = self.cookies
return headers return headers
def parse_url(self) -> str: async def parse_url(self) -> str:
"""URL에서 place ID를 추출합니다. 단축 URL인 경우 실제 URL로 변환합니다."""
place_pattern = r"/place/(\d+)" place_pattern = r"/place/(\d+)"
# URL에 place가 없는 경우 단축 URL 처리
if "place" not in self.url:
if "naver.me" in self.url:
async with aiohttp.ClientSession() as session:
async with session.get(self.url) as response:
self.url = str(response.url)
else:
raise GraphQLException("This URL does not contain a place ID")
match = re.search(place_pattern, self.url) match = re.search(place_pattern, self.url)
if not match: if not match:
raise GraphQLException("Failed to parse place ID from URL") raise GraphQLException("Failed to parse place ID from URL")
@@ -65,14 +95,17 @@ query getAccommodation($id: String!, $deviceType: String) {
async def scrap(self): async def scrap(self):
try: try:
place_id = self.parse_url() place_id = await self.parse_url()
data = await self._call_get_accommodation(place_id) data = await self._call_get_accommodation(place_id)
self.rawdata = data self.rawdata = data
fac_data = await self._get_facility_string(place_id)
self.rawdata["facilities"] = fac_data
self.image_link_list = [ self.image_link_list = [
nv_image["origin"] nv_image["origin"]
for nv_image in data["data"]["business"]["images"]["images"] for nv_image in data["data"]["business"]["images"]["images"]
] ]
self.base_info = data["data"]["business"]["base"] self.base_info = data["data"]["business"]["base"]
self.facility_info = fac_data
self.scrap_type = "GraphQL" self.scrap_type = "GraphQL"
except GraphQLException: except GraphQLException:
@@ -83,25 +116,81 @@ query getAccommodation($id: String!, $deviceType: String) {
return return
async def _call_get_accommodation(self, place_id: str) -> dict: async def _call_get_accommodation(self, place_id: str) -> dict:
"""GraphQL API를 호출하여 숙소 정보를 가져옵니다.
Args:
place_id: 네이버 지도 장소 ID
Returns:
GraphQL 응답 데이터
Raises:
GraphQLException: API 호출 실패 시
CrawlingTimeoutException: 타임아웃 발생 시
"""
payload = { payload = {
"operationName": "getAccommodation", "operationName": "getAccommodation",
"variables": {"id": place_id, "deviceType": "pc"}, "variables": {"id": place_id, "deviceType": "pc"},
"query": self.OVERVIEW_QUERY, "query": self.OVERVIEW_QUERY,
} }
json_payload = json.dumps(payload) json_payload = json.dumps(payload)
timeout = aiohttp.ClientTimeout(total=self.REQUEST_TIMEOUT)
async with aiohttp.ClientSession() as session: try:
logger.info(f"[NvMapScraper] Requesting place_id: {place_id}")
print(f"[NvMapScraper] Requesting place_id: {place_id}")
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.post( async with session.post(
self.GRAPHQL_URL, data=json_payload, headers=self._get_request_headers() self.GRAPHQL_URL,
data=json_payload,
headers=self._get_request_headers()
) as response: ) as response:
if response.status == 200: if response.status == 200:
logger.info(f"[NvMapScraper] SUCCESS - place_id: {place_id}")
print(f"[NvMapScraper] SUCCESS - place_id: {place_id}")
return await response.json() return await response.json()
else:
print("실패 상태 코드:", response.status) # 실패 상태 코드
logger.error(f"[NvMapScraper] Failed with status {response.status} - place_id: {place_id}")
print(f"[NvMapScraper] 실패 상태 코드: {response.status}")
raise GraphQLException( raise GraphQLException(
f"Request failed with status {response.status}" f"Request failed with status {response.status}"
) )
except (TimeoutError, asyncio.TimeoutError):
logger.error(f"[NvMapScraper] Timeout - place_id: {place_id}")
print(f"[NvMapScraper] Timeout - place_id: {place_id}")
raise CrawlingTimeoutException(f"Request timed out after {self.REQUEST_TIMEOUT}s")
except aiohttp.ClientError as e:
logger.error(f"[NvMapScraper] Client error: {e}")
print(f"[NvMapScraper] Client error: {e}")
raise GraphQLException(f"Client error: {e}")
async def _get_facility_string(self, place_id: str) -> str | None:
"""숙소 페이지에서 편의시설 정보를 크롤링합니다.
Args:
place_id: 네이버 지도 장소 ID
Returns:
편의시설 정보 문자열 또는 None
"""
url = f"https://pcmap.place.naver.com/accommodation/{place_id}/home"
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=self._get_request_headers()) as response:
soup = bs4.BeautifulSoup(await response.read(), "html.parser")
c_elem = soup.find("span", "place_blind", string="편의")
if c_elem:
facilities = c_elem.parent.parent.find("div").string
return facilities
return None
except Exception as e:
logger.warning(f"[NvMapScraper] Failed to get facility info: {e}")
return None
# if __name__ == "__main__": # if __name__ == "__main__":
# import asyncio # import asyncio
+174 -64
View File
@@ -20,13 +20,20 @@ URL 경로 형식:
success = await uploader.upload_image(file_path="my_image.png") success = await uploader.upload_image(file_path="my_image.png")
# 바이트 데이터로 직접 업로드 (media 저장 없이) # 바이트 데이터로 직접 업로드 (media 저장 없이)
success = await uploader.upload_music_bytes(audio_bytes, "my_song") # .mp3 자동 추가 success = await uploader.upload_music_bytes(audio_bytes, "my_song")
success = await uploader.upload_video_bytes(video_bytes, "my_video") # .mp4 자동 추가 success = await uploader.upload_video_bytes(video_bytes, "my_video")
success = await uploader.upload_image_bytes(image_bytes, "my_image.png") success = await uploader.upload_image_bytes(image_bytes, "my_image.png")
print(uploader.public_url) # 마지막 업로드의 공개 URL print(uploader.public_url) # 마지막 업로드의 공개 URL
성능 최적화:
- HTTP 클라이언트 재사용: 모듈 레벨의 공유 클라이언트로 커넥션 풀 재사용
- 동시 업로드: 공유 클라이언트를 통해 동시 요청 처리가 개선됩니다.
""" """
import asyncio
import logging
import time
from pathlib import Path from pathlib import Path
import aiofiles import aiofiles
@@ -34,6 +41,39 @@ import httpx
from config import azure_blob_settings from config import azure_blob_settings
# 로거 설정
logger = logging.getLogger(__name__)
# =============================================================================
# 모듈 레벨 공유 HTTP 클라이언트 (싱글톤 패턴)
# =============================================================================
# 모듈 레벨 공유 HTTP 클라이언트 (커넥션 풀 재사용)
_shared_blob_client: httpx.AsyncClient | None = None
async def get_shared_blob_client() -> httpx.AsyncClient:
"""공유 HTTP 클라이언트를 반환합니다. 없으면 생성합니다."""
global _shared_blob_client
if _shared_blob_client is None or _shared_blob_client.is_closed:
print("[AzureBlobUploader] Creating shared HTTP client...")
_shared_blob_client = httpx.AsyncClient(
timeout=httpx.Timeout(180.0, connect=10.0),
limits=httpx.Limits(max_keepalive_connections=10, max_connections=20),
)
print("[AzureBlobUploader] Shared HTTP client created - "
"max_connections: 20, max_keepalive: 10")
return _shared_blob_client
async def close_shared_blob_client() -> None:
"""공유 HTTP 클라이언트를 닫습니다. 앱 종료 시 호출하세요."""
global _shared_blob_client
if _shared_blob_client is not None and not _shared_blob_client.is_closed:
await _shared_blob_client.aclose()
_shared_blob_client = None
print("[AzureBlobUploader] Shared HTTP client closed")
class AzureBlobUploader: class AzureBlobUploader:
"""Azure Blob Storage 업로드 클래스 """Azure Blob Storage 업로드 클래스
@@ -85,12 +125,97 @@ class AzureBlobUploader:
"""업로드 URL 생성 (SAS 토큰 포함)""" """업로드 URL 생성 (SAS 토큰 포함)"""
# SAS 토큰 앞뒤의 ?, ', " 제거 # SAS 토큰 앞뒤의 ?, ', " 제거
sas_token = self._sas_token.strip("?'\"") sas_token = self._sas_token.strip("?'\"")
return f"{self._base_url}/{self._task_id}/{category}/{file_name}?{sas_token}" return (
f"{self._base_url}/{self._task_id}/{category}/{file_name}?{sas_token}"
)
def _build_public_url(self, category: str, file_name: str) -> str: def _build_public_url(self, category: str, file_name: str) -> str:
"""공개 URL 생성 (SAS 토큰 제외)""" """공개 URL 생성 (SAS 토큰 제외)"""
return f"{self._base_url}/{self._task_id}/{category}/{file_name}" return f"{self._base_url}/{self._task_id}/{category}/{file_name}"
async def _upload_bytes(
self,
file_content: bytes,
upload_url: str,
headers: dict,
timeout: float,
log_prefix: str,
) -> bool:
"""바이트 데이터를 업로드하는 공통 내부 메서드
Args:
file_content: 업로드할 바이트 데이터
upload_url: 업로드 URL
headers: HTTP 헤더
timeout: 요청 타임아웃 (초)
log_prefix: 로그 접두사
Returns:
bool: 업로드 성공 여부
"""
size = len(file_content)
start_time = time.perf_counter()
try:
logger.info(f"[{log_prefix}] Starting upload")
print(f"[{log_prefix}] Getting shared client...")
client = await get_shared_blob_client()
client_time = time.perf_counter()
elapsed_ms = (client_time - start_time) * 1000
print(f"[{log_prefix}] Client acquired in {elapsed_ms:.1f}ms")
print(f"[{log_prefix}] Starting upload... "
f"(size: {size} bytes, timeout: {timeout}s)")
response = await asyncio.wait_for(
client.put(upload_url, content=file_content, headers=headers),
timeout=timeout,
)
upload_time = time.perf_counter()
duration_ms = (upload_time - start_time) * 1000
if response.status_code in [200, 201]:
logger.info(f"[{log_prefix}] SUCCESS - Status: {response.status_code}")
print(f"[{log_prefix}] SUCCESS - Status: {response.status_code}, "
f"Duration: {duration_ms:.1f}ms")
print(f"[{log_prefix}] Public URL: {self._last_public_url}")
return True
# 업로드 실패
logger.error(f"[{log_prefix}] FAILED - Status: {response.status_code}")
print(f"[{log_prefix}] FAILED - Status: {response.status_code}, "
f"Duration: {duration_ms:.1f}ms")
print(f"[{log_prefix}] Response: {response.text[:500]}")
return False
except asyncio.TimeoutError:
elapsed = time.perf_counter() - start_time
logger.error(f"[{log_prefix}] TIMEOUT after {elapsed:.1f}s")
print(f"[{log_prefix}] TIMEOUT after {elapsed:.1f}s")
return False
except httpx.ConnectError as e:
elapsed = time.perf_counter() - start_time
logger.error(f"[{log_prefix}] CONNECT_ERROR: {e}")
print(f"[{log_prefix}] CONNECT_ERROR after {elapsed:.1f}s - "
f"{type(e).__name__}: {e}")
return False
except httpx.ReadError as e:
elapsed = time.perf_counter() - start_time
logger.error(f"[{log_prefix}] READ_ERROR: {e}")
print(f"[{log_prefix}] READ_ERROR after {elapsed:.1f}s - "
f"{type(e).__name__}: {e}")
return False
except Exception as e:
elapsed = time.perf_counter() - start_time
logger.error(f"[{log_prefix}] ERROR: {type(e).__name__}: {e}")
print(f"[{log_prefix}] ERROR after {elapsed:.1f}s - "
f"{type(e).__name__}: {e}")
return False
async def _upload_file( async def _upload_file(
self, self,
file_path: str, file_path: str,
@@ -116,27 +241,21 @@ class AzureBlobUploader:
upload_url = self._build_upload_url(category, file_name) upload_url = self._build_upload_url(category, file_name)
self._last_public_url = self._build_public_url(category, file_name) self._last_public_url = self._build_public_url(category, file_name)
print(f"[{log_prefix}] Upload URL (without SAS): {self._last_public_url}") print(f"[{log_prefix}] URL (without SAS): {self._last_public_url}")
headers = {"Content-Type": content_type, "x-ms-blob-type": "BlockBlob"} headers = {"Content-Type": content_type, "x-ms-blob-type": "BlockBlob"}
async with aiofiles.open(file_path, "rb") as file: async with aiofiles.open(file_path, "rb") as file:
file_content = await file.read() file_content = await file.read()
async with httpx.AsyncClient() as client: return await self._upload_bytes(
response = await client.put( file_content=file_content,
upload_url, content=file_content, headers=headers, timeout=timeout upload_url=upload_url,
headers=headers,
timeout=timeout,
log_prefix=log_prefix,
) )
if response.status_code in [200, 201]:
print(f"[{log_prefix}] Success - Status Code: {response.status_code}")
print(f"[{log_prefix}] Public URL: {self._last_public_url}")
return True
else:
print(f"[{log_prefix}] Failed - Status Code: {response.status_code}")
print(f"[{log_prefix}] Response: {response.text}")
return False
async def upload_music(self, file_path: str) -> bool: async def upload_music(self, file_path: str) -> bool:
"""음악 파일을 Azure Blob Storage에 업로드합니다. """음악 파일을 Azure Blob Storage에 업로드합니다.
@@ -151,7 +270,7 @@ class AzureBlobUploader:
Example: Example:
uploader = AzureBlobUploader(task_id="task-123") uploader = AzureBlobUploader(task_id="task-123")
success = await uploader.upload_music(file_path="my_song.mp3") success = await uploader.upload_music(file_path="my_song.mp3")
print(uploader.public_url) # {BASE_URL}/task-123/song/my_song.mp3 print(uploader.public_url)
""" """
return await self._upload_file( return await self._upload_file(
file_path=file_path, file_path=file_path,
@@ -161,7 +280,9 @@ class AzureBlobUploader:
log_prefix="upload_music", log_prefix="upload_music",
) )
async def upload_music_bytes(self, file_content: bytes, file_name: str) -> bool: async def upload_music_bytes(
self, file_content: bytes, file_name: str
) -> bool:
"""음악 바이트 데이터를 Azure Blob Storage에 직접 업로드합니다. """음악 바이트 데이터를 Azure Blob Storage에 직접 업로드합니다.
URL 경로: {task_id}/song/{파일명} URL 경로: {task_id}/song/{파일명}
@@ -176,7 +297,7 @@ class AzureBlobUploader:
Example: Example:
uploader = AzureBlobUploader(task_id="task-123") uploader = AzureBlobUploader(task_id="task-123")
success = await uploader.upload_music_bytes(audio_bytes, "my_song") success = await uploader.upload_music_bytes(audio_bytes, "my_song")
print(uploader.public_url) # {BASE_URL}/task-123/song/my_song.mp3 print(uploader.public_url)
""" """
# 확장자가 없으면 .mp3 추가 # 확장자가 없으면 .mp3 추가
if not Path(file_name).suffix: if not Path(file_name).suffix:
@@ -184,24 +305,19 @@ class AzureBlobUploader:
upload_url = self._build_upload_url("song", file_name) upload_url = self._build_upload_url("song", file_name)
self._last_public_url = self._build_public_url("song", file_name) self._last_public_url = self._build_public_url("song", file_name)
print(f"[upload_music_bytes] Upload URL (without SAS): {self._last_public_url}") log_prefix = "upload_music_bytes"
print(f"[{log_prefix}] URL (without SAS): {self._last_public_url}")
headers = {"Content-Type": "audio/mpeg", "x-ms-blob-type": "BlockBlob"} headers = {"Content-Type": "audio/mpeg", "x-ms-blob-type": "BlockBlob"}
async with httpx.AsyncClient() as client: return await self._upload_bytes(
response = await client.put( file_content=file_content,
upload_url, content=file_content, headers=headers, timeout=120.0 upload_url=upload_url,
headers=headers,
timeout=120.0,
log_prefix=log_prefix,
) )
if response.status_code in [200, 201]:
print(f"[upload_music_bytes] Success - Status Code: {response.status_code}")
print(f"[upload_music_bytes] Public URL: {self._last_public_url}")
return True
else:
print(f"[upload_music_bytes] Failed - Status Code: {response.status_code}")
print(f"[upload_music_bytes] Response: {response.text}")
return False
async def upload_video(self, file_path: str) -> bool: async def upload_video(self, file_path: str) -> bool:
"""영상 파일을 Azure Blob Storage에 업로드합니다. """영상 파일을 Azure Blob Storage에 업로드합니다.
@@ -216,7 +332,7 @@ class AzureBlobUploader:
Example: Example:
uploader = AzureBlobUploader(task_id="task-123") uploader = AzureBlobUploader(task_id="task-123")
success = await uploader.upload_video(file_path="my_video.mp4") success = await uploader.upload_video(file_path="my_video.mp4")
print(uploader.public_url) # {BASE_URL}/task-123/video/my_video.mp4 print(uploader.public_url)
""" """
return await self._upload_file( return await self._upload_file(
file_path=file_path, file_path=file_path,
@@ -226,7 +342,9 @@ class AzureBlobUploader:
log_prefix="upload_video", log_prefix="upload_video",
) )
async def upload_video_bytes(self, file_content: bytes, file_name: str) -> bool: async def upload_video_bytes(
self, file_content: bytes, file_name: str
) -> bool:
"""영상 바이트 데이터를 Azure Blob Storage에 직접 업로드합니다. """영상 바이트 데이터를 Azure Blob Storage에 직접 업로드합니다.
URL 경로: {task_id}/video/{파일명} URL 경로: {task_id}/video/{파일명}
@@ -241,7 +359,7 @@ class AzureBlobUploader:
Example: Example:
uploader = AzureBlobUploader(task_id="task-123") uploader = AzureBlobUploader(task_id="task-123")
success = await uploader.upload_video_bytes(video_bytes, "my_video") success = await uploader.upload_video_bytes(video_bytes, "my_video")
print(uploader.public_url) # {BASE_URL}/task-123/video/my_video.mp4 print(uploader.public_url)
""" """
# 확장자가 없으면 .mp4 추가 # 확장자가 없으면 .mp4 추가
if not Path(file_name).suffix: if not Path(file_name).suffix:
@@ -249,24 +367,19 @@ class AzureBlobUploader:
upload_url = self._build_upload_url("video", file_name) upload_url = self._build_upload_url("video", file_name)
self._last_public_url = self._build_public_url("video", file_name) self._last_public_url = self._build_public_url("video", file_name)
print(f"[upload_video_bytes] Upload URL (without SAS): {self._last_public_url}") log_prefix = "upload_video_bytes"
print(f"[{log_prefix}] URL (without SAS): {self._last_public_url}")
headers = {"Content-Type": "video/mp4", "x-ms-blob-type": "BlockBlob"} headers = {"Content-Type": "video/mp4", "x-ms-blob-type": "BlockBlob"}
async with httpx.AsyncClient() as client: return await self._upload_bytes(
response = await client.put( file_content=file_content,
upload_url, content=file_content, headers=headers, timeout=180.0 upload_url=upload_url,
headers=headers,
timeout=180.0,
log_prefix=log_prefix,
) )
if response.status_code in [200, 201]:
print(f"[upload_video_bytes] Success - Status Code: {response.status_code}")
print(f"[upload_video_bytes] Public URL: {self._last_public_url}")
return True
else:
print(f"[upload_video_bytes] Failed - Status Code: {response.status_code}")
print(f"[upload_video_bytes] Response: {response.text}")
return False
async def upload_image(self, file_path: str) -> bool: async def upload_image(self, file_path: str) -> bool:
"""이미지 파일을 Azure Blob Storage에 업로드합니다. """이미지 파일을 Azure Blob Storage에 업로드합니다.
@@ -281,7 +394,7 @@ class AzureBlobUploader:
Example: Example:
uploader = AzureBlobUploader(task_id="task-123") uploader = AzureBlobUploader(task_id="task-123")
success = await uploader.upload_image(file_path="my_image.png") success = await uploader.upload_image(file_path="my_image.png")
print(uploader.public_url) # {BASE_URL}/task-123/image/my_image.png print(uploader.public_url)
""" """
extension = Path(file_path).suffix.lower() extension = Path(file_path).suffix.lower()
content_type = self.IMAGE_CONTENT_TYPES.get(extension, "image/jpeg") content_type = self.IMAGE_CONTENT_TYPES.get(extension, "image/jpeg")
@@ -294,7 +407,9 @@ class AzureBlobUploader:
log_prefix="upload_image", log_prefix="upload_image",
) )
async def upload_image_bytes(self, file_content: bytes, file_name: str) -> bool: async def upload_image_bytes(
self, file_content: bytes, file_name: str
) -> bool:
"""이미지 바이트 데이터를 Azure Blob Storage에 직접 업로드합니다. """이미지 바이트 데이터를 Azure Blob Storage에 직접 업로드합니다.
URL 경로: {task_id}/image/{파일명} URL 경로: {task_id}/image/{파일명}
@@ -311,31 +426,26 @@ class AzureBlobUploader:
with open("my_image.png", "rb") as f: with open("my_image.png", "rb") as f:
content = f.read() content = f.read()
success = await uploader.upload_image_bytes(content, "my_image.png") success = await uploader.upload_image_bytes(content, "my_image.png")
print(uploader.public_url) # {BASE_URL}/task-123/image/my_image.png print(uploader.public_url)
""" """
extension = Path(file_name).suffix.lower() extension = Path(file_name).suffix.lower()
content_type = self.IMAGE_CONTENT_TYPES.get(extension, "image/jpeg") content_type = self.IMAGE_CONTENT_TYPES.get(extension, "image/jpeg")
upload_url = self._build_upload_url("image", file_name) upload_url = self._build_upload_url("image", file_name)
self._last_public_url = self._build_public_url("image", file_name) self._last_public_url = self._build_public_url("image", file_name)
print(f"[upload_image_bytes] Upload URL (without SAS): {self._last_public_url}") log_prefix = "upload_image_bytes"
print(f"[{log_prefix}] URL (without SAS): {self._last_public_url}")
headers = {"Content-Type": content_type, "x-ms-blob-type": "BlockBlob"} headers = {"Content-Type": content_type, "x-ms-blob-type": "BlockBlob"}
async with httpx.AsyncClient() as client: return await self._upload_bytes(
response = await client.put( file_content=file_content,
upload_url, content=file_content, headers=headers, timeout=60.0 upload_url=upload_url,
headers=headers,
timeout=60.0,
log_prefix=log_prefix,
) )
if response.status_code in [200, 201]:
print(f"[upload_image_bytes] Success - Status Code: {response.status_code}")
print(f"[upload_image_bytes] Public URL: {self._last_public_url}")
return True
else:
print(f"[upload_image_bytes] Failed - Status Code: {response.status_code}")
print(f"[upload_image_bytes] Response: {response.text}")
return False
# 사용 예시: # 사용 예시:
# import asyncio # import asyncio
BIN
View File
Binary file not shown.
+243 -112
View File
@@ -14,7 +14,9 @@ Video API Router
app.include_router(router, prefix="/api/v1") app.include_router(router, prefix="/api/v1")
""" """
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException from typing import Literal
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query
from sqlalchemy import func, select from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -23,13 +25,12 @@ from app.dependencies.pagination import (
PaginationParams, PaginationParams,
get_pagination_params, get_pagination_params,
) )
from app.home.models import Project from app.home.models import Image, Project
from app.lyric.models import Lyric from app.lyric.models import Lyric
from app.song.models import Song from app.song.models import Song
from app.video.models import Video from app.video.models import Video
from app.video.schemas.video_schema import ( from app.video.schemas.video_schema import (
DownloadVideoResponse, DownloadVideoResponse,
GenerateVideoRequest,
GenerateVideoResponse, GenerateVideoResponse,
PollingVideoResponse, PollingVideoResponse,
VideoListItem, VideoListItem,
@@ -43,20 +44,23 @@ from app.utils.pagination import PaginatedResponse
router = APIRouter(prefix="/video", tags=["video"]) router = APIRouter(prefix="/video", tags=["video"])
@router.post( @router.get(
"/generate/{task_id}", "/generate/{task_id}",
summary="영상 생성 요청", summary="영상 생성 요청",
description=""" description="""
Creatomate API를 통해 영상 생성을 요청합니다. Creatomate API를 통해 영상 생성을 요청합니다.
## 경로 파라미터 ## 경로 파라미터
- **task_id**: Project/Lyric/Song의 task_id (필수) - 연관된 프로젝트, 가사, 노래를 조회하는 데 사용 - **task_id**: Project/Lyric/Song/Image의 task_id (필수) - 연관된 프로젝트, 가사, 노래, 이미지를 조회하는 데 사용
## 요청 필드 ## 쿼리 파라미터
- **orientation**: 영상 방향 (horizontal: 가로형, vertical: 세로형, 기본값: vertical) - 선택 - **orientation**: 영상 방향 (horizontal: 가로형, vertical: 세로형, 기본값: vertical) - 선택
- **image_urls**: 영상에 사용할 이미지 URL 목록 (필수)
- **lyrics**: 영상에 표시할 가사 (필수) ## 자동 조회 정보
- **music_url**: 배경 음악 URL (필수) - **image_urls**: Image 테이블에서 task_id로 조회 (img_order 순서로 정렬)
- **music_url**: Song 테이블의 song_result_url 사용
- **duration**: Song 테이블의 duration 사용
- **lyrics**: Song 테이블의 song_prompt (가사) 사용
## 반환 정보 ## 반환 정보
- **success**: 요청 성공 여부 - **success**: 요청 성공 여부
@@ -66,142 +70,224 @@ Creatomate API를 통해 영상 생성을 요청합니다.
## 사용 예시 ## 사용 예시
``` ```
POST /video/generate/019123ab-cdef-7890-abcd-ef1234567890 GET /video/generate/0694b716-dbff-7219-8000-d08cb5fce431
{ GET /video/generate/0694b716-dbff-7219-8000-d08cb5fce431?orientation=horizontal
"image_urls": [
"https://naverbooking-phinf.pstatic.net/20240514_189/1715688030436xT14o_JPEG/1.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_48/1715688030574wTtQd_JPEG/2.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_92/17156880307484bvpH_JPEG/3.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_7/1715688031000y8Y5q_JPEG/4.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_259/17156880311809wCnY_JPEG/5.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_64/1715688031601oGNsV_JPEG/6.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_175/1715688031657oXc7l_JPEG/7.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_192/1715688031798MbFDj_JPEG/8.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_205/17156880318681JLwX_JPEG/9.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_142/1715688031946hhxHz_JPEG/10.jpg"
],
"lyrics": "가사 내용...",
"music_url": "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/0694e2d8-7ae2-730c-8000-308aacaa582d/song/스테이 머뭄.mp3"
}
```
## 가로형 영상 생성 예시
```
POST /video/generate/019123ab-cdef-7890-abcd-ef1234567890
{
"orientation": "horizontal",
"image_urls": [...],
"lyrics": "가사 내용...",
"music_url": "https://..."
}
``` ```
## 참고 ## 참고
- 이미지는 task_id로 Image 테이블에서 자동 조회됩니다 (img_order 순서).
- 배경 음악(music_url), 영상 길이(duration), 가사(lyrics)는 task_id로 Song 테이블을 조회하여 자동으로 가져옵니다.
- 같은 task_id로 여러 Song이 있을 경우 **가장 최근 생성된 노래**를 사용합니다.
- Song의 song_result_url과 song_prompt가 있어야 영상 생성이 가능합니다.
- creatomate_render_id를 사용하여 /status/{creatomate_render_id} 엔드포인트에서 생성 상태를 확인할 수 있습니다. - creatomate_render_id를 사용하여 /status/{creatomate_render_id} 엔드포인트에서 생성 상태를 확인할 수 있습니다.
- Video 테이블에 데이터가 저장되며, project_id, lyric_id, song_id가 자동으로 연결됩니다. - Video 테이블에 데이터가 저장되며, project_id, lyric_id, song_id가 자동으로 연결됩니다.
""", """,
response_model=GenerateVideoResponse, response_model=GenerateVideoResponse,
responses={ responses={
200: {"description": "영상 생성 요청 성공"}, 200: {"description": "영상 생성 요청 성공"},
404: {"description": "Project, Lyric 또는 Song을 찾을 수 없음"}, 400: {"description": "Song의 음악 URL, 가사(song_prompt) 또는 이미지가 없음"},
404: {"description": "Project, Lyric, Song 또는 Image를 찾을 수 없음"},
500: {"description": "영상 생성 요청 실패"}, 500: {"description": "영상 생성 요청 실패"},
}, },
) )
async def generate_video( async def generate_video(
task_id: str, task_id: str,
request_body: GenerateVideoRequest, orientation: Literal["horizontal", "vertical"] = Query(
session: AsyncSession = Depends(get_session), default="vertical",
description="영상 방향 (horizontal: 가로형, vertical: 세로형)",
),
) -> GenerateVideoResponse: ) -> GenerateVideoResponse:
"""Creatomate API를 통해 영상을 생성합니다. """Creatomate API를 통해 영상을 생성합니다.
1. task_id로 Project, Lyric, Song 조회 1. task_id로 Project, Lyric, Song, Image 순차 조회
2. Video 테이블에 초기 데이터 저장 (status: processing) 2. Video 테이블에 초기 데이터 저장 (status: processing)
3. Creatomate API 호출 (orientation에 따른 템플릿 자동 선택) 3. Creatomate API 호출 (orientation에 따른 템플릿 자동 선택)
4. creatomate_render_id 업데이트 후 응답 반환 4. creatomate_render_id 업데이트 후 응답 반환
Note: 이 함수는 Depends(get_session)을 사용하지 않고 명시적으로 세션을 관리합니다.
외부 API 호출 중 DB 커넥션이 유지되지 않도록 하여 커넥션 타임아웃 문제를 방지합니다.
중요: SQLAlchemy AsyncSession은 단일 세션에서 동시에 여러 쿼리를 실행하는 것을
지원하지 않습니다. asyncio.gather()로 병렬 쿼리를 실행하면 세션 상태 충돌이 발생합니다.
따라서 쿼리는 순차적으로 실행합니다.
""" """
print(f"[generate_video] START - task_id: {task_id}, orientation: {request_body.orientation}") import time
from app.database.session import AsyncSessionLocal
request_start = time.perf_counter()
print(f"[generate_video] START - task_id: {task_id}, orientation: {orientation}")
# ==========================================================================
# 1단계: DB 조회 및 초기 데이터 저장 (세션을 명시적으로 열고 닫음)
# ==========================================================================
# 외부 API 호출 전에 필요한 데이터를 저장할 변수들
project_id: int | None = None
lyric_id: int | None = None
song_id: int | None = None
video_id: int | None = None
music_url: str | None = None
song_duration: float | None = None
lyrics: str | None = None
image_urls: list[str] = []
try: try:
# 1. task_id로 Project 조회 # 세션을 명시적으로 열고 DB 작업 후 바로 닫음
async with AsyncSessionLocal() as session:
# ===== 순차 쿼리 실행: Project, Lyric, Song, Image =====
# Note: AsyncSession은 동일 세션에서 병렬 쿼리를 지원하지 않음
# Project 조회
project_result = await session.execute( project_result = await session.execute(
select(Project).where(Project.task_id == task_id) select(Project)
.where(Project.task_id == task_id)
.order_by(Project.created_at.desc())
.limit(1)
) )
project = project_result.scalar_one_or_none()
if not project: # Lyric 조회
print(f"[generate_video] Project NOT FOUND - task_id: {task_id}")
raise HTTPException(
status_code=404,
detail=f"task_id '{task_id}'에 해당하는 Project를 찾을 수 없습니다.",
)
print(f"[generate_video] Project found - project_id: {project.id}, task_id: {task_id}")
# 2. task_id로 Lyric 조회
lyric_result = await session.execute( lyric_result = await session.execute(
select(Lyric).where(Lyric.task_id == task_id) select(Lyric)
.where(Lyric.task_id == task_id)
.order_by(Lyric.created_at.desc())
.limit(1)
) )
lyric = lyric_result.scalar_one_or_none()
if not lyric: # Song 조회
print(f"[generate_video] Lyric NOT FOUND - task_id: {task_id}")
raise HTTPException(
status_code=404,
detail=f"task_id '{task_id}'에 해당하는 Lyric을 찾을 수 없습니다.",
)
print(f"[generate_video] Lyric found - lyric_id: {lyric.id}, task_id: {task_id}")
# 3. task_id로 Song 조회 (가장 최근 것)
song_result = await session.execute( song_result = await session.execute(
select(Song) select(Song)
.where(Song.task_id == task_id) .where(Song.task_id == task_id)
.order_by(Song.created_at.desc()) .order_by(Song.created_at.desc())
.limit(1) .limit(1)
) )
song = song_result.scalar_one_or_none()
# Image 조회
image_result = await session.execute(
select(Image)
.where(Image.task_id == task_id)
.order_by(Image.img_order.asc())
)
query_time = time.perf_counter()
print(f"[generate_video] Queries completed - task_id: {task_id}, "
f"elapsed: {(query_time - request_start)*1000:.1f}ms")
# ===== 결과 처리: Project =====
project = project_result.scalar_one_or_none()
if not project:
print(f"[generate_video] Project NOT FOUND - task_id: {task_id}")
raise HTTPException(
status_code=404,
detail=f"task_id '{task_id}'에 해당하는 Project를 찾을 수 없습니다.",
)
project_id = project.id
# ===== 결과 처리: Lyric =====
lyric = lyric_result.scalar_one_or_none()
if not lyric:
print(f"[generate_video] Lyric NOT FOUND - task_id: {task_id}")
raise HTTPException(
status_code=404,
detail=f"task_id '{task_id}'에 해당하는 Lyric을 찾을 수 없습니다.",
)
lyric_id = lyric.id
# ===== 결과 처리: Song =====
song = song_result.scalar_one_or_none()
if not song: if not song:
print(f"[generate_video] Song NOT FOUND - task_id: {task_id}") print(f"[generate_video] Song NOT FOUND - task_id: {task_id}")
raise HTTPException( raise HTTPException(
status_code=404, status_code=404,
detail=f"task_id '{task_id}'에 해당하는 Song을 찾을 수 없습니다.", detail=f"task_id '{task_id}'에 해당하는 Song을 찾을 수 없습니다.",
) )
print(f"[generate_video] Song found - song_id: {song.id}, task_id: {task_id}, duration: {song.duration}")
# 4. Video 테이블에 초기 데이터 저장 song_id = song.id
music_url = song.song_result_url
song_duration = song.duration
lyrics = song.song_prompt
if not music_url:
raise HTTPException(
status_code=400,
detail=f"Song(id={song_id})의 음악 URL이 없습니다.",
)
if not lyrics:
raise HTTPException(
status_code=400,
detail=f"Song(id={song_id})의 가사(song_prompt)가 없습니다.",
)
# ===== 결과 처리: Image =====
images = image_result.scalars().all()
if not images:
print(f"[generate_video] Image NOT FOUND - task_id: {task_id}")
raise HTTPException(
status_code=404,
detail=f"task_id '{task_id}'에 해당하는 이미지를 찾을 수 없습니다.",
)
image_urls = [img.img_url for img in images]
print(
f"[generate_video] Data loaded - task_id: {task_id}, "
f"project_id: {project_id}, lyric_id: {lyric_id}, "
f"song_id: {song_id}, images: {len(image_urls)}"
)
# ===== Video 테이블에 초기 데이터 저장 및 커밋 =====
video = Video( video = Video(
project_id=project.id, project_id=project_id,
lyric_id=lyric.id, lyric_id=lyric_id,
song_id=song.id, song_id=song_id,
task_id=task_id, task_id=task_id,
creatomate_render_id=None, creatomate_render_id=None,
status="processing", status="processing",
) )
session.add(video) session.add(video)
await session.flush() # ID 생성을 위해 flush await session.commit()
print(f"[generate_video] Video saved (processing) - task_id: {task_id}") video_id = video.id
stage1_time = time.perf_counter()
print(f"[generate_video] Video saved - task_id: {task_id}, id: {video_id}, "
f"stage1_elapsed: {(stage1_time - request_start)*1000:.1f}ms")
# 세션이 여기서 자동으로 닫힘 (async with 블록 종료)
# 5. Creatomate API 호출 (POC 패턴 적용) except HTTPException:
print(f"[generate_video] Creatomate API generation started - task_id: {task_id}") raise
# orientation에 따른 템플릿 선택, duration은 Song에서 가져옴 (없으면 config 기본값 사용) except Exception as e:
creatomate_service = CreatomateService( print(f"[generate_video] DB EXCEPTION - task_id: {task_id}, error: {e}")
orientation=request_body.orientation, return GenerateVideoResponse(
target_duration=song.duration, # Song의 duration 사용 (None이면 config 기본값) success=False,
task_id=task_id,
creatomate_render_id=None,
message="영상 생성 요청에 실패했습니다.",
error_message=str(e),
) )
print(f"[generate_video] Using template_id: {creatomate_service.template_id}, duration: {creatomate_service.target_duration} (song duration: {song.duration})")
# 5-1. 템플릿 조회 (비동기, CreatomateService에서 orientation에 맞는 template_id 사용) # ==========================================================================
# 2단계: 외부 API 호출 (세션 사용 안함 - 커넥션 풀 점유 없음)
# ==========================================================================
stage2_start = time.perf_counter()
try:
print(f"[generate_video] Stage 2 START - Creatomate API - task_id: {task_id}")
creatomate_service = CreatomateService(
orientation=orientation,
target_duration=song_duration,
)
print(f"[generate_video] Using template_id: {creatomate_service.template_id}, duration: {creatomate_service.target_duration} (song duration: {song_duration})")
# 6-1. 템플릿 조회 (비동기)
template = await creatomate_service.get_one_template_data_async(creatomate_service.template_id) template = await creatomate_service.get_one_template_data_async(creatomate_service.template_id)
print(f"[generate_video] Template fetched - task_id: {task_id}") print(f"[generate_video] Template fetched - task_id: {task_id}")
# 5-2. elements에서 리소스 매핑 생성 # 6-2. elements에서 리소스 매핑 생성
modifications = creatomate_service.elements_connect_resource_blackbox( modifications = creatomate_service.elements_connect_resource_blackbox(
elements=template["source"]["elements"], elements=template["source"]["elements"],
image_url_list=request_body.image_urls, image_url_list=image_urls,
lyric=request_body.lyrics, lyric=lyrics,
music_url=request_body.music_url, music_url=music_url,
) )
print(f"[generate_video] Modifications created - task_id: {task_id}") print(f"[generate_video] Modifications created - task_id: {task_id}")
# 5-3. elements 수정 # 6-3. elements 수정
new_elements = creatomate_service.modify_element( new_elements = creatomate_service.modify_element(
template["source"]["elements"], template["source"]["elements"],
modifications, modifications,
@@ -209,20 +295,20 @@ async def generate_video(
template["source"]["elements"] = new_elements template["source"]["elements"] = new_elements
print(f"[generate_video] Elements modified - task_id: {task_id}") print(f"[generate_video] Elements modified - task_id: {task_id}")
# 5-4. duration 확장 (target_duration: 영상 길이) # 6-4. duration 확장
final_template = creatomate_service.extend_template_duration( final_template = creatomate_service.extend_template_duration(
template, template,
creatomate_service.target_duration, creatomate_service.target_duration,
) )
print(f"[generate_video] Duration extended to {creatomate_service.target_duration}s - task_id: {task_id}") print(f"[generate_video] Duration extended to {creatomate_service.target_duration}s - task_id: {task_id}")
# 5-5. 커스텀 렌더링 요청 (비동기) # 6-5. 커스텀 렌더링 요청 (비동기)
render_response = await creatomate_service.make_creatomate_custom_call_async( render_response = await creatomate_service.make_creatomate_custom_call_async(
final_template["source"], final_template["source"],
) )
print(f"[generate_video] Creatomate API response - task_id: {task_id}, response: {render_response}") print(f"[generate_video] Creatomate API response - task_id: {task_id}, response: {render_response}")
# 렌더 ID 추출 (응답이 리스트인 경우 첫 번째 항목 사용) # 렌더 ID 추출
if isinstance(render_response, list) and len(render_response) > 0: if isinstance(render_response, list) and len(render_response) > 0:
creatomate_render_id = render_response[0].get("id") creatomate_render_id = render_response[0].get("id")
elif isinstance(render_response, dict): elif isinstance(render_response, dict):
@@ -230,10 +316,60 @@ async def generate_video(
else: else:
creatomate_render_id = None creatomate_render_id = None
# 6. creatomate_render_id 업데이트 stage2_time = time.perf_counter()
video.creatomate_render_id = creatomate_render_id print(
await session.commit() f"[generate_video] Stage 2 DONE - task_id: {task_id}, "
print(f"[generate_video] SUCCESS - task_id: {task_id}, creatomate_render_id: {creatomate_render_id}") f"render_id: {creatomate_render_id}, "
f"stage2_elapsed: {(stage2_time - stage2_start)*1000:.1f}ms"
)
except Exception as e:
print(f"[generate_video] Creatomate API EXCEPTION - task_id: {task_id}, error: {e}")
# 외부 API 실패 시 Video 상태를 failed로 업데이트
from app.database.session import AsyncSessionLocal
async with AsyncSessionLocal() as update_session:
video_result = await update_session.execute(
select(Video).where(Video.id == video_id)
)
video_to_update = video_result.scalar_one_or_none()
if video_to_update:
video_to_update.status = "failed"
await update_session.commit()
return GenerateVideoResponse(
success=False,
task_id=task_id,
creatomate_render_id=None,
message="영상 생성 요청에 실패했습니다.",
error_message=str(e),
)
# ==========================================================================
# 3단계: creatomate_render_id 업데이트 (새 세션으로 빠르게 처리)
# ==========================================================================
stage3_start = time.perf_counter()
print(f"[generate_video] Stage 3 START - DB update - task_id: {task_id}")
try:
from app.database.session import AsyncSessionLocal
async with AsyncSessionLocal() as update_session:
video_result = await update_session.execute(
select(Video).where(Video.id == video_id)
)
video_to_update = video_result.scalar_one_or_none()
if video_to_update:
video_to_update.creatomate_render_id = creatomate_render_id
await update_session.commit()
stage3_time = time.perf_counter()
total_time = stage3_time - request_start
print(
f"[generate_video] Stage 3 DONE - task_id: {task_id}, "
f"stage3_elapsed: {(stage3_time - stage3_start)*1000:.1f}ms"
)
print(
f"[generate_video] SUCCESS - task_id: {task_id}, "
f"render_id: {creatomate_render_id}, "
f"total_time: {total_time*1000:.1f}ms"
)
return GenerateVideoResponse( return GenerateVideoResponse(
success=True, success=True,
@@ -243,16 +379,13 @@ async def generate_video(
error_message=None, error_message=None,
) )
except HTTPException:
raise
except Exception as e: except Exception as e:
print(f"[generate_video] EXCEPTION - task_id: {task_id}, error: {e}") print(f"[generate_video] Update EXCEPTION - task_id: {task_id}, error: {e}")
await session.rollback()
return GenerateVideoResponse( return GenerateVideoResponse(
success=False, success=False,
task_id=task_id, task_id=task_id,
creatomate_render_id=None, creatomate_render_id=creatomate_render_id,
message="영상 생성 요청에 실패했습니다.", message="영상 생성은 요청되었으나 DB 업데이트에 실패했습니다.",
error_message=str(e), error_message=str(e),
) )
@@ -569,14 +702,19 @@ async def get_videos(
result = await session.execute(query) result = await session.execute(query)
videos = result.scalars().all() videos = result.scalars().all()
# Project 정보와 함께 VideoListItem으로 변환 # Project 정보 일괄 조회 (N+1 문제 해결)
project_ids = [v.project_id for v in videos if v.project_id]
projects_map: dict = {}
if project_ids:
projects_result = await session.execute(
select(Project).where(Project.id.in_(project_ids))
)
projects_map = {p.id: p for p in projects_result.scalars().all()}
# VideoListItem으로 변환
items = [] items = []
for video in videos: for video in videos:
# Project 조회 (video.project_id 직접 사용) project = projects_map.get(video.project_id)
project_result = await session.execute(
select(Project).where(Project.id == video.project_id)
)
project = project_result.scalar_one_or_none()
item = VideoListItem( item = VideoListItem(
store_name=project.store_name if project else None, store_name=project.store_name if project else None,
@@ -587,13 +725,6 @@ async def get_videos(
) )
items.append(item) items.append(item)
# 개별 아이템 로그
print(
f"[get_videos] Item - store_name: {item.store_name}, region: {item.region}, "
f"task_id: {item.task_id}, result_movie_url: {item.result_movie_url}, "
f"created_at: {item.created_at}"
)
response = PaginatedResponse.create( response = PaginatedResponse.create(
items=items, items=items,
total=total, total=total,
+15 -64
View File
@@ -5,61 +5,9 @@ Video API Schemas
""" """
from datetime import datetime from datetime import datetime
from typing import Any, Dict, List, Literal, Optional from typing import Any, Dict, Literal, Optional
from pydantic import BaseModel, Field from pydantic import BaseModel, ConfigDict, Field
# =============================================================================
# Request Schemas
# =============================================================================
class GenerateVideoRequest(BaseModel):
"""영상 생성 요청 스키마
Usage:
POST /video/generate/{task_id}
Request body for generating a video via Creatomate API.
Example Request:
{
"template_id": "abc123...",
"image_urls": ["https://...", "https://..."],
"lyrics": "가사 내용...",
"music_url": "https://..."
}
"""
model_config = {
"json_schema_extra": {
"example": {
"orientation": "vertical",
"image_urls": [
"https://naverbooking-phinf.pstatic.net/20240514_189/1715688030436xT14o_JPEG/1.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_48/1715688030574wTtQd_JPEG/2.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_92/17156880307484bvpH_JPEG/3.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_7/1715688031000y8Y5q_JPEG/4.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_259/17156880311809wCnY_JPEG/5.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_64/1715688031601oGNsV_JPEG/6.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_175/1715688031657oXc7l_JPEG/7.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_192/1715688031798MbFDj_JPEG/8.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_205/17156880318681JLwX_JPEG/9.jpg",
"https://naverbooking-phinf.pstatic.net/20240514_142/1715688031946hhxHz_JPEG/10.jpg",
],
"lyrics": "인스타 감성의 스테이 머뭄, 머물러봐요\n군산 신흥동 말랭이 마을의 마음 힐링",
"music_url": "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/0694e2d8-7ae2-730c-8000-308aacaa582d/song/스테이 머뭄.mp3",
}
}
}
orientation: Literal["horizontal", "vertical"] = Field(
default="vertical",
description="영상 방향 (horizontal: 가로형, vertical: 세로형, 기본값: vertical)",
)
image_urls: List[str] = Field(..., description="영상에 사용할 이미지 URL 목록")
lyrics: str = Field(..., description="영상에 표시할 가사")
music_url: str = Field(..., description="배경 음악 URL")
# ============================================================================= # =============================================================================
@@ -71,19 +19,22 @@ class GenerateVideoResponse(BaseModel):
"""영상 생성 응답 스키마 """영상 생성 응답 스키마
Usage: Usage:
POST /video/generate/{task_id} GET /video/generate/{task_id}
Returns the task IDs for tracking video generation. Returns the task IDs for tracking video generation.
Example Response (Success):
{
"success": true,
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
"creatomate_render_id": "render-id-123",
"message": "영상 생성 요청이 접수되었습니다.",
"error_message": null
}
""" """
model_config = ConfigDict(
json_schema_extra={
"example": {
"success": True,
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
"creatomate_render_id": "render-id-123456",
"message": "영상 생성 요청이 접수되었습니다. creatomate_render_id로 상태를 조회하세요.",
"error_message": None,
}
}
)
success: bool = Field(..., description="요청 성공 여부") success: bool = Field(..., description="요청 성공 여부")
task_id: Optional[str] = Field(None, description="내부 작업 ID (Project task_id)") task_id: Optional[str] = Field(None, description="내부 작업 ID (Project task_id)")
creatomate_render_id: Optional[str] = Field(None, description="Creatomate 렌더 ID") creatomate_render_id: Optional[str] = Field(None, description="Creatomate 렌더 ID")
+1 -1
View File
@@ -783,7 +783,7 @@ async def make_automation(request: Request, conn: Connection):
else "", else "",
"attr_value": ", ".join(selected_values) if selected_values else "", "attr_value": ", ".join(selected_values) if selected_values else "",
"ai": "ChatGPT", "ai": "ChatGPT",
"ai_model": "gpt-4o", "ai_model": "gpt-5-mini",
"genre": "후크송", "genre": "후크송",
"sample_song": combined_sample_song or "없음", "sample_song": combined_sample_song or "없음",
"result_song": final_lyrics, "result_song": final_lyrics,
+161 -70
View File
@@ -4,16 +4,109 @@ Video Background Tasks
영상 생성 관련 백그라운드 태스크를 정의합니다. 영상 생성 관련 백그라운드 태스크를 정의합니다.
""" """
import logging
import traceback
from pathlib import Path from pathlib import Path
import aiofiles import aiofiles
import httpx import httpx
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from app.database.session import AsyncSessionLocal from app.database.session import BackgroundSessionLocal
from app.video.models import Video from app.video.models import Video
from app.utils.upload_blob_as_request import AzureBlobUploader from app.utils.upload_blob_as_request import AzureBlobUploader
# 로거 설정
logger = logging.getLogger(__name__)
# HTTP 요청 설정
REQUEST_TIMEOUT = 300.0 # 초 (영상은 용량이 크므로 5분)
async def _update_video_status(
task_id: str,
status: str,
video_url: str | None = None,
creatomate_render_id: str | None = None,
) -> bool:
"""Video 테이블의 상태를 업데이트합니다.
Args:
task_id: 프로젝트 task_id
status: 변경할 상태 ("processing", "completed", "failed")
video_url: 영상 URL
creatomate_render_id: Creatomate render ID (선택)
Returns:
bool: 업데이트 성공 여부
"""
try:
async with BackgroundSessionLocal() as session:
if creatomate_render_id:
query_result = await session.execute(
select(Video)
.where(Video.creatomate_render_id == creatomate_render_id)
.order_by(Video.created_at.desc())
.limit(1)
)
else:
query_result = await session.execute(
select(Video)
.where(Video.task_id == task_id)
.order_by(Video.created_at.desc())
.limit(1)
)
video = query_result.scalar_one_or_none()
if video:
video.status = status
if video_url is not None:
video.result_movie_url = video_url
await session.commit()
logger.info(f"[Video] Status updated - task_id: {task_id}, status: {status}")
print(f"[Video] Status updated - task_id: {task_id}, status: {status}")
return True
else:
logger.warning(f"[Video] NOT FOUND in DB - task_id: {task_id}")
print(f"[Video] NOT FOUND in DB - task_id: {task_id}")
return False
except SQLAlchemyError as e:
logger.error(f"[Video] DB Error while updating status - task_id: {task_id}, error: {e}")
print(f"[Video] DB Error while updating status - task_id: {task_id}, error: {e}")
return False
except Exception as e:
logger.error(f"[Video] Unexpected error while updating status - task_id: {task_id}, error: {e}")
print(f"[Video] Unexpected error while updating status - task_id: {task_id}, error: {e}")
return False
async def _download_video(url: str, task_id: str) -> bytes:
"""URL에서 영상을 다운로드합니다.
Args:
url: 다운로드할 URL
task_id: 로그용 task_id
Returns:
bytes: 다운로드한 파일 내용
Raises:
httpx.HTTPError: 다운로드 실패 시
"""
logger.info(f"[VideoDownload] Downloading - task_id: {task_id}")
print(f"[VideoDownload] Downloading - task_id: {task_id}")
async with httpx.AsyncClient() as client:
response = await client.get(url, timeout=REQUEST_TIMEOUT)
response.raise_for_status()
logger.info(f"[VideoDownload] SUCCESS - task_id: {task_id}, size: {len(response.content)} bytes")
print(f"[VideoDownload] SUCCESS - task_id: {task_id}, size: {len(response.content)} bytes")
return response.content
async def download_and_upload_video_to_blob( async def download_and_upload_video_to_blob(
task_id: str, task_id: str,
@@ -27,6 +120,7 @@ async def download_and_upload_video_to_blob(
video_url: 다운로드할 영상 URL video_url: 다운로드할 영상 URL
store_name: 저장할 파일명에 사용할 업체명 store_name: 저장할 파일명에 사용할 업체명
""" """
logger.info(f"[download_and_upload_video_to_blob] START - task_id: {task_id}, store_name: {store_name}")
print(f"[download_and_upload_video_to_blob] START - task_id: {task_id}, store_name: {store_name}") print(f"[download_and_upload_video_to_blob] START - task_id: {task_id}, store_name: {store_name}")
temp_file_path: Path | None = None temp_file_path: Path | None = None
@@ -42,16 +136,19 @@ async def download_and_upload_video_to_blob(
temp_dir = Path("media") / "temp" / task_id temp_dir = Path("media") / "temp" / task_id
temp_dir.mkdir(parents=True, exist_ok=True) temp_dir.mkdir(parents=True, exist_ok=True)
temp_file_path = temp_dir / file_name temp_file_path = temp_dir / file_name
logger.info(f"[download_and_upload_video_to_blob] Temp directory created - path: {temp_file_path}")
print(f"[download_and_upload_video_to_blob] Temp directory created - path: {temp_file_path}") print(f"[download_and_upload_video_to_blob] Temp directory created - path: {temp_file_path}")
# 영상 파일 다운로드 # 영상 파일 다운로드
logger.info(f"[download_and_upload_video_to_blob] Downloading video - task_id: {task_id}, url: {video_url}")
print(f"[download_and_upload_video_to_blob] Downloading video - task_id: {task_id}, url: {video_url}") print(f"[download_and_upload_video_to_blob] Downloading video - task_id: {task_id}, url: {video_url}")
async with httpx.AsyncClient() as client:
response = await client.get(video_url, timeout=180.0) content = await _download_video(video_url, task_id)
response.raise_for_status()
async with aiofiles.open(str(temp_file_path), "wb") as f: async with aiofiles.open(str(temp_file_path), "wb") as f:
await f.write(response.content) await f.write(content)
logger.info(f"[download_and_upload_video_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}")
print(f"[download_and_upload_video_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}") print(f"[download_and_upload_video_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}")
# Azure Blob Storage에 업로드 # Azure Blob Storage에 업로드
@@ -63,51 +160,41 @@ async def download_and_upload_video_to_blob(
# SAS 토큰이 제외된 public_url 사용 # SAS 토큰이 제외된 public_url 사용
blob_url = uploader.public_url blob_url = uploader.public_url
logger.info(f"[download_and_upload_video_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}")
print(f"[download_and_upload_video_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}") print(f"[download_and_upload_video_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}")
# Video 테이블 업데이트 (새 세션 사용) # Video 테이블 업데이트
async with AsyncSessionLocal() as session: await _update_video_status(task_id, "completed", blob_url)
# 여러 개 있을 경우 가장 최근 것 선택 logger.info(f"[download_and_upload_video_to_blob] SUCCESS - task_id: {task_id}")
result = await session.execute( print(f"[download_and_upload_video_to_blob] SUCCESS - task_id: {task_id}")
select(Video)
.where(Video.task_id == task_id)
.order_by(Video.created_at.desc())
.limit(1)
)
video = result.scalar_one_or_none()
if video: except httpx.HTTPError as e:
video.status = "completed" logger.error(f"[download_and_upload_video_to_blob] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
video.result_movie_url = blob_url print(f"[download_and_upload_video_to_blob] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
await session.commit() traceback.print_exc()
print(f"[download_and_upload_video_to_blob] SUCCESS - task_id: {task_id}, status: completed") await _update_video_status(task_id, "failed")
else:
print(f"[download_and_upload_video_to_blob] Video NOT FOUND in DB - task_id: {task_id}") except SQLAlchemyError as e:
logger.error(f"[download_and_upload_video_to_blob] DB ERROR - task_id: {task_id}, error: {e}")
print(f"[download_and_upload_video_to_blob] DB ERROR - task_id: {task_id}, error: {e}")
traceback.print_exc()
await _update_video_status(task_id, "failed")
except Exception as e: except Exception as e:
logger.error(f"[download_and_upload_video_to_blob] EXCEPTION - task_id: {task_id}, error: {e}")
print(f"[download_and_upload_video_to_blob] EXCEPTION - task_id: {task_id}, error: {e}") print(f"[download_and_upload_video_to_blob] EXCEPTION - task_id: {task_id}, error: {e}")
# 실패 시 Video 테이블 업데이트 traceback.print_exc()
async with AsyncSessionLocal() as session: await _update_video_status(task_id, "failed")
result = await session.execute(
select(Video)
.where(Video.task_id == task_id)
.order_by(Video.created_at.desc())
.limit(1)
)
video = result.scalar_one_or_none()
if video:
video.status = "failed"
await session.commit()
print(f"[download_and_upload_video_to_blob] FAILED - task_id: {task_id}, status updated to failed")
finally: finally:
# 임시 파일 삭제 # 임시 파일 삭제
if temp_file_path and temp_file_path.exists(): if temp_file_path and temp_file_path.exists():
try: try:
temp_file_path.unlink() temp_file_path.unlink()
logger.info(f"[download_and_upload_video_to_blob] Temp file deleted - path: {temp_file_path}")
print(f"[download_and_upload_video_to_blob] Temp file deleted - path: {temp_file_path}") print(f"[download_and_upload_video_to_blob] Temp file deleted - path: {temp_file_path}")
except Exception as e: except Exception as e:
logger.warning(f"[download_and_upload_video_to_blob] Failed to delete temp file: {e}")
print(f"[download_and_upload_video_to_blob] Failed to delete temp file: {e}") print(f"[download_and_upload_video_to_blob] Failed to delete temp file: {e}")
# 임시 디렉토리 삭제 시도 # 임시 디렉토리 삭제 시도
@@ -131,13 +218,14 @@ async def download_and_upload_video_by_creatomate_render_id(
video_url: 다운로드할 영상 URL video_url: 다운로드할 영상 URL
store_name: 저장할 파일명에 사용할 업체명 store_name: 저장할 파일명에 사용할 업체명
""" """
logger.info(f"[download_and_upload_video_by_creatomate_render_id] START - creatomate_render_id: {creatomate_render_id}, store_name: {store_name}")
print(f"[download_and_upload_video_by_creatomate_render_id] START - creatomate_render_id: {creatomate_render_id}, store_name: {store_name}") print(f"[download_and_upload_video_by_creatomate_render_id] START - creatomate_render_id: {creatomate_render_id}, store_name: {store_name}")
temp_file_path: Path | None = None temp_file_path: Path | None = None
task_id: str | None = None task_id: str | None = None
try: try:
# creatomate_render_id로 Video 조회하여 task_id 가져오기 # creatomate_render_id로 Video 조회하여 task_id 가져오기
async with AsyncSessionLocal() as session: async with BackgroundSessionLocal() as session:
result = await session.execute( result = await session.execute(
select(Video) select(Video)
.where(Video.creatomate_render_id == creatomate_render_id) .where(Video.creatomate_render_id == creatomate_render_id)
@@ -147,10 +235,12 @@ async def download_and_upload_video_by_creatomate_render_id(
video = result.scalar_one_or_none() video = result.scalar_one_or_none()
if not video: if not video:
logger.warning(f"[download_and_upload_video_by_creatomate_render_id] Video NOT FOUND - creatomate_render_id: {creatomate_render_id}")
print(f"[download_and_upload_video_by_creatomate_render_id] Video NOT FOUND - creatomate_render_id: {creatomate_render_id}") print(f"[download_and_upload_video_by_creatomate_render_id] Video NOT FOUND - creatomate_render_id: {creatomate_render_id}")
return return
task_id = video.task_id task_id = video.task_id
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Video found - creatomate_render_id: {creatomate_render_id}, task_id: {task_id}")
print(f"[download_and_upload_video_by_creatomate_render_id] Video found - creatomate_render_id: {creatomate_render_id}, task_id: {task_id}") print(f"[download_and_upload_video_by_creatomate_render_id] Video found - creatomate_render_id: {creatomate_render_id}, task_id: {task_id}")
# 파일명에 사용할 수 없는 문자 제거 # 파일명에 사용할 수 없는 문자 제거
@@ -164,16 +254,19 @@ async def download_and_upload_video_by_creatomate_render_id(
temp_dir = Path("media") / "temp" / task_id temp_dir = Path("media") / "temp" / task_id
temp_dir.mkdir(parents=True, exist_ok=True) temp_dir.mkdir(parents=True, exist_ok=True)
temp_file_path = temp_dir / file_name temp_file_path = temp_dir / file_name
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Temp directory created - path: {temp_file_path}")
print(f"[download_and_upload_video_by_creatomate_render_id] Temp directory created - path: {temp_file_path}") print(f"[download_and_upload_video_by_creatomate_render_id] Temp directory created - path: {temp_file_path}")
# 영상 파일 다운로드 # 영상 파일 다운로드
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Downloading video - creatomate_render_id: {creatomate_render_id}, url: {video_url}")
print(f"[download_and_upload_video_by_creatomate_render_id] Downloading video - creatomate_render_id: {creatomate_render_id}, url: {video_url}") print(f"[download_and_upload_video_by_creatomate_render_id] Downloading video - creatomate_render_id: {creatomate_render_id}, url: {video_url}")
async with httpx.AsyncClient() as client:
response = await client.get(video_url, timeout=180.0) content = await _download_video(video_url, task_id)
response.raise_for_status()
async with aiofiles.open(str(temp_file_path), "wb") as f: async with aiofiles.open(str(temp_file_path), "wb") as f:
await f.write(response.content) await f.write(content)
logger.info(f"[download_and_upload_video_by_creatomate_render_id] File downloaded - creatomate_render_id: {creatomate_render_id}, path: {temp_file_path}")
print(f"[download_and_upload_video_by_creatomate_render_id] File downloaded - creatomate_render_id: {creatomate_render_id}, path: {temp_file_path}") print(f"[download_and_upload_video_by_creatomate_render_id] File downloaded - creatomate_render_id: {creatomate_render_id}, path: {temp_file_path}")
# Azure Blob Storage에 업로드 # Azure Blob Storage에 업로드
@@ -185,51 +278,49 @@ async def download_and_upload_video_by_creatomate_render_id(
# SAS 토큰이 제외된 public_url 사용 # SAS 토큰이 제외된 public_url 사용
blob_url = uploader.public_url blob_url = uploader.public_url
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Uploaded to Blob - creatomate_render_id: {creatomate_render_id}, url: {blob_url}")
print(f"[download_and_upload_video_by_creatomate_render_id] Uploaded to Blob - creatomate_render_id: {creatomate_render_id}, url: {blob_url}") print(f"[download_and_upload_video_by_creatomate_render_id] Uploaded to Blob - creatomate_render_id: {creatomate_render_id}, url: {blob_url}")
# Video 테이블 업데이트 (새 세션 사용) # Video 테이블 업데이트
async with AsyncSessionLocal() as session: await _update_video_status(
result = await session.execute( task_id=task_id,
select(Video) status="completed",
.where(Video.creatomate_render_id == creatomate_render_id) video_url=blob_url,
.order_by(Video.created_at.desc()) creatomate_render_id=creatomate_render_id,
.limit(1)
) )
video = result.scalar_one_or_none() logger.info(f"[download_and_upload_video_by_creatomate_render_id] SUCCESS - creatomate_render_id: {creatomate_render_id}")
print(f"[download_and_upload_video_by_creatomate_render_id] SUCCESS - creatomate_render_id: {creatomate_render_id}")
if video: except httpx.HTTPError as e:
video.status = "completed" logger.error(f"[download_and_upload_video_by_creatomate_render_id] DOWNLOAD ERROR - creatomate_render_id: {creatomate_render_id}, error: {e}")
video.result_movie_url = blob_url print(f"[download_and_upload_video_by_creatomate_render_id] DOWNLOAD ERROR - creatomate_render_id: {creatomate_render_id}, error: {e}")
await session.commit() traceback.print_exc()
print(f"[download_and_upload_video_by_creatomate_render_id] SUCCESS - creatomate_render_id: {creatomate_render_id}, status: completed") if task_id:
else: await _update_video_status(task_id, "failed", creatomate_render_id=creatomate_render_id)
print(f"[download_and_upload_video_by_creatomate_render_id] Video NOT FOUND in DB - creatomate_render_id: {creatomate_render_id}")
except SQLAlchemyError as e:
logger.error(f"[download_and_upload_video_by_creatomate_render_id] DB ERROR - creatomate_render_id: {creatomate_render_id}, error: {e}")
print(f"[download_and_upload_video_by_creatomate_render_id] DB ERROR - creatomate_render_id: {creatomate_render_id}, error: {e}")
traceback.print_exc()
if task_id:
await _update_video_status(task_id, "failed", creatomate_render_id=creatomate_render_id)
except Exception as e: except Exception as e:
logger.error(f"[download_and_upload_video_by_creatomate_render_id] EXCEPTION - creatomate_render_id: {creatomate_render_id}, error: {e}")
print(f"[download_and_upload_video_by_creatomate_render_id] EXCEPTION - creatomate_render_id: {creatomate_render_id}, error: {e}") print(f"[download_and_upload_video_by_creatomate_render_id] EXCEPTION - creatomate_render_id: {creatomate_render_id}, error: {e}")
# 실패 시 Video 테이블 업데이트 traceback.print_exc()
if task_id: if task_id:
async with AsyncSessionLocal() as session: await _update_video_status(task_id, "failed", creatomate_render_id=creatomate_render_id)
result = await session.execute(
select(Video)
.where(Video.creatomate_render_id == creatomate_render_id)
.order_by(Video.created_at.desc())
.limit(1)
)
video = result.scalar_one_or_none()
if video:
video.status = "failed"
await session.commit()
print(f"[download_and_upload_video_by_creatomate_render_id] FAILED - creatomate_render_id: {creatomate_render_id}, status updated to failed")
finally: finally:
# 임시 파일 삭제 # 임시 파일 삭제
if temp_file_path and temp_file_path.exists(): if temp_file_path and temp_file_path.exists():
try: try:
temp_file_path.unlink() temp_file_path.unlink()
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Temp file deleted - path: {temp_file_path}")
print(f"[download_and_upload_video_by_creatomate_render_id] Temp file deleted - path: {temp_file_path}") print(f"[download_and_upload_video_by_creatomate_render_id] Temp file deleted - path: {temp_file_path}")
except Exception as e: except Exception as e:
logger.warning(f"[download_and_upload_video_by_creatomate_render_id] Failed to delete temp file: {e}")
print(f"[download_and_upload_video_by_creatomate_render_id] Failed to delete temp file: {e}") print(f"[download_and_upload_video_by_creatomate_render_id] Failed to delete temp file: {e}")
# 임시 디렉토리 삭제 시도 # 임시 디렉토리 삭제 시도
BIN
View File
Binary file not shown.
@@ -0,0 +1,792 @@
# O2O-CASTAD Backend 비동기 아키텍처 및 설계 분석 보고서
> **문서 버전**: 1.0
> **작성일**: 2025-12-29
> **대상**: 개발자, 아키텍트, 코드 리뷰어
---
## 목차
1. [Executive Summary](#1-executive-summary)
2. [데이터베이스 세션 관리 아키텍처](#2-데이터베이스-세션-관리-아키텍처)
3. [비동기 처리 패턴](#3-비동기-처리-패턴)
4. [외부 API 통합 설계](#4-외부-api-통합-설계)
5. [백그라운드 태스크 워크플로우](#5-백그라운드-태스크-워크플로우)
6. [쿼리 최적화 전략](#6-쿼리-최적화-전략)
7. [설계 강점 분석](#7-설계-강점-분석)
8. [개선 권장 사항](#8-개선-권장-사항)
9. [아키텍처 다이어그램](#9-아키텍처-다이어그램)
10. [결론](#10-결론)
---
## 1. Executive Summary
### 1.1 프로젝트 개요
O2O-CASTAD Backend는 FastAPI 기반의 비동기 백엔드 서비스로, AI 기반 광고 영상 자동 생성 파이프라인을 제공합니다. 주요 외부 서비스(Creatomate, Suno, ChatGPT, Azure Blob Storage)와의 통합을 통해 가사 생성 → 노래 생성 → 영상 생성의 파이프라인을 구현합니다.
### 1.2 주요 성과
| 영역 | 개선 전 | 개선 후 | 개선율 |
|------|---------|---------|--------|
| 템플릿 API 호출 | 매번 호출 (1-2s) | 캐시 HIT (0ms) | **100% 감소** |
| HTTP 클라이언트 | 매번 생성 (50ms) | 풀 재사용 (0ms) | **100% 감소** |
| 세션 타임아웃 에러 | 빈번 | 해결 | **안정성 확보** |
### 1.3 핵심 아키텍처 결정
1. **이중 커넥션 풀 아키텍처**: 요청/백그라운드 분리
2. **명시적 세션 라이프사이클**: 외부 API 호출 전 세션 해제
3. **모듈 레벨 싱글톤**: HTTP 클라이언트 및 템플릿 캐시
4. **순차 쿼리 실행**: AsyncSession 제약으로 단일 세션 내 병렬 쿼리 불가
---
## 2. 데이터베이스 세션 관리 아키텍처
### 2.1 이중 엔진 구조
```
┌─────────────────────────────────────────────────────────────┐
│ DATABASE LAYER │
├─────────────────────────────┬───────────────────────────────┤
│ MAIN ENGINE │ BACKGROUND ENGINE │
│ (FastAPI Requests) │ (Worker Tasks) │
├─────────────────────────────┼───────────────────────────────┤
│ pool_size: 20 │ pool_size: 10 │
│ max_overflow: 20 │ max_overflow: 10 │
│ pool_timeout: 30s │ pool_timeout: 60s │
│ Total: 최대 40 연결 │ Total: 최대 20 연결 │
├─────────────────────────────┼───────────────────────────────┤
│ AsyncSessionLocal │ BackgroundSessionLocal │
│ → Router endpoints │ → download_and_upload_* │
│ → Direct API calls │ → generate_lyric_background │
└─────────────────────────────┴───────────────────────────────┘
```
**위치**: `app/database/session.py`
### 2.2 엔진 설정 상세
```python
# 메인 엔진 (FastAPI 요청용)
engine = create_async_engine(
url=db_settings.MYSQL_URL,
pool_size=20, # 기본 풀 크기
max_overflow=20, # 추가 연결 허용
pool_timeout=30, # 연결 대기 최대 시간
pool_recycle=280, # MySQL wait_timeout 보다 짧게 설정
pool_pre_ping=True, # 연결 유효성 검사 (핵심!)
pool_reset_on_return="rollback", # 반환 시 롤백
)
# 백그라운드 엔진 (워커 태스크용)
background_engine = create_async_engine(
url=db_settings.MYSQL_URL,
pool_size=10, # 더 작은 풀
max_overflow=10,
pool_timeout=60, # 백그라운드는 대기 여유
pool_recycle=280, # MySQL wait_timeout 보다 짧게 설정
pool_pre_ping=True,
pool_reset_on_return="rollback", # 반환 시 롤백
)
```
### 2.3 세션 관리 패턴
#### 패턴 1: FastAPI 의존성 주입 (단순 CRUD)
```python
@router.get("/items/{id}")
async def get_item(
id: int,
session: AsyncSession = Depends(get_session),
):
result = await session.execute(select(Item).where(Item.id == id))
return result.scalar_one_or_none()
```
**적용 엔드포인트:**
- `GET /videos/` - 목록 조회
- `GET /video/download/{task_id}` - 상태 조회
- `GET /songs/` - 목록 조회
#### 패턴 2: 명시적 세션 관리 (외부 API 호출 포함)
```python
@router.get("/generate/{task_id}")
async def generate_video(task_id: str):
# 1단계: 명시적 세션 열기 → DB 작업 → 세션 닫기
async with AsyncSessionLocal() as session:
# 순차 쿼리 실행 (AsyncSession은 병렬 쿼리 미지원)
project = await session.execute(select(Project).where(...))
lyric = await session.execute(select(Lyric).where(...))
song = await session.execute(select(Song).where(...))
# 초기 데이터 저장
await session.commit()
# 세션 닫힘 (async with 블록 종료)
# 2단계: 외부 API 호출 (세션 없음 - 커넥션 점유 안함)
response = await creatomate_service.make_api_call()
# 3단계: 새 세션으로 업데이트
async with AsyncSessionLocal() as update_session:
video.render_id = response["id"]
await update_session.commit()
```
**적용 엔드포인트:**
- `GET /video/generate/{task_id}` - 영상 생성
- `GET /song/generate/{task_id}` - 노래 생성
#### 패턴 3: 백그라운드 태스크 세션
```python
async def download_and_upload_video_to_blob(task_id: str, ...):
# 백그라운드 전용 세션 팩토리 사용
async with BackgroundSessionLocal() as session:
result = await session.execute(...)
video.status = "completed"
await session.commit()
```
**적용 함수:**
- `download_and_upload_video_to_blob()`
- `download_and_upload_song_to_blob()`
- `generate_lyric_background()`
### 2.4 해결된 문제: 세션 타임아웃
**문제 상황:**
```
RuntimeError: unable to perform operation on
<TCPTransport closed=True reading=False ...>; the handler is closed
```
**원인:**
- `Depends(get_session)`으로 주입된 세션이 요청 전체 동안 유지
- 외부 API 호출 (수 초~수 분) 중 TCP 커넥션 타임아웃
- 요청 종료 시점에 이미 닫힌 커넥션 정리 시도
**해결책:**
```python
# 변경 전: 세션이 요청 전체 동안 유지
async def generate_video(session: AsyncSession = Depends(get_session)):
await session.execute(...) # DB 작업
await creatomate_api() # 외부 API (세션 유지됨 - 문제!)
await session.commit() # 타임아웃 에러 발생 가능
# 변경 후: 명시적 세션 관리
async def generate_video():
async with AsyncSessionLocal() as session:
await session.execute(...)
await session.commit()
# 세션 닫힘
await creatomate_api() # 외부 API (세션 없음 - 안전!)
async with AsyncSessionLocal() as session:
# 업데이트
```
---
## 3. 비동기 처리 패턴
### 3.1 순차 쿼리 실행 (AsyncSession 제약)
**위치**: `app/video/api/routers/v1/video.py`
> **중요**: SQLAlchemy AsyncSession은 단일 세션에서 동시에 여러 쿼리를 실행하는 것을 지원하지 않습니다.
> `asyncio.gather()`로 병렬 쿼리를 실행하면 세션 상태 충돌이 발생합니다.
```python
# 순차 쿼리 실행: Project, Lyric, Song, Image
# Note: AsyncSession은 동일 세션에서 병렬 쿼리를 지원하지 않음
# Project 조회
project_result = await session.execute(
select(Project).where(Project.task_id == task_id)
.order_by(Project.created_at.desc()).limit(1)
)
# Lyric 조회
lyric_result = await session.execute(
select(Lyric).where(Lyric.task_id == task_id)
.order_by(Lyric.created_at.desc()).limit(1)
)
# Song 조회
song_result = await session.execute(
select(Song).where(Song.task_id == task_id)
.order_by(Song.created_at.desc()).limit(1)
)
# Image 조회
image_result = await session.execute(
select(Image).where(Image.task_id == task_id)
.order_by(Image.img_order.asc())
)
```
**AsyncSession 병렬 쿼리 제약 사항:**
- 단일 세션 내에서 `asyncio.gather()`로 여러 쿼리 동시 실행 불가
- 세션 상태 충돌 및 예기치 않은 동작 발생 가능
- 병렬 쿼리가 필요한 경우 별도의 세션을 각각 생성해야 함
### 3.2 FastAPI BackgroundTasks 활용
```python
@router.post("/generate")
async def generate_lyric(
request_body: GenerateLyricRequest,
background_tasks: BackgroundTasks,
session: AsyncSession = Depends(get_session),
):
# 즉시 응답할 데이터 저장
lyric = Lyric(task_id=task_id, status="processing")
session.add(lyric)
await session.commit()
# 백그라운드 태스크 스케줄링
background_tasks.add_task(
generate_lyric_background,
task_id=task_id,
prompt=prompt,
language=request_body.language,
)
# 즉시 응답 반환
return GenerateLyricResponse(success=True, task_id=task_id)
```
### 3.3 비동기 컨텍스트 관리자
```python
# 앱 라이프사이클 관리
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
await create_db_tables()
print("Database tables created")
yield # 앱 실행
# Shutdown
await dispose_engine()
print("Database engines disposed")
```
---
## 4. 외부 API 통합 설계
### 4.1 Creatomate 서비스
**위치**: `app/utils/creatomate.py`
#### HTTP 클라이언트 싱글톤
```python
# 모듈 레벨 공유 클라이언트
_shared_client: httpx.AsyncClient | None = None
async def get_shared_client() -> httpx.AsyncClient:
global _shared_client
if _shared_client is None or _shared_client.is_closed:
_shared_client = httpx.AsyncClient(
timeout=httpx.Timeout(60.0, connect=10.0),
limits=httpx.Limits(
max_keepalive_connections=10,
max_connections=20,
),
)
return _shared_client
```
**장점:**
- 커넥션 풀 재사용으로 TCP handshake 오버헤드 제거
- Keep-alive로 연결 유지
- 앱 종료 시 `close_shared_client()` 호출로 정리
#### 템플릿 캐싱
```python
# 모듈 레벨 캐시
_template_cache: dict[str, dict] = {}
CACHE_TTL_SECONDS = 300 # 5분
async def get_one_template_data(self, template_id: str, use_cache: bool = True):
# 캐시 확인
if use_cache and template_id in _template_cache:
cached = _template_cache[template_id]
if _is_cache_valid(cached["cached_at"]):
print(f"[CreatomateService] Cache HIT - {template_id}")
return copy.deepcopy(cached["data"])
# API 호출 및 캐시 저장
data = await self._fetch_from_api(template_id)
_template_cache[template_id] = {
"data": data,
"cached_at": time.time(),
}
return copy.deepcopy(data)
```
**캐싱 전략:**
- 첫 번째 요청: API 호출 (1-2초)
- 이후 요청 (5분 내): 캐시 반환 (~0ms)
- TTL 만료 후: 자동 갱신
### 4.2 Suno 서비스
**위치**: `app/utils/suno.py`
```python
class SunoService:
async def generate_music(self, prompt: str, callback_url: str = None):
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/generate/music",
json={
"prompt": prompt,
"callback_url": callback_url,
},
timeout=60.0,
)
return response.json()
```
### 4.3 ChatGPT 서비스
**위치**: `app/utils/chatgpt_prompt.py`
```python
class ChatgptService:
async def generate(self, prompt: str) -> str:
# OpenAI API 호출
response = await self.client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content
```
### 4.4 Azure Blob Storage
**위치**: `app/utils/upload_blob_as_request.py`
```python
class AzureBlobUploader:
async def upload_video(self, file_path: str) -> bool:
# 비동기 업로드
async with aiofiles.open(file_path, "rb") as f:
content = await f.read()
# Blob 업로드 로직
return True
```
---
## 5. 백그라운드 태스크 워크플로우
### 5.1 3단계 워크플로우 패턴
```
┌─────────────────────────────────────────────────────────────────┐
│ REQUEST PHASE │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ API Request │───▶│ Save Initial │───▶│ Return Response │ │
│ │ │ │ Record │ │ (task_id) │ │
│ └──────────────┘ │ status= │ └──────────────────┘ │
│ │ "processing" │ │
│ └──────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ POLLING PHASE │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Client Polls │───▶│ Query │───▶│ Return Status │ │
│ │ /status/id │ │ External API │ │ + Trigger BG │ │
│ └──────────────┘ └──────────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│ (if status == "succeeded")
▼
┌─────────────────────────────────────────────────────────────────┐
│ BACKGROUND COMPLETION PHASE │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ Download │───▶│ Upload to │───▶│ Update DB │ │
│ │ Result File │ │ Azure Blob │ │ status=completed │ │
│ └──────────────┘ └──────────────┘ │ result_url=... │ │
│ └──────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```
### 5.2 영상 생성 플로우
```python
# 1단계: 즉시 응답
@router.get("/video/generate/{task_id}")
async def generate_video(task_id: str):
# DB에서 필요한 데이터 조회 (병렬)
# Video 레코드 생성 (status="processing")
# Creatomate API 호출 → render_id 획득
return {"success": True, "render_id": render_id}
# 2단계: 폴링
@router.get("/video/status/{render_id}")
async def get_video_status(render_id: str, background_tasks: BackgroundTasks):
status = await creatomate_service.get_render_status(render_id)
if status == "succeeded":
# 백그라운드 태스크 트리거
background_tasks.add_task(
download_and_upload_video_to_blob,
task_id=video.task_id,
video_url=status["url"],
)
return {"status": status}
# 3단계: 백그라운드 완료
async def download_and_upload_video_to_blob(task_id: str, video_url: str):
# 임시 파일 다운로드
# Azure Blob 업로드
# DB 업데이트 (BackgroundSessionLocal 사용)
# 임시 파일 삭제
```
### 5.3 에러 처리 전략
```python
async def download_and_upload_video_to_blob(task_id: str, video_url: str):
temp_file_path: Path | None = None
try:
# 다운로드 및 업로드 로직
...
async with BackgroundSessionLocal() as session:
video.status = "completed"
await session.commit()
except Exception as e:
print(f"[EXCEPTION] {task_id}: {e}")
# 실패 상태로 업데이트
async with BackgroundSessionLocal() as session:
video.status = "failed"
await session.commit()
finally:
# 임시 파일 정리 (항상 실행)
if temp_file_path and temp_file_path.exists():
temp_file_path.unlink()
# 임시 디렉토리 정리
temp_dir.rmdir()
```
---
## 6. 쿼리 최적화 전략
### 6.1 N+1 문제 해결
**문제 코드:**
```python
# 각 video마다 project를 개별 조회 (N+1)
for video in videos:
project = await session.execute(
select(Project).where(Project.id == video.project_id)
)
```
**해결 코드:**
```python
# 1. Video 목록 조회
videos = await session.execute(video_query)
video_list = videos.scalars().all()
# 2. Project ID 수집
project_ids = [v.project_id for v in video_list if v.project_id]
# 3. Project 일괄 조회 (IN 절)
projects_result = await session.execute(
select(Project).where(Project.id.in_(project_ids))
)
# 4. 딕셔너리로 매핑
projects_map = {p.id: p for p in projects_result.scalars().all()}
# 5. 조합
for video in video_list:
project = projects_map.get(video.project_id)
```
**위치**: `app/video/api/routers/v1/video.py` - `get_videos()`
### 6.2 서브쿼리를 활용한 중복 제거
```python
# task_id별 최신 Video의 id만 추출
subquery = (
select(func.max(Video.id).label("max_id"))
.where(Video.status == "completed")
.group_by(Video.task_id)
.subquery()
)
# 최신 Video만 조회
query = (
select(Video)
.where(Video.id.in_(select(subquery.c.max_id)))
.order_by(Video.created_at.desc())
)
```
---
## 7. 설계 강점 분석
### 7.1 안정성 (Stability)
| 요소 | 구현 | 효과 |
|------|------|------|
| pool_pre_ping | 쿼리 전 연결 검증 | Stale 커넥션 방지 |
| pool_reset_on_return | 반환 시 롤백 | 트랜잭션 상태 초기화 |
| 이중 커넥션 풀 | 요청/백그라운드 분리 | 리소스 경합 방지 |
| Finally 블록 | 임시 파일 정리 | 리소스 누수 방지 |
### 7.2 성능 (Performance)
| 요소 | 구현 | 효과 |
|------|------|------|
| 템플릿 캐싱 | TTL 기반 메모리 캐시 | API 호출 100% 감소 |
| HTTP 클라이언트 풀 | 싱글톤 패턴 | 커넥션 재사용 |
| N+1 해결 | IN 절 배치 조회 | 쿼리 수 N→2 감소 |
### 7.3 확장성 (Scalability)
| 요소 | 구현 | 효과 |
|------|------|------|
| 명시적 세션 관리 | 외부 API 시 세션 해제 | 커넥션 풀 점유 최소화 |
| 백그라운드 태스크 | FastAPI BackgroundTasks | 논블로킹 처리 |
| 폴링 패턴 | Status endpoint | 클라이언트 주도 동기화 |
### 7.4 유지보수성 (Maintainability)
| 요소 | 구현 | 효과 |
|------|------|------|
| 구조화된 로깅 | `[function_name]` prefix | 추적 용이 |
| 타입 힌트 | Python 3.11+ 문법 | IDE 지원, 버그 감소 |
| 문서화 | Docstring, 주석 | 코드 이해도 향상 |
---
## 8. 개선 권장 사항
### 8.1 Song 라우터 N+1 문제
**현재 상태** (`app/song/api/routers/v1/song.py`):
```python
# N+1 발생
for song in songs:
project_result = await session.execute(
select(Project).where(Project.id == song.project_id)
)
```
**권장 수정**:
```python
# video.py의 패턴 적용
project_ids = [s.project_id for s in songs if s.project_id]
projects_result = await session.execute(
select(Project).where(Project.id.in_(project_ids))
)
projects_map = {p.id: p for p in projects_result.scalars().all()}
```
### 8.2 Suno 서비스 HTTP 클라이언트 풀링
**현재 상태** (`app/utils/suno.py`):
```python
# 요청마다 새 클라이언트 생성
async with httpx.AsyncClient() as client:
...
```
**권장 수정**:
```python
# creatomate.py 패턴 적용
_suno_client: httpx.AsyncClient | None = None
async def get_suno_client() -> httpx.AsyncClient:
global _suno_client
if _suno_client is None or _suno_client.is_closed:
_suno_client = httpx.AsyncClient(
timeout=httpx.Timeout(60.0, connect=10.0),
limits=httpx.Limits(max_keepalive_connections=5, max_connections=10),
)
return _suno_client
```
### 8.3 동시성 제한
**권장 추가**:
```python
# 백그라운드 태스크 동시 실행 수 제한
BACKGROUND_TASK_SEMAPHORE = asyncio.Semaphore(5)
async def download_and_upload_video_to_blob(...):
async with BACKGROUND_TASK_SEMAPHORE:
# 기존 로직
```
### 8.4 분산 락 (선택적)
**높은 동시성 환경에서 권장**:
```python
# Redis 기반 분산 락
async def generate_video_with_lock(task_id: str):
lock_key = f"video_gen:{task_id}"
if not await redis.setnx(lock_key, "1"):
raise HTTPException(409, "Already processing")
try:
await redis.expire(lock_key, 300) # 5분 TTL
# 영상 생성 로직
finally:
await redis.delete(lock_key)
```
---
## 9. 아키텍처 다이어그램
### 9.1 전체 요청 흐름
```
┌─────────────────────────────────────────────────────────────────────────┐
│ CLIENT │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ FASTAPI SERVER │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ ROUTERS │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ /video │ │ /song │ │ /lyric │ │ /project │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌───────────────┼───────────────┐ │
│ ▼ ▼ ▼ │
│ ┌─────────────────────┐ ┌─────────────────┐ ┌─────────────────────┐ │
│ │ MAIN ENGINE │ │ BACKGROUND │ │ EXTERNAL SERVICES │ │
│ │ (AsyncSession) │ │ ENGINE │ │ ┌───────────────┐ │ │
│ │ pool_size: 20 │ │ (BackgroundSess)│ │ │ Creatomate │ │ │
│ │ max_overflow: 20 │ │ pool_size: 10 │ │ ├───────────────┤ │ │
│ └─────────────────────┘ └─────────────────┘ │ │ Suno │ │ │
│ │ │ │ ├───────────────┤ │ │
│ ▼ ▼ │ │ ChatGPT │ │ │
│ ┌─────────────────────────────────────────┐ │ ├───────────────┤ │ │
│ │ MySQL DATABASE │ │ │ Azure Blob │ │ │
│ │ ┌────────┐ ┌────────┐ ┌────────────┐ │ │ └───────────────┘ │ │
│ │ │Project │ │ Song │ │ Video │ │ └─────────────────────┘ │
│ │ │ Lyric │ │ Image │ │ │ │ │
│ │ └────────┘ └────────┘ └────────────┘ │ │
│ └─────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
```
### 9.2 데이터 흐름
```
[영상 생성 파이프라인]
1. 프로젝트 생성
Client ─▶ POST /project ─▶ DB(Project) ─▶ task_id
2. 이미지 업로드
Client ─▶ POST /project/image ─▶ Azure Blob ─▶ DB(Image)
3. 가사 생성
Client ─▶ POST /lyric/generate ─▶ DB(Lyric) ─▶ BackgroundTask
│
▼
ChatGPT API
│
▼
DB Update
4. 노래 생성
Client ─▶ GET /song/generate/{task_id} ─▶ Suno API ─▶ DB(Song)
Client ◀──── polling ─────────────────────────────────┘
5. 영상 생성
Client ─▶ GET /video/generate/{task_id}
│
├─ 순차 쿼리 ─▶ DB(Project, Lyric, Song, Image)
│
├─ Creatomate API ─▶ render_id
│
└─ DB(Video) status="processing"
Client ─▶ GET /video/status/{render_id}
│
├─ Creatomate Status Check
│
└─ if succeeded ─▶ BackgroundTask
│
├─ Download MP4
├─ Upload to Azure Blob
└─ DB Update status="completed"
6. 결과 조회
Client ─▶ GET /video/download/{task_id} ─▶ result_movie_url
```
---
## 10. 결론
### 10.1 현재 아키텍처 평가
O2O-CASTAD Backend는 **프로덕션 준비 수준의 비동기 아키텍처**를 갖추고 있습니다:
1. **안정성**: 이중 커넥션 풀, pool_pre_ping, 명시적 세션 관리로 런타임 에러 최소화
2. **성능**: 캐싱, HTTP 클라이언트 풀링으로 응답 시간 최적화
3. **확장성**: 백그라운드 태스크 분리, 폴링 패턴으로 부하 분산
4. **유지보수성**: 일관된 패턴, 구조화된 로깅, 타입 힌트
### 10.2 핵심 성과
```
┌─────────────────────────────────────────────────────────────┐
│ BEFORE → AFTER │
├─────────────────────────────────────────────────────────────┤
│ Session Timeout Errors │ Frequent → Resolved │
│ Template API Calls │ Every req → Cached (100%↓) │
│ HTTP Client Overhead │ 50ms/req → 0ms (100%↓) │
│ N+1 Query Problem │ N queries → 2 queries │
│ Connection Pool Conflicts │ Frequent → Isolated │
└─────────────────────────────────────────────────────────────┘
```
### 10.3 권장 다음 단계
1. **단기**: Song 라우터 N+1 문제 해결
2. **단기**: Suno 서비스 HTTP 클라이언트 풀링 적용
3. **중기**: 동시성 제한 (Semaphore) 추가
4. **장기**: Redis 캐시 레이어 도입 (템플릿 캐시 영속화)
5. **장기**: 분산 락 구현 (높은 동시성 환경 대비)
---
> **문서 끝**
> 추가 질문이나 개선 제안은 개발팀에 문의하세요.
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+500
View File
@@ -0,0 +1,500 @@
# ORM 동기식 전환 보고서
## 개요
현재 프로젝트는 **SQLAlchemy 2.0+ 비동기 방식**으로 구현되어 있습니다. 이 보고서는 동기식으로 전환할 경우 필요한 코드 수정 사항을 정리합니다.
---
## 1. 현재 비동기 구현 현황
### 1.1 사용 중인 라이브러리
- `sqlalchemy[asyncio]>=2.0.45`
- `asyncmy>=0.2.10` (MySQL 비동기 드라이버)
- `aiomysql>=0.3.2`
### 1.2 주요 비동기 컴포넌트
| 컴포넌트 | 현재 (비동기) | 변경 후 (동기) |
|---------|-------------|--------------|
| 엔진 | `create_async_engine` | `create_engine` |
| 세션 팩토리 | `async_sessionmaker` | `sessionmaker` |
| 세션 클래스 | `AsyncSession` | `Session` |
| DB 드라이버 | `mysql+asyncmy` | `mysql+pymysql` |
---
## 2. 파일별 수정 사항
### 2.1 pyproject.toml - 의존성 변경
**파일**: `pyproject.toml`
```diff
dependencies = [
"fastapi[standard]>=0.115.8",
- "sqlalchemy[asyncio]>=2.0.45",
+ "sqlalchemy>=2.0.45",
- "asyncmy>=0.2.10",
- "aiomysql>=0.3.2",
+ "pymysql>=1.1.0",
...
]
```
---
### 2.2 config.py - 데이터베이스 URL 변경
**파일**: `config.py` (라인 74-96)
```diff
class DatabaseSettings(BaseSettings):
@property
def MYSQL_URL(self) -> str:
- return f"mysql+asyncmy://{self.MYSQL_USER}:{self.MYSQL_PASSWORD}@{self.MYSQL_HOST}:{self.MYSQL_PORT}/{self.MYSQL_DB}"
+ return f"mysql+pymysql://{self.MYSQL_USER}:{self.MYSQL_PASSWORD}@{self.MYSQL_HOST}:{self.MYSQL_PORT}/{self.MYSQL_DB}"
```
---
### 2.3 app/database/session.py - 세션 설정 전면 수정
**파일**: `app/database/session.py`
#### 현재 코드 (비동기)
```python
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from typing import AsyncGenerator
engine = create_async_engine(
url=db_settings.MYSQL_URL,
echo=False,
pool_size=10,
max_overflow=10,
pool_timeout=5,
pool_recycle=3600,
pool_pre_ping=True,
pool_reset_on_return="rollback",
)
AsyncSessionLocal = async_sessionmaker(
bind=engine,
class_=AsyncSession,
expire_on_commit=False,
autoflush=False,
)
async def get_session() -> AsyncGenerator[AsyncSession, None]:
async with AsyncSessionLocal() as session:
try:
yield session
except Exception as e:
await session.rollback()
raise e
```
#### 변경 후 코드 (동기)
```python
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from typing import Generator
engine = create_engine(
url=db_settings.MYSQL_URL,
echo=False,
pool_size=10,
max_overflow=10,
pool_timeout=5,
pool_recycle=3600,
pool_pre_ping=True,
pool_reset_on_return="rollback",
)
SessionLocal = sessionmaker(
bind=engine,
class_=Session,
expire_on_commit=False,
autoflush=False,
)
def get_session() -> Generator[Session, None, None]:
with SessionLocal() as session:
try:
yield session
except Exception as e:
session.rollback()
raise e
```
#### get_worker_session 함수 변경
```diff
- from contextlib import asynccontextmanager
+ from contextlib import contextmanager
- @asynccontextmanager
- async def get_worker_session() -> AsyncGenerator[AsyncSession, None]:
- worker_engine = create_async_engine(
+ @contextmanager
+ def get_worker_session() -> Generator[Session, None, None]:
+ worker_engine = create_engine(
url=db_settings.MYSQL_URL,
poolclass=NullPool,
)
- session_factory = async_sessionmaker(
- bind=worker_engine,
- class_=AsyncSession,
+ session_factory = sessionmaker(
+ bind=worker_engine,
+ class_=Session,
expire_on_commit=False,
autoflush=False,
)
- async with session_factory() as session:
+ with session_factory() as session:
try:
yield session
finally:
- await session.close()
- await worker_engine.dispose()
+ session.close()
+ worker_engine.dispose()
```
---
### 2.4 app/*/dependencies.py - 타입 힌트 변경
**파일**: `app/song/dependencies.py`, `app/lyric/dependencies.py`, `app/video/dependencies.py`
```diff
- from sqlalchemy.ext.asyncio import AsyncSession
+ from sqlalchemy.orm import Session
- SessionDep = Annotated[AsyncSession, Depends(get_session)]
+ SessionDep = Annotated[Session, Depends(get_session)]
```
---
### 2.5 라우터 파일들 - async/await 제거
**영향받는 파일**:
- `app/home/api/routers/v1/home.py`
- `app/lyric/api/routers/v1/lyric.py`
- `app/song/api/routers/v1/song.py`
- `app/video/api/routers/v1/video.py`
#### 예시: lyric.py (라인 70-90)
```diff
- async def get_lyric_by_task_id(
+ def get_lyric_by_task_id(
task_id: str,
- session: AsyncSession = Depends(get_session),
+ session: Session = Depends(get_session),
):
- result = await session.execute(select(Lyric).where(Lyric.task_id == task_id))
+ result = session.execute(select(Lyric).where(Lyric.task_id == task_id))
lyric = result.scalar_one_or_none()
...
```
#### 예시: CRUD 작업 (라인 218-260)
```diff
- async def create_project(
+ def create_project(
request_body: ProjectCreateRequest,
- session: AsyncSession = Depends(get_session),
+ session: Session = Depends(get_session),
):
project = Project(
store_name=request_body.customer_name,
region=request_body.region,
task_id=task_id,
)
session.add(project)
- await session.commit()
- await session.refresh(project)
+ session.commit()
+ session.refresh(project)
return project
```
#### 예시: 플러시 작업 (home.py 라인 340-350)
```diff
session.add(image)
- await session.flush()
+ session.flush()
result = image.id
```
---
### 2.6 서비스 파일들 - Raw SQL 쿼리 변경
**영향받는 파일**:
- `app/lyric/services/lyrics.py`
- `app/song/services/song.py`
- `app/video/services/video.py`
#### 예시: lyrics.py (라인 20-30)
```diff
- async def get_store_default_info(conn: AsyncConnection):
+ def get_store_default_info(conn: Connection):
query = """SELECT * FROM store_default_info;"""
- result = await conn.execute(text(query))
+ result = conn.execute(text(query))
return result.fetchall()
```
#### 예시: INSERT 쿼리 (라인 360-400)
```diff
- async def insert_song_result(conn: AsyncConnection, params: dict):
+ def insert_song_result(conn: Connection, params: dict):
insert_query = """INSERT INTO song_results_all (...) VALUES (...)"""
- await conn.execute(text(insert_query), params)
- await conn.commit()
+ conn.execute(text(insert_query), params)
+ conn.commit()
```
---
### 2.7 app/home/services/base.py - BaseService 클래스
**파일**: `app/home/services/base.py`
```diff
- from sqlalchemy.ext.asyncio import AsyncSession
+ from sqlalchemy.orm import Session
class BaseService:
- def __init__(self, model, session: AsyncSession):
+ def __init__(self, model, session: Session):
self.model = model
self.session = session
- async def _get(self, id: UUID):
- return await self.session.get(self.model, id)
+ def _get(self, id: UUID):
+ return self.session.get(self.model, id)
- async def _add(self, entity):
+ def _add(self, entity):
self.session.add(entity)
- await self.session.commit()
- await self.session.refresh(entity)
+ self.session.commit()
+ self.session.refresh(entity)
return entity
- async def _update(self, entity):
- return await self._add(entity)
+ def _update(self, entity):
+ return self._add(entity)
- async def _delete(self, entity):
- await self.session.delete(entity)
+ def _delete(self, entity):
+ self.session.delete(entity)
```
---
## 3. 모델 파일 - 변경 불필요
다음 모델 파일들은 **변경이 필요 없습니다**:
- `app/home/models.py`
- `app/lyric/models.py`
- `app/song/models.py`
- `app/video/models.py`
모델 정의 자체는 비동기/동기와 무관하게 동일합니다. `Mapped`, `mapped_column`, `relationship` 등은 그대로 사용 가능합니다.
단, **관계 로딩 전략**에서 `lazy="selectin"` 설정은 동기 환경에서도 작동하지만, 필요에 따라 `lazy="joined"` 또는 `lazy="subquery"`로 변경할 수 있습니다.
---
## 4. 수정 패턴 요약
### 4.1 Import 변경 패턴
```diff
# 엔진/세션 관련
- from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
+ from sqlalchemy import create_engine
+ from sqlalchemy.orm import Session, sessionmaker
# 타입 힌트
- from typing import AsyncGenerator
+ from typing import Generator
# 컨텍스트 매니저
- from contextlib import asynccontextmanager
+ from contextlib import contextmanager
```
### 4.2 함수 정의 변경 패턴
```diff
- async def function_name(...):
+ def function_name(...):
```
### 4.3 await 제거 패턴
```diff
- result = await session.execute(query)
+ result = session.execute(query)
- await session.commit()
+ session.commit()
- await session.refresh(obj)
+ session.refresh(obj)
- await session.flush()
+ session.flush()
- await session.rollback()
+ session.rollback()
- await session.close()
+ session.close()
- await engine.dispose()
+ engine.dispose()
```
### 4.4 컨텍스트 매니저 변경 패턴
```diff
- async with SessionLocal() as session:
+ with SessionLocal() as session:
```
---
## 5. 영향받는 파일 목록
### 5.1 반드시 수정해야 하는 파일
| 파일 | 수정 범위 |
|-----|---------|
| `pyproject.toml` | 의존성 변경 |
| `config.py` | DB URL 변경 |
| `app/database/session.py` | 전면 수정 |
| `app/database/session-prod.py` | 전면 수정 |
| `app/home/api/routers/v1/home.py` | async/await 제거 |
| `app/lyric/api/routers/v1/lyric.py` | async/await 제거 |
| `app/song/api/routers/v1/song.py` | async/await 제거 |
| `app/video/api/routers/v1/video.py` | async/await 제거 |
| `app/lyric/services/lyrics.py` | async/await 제거 |
| `app/song/services/song.py` | async/await 제거 |
| `app/video/services/video.py` | async/await 제거 |
| `app/home/services/base.py` | async/await 제거 |
| `app/song/dependencies.py` | 타입 힌트 변경 |
| `app/lyric/dependencies.py` | 타입 힌트 변경 |
| `app/video/dependencies.py` | 타입 힌트 변경 |
| `app/dependencies/database.py` | 타입 힌트 변경 |
### 5.2 수정 불필요한 파일
| 파일 | 이유 |
|-----|-----|
| `app/home/models.py` | 모델 정의는 동기/비동기 무관 |
| `app/lyric/models.py` | 모델 정의는 동기/비동기 무관 |
| `app/song/models.py` | 모델 정의는 동기/비동기 무관 |
| `app/video/models.py` | 모델 정의는 동기/비동기 무관 |
---
## 6. 주의사항
### 6.1 FastAPI와의 호환성
FastAPI는 동기 엔드포인트도 지원합니다. 동기 함수는 스레드 풀에서 실행됩니다:
```python
# 동기 엔드포인트 - FastAPI가 자동으로 스레드풀에서 실행
@router.get("/items/{item_id}")
def get_item(item_id: int, session: Session = Depends(get_session)):
return session.get(Item, item_id)
```
### 6.2 성능 고려사항
동기식으로 전환 시 고려할 점:
- **동시성 감소**: 비동기 I/O의 이점 상실
- **스레드 풀 의존**: 동시 요청이 많을 경우 스레드 풀 크기 조정 필요
- **블로킹 I/O**: DB 쿼리 중 다른 요청 처리 불가
### 6.3 백그라운드 작업
현재 `get_worker_session()`으로 별도 이벤트 루프에서 실행되는 백그라운드 작업이 있습니다. 동기식 전환 시 스레드 기반 백그라운드 작업으로 변경해야 합니다:
```python
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=4)
def background_task():
with get_worker_session() as session:
# 작업 수행
pass
# 실행
executor.submit(background_task)
```
---
## 7. 마이그레이션 단계
### Step 1: 의존성 변경
1. `pyproject.toml` 수정
2. `pip install pymysql` 또는 `uv sync` 실행
### Step 2: 설정 파일 수정
1. `config.py`의 DB URL 변경
2. `app/database/session.py` 전면 수정
### Step 3: 라우터 수정
1. 각 라우터 파일의 `async def` → `def` 변경
2. 모든 `await` 키워드 제거
3. `AsyncSession` → `Session` 타입 힌트 변경
### Step 4: 서비스 수정
1. 서비스 파일들의 async/await 제거
2. Raw SQL 쿼리 함수들 수정
### Step 5: 의존성 수정
1. `dependencies.py` 파일들의 타입 힌트 변경
### Step 6: 테스트
1. 모든 엔드포인트 기능 테스트
2. 성능 테스트 (동시 요청 처리 확인)
---
## 8. 결론
비동기에서 동기로 전환은 기술적으로 가능하지만, 다음을 고려해야 합니다:
**장점**:
- 코드 복잡도 감소 (async/await 제거)
- 디버깅 용이
- 레거시 라이브러리와의 호환성 향상
**단점**:
- 동시성 처리 능력 감소
- I/O 바운드 작업에서 성능 저하 가능
- FastAPI의 비동기 장점 미활용
현재 프로젝트가 FastAPI 기반이고 I/O 작업(DB, 외부 API 호출)이 많다면, **비동기 유지를 권장**합니다. 동기 전환은 특별한 요구사항(레거시 통합, 팀 역량 등)이 있을 때만 고려하시기 바랍니다.
+297
View File
@@ -0,0 +1,297 @@
# 비동기 처리 문제 분석 보고서
## 요약
전반적으로 이 프로젝트는 현대적인 비동기 아키텍처를 잘 구현하고 있습니다. 그러나 몇 가지 잠재적인 문제점과 개선 가능한 부분이 발견되었습니다.
---
## 1. 심각도 높음 - 즉시 개선 권장
### 1.1 N+1 쿼리 문제 (video.py:596-612)
```python
# get_videos() 엔드포인트에서
for video in videos:
# 매 video마다 별도의 DB 쿼리 실행 - N+1 문제!
project_result = await session.execute(
select(Project).where(Project.id == video.project_id)
)
project = project_result.scalar_one_or_none()
```
**문제점**: 비디오 목록 조회 시 각 비디오마다 별도의 Project 쿼리가 발생합니다. 10개 비디오 조회 시 11번의 DB 쿼리가 실행됩니다.
**개선 방안**:
```python
# selectinload를 사용한 eager loading
from sqlalchemy.orm import selectinload
query = (
select(Video)
.options(selectinload(Video.project)) # relationship 필요
.where(Video.id.in_(select(subquery.c.max_id)))
.order_by(Video.created_at.desc())
.offset(offset)
.limit(pagination.page_size)
)
# 또는 한 번에 project_ids 수집 후 일괄 조회
project_ids = [v.project_id for v in videos]
projects_result = await session.execute(
select(Project).where(Project.id.in_(project_ids))
)
projects_map = {p.id: p for p in projects_result.scalars().all()}
```
---
### 1.2 가사 생성 API의 블로킹 문제 (lyric.py:274-276)
```python
# ChatGPT API 호출이 완료될 때까지 HTTP 응답이 블로킹됨
print(f"[generate_lyric] ChatGPT generation started - task_id: {task_id}")
result = await service.generate(prompt=prompt) # 수 초~수십 초 소요
print(f"[generate_lyric] ChatGPT generation completed - task_id: {task_id}")
```
**문제점**:
- ChatGPT API 응답이 5-30초 이상 걸릴 수 있음
- 이 시간 동안 클라이언트 연결이 유지되어야 함
- 다수 동시 요청 시 worker 스레드 고갈 가능성
**개선 방안 (BackgroundTask 패턴)**:
```python
@router.post("/generate")
async def generate_lyric(
request_body: GenerateLyricRequest,
background_tasks: BackgroundTasks,
session: AsyncSession = Depends(get_session),
) -> GenerateLyricResponse:
# DB에 processing 상태로 저장
lyric = Lyric(status="processing", ...)
session.add(lyric)
await session.commit()
# 백그라운드에서 ChatGPT 호출
background_tasks.add_task(
generate_lyric_background,
task_id=task_id,
prompt=prompt,
)
# 즉시 응답 반환
return GenerateLyricResponse(
success=True,
task_id=task_id,
message="가사 생성이 시작되었습니다. /status/{task_id}로 상태를 확인하세요.",
)
```
---
### 1.3 Creatomate 서비스의 동기/비동기 메서드 혼재 (creatomate.py)
**문제점**: 동기 메서드가 여전히 존재하여 실수로 async 컨텍스트에서 호출될 수 있습니다.
| 동기 메서드 | 비동기 메서드 |
|------------|--------------|
| `get_all_templates_data()` | 없음 |
| `get_one_template_data()` | `get_one_template_data_async()` |
| `make_creatomate_call()` | 없음 |
| `make_creatomate_custom_call()` | `make_creatomate_custom_call_async()` |
| `get_render_status()` | `get_render_status_async()` |
**개선 방안**:
```python
# 모든 HTTP 호출 메서드를 async로 통일
async def get_all_templates_data(self) -> dict:
url = f"{self.BASE_URL}/v1/templates"
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=self.headers, timeout=30.0)
response.raise_for_status()
return response.json()
# 동기 버전 제거 또는 deprecated 표시
```
---
## 2. 심각도 중간 - 개선 권장
### 2.1 백그라운드 태스크에서 매번 엔진 생성 (session.py:82-127)
```python
@asynccontextmanager
async def get_worker_session() -> AsyncGenerator[AsyncSession, None]:
# 매 호출마다 새 엔진 생성 - 오버헤드 발생
worker_engine = create_async_engine(
url=db_settings.MYSQL_URL,
poolclass=NullPool,
...
)
```
**문제점**: 백그라운드 태스크가 빈번하게 호출되면 엔진 생성/소멸 오버헤드가 증가합니다.
**개선 방안**:
```python
# 모듈 레벨에서 워커 전용 엔진 생성
_worker_engine = create_async_engine(
url=db_settings.MYSQL_URL,
poolclass=NullPool,
)
_WorkerSessionLocal = async_sessionmaker(bind=_worker_engine, ...)
@asynccontextmanager
async def get_worker_session() -> AsyncGenerator[AsyncSession, None]:
async with _WorkerSessionLocal() as session:
try:
yield session
except Exception as e:
await session.rollback()
raise e
```
---
### 2.2 대용량 파일 다운로드 시 메모리 사용 (video_task.py:49-54)
```python
async with httpx.AsyncClient() as client:
response = await client.get(video_url, timeout=180.0)
response.raise_for_status()
# 전체 파일을 메모리에 로드 - 대용량 영상 시 문제
async with aiofiles.open(str(temp_file_path), "wb") as f:
await f.write(response.content)
```
**문제점**: 수백 MB 크기의 영상 파일을 한 번에 메모리에 로드합니다.
**개선 방안 - 스트리밍 다운로드**:
```python
async with httpx.AsyncClient() as client:
async with client.stream("GET", video_url, timeout=180.0) as response:
response.raise_for_status()
async with aiofiles.open(str(temp_file_path), "wb") as f:
async for chunk in response.aiter_bytes(chunk_size=8192):
await f.write(chunk)
```
---
### 2.3 httpx.AsyncClient 반복 생성
여러 곳에서 `async with httpx.AsyncClient() as client:`를 사용하여 매번 새 클라이언트를 생성합니다.
**개선 방안 - 재사용 가능한 클라이언트**:
```python
# app/utils/http_client.py
from contextlib import asynccontextmanager
import httpx
_client: httpx.AsyncClient | None = None
async def get_http_client() -> httpx.AsyncClient:
global _client
if _client is None:
_client = httpx.AsyncClient(timeout=30.0)
return _client
async def close_http_client():
global _client
if _client:
await _client.aclose()
_client = None
```
---
## 3. 심각도 낮음 - 선택적 개선
### 3.1 generate_video 엔드포인트의 다중 DB 조회 (video.py:109-191)
```python
# 4개의 개별 쿼리가 순차적으로 실행됨
project_result = await session.execute(select(Project).where(...))
lyric_result = await session.execute(select(Lyric).where(...))
song_result = await session.execute(select(Song).where(...))
image_result = await session.execute(select(Image).where(...))
```
**개선 방안 - 병렬 쿼리 실행**:
```python
import asyncio
project_task = session.execute(select(Project).where(Project.task_id == task_id))
lyric_task = session.execute(select(Lyric).where(Lyric.task_id == task_id))
song_task = session.execute(
select(Song).where(Song.task_id == task_id).order_by(Song.created_at.desc()).limit(1)
)
image_task = session.execute(
select(Image).where(Image.task_id == task_id).order_by(Image.img_order.asc())
)
project_result, lyric_result, song_result, image_result = await asyncio.gather(
project_task, lyric_task, song_task, image_task
)
```
---
### 3.2 템플릿 조회 캐싱 미적용
`get_one_template_data_async()`가 매번 Creatomate API를 호출합니다.
**개선 방안 - 간단한 메모리 캐싱**:
```python
from functools import lru_cache
from cachetools import TTLCache
_template_cache = TTLCache(maxsize=100, ttl=3600) # 1시간 캐시
async def get_one_template_data_async(self, template_id: str) -> dict:
if template_id in _template_cache:
return _template_cache[template_id]
url = f"{self.BASE_URL}/v1/templates/{template_id}"
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=self.headers, timeout=30.0)
response.raise_for_status()
data = response.json()
_template_cache[template_id] = data
return data
```
---
## 4. 긍정적인 부분 (잘 구현된 패턴)
| 항목 | 상태 | 설명 |
|------|------|------|
| SQLAlchemy AsyncSession | O | `asyncmy` 드라이버와 `AsyncSessionLocal` 사용 |
| 파일 I/O | O | `aiofiles` 사용으로 비동기 파일 처리 |
| HTTP 클라이언트 | O | `httpx.AsyncClient` 사용 |
| OpenAI API | O | `AsyncOpenAI` 클라이언트 사용 |
| 백그라운드 태스크 | O | FastAPI `BackgroundTasks` 적절히 사용 |
| 세션 관리 | O | 메인/워커 세션 분리로 이벤트 루프 충돌 방지 |
| 연결 풀 설정 | O | `pool_size`, `pool_recycle`, `pool_pre_ping` 적절히 설정 |
---
## 5. 우선순위별 개선 권장 사항
| 우선순위 | 항목 | 예상 효과 |
|----------|------|----------|
| **1** | N+1 쿼리 문제 해결 | DB 부하 감소, 응답 속도 개선 |
| **2** | 가사 생성 백그라운드 처리 | 동시 요청 처리 능력 향상 |
| **3** | Creatomate 동기 메서드 제거 | 실수로 인한 블로킹 방지 |
| **4** | 대용량 파일 스트리밍 다운로드 | 메모리 사용량 감소 |
| **5** | 워커 세션 엔진 재사용 | 오버헤드 감소 |
---
## 분석 일자
2024-12-29
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

BIN
View File
Binary file not shown.
+77
View File
@@ -0,0 +1,77 @@
import aiohttp
from pathlib import Path
class BlobUploader():
root_url = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/"
image_content_types = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".webp": "image/webp",
".bmp": "image/bmp"
}
# SAS TOKEN 입력으로 저장
def __init__(self, sas_token):
self.sas_token = sas_token
# 음악 파일 업로드 (user_idx/task_idx/music_file_name 경로에 저장), 해당 업로드 링크 출력 / 실패 시 None
async def upload_music_to_azure_blob(self, file_data : bytes, user_idx : str, task_idx : str, music_file_name : str) -> str:
url = f"{self.root_url}/{user_idx}/{task_idx}/{music_file_name}"
access_url = f"{url}?{self.sas_token}"
headers = {
"Content-Type": "audio/mpeg",
"x-ms-blob-type": "BlockBlob"
}
async with aiohttp.ClientSession() as session:
async with session.put(access_url, data=file_data, headers=headers) as resp:
if resp.status in [200, 201]:
return url
else:
text = await resp.text()
print(f"Failed Status Code: {resp.status}")
print(f"Response: {text}")
return None
# 영상 파일 업로드 (user_idx/task_idx/video_file_name 경로에 저장), 해당 업로드 링크 출력 / 실패 시 None
async def upload_video_to_azure_blob(self, file_data : bytes, user_idx : str, task_idx : str, video_file_name : str) -> str:
url = f"{self.root_url}/{user_idx}/{task_idx}/{video_file_name}"
access_url = f"{url}?{self.sas_token}"
headers = {
"Content-Type": "video/mp4",
"x-ms-blob-type": "BlockBlob"
}
async with aiohttp.ClientSession() as session:
async with session.put(access_url, data=file_data, headers=headers) as resp:
if resp.status in [200, 201]:
return url
else:
text = await resp.text()
print(f"Failed Status Code: {resp.status}")
print(f"Response: {text}")
return None
# 이미지 파일 업로드 (user_idx/task_idx/image_file_name 경로에 저장), 해당 업로드 링크 출력 / 실패 시 None
async def upload_image_to_azure_blob(self, file_data : bytes, user_idx : str, task_idx : str, image_file_name : str) -> str:
url = f"{self.root_url}/{user_idx}/{task_idx}/{image_file_name}"
access_url = f"{url}?{self.sas_token}"
extension = Path(image_file_name).suffix.lower()
content_type = self.image_content_types.get(extension, "image/jpeg")
headers = {
"Content-Type": content_type,
"x-ms-blob-type": "BlockBlob"
}
async with aiohttp.ClientSession() as session:
async with session.put(access_url, data=file_data, headers=headers) as resp:
if resp.status in [200, 201]:
return url
else:
text = await resp.text()
print(f"Failed Status Code: {resp.status}")
print(f"Response: {text}")
return None
+29
View File
@@ -0,0 +1,29 @@
import asyncio
from nvMapScraper import nvMapScraper
from nvMapPwScraper import nvMapPwScraper
async def main_function():
await nvMapPwScraper.initiate_scraper()
selected = {'title': '<b>스테이</b>,<b>머뭄</b>',
'link': 'https://www.instagram.com/staymeomoom',
'category': '숙박>펜션',
'description': '',
'telephone': '',
'address': '전북특별자치도 군산시 신흥동 63-18',
'roadAddress': '전북특별자치도 군산시 절골길 18',
'mapx': '1267061254',
'mapy': '359864175',
'lng': 126.7061254,
'lat': 35.9864175}
async with nvMapPwScraper() as pw_scraper:
new_url = await pw_scraper.get_place_id_url(selected)
print(new_url)
nv_scraper = nvMapScraper(new_url) # 이후 동일한 플로우
await nv_scraper.scrap()
print(nv_scraper.rawdata)
return
print("running main_funtion..")
asyncio.run(main_function())
+113
View File
@@ -0,0 +1,113 @@
import asyncio
from playwright.async_api import async_playwright
from urllib import parse
class nvMapPwScraper():
# cls vars
is_ready = False
_playwright = None
_browser = None
_context = None
_win_width = 1280
_win_height = 720
_max_retry = 30 # place id timeout threshold seconds
# instance var
page = None
@classmethod
def default_context_builder(cls):
context_builder_dict = {}
context_builder_dict['viewport'] = {
'width' : cls._win_width,
'height' : cls._win_height
}
context_builder_dict['screen'] = {
'width' : cls._win_width,
'height' : cls._win_height
}
context_builder_dict['user_agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
context_builder_dict['locale'] = 'ko-KR'
context_builder_dict['timezone_id']='Asia/Seoul'
return context_builder_dict
@classmethod
async def initiate_scraper(cls):
if not cls._playwright:
cls._playwright = await async_playwright().start()
if not cls._browser:
cls._browser = await cls._playwright.chromium.launch(headless=True)
if not cls._context:
cls._context = await cls._browser.new_context(**cls.default_context_builder())
cls.is_ready = True
def __init__(self):
if not self.is_ready:
raise Exception("nvMapScraper is not initiated")
async def __aenter__(self):
await self.create_page()
return self
async def __aexit__(self, exc_type, exc, tb):
await self.page.close()
async def create_page(self):
self.page = await self._context.new_page()
await self.page.add_init_script(
'''const defaultGetter = Object.getOwnPropertyDescriptor(
Navigator.prototype,
"webdriver"
).get;
defaultGetter.apply(navigator);
defaultGetter.toString();
Object.defineProperty(Navigator.prototype, "webdriver", {
set: undefined,
enumerable: true,
configurable: true,
get: new Proxy(defaultGetter, {
apply: (target, thisArg, args) => {
Reflect.apply(target, thisArg, args);
return false;
},
}),
});
const patchedGetter = Object.getOwnPropertyDescriptor(
Navigator.prototype,
"webdriver"
).get;
patchedGetter.apply(navigator);
patchedGetter.toString();''')
await self.page.set_extra_http_headers({
'sec-ch-ua': '\"Not?A_Brand\";v=\"99\", \"Chromium\";v=\"130\"'
})
await self.page.goto("http://google.com")
async def goto_url(self, url, wait_until="domcontentloaded", timeout=20000):
page = self.page
await page.goto(url, wait_until=wait_until, timeout=timeout)
async def get_place_id_url(self, selected):
title = selected['title'].replace("<b>", "").replace("</b>", "")
address = selected.get('roadAddress', selected['address']).replace("<b>", "").replace("</b>", "")
encoded_query = parse.quote(f"{address} {title}")
url = f"https://map.naver.com/p/search/{encoded_query}"
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
if "/place/" in self.page.url:
return self.page.url
url = self.page.url.replace("?","?isCorrectAnswer=true&")
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
if "/place/" in self.page.url:
return self.page.url
if (count == self._max_retry / 2):
raise Exception("Failed to identify place id. loading timeout")
else:
raise Exception("Failed to identify place id. item is ambiguous")
+119
View File
@@ -0,0 +1,119 @@
import re
import aiohttp
import json
import asyncio
import bs4
PLACE_PATTERN = r"/place/(\d+)"
GRAPHQL_URL = "https://pcmap-api.place.naver.com/graphql"
NAVER_COOKIES="NAC=mQ7mBownbQf4A; NNB=TQPII6AKDBFGQ; PLACE_LANGUAGE=ko; NACT=1; nid_inf=1431570813; NID_AUT=k2T7FraXOdIMRCHzEZIFtHQup+I7b87M5fd7+p65AXZTdGB/gelRmW8s/Q4oDxm8; tooltipDisplayed=true; SRT30=1762660151; NID_SES=AAAB1Lpy3y3hGzuPbJpJl8vvFx18C+HXXuZEFou/YPgocHe7k2/5MpFlgE48X1JF7c7IPoU2khZKkkuLx+tsvWAzOf0TnG/G8RrBGeawnSluSJcKcTdKKRJ4cygKc/OabVxoc3TNZJWxer3vFtXBoXkDS5querVNS6wvcMhA/p4vkPKOeepwKLR+1IJERlQJWZw4q29IdAysrbBNn3Akf9mDA5eTYvMDLYyRkToRh10TVMW/yhyNQeMXlIdnR8U1ZCNqe/9ErYdos5gQDstswEJQQA0T2cHFGJOtmlYMPlnhWado5w521iZXGJyKcA9ZawizM/i5nK5xNYtPGS3cvImUYl6B5ulIipUJSqpj8v2XstK0TZlOGxHToXaVDrCNmSfCA9vFYbTb6xJHB2JRAT3Jik/z6QgLjJLBWRnsucMDqldxoiEDAUHEhY3pjgZ89quR3c3hwAuTlI9hBn5I3e5VQR0Y/GxoS9mIkMF8pJmcGneqnE0BNIt91RN6Se5rDM69B+JWppBXtSir1JGuXADaRLLMP8VlxJX949iH0UYTKWKsrD4OgNNK5aUx24nAH494WPknBMlx4fCMIeWzy7K3sEZkNUn/+A+eHraqIFfbGpveSCNM+8EqEjMgA+YRgg3eig==; _naver_usersession_=Kkgzim/64JicPJzgkIIvqQ==; page_uid=jesTPsqVWUZssE4qJeossssssD0-011300; SRT5=1762662010; BUC=z5Fu3sAYtFwpbRDrrDFYdn4AgK5hNkOqX-DdaLU7VJM="
OVERVIEW_QUERY = '''
query getAccommodation($id: String!, $deviceType: String) {
business: placeDetail(input: {id: $id, isNx: true, deviceType: $deviceType}) {
base {
id
name
category
roadAddress
address
phone
virtualPhone
microReviews
conveniences
visitorReviewsTotal
}
images { images { origin url } }
cpImages(source: [ugcImage]) { images { origin url } }
}
}'''
REQUEST_HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
"Referer": "https://map.naver.com/",
"Origin": "https://map.naver.com",
"Content-Type": "application/json",
"Cookie": NAVER_COOKIES
}
class GraphQLException(Exception):
pass
class nvMapScraper():
url : str = None
scrap_type : str = None
rawdata : dict = None
image_link_list : list[str] = None
base_info : dict = None
def __init__(self, url):
self.url = url
async def parse_url(self):
if 'place' not in self.url:
if 'naver.me' in self.url:
async with aiohttp.ClientSession() as session:
async with session.get(self.url) as response:
self.url = str(response.url)
else:
raise GraphQLException("this shorten url not have place id")
try:
place_id = re.search(PLACE_PATTERN, self.url)[1]
except Exception as E:
raise GraphQLException("Cannot find place id")
return place_id
async def scrap(self):
try:
place_id = await self.parse_url()
data = await self.call_get_accomodation(place_id)
self.rawdata = data
fac_data = await self.get_facility_string(place_id)
self.rawdata['facilities'] = fac_data
self.image_link_list = [nv_image['origin'] for nv_image in data['data']['business']['images']['images']]
self.base_info = data['data']['business']['base']
self.facility_info = fac_data
self.scrap_type = "GraphQL"
except GraphQLException as G:
print (G)
print("fallback")
self.scrap_type = "Playwright"
pass # 나중에 pw 이용한 crawling으로 fallback 추가
return
async def call_get_accomodation(self, place_id):
payload = {
"operationName" : "getAccommodation",
"variables": { "id": place_id, "deviceType": "pc" },
"query": OVERVIEW_QUERY,
}
json_payload = json.dumps(payload)
async with aiohttp.ClientSession() as session:
async with session.post(GRAPHQL_URL, data=json_payload, headers=REQUEST_HEADERS) as response:
response.encoding = 'utf-8'
if response.status == 200: # 요청 성공
return await response.json() # await 주의
else: # 요청 실패
print('실패 상태 코드:', response.status)
print(response.text)
raise Exception()
async def get_facility_string(self, place_id):
url = f"https://pcmap.place.naver.com/accommodation/{place_id}/home"
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=REQUEST_HEADERS) as response:
soup = bs4.BeautifulSoup(await response.read(), 'html.parser')
c_elem = soup.find('span', 'place_blind', string='편의')
facilities = c_elem.parent.parent.find('div').string
return facilities
# url = "https://naver.me/IgJGCCic"
# scraper = nvMapScraper(url)
# asyncio.run(scraper.scrap())
# print(scraper.image_link_list)
# print(len(scraper.image_link_list))
+29
View File
@@ -0,0 +1,29 @@
import asyncio
from nvMapScraper import nvMapScraper
from nvMapPwScraper import nvMapPwScraper
async def main_function():
await nvMapPwScraper.initiate_scraper()
selected = {'title': '<b>스테이</b>,<b>머뭄</b>',
'link': 'https://www.instagram.com/staymeomoom',
'category': '숙박>펜션',
'description': '',
'telephone': '',
'address': '전북특별자치도 군산시 신흥동 63-18',
'roadAddress': '전북특별자치도 군산시 절골길 18',
'mapx': '1267061254',
'mapy': '359864175',
'lng': 126.7061254,
'lat': 35.9864175}
async with nvMapPwScraper() as pw_scraper:
new_url = await pw_scraper.get_place_id_url(selected)
print(new_url)
nv_scraper = nvMapScraper(new_url) # 이후 동일한 플로우
await nv_scraper.scrap()
print(nv_scraper.rawdata)
return
print("running main_funtion..")
asyncio.run(main_function())
+113
View File
@@ -0,0 +1,113 @@
import asyncio
from playwright.async_api import async_playwright
from urllib import parse
class nvMapPwScraper():
# cls vars
is_ready = False
_playwright = None
_browser = None
_context = None
_win_width = 1280
_win_height = 720
_max_retry = 30 # place id timeout threshold seconds
# instance var
page = None
@classmethod
def default_context_builder(cls):
context_builder_dict = {}
context_builder_dict['viewport'] = {
'width' : cls._win_width,
'height' : cls._win_height
}
context_builder_dict['screen'] = {
'width' : cls._win_width,
'height' : cls._win_height
}
context_builder_dict['user_agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
context_builder_dict['locale'] = 'ko-KR'
context_builder_dict['timezone_id']='Asia/Seoul'
return context_builder_dict
@classmethod
async def initiate_scraper(cls):
if not cls._playwright:
cls._playwright = await async_playwright().start()
if not cls._browser:
cls._browser = await cls._playwright.chromium.launch(headless=True)
if not cls._context:
cls._context = await cls._browser.new_context(**cls.default_context_builder())
cls.is_ready = True
def __init__(self):
if not self.is_ready:
raise Exception("nvMapScraper is not initiated")
async def __aenter__(self):
await self.create_page()
return self
async def __aexit__(self, exc_type, exc, tb):
await self.page.close()
async def create_page(self):
self.page = await self._context.new_page()
await self.page.add_init_script(
'''const defaultGetter = Object.getOwnPropertyDescriptor(
Navigator.prototype,
"webdriver"
).get;
defaultGetter.apply(navigator);
defaultGetter.toString();
Object.defineProperty(Navigator.prototype, "webdriver", {
set: undefined,
enumerable: true,
configurable: true,
get: new Proxy(defaultGetter, {
apply: (target, thisArg, args) => {
Reflect.apply(target, thisArg, args);
return false;
},
}),
});
const patchedGetter = Object.getOwnPropertyDescriptor(
Navigator.prototype,
"webdriver"
).get;
patchedGetter.apply(navigator);
patchedGetter.toString();''')
await self.page.set_extra_http_headers({
'sec-ch-ua': '\"Not?A_Brand\";v=\"99\", \"Chromium\";v=\"130\"'
})
await self.page.goto("http://google.com")
async def goto_url(self, url, wait_until="domcontentloaded", timeout=20000):
page = self.page
await page.goto(url, wait_until=wait_until, timeout=timeout)
async def get_place_id_url(self, selected):
title = selected['title'].replace("<b>", "").replace("</b>", "")
address = selected.get('roadAddress', selected['address']).replace("<b>", "").replace("</b>", "")
encoded_query = parse.quote(f"{address} {title}")
url = f"https://map.naver.com/p/search/{encoded_query}"
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
if "/place/" in self.page.url:
return self.page.url
url = self.page.url.replace("?","?isCorrectAnswer=true&")
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
if "/place/" in self.page.url:
return self.page.url
if (count == self._max_retry / 2):
raise Exception("Failed to identify place id. loading timeout")
else:
raise Exception("Failed to identify place id. item is ambiguous")
+46 -32
View File
@@ -1,13 +1,14 @@
import asyncio
import json
import re import re
import aiohttp import aiohttp
import json
import asyncio
import bs4
PLACE_PATTERN = r"/place/(\d+)"
GRAPHQL_URL = "https://pcmap-api.place.naver.com/graphql" GRAPHQL_URL = "https://pcmap-api.place.naver.com/graphql"
NAVER_COOKIES="NAC=mQ7mBownbQf4A; NNB=TQPII6AKDBFGQ; PLACE_LANGUAGE=ko; NACT=1; nid_inf=1431570813; NID_AUT=k2T7FraXOdIMRCHzEZIFtHQup+I7b87M5fd7+p65AXZTdGB/gelRmW8s/Q4oDxm8; tooltipDisplayed=true; SRT30=1762660151; NID_SES=AAAB1Lpy3y3hGzuPbJpJl8vvFx18C+HXXuZEFou/YPgocHe7k2/5MpFlgE48X1JF7c7IPoU2khZKkkuLx+tsvWAzOf0TnG/G8RrBGeawnSluSJcKcTdKKRJ4cygKc/OabVxoc3TNZJWxer3vFtXBoXkDS5querVNS6wvcMhA/p4vkPKOeepwKLR+1IJERlQJWZw4q29IdAysrbBNn3Akf9mDA5eTYvMDLYyRkToRh10TVMW/yhyNQeMXlIdnR8U1ZCNqe/9ErYdos5gQDstswEJQQA0T2cHFGJOtmlYMPlnhWado5w521iZXGJyKcA9ZawizM/i5nK5xNYtPGS3cvImUYl6B5ulIipUJSqpj8v2XstK0TZlOGxHToXaVDrCNmSfCA9vFYbTb6xJHB2JRAT3Jik/z6QgLjJLBWRnsucMDqldxoiEDAUHEhY3pjgZ89quR3c3hwAuTlI9hBn5I3e5VQR0Y/GxoS9mIkMF8pJmcGneqnE0BNIt91RN6Se5rDM69B+JWppBXtSir1JGuXADaRLLMP8VlxJX949iH0UYTKWKsrD4OgNNK5aUx24nAH494WPknBMlx4fCMIeWzy7K3sEZkNUn/+A+eHraqIFfbGpveSCNM+8EqEjMgA+YRgg3eig==; _naver_usersession_=Kkgzim/64JicPJzgkIIvqQ==; page_uid=jesTPsqVWUZssE4qJeossssssD0-011300; SRT5=1762662010; BUC=z5Fu3sAYtFwpbRDrrDFYdn4AgK5hNkOqX-DdaLU7VJM=" NAVER_COOKIES="NAC=mQ7mBownbQf4A; NNB=TQPII6AKDBFGQ; PLACE_LANGUAGE=ko; NACT=1; nid_inf=1431570813; NID_AUT=k2T7FraXOdIMRCHzEZIFtHQup+I7b87M5fd7+p65AXZTdGB/gelRmW8s/Q4oDxm8; tooltipDisplayed=true; SRT30=1762660151; NID_SES=AAAB1Lpy3y3hGzuPbJpJl8vvFx18C+HXXuZEFou/YPgocHe7k2/5MpFlgE48X1JF7c7IPoU2khZKkkuLx+tsvWAzOf0TnG/G8RrBGeawnSluSJcKcTdKKRJ4cygKc/OabVxoc3TNZJWxer3vFtXBoXkDS5querVNS6wvcMhA/p4vkPKOeepwKLR+1IJERlQJWZw4q29IdAysrbBNn3Akf9mDA5eTYvMDLYyRkToRh10TVMW/yhyNQeMXlIdnR8U1ZCNqe/9ErYdos5gQDstswEJQQA0T2cHFGJOtmlYMPlnhWado5w521iZXGJyKcA9ZawizM/i5nK5xNYtPGS3cvImUYl6B5ulIipUJSqpj8v2XstK0TZlOGxHToXaVDrCNmSfCA9vFYbTb6xJHB2JRAT3Jik/z6QgLjJLBWRnsucMDqldxoiEDAUHEhY3pjgZ89quR3c3hwAuTlI9hBn5I3e5VQR0Y/GxoS9mIkMF8pJmcGneqnE0BNIt91RN6Se5rDM69B+JWppBXtSir1JGuXADaRLLMP8VlxJX949iH0UYTKWKsrD4OgNNK5aUx24nAH494WPknBMlx4fCMIeWzy7K3sEZkNUn/+A+eHraqIFfbGpveSCNM+8EqEjMgA+YRgg3eig==; _naver_usersession_=Kkgzim/64JicPJzgkIIvqQ==; page_uid=jesTPsqVWUZssE4qJeossssssD0-011300; SRT5=1762662010; BUC=z5Fu3sAYtFwpbRDrrDFYdn4AgK5hNkOqX-DdaLU7VJM="
OVERVIEW_QUERY = """ OVERVIEW_QUERY = '''
query getAccommodation($id: String!, $deviceType: String) { query getAccommodation($id: String!, $deviceType: String) {
business: placeDetail(input: {id: $id, isNx: true, deviceType: $deviceType}) { business: placeDetail(input: {id: $id, isNx: true, deviceType: $deviceType}) {
base { base {
@@ -25,52 +26,59 @@ query getAccommodation($id: String!, $deviceType: String) {
images { images { origin url } } images { images { origin url } }
cpImages(source: [ugcImage]) { images { origin url } } cpImages(source: [ugcImage]) { images { origin url } }
} }
}""" }'''
REQUEST_HEADERS = { REQUEST_HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
"Referer": "https://map.naver.com/", "Referer": "https://map.naver.com/",
"Origin": "https://map.naver.com", "Origin": "https://map.naver.com",
"Content-Type": "application/json", "Content-Type": "application/json",
"Cookie": NAVER_COOKIES, "Cookie": NAVER_COOKIES
} }
class GraphQLException(Exception): class GraphQLException(Exception):
pass pass
class nvMapScraper():
class nvMapScraper:
url : str = None url : str = None
scrap_type : str = None scrap_type : str = None
rawdata : dict = None rawdata : dict = None
image_link_list : list[str] = None image_link_list : list[str] = None
base_info : dict = None base_info : dict = None
def __init__(self, url): def __init__(self, url):
self.url = url self.url = url
def parse_url(self): async def parse_url(self):
place_pattern = r"/place/(\d+)" if 'place' not in self.url:
if 'naver.me' in self.url:
async with aiohttp.ClientSession() as session:
async with session.get(self.url) as response:
self.url = str(response.url)
else:
raise GraphQLException("this shorten url not have place id")
try: try:
place_id = re.search(place_pattern, self.url)[1] place_id = re.search(PLACE_PATTERN, self.url)[1]
except: except Exception as E:
raise GraphQLException() raise GraphQLException("Cannot find place id")
return place_id return place_id
async def scrap(self): async def scrap(self):
try: try:
place_id = self.parse_url() place_id = await self.parse_url()
data = await self.call_get_accomodation(place_id) data = await self.call_get_accomodation(place_id)
self.rawdata = data self.rawdata = data
self.image_link_list = [ fac_data = await self.get_facility_string(place_id)
nv_image["origin"] self.rawdata['facilities'] = fac_data
for nv_image in data["data"]["business"]["images"]["images"] self.image_link_list = [nv_image['origin'] for nv_image in data['data']['business']['images']['images']]
] self.base_info = data['data']['business']['base']
self.base_info = data["data"]["business"]["base"] self.facility_info = fac_data
self.scrap_type = "GraphQL" self.scrap_type = "GraphQL"
except GraphQLException: except GraphQLException as G:
print (G)
print("fallback") print("fallback")
self.scrap_type = "Playwright" self.scrap_type = "Playwright"
pass # 나중에 pw 이용한 crawling으로 fallback 추가 pass # 나중에 pw 이용한 crawling으로 fallback 추가
@@ -86,20 +94,26 @@ class nvMapScraper:
json_payload = json.dumps(payload) json_payload = json.dumps(payload)
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.post( async with session.post(GRAPHQL_URL, data=json_payload, headers=REQUEST_HEADERS) as response:
GRAPHQL_URL, data=json_payload, headers=REQUEST_HEADERS response.encoding = 'utf-8'
) as response:
response.encoding = "utf-8"
if response.status == 200: # 요청 성공 if response.status == 200: # 요청 성공
return await response.json() # await 주의 return await response.json() # await 주의
else: # 요청 실패 else: # 요청 실패
print("실패 상태 코드:", response.status) print('실패 상태 코드:', response.status)
print(response.text)
raise Exception() raise Exception()
async def get_facility_string(self, place_id):
url = f"https://pcmap.place.naver.com/accommodation/{place_id}/home"
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=REQUEST_HEADERS) as response:
soup = bs4.BeautifulSoup(await response.read(), 'html.parser')
c_elem = soup.find('span', 'place_blind', string='편의')
facilities = c_elem.parent.parent.find('div').string
return facilities
url = "https://map.naver.com/p/search/%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84/place/1133638931?c=14.70,0,0,0,dh&placePath=/photo?businessCategory=pension&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5&timestamp=202512191123&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5&timestamp=202512191007&from=map&entry=bmp&filterType=%EC%97%85%EC%B2%B4&businessCategory=pension" # url = "https://naver.me/IgJGCCic"
scraper = nvMapScraper(url) # scraper = nvMapScraper(url)
asyncio.run(scraper.scrap()) # asyncio.run(scraper.scrap())
print(scraper.image_link_list) # print(scraper.image_link_list)
print(len(scraper.image_link_list)) # print(len(scraper.image_link_list))
print(scraper.base_info)
+239
View File
@@ -0,0 +1,239 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 26,
"id": "99398cc7-e36a-494c-88f9-b26874ff0294",
"metadata": {},
"outputs": [],
"source": [
"import aiohttp\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "28c3e49b-1133-4a18-ab70-fd321b4d2734",
"metadata": {},
"outputs": [],
"source": [
"SUNO_API_KEY= '347da228e2d6ea273ef0558795a75892'\n",
"SUNO_BASE_URL=\"https://api.sunoapi.org\"\n",
"SUNO_TIMESTAPM_ROUTE = \"/api/v1/generate/get-timestamped-lyrics\"\n",
"SUNO_DETAIL_ROUTE = \"/api/v1/generate/record-info\"\n",
"suno_task_id = \"46bc90e6a2f9e9af58d7017e23f2115e\"\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "fe09b1d5-7198-4c40-9667-d7d0885c62a3",
"metadata": {},
"outputs": [],
"source": [
"headers = {\n",
" \"Authorization\": f\"Bearer {SUNO_API_KEY}\",\n",
" \"Content-Type\": \"application/json\",\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "81bacedc-e488-4d04-84b1-8e8a06a64565",
"metadata": {},
"outputs": [],
"source": [
"async def get_suno_audio_id_from_task_id(suno_task_id): # expire if db save audio id\n",
" url = f\"{SUNO_BASE_URL}{SUNO_DETAIL_ROUTE}\"\n",
" headers = {\"Authorization\": f\"Bearer {SUNO_API_KEY}\"}\n",
" async with aiohttp.ClientSession() as session:\n",
" async with session.get(url, headers=headers, params={\"taskId\" : suno_task_id}) as response:\n",
" detail = await response.json()\n",
" result = detail['data']['response']['sunoData'][0]['id']\n",
" return result "
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "26346a13-0663-489f-98d0-69743dd8553f",
"metadata": {},
"outputs": [],
"source": [
"async def get_suno_timestamp(suno_task_id, suno_audio_id): # expire if db save audio id\n",
" url = f\"{SUNO_BASE_URL}{SUNO_TIMESTAPM_ROUTE}\"\n",
" headers = {\"Authorization\": f\"Bearer {SUNO_API_KEY}\"}\n",
" payload = {\n",
" \"task_id\" : suno_task_id,\n",
" \"audio_id\" : suno_audio_id\n",
" }\n",
" async with aiohttp.ClientSession() as session:\n",
" async with session.post(url, headers=headers, data=json.dumps(payload)) as response:\n",
" result = await response.json()\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "78db0f6b-a54c-4415-9e82-972b00fefefb",
"metadata": {},
"outputs": [],
"source": [
"data = await get_suno_timestamp(suno_task_id, await get_suno_audio_id_from_task_id(suno_task_id))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "44d8da8e-5a67-4125-809f-bbdb1efba55f",
"metadata": {},
"outputs": [],
"source": [
"gt_lyric = \"\"\"\n",
"---\n",
"스테이,머뭄의 추억을 담아 \n",
"군산에서의 여행을 떠나보세 \n",
"인스타 감성 가득한 사진같은 하루, \n",
"힐링의 시간, 감성 숙소에서의 휴식\n",
"\n",
"은파호수공원의 자연 속, \n",
"시간이 멈춘 듯한 절골길을 걸어봐요 \n",
"Instagram vibes, 그림 같은 힐링 장소, \n",
"잊지 못할 여행 스토리 만들어지네\n",
"\n",
"넷이서 웃고 떠들던 그 날의 사진 속, \n",
"그 순간 훌쩍 떠나볼까요, 새로운 길로 \n",
"스테이,머뭄이 준비한 특별한 여행지 \n",
"몸과 마음이 따뜻해지는 그런 곳이에요 \n",
"---\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e4e9ba7d-964f-4f29-95f3-0f8514fad7ee",
"metadata": {},
"outputs": [],
"source": [
"lyric_line_list = gt_lyric.split(\"\\n\")\n",
"lyric_line_list = [lyric_line.strip(',. ') for lyric_line in lyric_line_list if lyric_line and lyric_line != \"---\"]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "84a64cd5-7374-4c33-8634-6ac6ed0de425",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['스테이,머뭄의 추억을 담아',\n",
" '군산에서의 여행을 떠나보세',\n",
" '인스타 감성 가득한 사진같은 하루',\n",
" '힐링의 시간, 감성 숙소에서의 휴식',\n",
" '은파호수공원의 자연 속',\n",
" '시간이 멈춘 듯한 절골길을 걸어봐요',\n",
" 'Instagram vibes, 그림 같은 힐링 장소',\n",
" '잊지 못할 여행 스토리 만들어지네',\n",
" '넷이서 웃고 떠들던 그 날의 사진 속',\n",
" '그 순간 훌쩍 떠나볼까요, 새로운 길로',\n",
" '스테이,머뭄이 준비한 특별한 여행지',\n",
" '몸과 마음이 따뜻해지는 그런 곳이에요']"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lyric_line_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17ca1a6e-c3a8-4683-958b-14bb3a46e63a",
"metadata": {},
"outputs": [],
"source": [
"matching = "
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "a8df83b4-99ef-4751-8c98-e5423c5c2494",
"metadata": {},
"outputs": [],
"source": [
"aligned_words = data['data']['alignedWords']"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "c1a1b2be-0796-4e40-b8bf-cd7c08e81e3e",
"metadata": {},
"outputs": [
{
"ename": "_IncompleteInputError",
"evalue": "incomplete input (2013651467.py, line 9)",
"output_type": "error",
"traceback": [
" \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[36]\u001b[39m\u001b[32m, line 9\u001b[39m\n\u001b[31m \u001b[39m\n ^\n\u001b[31m_IncompleteInputError\u001b[39m\u001b[31m:\u001b[39m incomplete input\n"
]
}
],
"source": [
"alignment_lyric = {}\n",
"lyric_index = 0 \n",
"for aligned_word in aligned_words:\n",
" if not aligned_word['succsess']:\n",
" continue\n",
" if aligned_word['word'] in lyric_line_list[lyric_index]:\n",
" if lyric_index in alignment_lyric:\n",
" raise Exception\n",
" else:\n",
" \n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c59c4eb1-d916-4d3a-8d02-a212b45f20ba",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@@ -0,0 +1,42 @@
from openai import OpenAI
from difflib import SequenceMatcher
from dataclasses import dataclass
from typing import List, Tuple
import aiohttp, json
@dataclass
class TimestampedLyric:
text: str
start: float
end: float
SUNO_BASE_URL="https://api.sunoapi.org"
SUNO_TIMESTAMP_ROUTE = "/api/v1/generate/get-timestamped-lyrics"
SUNO_DETAIL_ROUTE = "/api/v1/generate/record-info"
class LyricTimestampMapper:
suno_api_key : str
def __init__(self, suno_api_key):
self.suno_api_key = suno_api_key
async def get_suno_audio_id_from_task_id(self, suno_task_id): # expire if db save audio id
url = f"{SUNO_BASE_URL}{SUNO_DETAIL_ROUTE}"
headers = {"Authorization": f"Bearer {self.suno_api_key}"}
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers, params={"taskId" : suno_task_id}) as response:
detail = await response.json()
result = detail['data']['response']['sunoData'][0]['id']
return result
async def get_suno_timestamp(self, suno_task_id, suno_audio_id): # expire if db save audio id
url = f"{SUNO_BASE_URL}{SUNO_TIMESTAMP_ROUTE}"
headers = {"Authorization": f"Bearer {self.suno_api_key}"}
payload = {
"task_id" : suno_task_id,
"audio_id" : suno_audio_id
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, data=json.dumps(payload)) as response:
result = await response.json()
return result
+19
View File
@@ -0,0 +1,19 @@
from lyric_timestamp_mapper import LyricTimestampMapper
API_KEY = "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"
AUDIO_PATH = "test_audio.mp3"
GROUND_TRUTH_LYRICS = [
"첫 번째 가사 라인입니다",
"두 번째 가사 라인입니다",
"세 번째 가사 라인입니다",
]
mapper = LyricTimestampMapper(api_key=API_KEY)
result = mapper.map_ground_truth(AUDIO_PATH, GROUND_TRUTH_LYRICS)
for lyric in result:
if lyric.start >= 0:
print(f"[{lyric.start:.2f} - {lyric.end:.2f}] {lyric.text}")
else:
print(f"[매칭 실패] {lyric.text}")
+1
View File
@@ -9,6 +9,7 @@ dependencies = [
"aiohttp>=3.13.2", "aiohttp>=3.13.2",
"aiomysql>=0.3.2", "aiomysql>=0.3.2",
"asyncmy>=0.2.10", "asyncmy>=0.2.10",
"beautifulsoup4>=4.14.3",
"fastapi-cli>=0.0.16", "fastapi-cli>=0.0.16",
"fastapi[standard]>=0.125.0", "fastapi[standard]>=0.125.0",
"openai>=2.13.0", "openai>=2.13.0",
Generated
+24
View File
@@ -157,6 +157,19 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/3a/2a/7cc015f5b9f5db42b7d48157e23356022889fc354a2813c15934b7cb5c0e/attrs-25.4.0-py3-none-any.whl", hash = "sha256:adcf7e2a1fb3b36ac48d97835bb6d8ade15b8dcce26aba8bf1d14847b57a3373", size = 67615, upload-time = "2025-10-06T13:54:43.17Z" }, { url = "https://files.pythonhosted.org/packages/3a/2a/7cc015f5b9f5db42b7d48157e23356022889fc354a2813c15934b7cb5c0e/attrs-25.4.0-py3-none-any.whl", hash = "sha256:adcf7e2a1fb3b36ac48d97835bb6d8ade15b8dcce26aba8bf1d14847b57a3373", size = 67615, upload-time = "2025-10-06T13:54:43.17Z" },
] ]
[[package]]
name = "beautifulsoup4"
version = "4.14.3"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "soupsieve" },
{ name = "typing-extensions" },
]
sdist = { url = "https://files.pythonhosted.org/packages/c3/b0/1c6a16426d389813b48d95e26898aff79abbde42ad353958ad95cc8c9b21/beautifulsoup4-4.14.3.tar.gz", hash = "sha256:6292b1c5186d356bba669ef9f7f051757099565ad9ada5dd630bd9de5fa7fb86", size = 627737, upload-time = "2025-11-30T15:08:26.084Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/1a/39/47f9197bdd44df24d67ac8893641e16f386c984a0619ef2ee4c51fbbc019/beautifulsoup4-4.14.3-py3-none-any.whl", hash = "sha256:0918bfe44902e6ad8d57732ba310582e98da931428d231a5ecb9e7c703a735bb", size = 107721, upload-time = "2025-11-30T15:08:24.087Z" },
]
[[package]] [[package]]
name = "certifi" name = "certifi"
version = "2025.11.12" version = "2025.11.12"
@@ -751,6 +764,7 @@ dependencies = [
{ name = "aiohttp" }, { name = "aiohttp" },
{ name = "aiomysql" }, { name = "aiomysql" },
{ name = "asyncmy" }, { name = "asyncmy" },
{ name = "beautifulsoup4" },
{ name = "fastapi", extra = ["standard"] }, { name = "fastapi", extra = ["standard"] },
{ name = "fastapi-cli" }, { name = "fastapi-cli" },
{ name = "openai" }, { name = "openai" },
@@ -775,6 +789,7 @@ requires-dist = [
{ name = "aiohttp", specifier = ">=3.13.2" }, { name = "aiohttp", specifier = ">=3.13.2" },
{ name = "aiomysql", specifier = ">=0.3.2" }, { name = "aiomysql", specifier = ">=0.3.2" },
{ name = "asyncmy", specifier = ">=0.2.10" }, { name = "asyncmy", specifier = ">=0.2.10" },
{ name = "beautifulsoup4", specifier = ">=4.14.3" },
{ name = "fastapi", extras = ["standard"], specifier = ">=0.125.0" }, { name = "fastapi", extras = ["standard"], specifier = ">=0.125.0" },
{ name = "fastapi-cli", specifier = ">=0.0.16" }, { name = "fastapi-cli", specifier = ">=0.0.16" },
{ name = "openai", specifier = ">=2.13.0" }, { name = "openai", specifier = ">=2.13.0" },
@@ -1241,6 +1256,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235, upload-time = "2024-02-25T23:20:01.196Z" }, { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235, upload-time = "2024-02-25T23:20:01.196Z" },
] ]
[[package]]
name = "soupsieve"
version = "2.8.1"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/89/23/adf3796d740536d63a6fbda113d07e60c734b6ed5d3058d1e47fc0495e47/soupsieve-2.8.1.tar.gz", hash = "sha256:4cf733bc50fa805f5df4b8ef4740fc0e0fa6218cf3006269afd3f9d6d80fd350", size = 117856, upload-time = "2025-12-18T13:50:34.655Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/48/f3/b67d6ea49ca9154453b6d70b34ea22f3996b9fa55da105a79d8732227adc/soupsieve-2.8.1-py3-none-any.whl", hash = "sha256:a11fe2a6f3d76ab3cf2de04eb339c1be5b506a8a47f2ceb6d139803177f85434", size = 36710, upload-time = "2025-12-18T13:50:33.267Z" },
]
[[package]] [[package]]
name = "sqladmin" name = "sqladmin"
version = "0.22.0" version = "0.22.0"