import json import logging from typing import Dict, Any, List from config import configure_gemini logger = logging.getLogger(__name__) def generate_comparison_summary(analyses: List[Dict[str, Any]], user_prompt: str) -> str: """ Generate a natural-language comparison between videos, given their AdAnalysis JSON and a user prompt. """ client = configure_gemini() try: analyses_json = json.dumps( [{"video": a.get("video_name"), "analysis": a.get("analysis")} for a in analyses], ensure_ascii=False, indent=2 ) except Exception: logger.exception("Failed to serialize analyses for comparison") analyses_json = "[]" system_prompt = ( "You are an expert video ad strategist. Compare multiple video ad analyses and return " "a clear, structured comparison. Highlight:\n" "- Hooks and opening strategies\n" "- Copywriting / psychology frameworks\n" "- Target audience differences\n" "- Average effectiveness scores\n" "- Major timestamp improvements\n" "- Strengths & weaknesses of each video\n\n" "Always structure output into sections and provide actionable insights." ) user_message = ( f"Here are the analyses for multiple videos:\n\n{analyses_json}\n\n" f"Now, based on this data, {user_prompt}." ) try: resp = client.chat.completions.create( model="gemini-2.0-flash", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_message} ], temperature=0.2, ) return resp.choices[0].message.content or "No summary generated." except Exception: logger.exception("Comparison summary generation failed") return "Failed to generate comparison summary."