리포트/플랜에 브랜드·영문채널 반영
- overrides에 brandAssets·영문 인스타/페북 audit 보장 (채널별 빌더 분리) - logoRules·other_channels·channel_scores 프롬프트 수정, 스키마 입력 필드 추가 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4855d44381
commit
238c16334b
5 files changed
+211
-62
No files matched your search
+129
-59
@@ -39,6 +39,12 @@ async def generate_report(analysis_run_id: str) -> ReportOutput:
|
||||
"market_keywords": _json(market.get("keywords")),
|
||||
"market_trend": _json(market.get("trend")),
|
||||
"market_target_audience": _json(market.get("target_audience")),
|
||||
"branding": _json(clinic.get("branding")),
|
||||
"brand_assets": _json(clinic.get("brandAssets")),
|
||||
"tiktok": _json(clinic.get("tiktok")),
|
||||
"instagram_en": _json(clinic.get("instagramEn")),
|
||||
"facebook_en": _json(clinic.get("facebookEn")),
|
||||
"channel_logos": _json(clinic.get("channelLogos")),
|
||||
**{
|
||||
channel: _json(data)
|
||||
for channel, data in raw.items()
|
||||
@@ -78,11 +84,110 @@ async def generate_plan(analysis_run_id: str) -> PlanOutput:
|
||||
"market_keywords": _json(market.get("keywords")),
|
||||
"market_trend": _json(market.get("trend")),
|
||||
"market_target_audience": _json(market.get("target_audience")),
|
||||
"tiktok": _json(clinic.get("tiktok")),
|
||||
"instagram_en": _json(clinic.get("instagramEn")),
|
||||
"facebook_en": _json(clinic.get("facebookEn")),
|
||||
"channel_logos": _json(clinic.get("channelLogos")),
|
||||
"brand_assets": _json(clinic.get("brandAssets")),
|
||||
}
|
||||
|
||||
return await LLMService(provider="perplexity").generate(plan_prompt, input_data)
|
||||
|
||||
|
||||
def _en_instagram_account(d: dict) -> dict:
|
||||
"""영문 인스타 raw_data → InstagramAccount dict (factual 값 + 빈 정성필드). audit 보강용."""
|
||||
return {
|
||||
"handle": d["username"], "language": "EN", "label": "인스타그램 EN",
|
||||
"posts": d.get("posts") or 0, "followers": d.get("followers") or 0,
|
||||
"following": d.get("following") or 0, "category": "",
|
||||
"profile_link": f"https://www.instagram.com/{d['username']}/",
|
||||
"highlights": [], "reels_count": 0, "content_format": "", "profile_photo": "",
|
||||
"bio": d.get("bio") or "",
|
||||
}
|
||||
|
||||
|
||||
def _en_facebook_page(d: dict) -> dict:
|
||||
"""영문 페북 raw_data → FacebookPage dict (factual 값 + 빈 정성필드). audit 보강용."""
|
||||
url = d.get("pageUrl") or ""
|
||||
return {
|
||||
"url": url, "page_name": d.get("pageName") or "", "language": "EN", "label": "페이스북 EN",
|
||||
"followers": d.get("followers") or 0, "following": 0,
|
||||
"category": ", ".join(d.get("categories") or []), "bio": d.get("intro") or "",
|
||||
"logo": "", "logo_description": "", "link": url, "linked_domain": d.get("website") or "",
|
||||
"reviews": 0, "recent_post_age": "", "has_whatsapp": False,
|
||||
}
|
||||
|
||||
|
||||
def _clinic_snapshot(brand_assets: dict, g: dict) -> dict:
|
||||
"""brandAssets(색·로고) + 강남언니(평점/리뷰/대표의) → clinic_snapshot 정확값."""
|
||||
snap: dict = {}
|
||||
if brand_assets.get("brand_colors"): snap["brand_colors"] = brand_assets["brand_colors"]
|
||||
if brand_assets.get("logo_images"): snap["logo_images"] = brand_assets["logo_images"]
|
||||
if g.get("name"): snap["name"] = g["name"]
|
||||
if g.get("rating"): snap["overall_rating"] = g["rating"]
|
||||
if g.get("totalReviews"): snap["total_reviews"] = g["totalReviews"]
|
||||
if g.get("address"): snap["location"] = g["address"]
|
||||
if g.get("badges"): snap["certifications"] = g["badges"]
|
||||
if g.get("totalMajorStaffs"): snap["staff_count"] = g["totalMajorStaffs"]
|
||||
doctors = g.get("doctors", [])
|
||||
if doctors:
|
||||
lead = max(doctors, key=lambda d: d.get("reviews", 0))
|
||||
snap["lead_doctor"] = {
|
||||
"name": lead.get("name"), "credentials": lead.get("specialty"),
|
||||
"rating": lead.get("rating"), "review_count": lead.get("reviews"),
|
||||
}
|
||||
return snap
|
||||
|
||||
|
||||
def _instagram_patch(ig: dict) -> dict:
|
||||
"""instagram_data(KR) → instagram_audit.accounts factual 덮어쓰기 값."""
|
||||
p: dict = {}
|
||||
if ig.get("username"):
|
||||
p["handle"] = ig["username"]
|
||||
p["profile_link"] = f"https://www.instagram.com/{ig['username']}/"
|
||||
if ig.get("posts"): p["posts"] = ig["posts"]
|
||||
if ig.get("followers"): p["followers"] = ig["followers"]
|
||||
if ig.get("following"): p["following"] = ig["following"]
|
||||
if ig.get("bio"): p["bio"] = ig["bio"]
|
||||
return p
|
||||
|
||||
|
||||
def _facebook_patch(fb: dict) -> dict:
|
||||
"""facebook_data(KR) → facebook_audit.pages factual 덮어쓰기 값."""
|
||||
p: dict = {}
|
||||
if fb.get("pageUrl"):
|
||||
p["url"] = fb["pageUrl"]
|
||||
p["link"] = fb["pageUrl"]
|
||||
if fb.get("pageName"): p["page_name"] = fb["pageName"]
|
||||
if fb.get("followers"): p["followers"] = fb["followers"]
|
||||
if fb.get("intro"): p["bio"] = fb["intro"]
|
||||
if fb.get("categories"): p["category"] = ", ".join(fb["categories"])
|
||||
if fb.get("website"): p["linked_domain"] = fb["website"]
|
||||
return p
|
||||
|
||||
|
||||
def _youtube_patch(yt: dict) -> dict:
|
||||
"""youtube_data → youtube_audit factual 덮어쓰기 값."""
|
||||
p: dict = {}
|
||||
if yt.get("channelName"): p["channel_name"] = yt["channelName"]
|
||||
if yt.get("handle"): p["handle"] = yt["handle"]
|
||||
if yt.get("subscribers"): p["subscribers"] = yt["subscribers"]
|
||||
if yt.get("totalVideos"): p["total_videos"] = yt["totalVideos"]
|
||||
if yt.get("totalViews"): p["total_views"] = yt["totalViews"]
|
||||
if yt.get("publishedAt"): p["channel_created_date"] = yt["publishedAt"][:10]
|
||||
if yt.get("description"): p["channel_description"] = yt["description"]
|
||||
if yt.get("videos"):
|
||||
p["top_videos"] = [
|
||||
{
|
||||
"title": v["title"], "views": v["views"], "duration": v.get("duration"),
|
||||
"type": "Short" if "M" not in v.get("duration", "") else "Long",
|
||||
"uploaded_ago": v.get("date", "")[:10],
|
||||
}
|
||||
for v in yt["videos"]
|
||||
]
|
||||
return p
|
||||
|
||||
|
||||
async def _build_overrides(analysis_run_id: str) -> dict:
|
||||
run = await fetchone(
|
||||
"SELECT hospital_id, instagram_data_id, facebook_data_id,"
|
||||
@@ -100,68 +205,15 @@ async def _build_overrides(analysis_run_id: str) -> dict:
|
||||
hospital = json.loads(hospital_row["raw_data"]) if hospital_row and isinstance(hospital_row.get("raw_data"), str) else (hospital_row or {}).get("raw_data") or {}
|
||||
instagram = await fetch_raw("instagram_data", run["instagram_data_id"]) or {}
|
||||
facebook = await fetch_raw("facebook_data", run["facebook_data_id"]) or {}
|
||||
naver_blog = await fetch_raw("naver_blog_data", run["naver_blog_data_id"]) or {}
|
||||
youtube = await fetch_raw("youtube_data", run["youtube_data_id"]) or {}
|
||||
gangnam_unni = await fetch_raw("gangnam_unni_data", run["gangnam_unni_data_id"]) or {}
|
||||
|
||||
snapshot: dict = {}
|
||||
|
||||
# ── gangnam_unni ──────────────────────────────────────────────────────────
|
||||
doctors = gangnam_unni.get("doctors", [])
|
||||
lead = max(doctors, key=lambda d: d.get("reviews", 0)) if doctors else None
|
||||
if gangnam_unni.get("name"): snapshot["name"] = gangnam_unni["name"]
|
||||
if gangnam_unni.get("rating"): snapshot["overall_rating"] = gangnam_unni["rating"]
|
||||
if gangnam_unni.get("totalReviews"): snapshot["total_reviews"] = gangnam_unni["totalReviews"]
|
||||
if gangnam_unni.get("address"): snapshot["location"] = gangnam_unni["address"]
|
||||
if gangnam_unni.get("badges"): snapshot["certifications"] = gangnam_unni["badges"]
|
||||
if gangnam_unni.get("totalMajorStaffs"): snapshot["staff_count"] = gangnam_unni["totalMajorStaffs"]
|
||||
if lead:
|
||||
snapshot["lead_doctor"] = {
|
||||
"name": lead.get("name"),
|
||||
"credentials": lead.get("specialty"),
|
||||
"rating": lead.get("rating"),
|
||||
"review_count": lead.get("reviews"),
|
||||
}
|
||||
|
||||
# ── instagram ─────────────────────────────────────────────────────────────
|
||||
ig_patch: dict = {}
|
||||
if instagram.get("username"): ig_patch["handle"] = instagram["username"]
|
||||
if instagram.get("posts"): ig_patch["posts"] = instagram["posts"]
|
||||
if instagram.get("followers"): ig_patch["followers"] = instagram["followers"]
|
||||
if instagram.get("following"): ig_patch["following"] = instagram["following"]
|
||||
if instagram.get("bio"): ig_patch["bio"] = instagram["bio"]
|
||||
if instagram.get("username"): ig_patch["profile_link"] = f"https://www.instagram.com/{instagram['username']}/"
|
||||
|
||||
# ── facebook ──────────────────────────────────────────────────────────────
|
||||
fb_patch: dict = {}
|
||||
if facebook.get("pageUrl"): fb_patch["url"] = facebook["pageUrl"]
|
||||
if facebook.get("pageUrl"): fb_patch["link"] = facebook["pageUrl"]
|
||||
if facebook.get("pageName"): fb_patch["page_name"] = facebook["pageName"]
|
||||
if facebook.get("followers"): fb_patch["followers"] = facebook["followers"]
|
||||
if facebook.get("intro"): fb_patch["bio"] = facebook["intro"]
|
||||
if facebook.get("categories"): fb_patch["category"] = ", ".join(facebook["categories"])
|
||||
if facebook.get("website"): fb_patch["linked_domain"] = facebook["website"]
|
||||
|
||||
# ── youtube ───────────────────────────────────────────────────────────────
|
||||
yt_patch: dict = {}
|
||||
if youtube.get("channelName"): yt_patch["channel_name"] = youtube["channelName"]
|
||||
if youtube.get("handle"): yt_patch["handle"] = youtube["handle"]
|
||||
if youtube.get("subscribers"): yt_patch["subscribers"] = youtube["subscribers"]
|
||||
if youtube.get("totalVideos"): yt_patch["total_videos"] = youtube["totalVideos"]
|
||||
if youtube.get("totalViews"): yt_patch["total_views"] = youtube["totalViews"]
|
||||
if youtube.get("publishedAt"): yt_patch["channel_created_date"] = youtube["publishedAt"][:10]
|
||||
if youtube.get("description"): yt_patch["channel_description"] = youtube["description"]
|
||||
if youtube.get("videos"):
|
||||
yt_patch["top_videos"] = [
|
||||
{
|
||||
"title": v["title"],
|
||||
"views": v["views"],
|
||||
"duration": v.get("duration"),
|
||||
"type": "Short" if "M" not in v.get("duration", "") else "Long",
|
||||
"uploaded_ago": v.get("date", "")[:10],
|
||||
}
|
||||
for v in youtube["videos"]
|
||||
]
|
||||
snapshot = _clinic_snapshot(hospital.get("brandAssets") or {}, gangnam_unni)
|
||||
ig_patch = _instagram_patch(instagram)
|
||||
fb_patch = _facebook_patch(facebook)
|
||||
yt_patch = _youtube_patch(youtube)
|
||||
ig_en = hospital.get("instagramEn") or {}
|
||||
fb_en = hospital.get("facebookEn") or {}
|
||||
|
||||
overrides: dict = {}
|
||||
if snapshot:
|
||||
@@ -172,6 +224,10 @@ async def _build_overrides(analysis_run_id: str) -> dict:
|
||||
overrides["facebook_audit"] = {"pages": [fb_patch]}
|
||||
if yt_patch:
|
||||
overrides["youtube_audit"] = yt_patch
|
||||
if ig_en.get("username"):
|
||||
overrides["_en_ig_account"] = _en_instagram_account(ig_en)
|
||||
if fb_en.get("pageUrl") or fb_en.get("pageName"):
|
||||
overrides["_en_fb_page"] = _en_facebook_page(fb_en)
|
||||
return overrides
|
||||
|
||||
|
||||
@@ -187,8 +243,22 @@ def _deep_merge(base: dict, overrides: dict) -> dict:
|
||||
base[k] = v
|
||||
return base
|
||||
|
||||
def _ensure_en_entry(audit: dict, list_key: str, en_entry: dict | None) -> None:
|
||||
"""audit 리스트(accounts/pages)에 EN 항목이 없으면 추가 — LLM 누락 대비, 중복 방지."""
|
||||
if not en_entry:
|
||||
return
|
||||
items = audit.setdefault(list_key, [])
|
||||
if not any(it.get("language") == "EN" for it in items):
|
||||
items.append(en_entry)
|
||||
|
||||
|
||||
def _patch_report(result: ReportOutput, overrides: dict) -> ReportOutput:
|
||||
en_ig = overrides.pop("_en_ig_account", None)
|
||||
en_fb = overrides.pop("_en_fb_page", None)
|
||||
merged = _deep_merge(result.model_dump(), overrides)
|
||||
# LLM이 audit에 영문 계정을 빠뜨려도 항상 KR+EN 둘 다 보장.
|
||||
_ensure_en_entry(merged.setdefault("instagram_audit", {}), "accounts", en_ig)
|
||||
_ensure_en_entry(merged.setdefault("facebook_audit", {}), "pages", en_fb)
|
||||
return ReportOutput(**merged)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user