이미지 크롤링 로직 변경 및 자막 이미지 매칭 보완
This commit is contained in:
1 parent
04d8614919
commit
46449a7a3f
20 files changed
+1828
-1116
No files matched your search
@@ -26,13 +26,12 @@ from app.dependencies.pagination import PaginationParams, get_pagination_params
|
||||
from app.user.dependencies.auth import get_current_user, get_current_user_optional
|
||||
from app.user.models import User
|
||||
from app.utils.pagination import PaginatedResponse
|
||||
from app.home.models import Image, Project, MarketingIntel, ImageTag
|
||||
from app.home.models import Image, Project, MarketingIntel
|
||||
from app.home.api.routers.v1.home import _extract_region_from_address
|
||||
from app.utils.address_parser import SIDO_CITIES, SIDO_SEARCH_ALIASES
|
||||
from app.lyric.models import Lyric
|
||||
from app.song.models import Song, SongTimestamp
|
||||
from app.utils.creatomate import CreatomateService
|
||||
from app.utils.subtitles import SubtitleContentsGenerator
|
||||
|
||||
from app.comment.models import Comment
|
||||
from app.database.like_cache import (
|
||||
@@ -59,7 +58,7 @@ from app.video.schemas.video_schema import (
|
||||
VideoThumbnailItem,
|
||||
)
|
||||
from app.video.worker.video_task import download_and_upload_video_to_blob
|
||||
from app.video.services.video import get_image_tags_by_task_id
|
||||
|
||||
|
||||
from config import creatomate_settings
|
||||
|
||||
@@ -199,16 +198,22 @@ async def generate_video(
|
||||
)
|
||||
marketing_intelligence: MarketingIntel = marketing_result.scalar_one_or_none()
|
||||
|
||||
# subtitle 미완료 시 즉시 반환 — Lyric/Song/Image 쿼리 전에 체크하여 불필요한 조회 방지
|
||||
# 자막 + 이미지 배정 미완료 시 즉시 반환 — Lyric/Song/Image 쿼리 전에 체크하여 불필요한 조회 방지
|
||||
# 클라이언트가 /lyric/subtitle/status/{task_id} 폴링 후 재시도
|
||||
if not marketing_intelligence.subtitle:
|
||||
logger.info(f"[generate_video] subtitle pending - task_id: {task_id}")
|
||||
if not marketing_intelligence.subtitle or not marketing_intelligence.image_match:
|
||||
pending_what = []
|
||||
if not marketing_intelligence.subtitle:
|
||||
pending_what.append("자막")
|
||||
if not marketing_intelligence.image_match:
|
||||
pending_what.append("이미지 배정")
|
||||
pending_msg = ", ".join(pending_what)
|
||||
logger.info(f"[generate_video] 사전 준비 미완료 ({pending_msg}) - task_id: {task_id}")
|
||||
return GenerateVideoResponse(
|
||||
success=False,
|
||||
status="subtitle_pending",
|
||||
task_id=task_id,
|
||||
creatomate_render_id=None,
|
||||
message="자막 생성이 아직 완료되지 않았습니다. /lyric/subtitle/status/{task_id}로 완료 확인 후 재요청하세요.",
|
||||
message=f"{pending_msg} 생성이 아직 완료되지 않았습니다. /lyric/subtitle/status/{{task_id}}로 완료 확인 후 재요청하세요.",
|
||||
error_message=None,
|
||||
)
|
||||
|
||||
@@ -365,25 +370,15 @@ async def generate_video(
|
||||
logger.debug(f"[generate_video] Template fetched - task_id: {task_id}")
|
||||
|
||||
# 6-2. elements에서 리소스 매핑 생성
|
||||
# modifications = creatomate_service.elements_connect_resource_blackbox(
|
||||
# elements=template["source"]["elements"],
|
||||
# image_url_list=image_urls,
|
||||
# music_url=music_url,
|
||||
# address=store_address
|
||||
taged_image_list = await get_image_tags_by_task_id(task_id)
|
||||
min_image_num = creatomate_service.counting_component(
|
||||
template = template,
|
||||
target_template_type = "image"
|
||||
)
|
||||
duplicate = bool(len(taged_image_list) < min_image_num)
|
||||
logger.info(f"[generate_video] Duplicate : {duplicate} | length of taged_image {len(taged_image_list)}, min_len {min_image_num},- task_id: {task_id}")
|
||||
modifications = creatomate_service.template_matching_taged_image(
|
||||
template = template,
|
||||
taged_image_list = taged_image_list,
|
||||
music_url = music_url,
|
||||
address = store_address,
|
||||
duplicate = duplicate,
|
||||
)
|
||||
# 이미지 배정은 /lyric/generate 사전 단계에서 이미 수행되어 marketing_intelligence.image_match에 저장됨.
|
||||
# 여기서는 사전 산출물을 읽어 음악 URL과 주소만 보완한다.
|
||||
modifications: dict = dict(marketing_intelligence.image_match) # 슬롯→image_url 사전 배정 결과
|
||||
modifications["audio-music"] = music_url
|
||||
# address_input 슬롯은 사전 단계에서도 채워지지만 영상 시점에 재확인
|
||||
for key in list(modifications.keys()):
|
||||
if "address_input" in key:
|
||||
modifications[key] = store_address
|
||||
logger.info(f"[generate_video] image_match loaded from DB (slots: {len(modifications)}) - task_id: {task_id}")
|
||||
logger.debug(f"[generate_video] Modifications created - task_id: {task_id}")
|
||||
|
||||
subtitle_modifications = marketing_intelligence.subtitle
|
||||
@@ -391,14 +386,14 @@ async def generate_video(
|
||||
modifications.update(subtitle_modifications)
|
||||
|
||||
# revert thumbnail scene
|
||||
# thumbnail_modifications = creatomate_service.make_thumbnail_modification(
|
||||
# brand_name =brand_name,
|
||||
# region = region,
|
||||
# brand_concept = brand_concept,
|
||||
# category_definition= category_definition,
|
||||
# target_keywords=target_keywords)
|
||||
thumbnail_modifications = creatomate_service.make_thumbnail_modification(
|
||||
brand_name =brand_name,
|
||||
region = region,
|
||||
brand_concept = brand_concept,
|
||||
category_definition= category_definition,
|
||||
target_keywords=target_keywords)
|
||||
|
||||
# modifications.update(thumbnail_modifications)
|
||||
modifications.update(thumbnail_modifications)
|
||||
|
||||
# 6-3. elements 수정
|
||||
new_elements = creatomate_service.modify_element(
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
"""
|
||||
Creative Assets Background Tasks
|
||||
|
||||
영상 생성 전 사전 준비(이미지 배정 + 자막 생성)를 수행하는 백그라운드 태스크를 정의합니다.
|
||||
"""
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.database.session import BackgroundSessionLocal
|
||||
from app.home.models import Project, MarketingIntel
|
||||
from app.utils.subtitles import SubtitleContentsGenerator
|
||||
from app.utils.creatomate import CreatomateService
|
||||
from app.utils.logger import get_logger
|
||||
from app.video.services.video import get_image_tags_by_task_id
|
||||
from app.utils.prompts.schemas.image import NARRATIVE_PHASE_LABEL
|
||||
|
||||
# 로거 설정
|
||||
logger = get_logger("video")
|
||||
|
||||
|
||||
async def generate_subtitle_background(
|
||||
orientation: str,
|
||||
task_id: str,
|
||||
max_retries: int = 3,
|
||||
) -> None:
|
||||
"""자막 + 이미지 배정을 사전에 수행하는 백그라운드 태스크.
|
||||
|
||||
내부적으로 `generate_creative_assets_background`를 호출한다.
|
||||
"""
|
||||
await generate_creative_assets_background(
|
||||
orientation=orientation,
|
||||
task_id=task_id,
|
||||
max_retries=max_retries,
|
||||
)
|
||||
|
||||
|
||||
async def generate_creative_assets_background(
|
||||
orientation: str,
|
||||
task_id: str,
|
||||
max_retries: int = 3,
|
||||
) -> None:
|
||||
"""이미지 배정 + 자막 생성을 사전에 수행하는 백그라운드 태스크.
|
||||
|
||||
실행 순서:
|
||||
1. 템플릿 조회 (읽기 전용, 렌더 없음)
|
||||
2. 이미지 매칭 — get_image_tags_by_task_id → template_matching_taged_image
|
||||
→ MarketingIntel.image_match 저장
|
||||
3. 자막 생성 — 배정된 이미지 컨텍스트(슬롯별 공간/단계 정보)를 pitching 리스트에 포함
|
||||
→ MarketingIntel.subtitle 저장
|
||||
"""
|
||||
logger.info(f"[generate_creative_assets_background] START - task_id: {task_id}, orientation: {orientation}")
|
||||
|
||||
for attempt in range(1, max_retries + 1):
|
||||
try:
|
||||
creatomate_service = CreatomateService(orientation=orientation)
|
||||
template = await creatomate_service.get_one_template_data(creatomate_service.template_id)
|
||||
|
||||
# ── 프로젝트 / 마케팅 인텔 로드 ──────────────────────────────────
|
||||
async with BackgroundSessionLocal() as session:
|
||||
project_result = await session.execute(
|
||||
select(Project)
|
||||
.where(Project.task_id == task_id)
|
||||
.order_by(Project.created_at.desc())
|
||||
.limit(1)
|
||||
)
|
||||
project = project_result.scalar_one_or_none()
|
||||
marketing_result = await session.execute(
|
||||
select(MarketingIntel).where(MarketingIntel.id == project.marketing_intelligence)
|
||||
)
|
||||
marketing_intelligence = marketing_result.scalar_one_or_none()
|
||||
|
||||
store_address = project.detail_region_info
|
||||
customer_name = project.store_name
|
||||
language = project.language or "Korean"
|
||||
logger.info(
|
||||
f"[generate_creative_assets_background] customer: {customer_name}, "
|
||||
f"address: {store_address}, language: {language} - task_id: {task_id}"
|
||||
)
|
||||
|
||||
# ── Step 1: 이미지 매칭 ─────────────────────────────────────────
|
||||
# marketing_acceptable 필터링은 크롤링 단계에서 이미 완료되었으므로 재필터링하지 않는다.
|
||||
taged_image_list = await get_image_tags_by_task_id(task_id)
|
||||
min_image_num = creatomate_service.counting_component(
|
||||
template=template,
|
||||
target_template_type="image",
|
||||
)
|
||||
duplicate = len(taged_image_list) < min_image_num
|
||||
logger.info(
|
||||
f"[generate_creative_assets_background] image matching — "
|
||||
f"pool={len(taged_image_list)}, slots={min_image_num}, "
|
||||
f"duplicate={duplicate} - task_id: {task_id}"
|
||||
)
|
||||
|
||||
image_modifications, assigned_image_tags = creatomate_service.template_matching_taged_image(
|
||||
template=template,
|
||||
taged_image_list=taged_image_list,
|
||||
music_url="", # 음악 URL은 영상 생성 시점에 결정 — 사전 단계에서는 빈 값
|
||||
address=store_address,
|
||||
duplicate=duplicate,
|
||||
)
|
||||
# audio-music은 영상 생성 시점에 덮어씌워지므로 image_match에서 제거
|
||||
image_match = {k: v for k, v in image_modifications.items() if k != "audio-music"}
|
||||
|
||||
logger.info(f"[generate_creative_assets_background] image_match: {list(image_match.keys())} - task_id: {task_id}")
|
||||
|
||||
# image_match 저장
|
||||
async with BackgroundSessionLocal() as session:
|
||||
marketing_result = await session.execute(
|
||||
select(MarketingIntel).where(MarketingIntel.id == project.marketing_intelligence)
|
||||
)
|
||||
mi = marketing_result.scalar_one_or_none()
|
||||
mi.image_match = image_match
|
||||
await session.commit()
|
||||
|
||||
logger.info(f"[generate_creative_assets_background] image_match saved - task_id: {task_id}")
|
||||
|
||||
# ── Step 2: 자막 생성 (이미지 컨텍스트 포함) ──────────────────────
|
||||
# 슬롯별 배정된 이미지의 공간/내러티브 정보를 pitching 레이블에 포함하여
|
||||
# LLM이 "이 자막이 나올 때 화면에 보이는 이미지"를 인지하고 자막을 작성할 수 있게 함.
|
||||
pitchings_raw = creatomate_service.extract_text_format_from_template(template)
|
||||
pitchings_with_context = _build_pitching_list_with_image_context(
|
||||
pitching_label_list=pitchings_raw,
|
||||
assigned_image_tags=assigned_image_tags,
|
||||
)
|
||||
|
||||
subtitle_generator = SubtitleContentsGenerator()
|
||||
generated_subtitles = await subtitle_generator.generate_subtitle_contents(
|
||||
marketing_intelligence=marketing_intelligence.intel_result,
|
||||
pitching_label_list=pitchings_with_context,
|
||||
customer_name=customer_name,
|
||||
detail_region_info=store_address,
|
||||
language=language,
|
||||
industry=project.industry,
|
||||
)
|
||||
pitching_output_list = generated_subtitles.pitching_results
|
||||
|
||||
subtitle_modifications = {
|
||||
pitching_output.pitching_tag: pitching_output.pitching_data
|
||||
for pitching_output in pitching_output_list
|
||||
}
|
||||
logger.info(f"[generate_creative_assets_background] subtitle_modifications: {subtitle_modifications}")
|
||||
|
||||
# subtitle 저장
|
||||
async with BackgroundSessionLocal() as session:
|
||||
marketing_result = await session.execute(
|
||||
select(MarketingIntel).where(MarketingIntel.id == project.marketing_intelligence)
|
||||
)
|
||||
mi = marketing_result.scalar_one_or_none()
|
||||
mi.subtitle = subtitle_modifications
|
||||
await session.commit()
|
||||
|
||||
logger.info(
|
||||
f"[generate_creative_assets_background] DONE - task_id: {task_id} "
|
||||
f"(attempt {attempt}/{max_retries})"
|
||||
)
|
||||
return
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"[generate_creative_assets_background] FAILED (attempt {attempt}/{max_retries}) "
|
||||
f"- task_id: {task_id}, error: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
if attempt < max_retries:
|
||||
logger.info(
|
||||
f"[generate_creative_assets_background] 재시도 중... "
|
||||
f"({attempt + 1}/{max_retries}) - task_id: {task_id}"
|
||||
)
|
||||
|
||||
logger.error(f"[generate_creative_assets_background] 모든 재시도 실패 - task_id: {task_id}")
|
||||
|
||||
|
||||
def _build_pitching_list_with_image_context(
|
||||
pitching_label_list: list[str],
|
||||
assigned_image_tags: dict[str, dict],
|
||||
) -> list[str]:
|
||||
"""pitching 레이블 리스트에 배정 이미지 컨텍스트를 포함하여 반환합니다.
|
||||
|
||||
슬롯명(`bedroom-furniture-tight_crop-slow_zoom_in-welcome`)의 5번째 토큰과
|
||||
pitching 레이어명(`subtitle-welcome-emotion_cue-empathic-001`)의 2번째 토큰이
|
||||
동일한 narrative_phase이므로 시간 겹침 계산 없이 phase로 직접 매칭합니다.
|
||||
|
||||
출력 예시:
|
||||
"subtitle-welcome-emotion_cue-empathic-001 | 화면: lobby / furniture / 진입(welcome) 단계"
|
||||
"""
|
||||
# narrative_phase → 해당 phase에 배정된 image_tag 매핑 구성
|
||||
# 슬롯명 형식: {space_type}-{subject}-{camera}-{motion}-{narrative_phase}
|
||||
phase_to_tag: dict[str, dict] = {}
|
||||
for slot_name, tag in assigned_image_tags.items():
|
||||
tokens = slot_name.split("-")
|
||||
if len(tokens) >= 5:
|
||||
phase = tokens[4]
|
||||
# 같은 phase에 슬롯이 여러 개면 첫 번째만 사용
|
||||
if phase not in phase_to_tag:
|
||||
phase_to_tag[phase] = tag
|
||||
|
||||
if not phase_to_tag:
|
||||
return pitching_label_list
|
||||
|
||||
result = []
|
||||
for label in pitching_label_list:
|
||||
# pitching 레이어명 형식: {track_role}-{narrative_phase}-{content_type}-{tone}-{pair_id}
|
||||
tokens = label.split("-")
|
||||
context_str = ""
|
||||
if len(tokens) >= 2:
|
||||
phase = tokens[1]
|
||||
tag = phase_to_tag.get(phase)
|
||||
if tag:
|
||||
space_types = tag.get("space_type", [])
|
||||
subjects = tag.get("subject", [])
|
||||
space_str = "/".join(space_types[:2]) if space_types else "?"
|
||||
subject_str = subjects[0] if subjects else "?"
|
||||
phase_str = NARRATIVE_PHASE_LABEL.get(phase, phase)
|
||||
context_str = f" | 화면: {space_str} / {subject_str} / {phase_str} 단계"
|
||||
|
||||
result.append(label + context_str)
|
||||
|
||||
return result
|
||||
Reference in new issue
Block a user