File size: 1,906 Bytes
1924d43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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."