File size: 5,496 Bytes
a3c17c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""4๊ฐœ ์˜ˆ์‹œ ์˜์ƒ ์ผ๊ด„ ๋ถ„์„ โ€” Streamlit ์—†์ด ๋น ๋ฅด๊ฒŒ ๊ฒ€์ฆ.

์‚ฌ์šฉ:
  python cli_test.py                              # `๋ฆฌ์ฅฌ์˜ฌ OKR/์˜์ƒ ์˜ˆ์‹œ ๋ชจ์Œ/` 4๊ฐœ ์ž๋™ ๋ถ„์„
  python cli_test.py path.mp4                     # ํŠน์ • ํŒŒ์ผ๋งŒ
  python cli_test.py path.mp4 --focus "..."       # ์ง‘์ค‘ ๋ถ„์„ ์งˆ๋ฌธ ์ถ”๊ฐ€ (7๋ฒˆ ์นดํ…Œ๊ณ ๋ฆฌ)
  python cli_test.py --focus "PDRN ๋†๋„๊ฐ€ ๋‚˜์˜ค๋‚˜?" # 4๊ฐœ ๋ชจ๋‘ ๋™์ผ ์งˆ๋ฌธ์œผ๋กœ
"""
from __future__ import annotations
import os
import sys
import tempfile
from pathlib import Path

# ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ๊ฒฝ๋กœ ์ถ”๊ฐ€
ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT))

import schema
import pipeline


DEFAULT_VIDEO_DIR = ROOT.parent / "์˜์ƒ ์˜ˆ์‹œ ๋ชจ์Œ"


def _load_secrets() -> str | None:
    """ํ™˜๊ฒฝ๋ณ€์ˆ˜ โ†’ .streamlit/secrets.toml ์ˆœ์œผ๋กœ API ํ‚ค ๋กœ๋“œ."""
    key = os.environ.get("ANTHROPIC_API_KEY")
    if key:
        return key
    secrets_path = ROOT / ".streamlit" / "secrets.toml"
    if secrets_path.exists():
        # toml ํŒŒ์‹ฑ (๊ฐ„๋‹จ)
        for line in secrets_path.read_text(encoding="utf-8").splitlines():
            line = line.strip()
            if line.startswith("ANTHROPIC_API_KEY"):
                _, _, v = line.partition("=")
                v = v.strip().strip('"').strip("'")
                if v and not v.startswith("sk-ant-์—ฌ๊ธฐ์—"):
                    return v
    return None


def _print_progress(label: str, pct: float) -> None:
    bar = "โ–ˆ" * int(pct * 30) + "โ–‘" * (30 - int(pct * 30))
    print(f"  [{bar}] {pct*100:5.1f}% โ€” {label}")


def analyze_file(client, mp4: Path, focus_question: str | None = None) -> None:
    print(f"\n{'โ•'*70}")
    print(f"โ–ถ ๋ถ„์„: {mp4.name}")
    if focus_question:
        print(f"  ์ง‘์ค‘ ์งˆ๋ฌธ: {focus_question}")
    print(f"{'โ•'*70}")

    with tempfile.TemporaryDirectory(prefix="reelsbench_cli_") as tmp:
        try:
            a = pipeline.run_pipeline(
                client=client,
                input_source=mp4,
                workdir_root=Path(tmp),
                keep_mp4=False,
                progress=_print_progress,
                focus_question=focus_question,
            )
        except Exception as e:
            print(f"โŒ ์‹คํŒจ: {e}")
            import traceback; traceback.print_exc()
            return

    print(f"\nโœ… video_id={a.video_id}")
    print(f"   ๋น„์šฉ: ${a.cost_usd:.4f}")
    print(f"   ๊ฒ€์ฆ ์‹คํŒจ: {a.verification_failed_count}๊ฑด")
    print(f"\n๐Ÿ“Š ์นดํ…Œ๊ณ ๋ฆฌ ์š”์•ฝ:")
    print(f"  1. ์ œํ’ˆ/๋ธŒ๋žœ๋“œ:   {len(a.products)}๊ฑด")
    print(f"  2. ์†Œ๊ตฌ ํšจ๋Šฅ:     {len(a.efficacy_claims)}๊ฑด")
    print(f"  3. ํ•ต์‹ฌ ์„ฑ๋ถ„:     {len(a.ingredients)}๊ฑด")
    print(f"  4. ํ›„ํ‚น ํฌ์ธํŠธ:   {len(a.hooks)}๊ฑด")
    print(f"  5. ํ•™์ˆ /๋ฉ”์ปค๋‹ˆ์ฆ˜: {len(a.science)}๊ฑด")
    print(f"  6. ํƒ€๊นƒ & ํ†ค:     {len(a.audiences)}๊ฑด")
    if a.focus_answer:
        fa = a.focus_answer
        print(f"  7. ์ง‘์ค‘ ํฌ์ธํŠธ:   {'๋‹ต๋ณ€ ์žˆ์Œ' if fa.get('stated_in_video') else '์˜์ƒ์—์„œ ๋‹ต์„ ๋ชป ์ฐพ์Œ'}")

    # ์ž์„ธํ•œ ์ถœ๋ ฅ
    for cat_name, items in [
        ("์ œํ’ˆ", a.products), ("ํšจ๋Šฅ", a.efficacy_claims), ("์„ฑ๋ถ„", a.ingredients),
        ("ํ›„ํ‚น", a.hooks), ("ํ•™์ˆ ", a.science), ("ํƒ€๊นƒ", a.audiences),
    ]:
        if not items:
            continue
        print(f"\n  [{cat_name}]")
        for it in items:
            conf = it.get("confidence", 0)
            print(f"    ยท conf={conf:.2f} eid={it.get('evidence_id')} โ†’ {it}")

    if a.focus_answer:
        fa = a.focus_answer
        print(f"\n  [์ง‘์ค‘ ํฌ์ธํŠธ 7๋ฒˆ]")
        print(f"    Q: {a.focus_question}")
        print(f"    A: {fa.get('answer')}")
        print(f"    ๊ทผ๊ฑฐ evidence_ids: {fa.get('evidence_ids')}  (conf={fa.get('confidence'):.2f})")
        for q in (fa.get("key_quotes") or []):
            print(f"      \"{q}\"")


def main():
    key = _load_secrets()
    if not key:
        print("โŒ ANTHROPIC_API_KEY ์„ค์ • ์•ˆ ๋จ.")
        print("   .streamlit/secrets.toml ๋˜๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ์„ค์ • ํ›„ ์žฌ์‹คํ–‰.")
        sys.exit(1)

    from anthropic import Anthropic
    client = Anthropic(api_key=key)

    # --focus "..." ํŒŒ์‹ฑ (๊ฐ„๋‹จํ•œ ์ธ์ž ์ฒ˜๋ฆฌ)
    focus_question: str | None = None
    raw_args = sys.argv[1:]
    cleaned_args: list[str] = []
    i = 0
    while i < len(raw_args):
        if raw_args[i] == "--focus" and i + 1 < len(raw_args):
            focus_question = raw_args[i + 1]
            i += 2
        else:
            cleaned_args.append(raw_args[i])
            i += 1

    if cleaned_args:
        targets = [Path(a) for a in cleaned_args]
    else:
        if not DEFAULT_VIDEO_DIR.exists():
            print(f"โŒ ๊ธฐ๋ณธ ์˜์ƒ ํด๋” ์—†์Œ: {DEFAULT_VIDEO_DIR}")
            sys.exit(1)
        targets = sorted([p for p in DEFAULT_VIDEO_DIR.glob("*.mp4")])
        if not targets:
            print(f"โŒ {DEFAULT_VIDEO_DIR} ์•ˆ์— .mp4 ํŒŒ์ผ ์—†์Œ.")
            sys.exit(1)

    print(f"๐ŸŽฌ reels-bench CLI ํ…Œ์ŠคํŠธ โ€” {len(targets)}๊ฐœ ์˜์ƒ")
    print(f"   ๋ชจ๋ธ: {schema.HAIKU_MODEL} (Pass 1) + {schema.SONNET_MODEL} (Pass 2)")
    if focus_question:
        print(f"   ์ง‘์ค‘ ์งˆ๋ฌธ: {focus_question}")

    for mp4 in targets:
        analyze_file(client, mp4, focus_question=focus_question)

    print(f"\n{'โ•'*70}")
    print("โœ… ์ „์ฒด ์™„๋ฃŒ. Streamlit์œผ๋กœ ๊ฒฐ๊ณผ ๋ณด๊ธฐ: `streamlit run app.py`")


if __name__ == "__main__":
    main()