File size: 4,804 Bytes
26ca8c8
e420f43
 
4be365e
dd53e8d
9db6da0
 
4be365e
 
 
9db6da0
 
 
f065a36
e420f43
f065a36
 
 
26ca8c8
 
5b4cfb6
 
26ca8c8
4be365e
 
5b4cfb6
 
e420f43
5b4cfb6
f065a36
5b4cfb6
 
 
 
 
e420f43
 
 
 
 
 
 
 
 
7f0c2bb
e420f43
7f0c2bb
 
e420f43
 
 
 
 
 
 
 
7f0c2bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4be365e
7f0c2bb
 
 
e420f43
 
 
 
 
 
7f0c2bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e420f43
 
 
 
 
7f0c2bb
e420f43
 
7f0c2bb
e420f43
 
 
7f0c2bb
e420f43
 
7f0c2bb
5b4cfb6
4be365e
 
 
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
"""
Square Theory Generator (Markdownโ€‘only, up to 20 ideas)
=====================================================
2025โ€‘05โ€‘28ย v10 โ— ImportErrorย fix (OpenAI error module)
---------------------------------------------------
๋ณ€๊ฒฝ ์š”์•ฝ
---------
* **๋ฌธ์ œ**: `from openai import OpenAI, error as oai_err` โ†’ v1ย SDK์—๋Š” `error` ์„œ๋ธŒ๋ชจ๋“ˆ์ด ์—†์Œ โ‡’ `ImportError`.
* **ํ•ด๊ฒฐ**: `import openai` ํ›„ `openai.OpenAIError` ์ฐธ์กฐ.  
  `except` ์ ˆ์„ `openai.OpenAIError` ๋กœ ๊ต์ฒด.

์‹คํ–‰๋ฒ•
------
```bash
pip install --upgrade gradio openai
export OPENAI_API_KEY="sk-..."
python square_theory_gradio.py
```
"""

import os
import json
import gradio as gr
import openai
from openai import OpenAI

# -------------------------------------------------
# 0. OpenAI ํด๋ผ์ด์–ธํŠธ
# -------------------------------------------------
if not os.getenv("OPENAI_API_KEY"):
    raise EnvironmentError("OPENAI_API_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์„ธ์š”.")

client = OpenAI()

# -------------------------------------------------
# 1. LLM Prompt & Utilities
# -------------------------------------------------
SYSTEM_PROMPT = (
    "๋„ˆ๋Š” ํ•œ๊ตญ์–ด ์นดํ”ผยท๋ธŒ๋žœ๋“œ ๋„ค์ด๋ฐ ์ „๋ฌธ๊ฐ€์ด์ž Square Theory ๋„์šฐ๋ฏธ๋‹ค. "
    "์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํ•˜๋‚˜์˜ ๋‹จ์–ด(tl)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ’ˆ์งˆ์ด ๋›ฐ์–ด๋‚œ ์ˆœ์„œ๋Œ€๋กœ ์ตœ๋Œ€ 20๊ฐœ์˜ ์ œ์•ˆ์„ JSON ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•ด๋ผ. "
    "๊ฐ ์›์†Œ๋Š” tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ•„๋“œ๋ฅผ ๊ฐ€์ง„๋‹ค. "
    "๋ฐฐ์—ด ์ฒซ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ ๊ฐ€์žฅ ์šฐ์ˆ˜ํ•ด์•ผ ํ•œ๋‹ค. JSON ์™ธ ๋ฌธ์ž๋Š” ๊ธˆ์ง€ํ•œ๋‹ค."
)

FALLBACK_MODELS = ["gpt-4o-mini", "gpt-4o", "gpt-4o-preview", "gpt-4-turbo"]


def _clean_json_block(text: str) -> str:
    text = text.strip()
    if text.startswith("```"):
        text = text.split("\n", 1)[1] if "\n" in text else text[3:]
        if text.endswith("```"):
            text = text[:-3]
    return text.strip()


def _call_llm(seed: str):
    last_exc = None
    for model in FALLBACK_MODELS:
        try:
            resp = client.chat.completions.create(
                model=model,
                messages=[
                    {"role": "system", "content": SYSTEM_PROMPT},
                    {"role": "user", "content": seed},
                ],
                temperature=0.9,
                max_tokens=2048,
            )
            cleaned = _clean_json_block(resp.choices[0].message.content)
            data = json.loads(cleaned) if cleaned else []
            if isinstance(data, dict):
                data = [data]
            if not isinstance(data, list) or not (1 <= len(data) <= 20):
                raise ValueError("LLM ์‘๋‹ต์ด ์˜ฌ๋ฐ”๋ฅธ 1โ€‘20๊ฐœ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹˜")
            return data
        except (openai.OpenAIError, json.JSONDecodeError, ValueError, TypeError) as exc:
            last_exc = exc
            continue
    raise RuntimeError(f"LLM ํ˜ธ์ถœ ์‹คํŒจ: {last_exc}")

# -------------------------------------------------
# 2. Gradio callback
# -------------------------------------------------

def generate(seed_word: str):
    seed_word = seed_word.strip()
    if not seed_word:
        return "โš ๏ธ **์‹œ๋“œ ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.**"
    try:
        results = _call_llm(seed_word)
        md = [f"## ์ด {len(results)}๊ฐœ ์ œ์•ˆ"]
        for idx, item in enumerate(results, 1):
            md.append(
                f"### {idx}. ๋ธŒ๋žœ๋“œ ๋„ค์ž„: {item['brand']}\n"
                f"**๋ฉ”์ธ ์นดํ”ผ**  \
                โ€ข ์ƒ๋‹จ: {item['top_phrase']}  \
                โ€ข ํ•˜๋‹จ: {item['bottom_phrase']}\n\n"
                f"**์Šฌ๋กœ๊ฑด**  \
                > {item['slogan']}\n\n"
                f"**์‚ฌ๊ฐํ˜• ํ‚ค์›Œ๋“œ**  \
                TL: {item['tl']} ยท TR: {item['tr']} ยท BR: {item['br']} ยท BL: {item['bl']}\n"
            )
        return "\n".join(md)
    except Exception as exc:
        return f"โŒ **์˜ค๋ฅ˜:** {exc}"

# -------------------------------------------------
# 3. UI
# -------------------------------------------------
with gr.Blocks(title="Square Theory โ€“ ์ตœ๋Œ€ 20๊ฐœ ๐Ÿ‡ฐ๐Ÿ‡ท") as demo:
    gr.Markdown("""# ๐ŸŸง Square Theory ์ œ์•ˆ (์ตœ๋Œ€ 20๊ฐœ)\n๋‹จ์–ด ํ•˜๋‚˜ ์ž…๋ ฅ โ†’ LLM์ด ์ •๋ ฌํ•œ ์‚ฌ๊ฐํ˜•/์นดํ”ผ/๋ธŒ๋žœ๋“œ ๋„ค์ž„""")
    seed = gr.Textbox(label="์‹œ๋“œ ๋‹จ์–ด(TL)", placeholder="์˜ˆ: ๊ณจ๋“ ")
    run = gr.Button("์ƒ์„ฑ")
    md_out = gr.Markdown()

    run.click(generate, inputs=seed, outputs=md_out)

    demo.queue().launch()

# -------------------------------------------------
# 4. (Optional) ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ์•„์ด๋””์–ด
# -------------------------------------------------
# - JSON์— "why" ํ•„๋“œ ์š”์ฒญ โ€” ๊ฐ ์ œ์•ˆ์˜ ์„ ์ • ์ด์œ ๋ฅผ ํ‘œ์‹œํ•ด ์„ค๋“๋ ฅ ๊ฐ•ํ™”
# - ํƒ€๊นƒ ์ฑ„๋„๋ณ„ ํƒœ๊ทธ๋ผ์ธ ๋ณ€ํ˜• ์˜ต์…˜(OHHยทSNSยทTV)
# - CSV ๋‹ค์šด๋กœ๋“œ ๋ฒ„ํŠผ์œผ๋กœ ๊ฒฐ๊ณผ ์ผ๊ด„ ์ €์žฅ