File size: 7,910 Bytes
26ca8c8 e420f43 dd53e8d 9db6da0 e420f43 9db6da0 f065a36 e420f43 f065a36 26ca8c8 5b4cfb6 26ca8c8 5b4cfb6 e420f43 5b4cfb6 f065a36 5b4cfb6 e420f43 5b4cfb6 e420f43 5b4cfb6 e420f43 26ca8c8 5b4cfb6 e420f43 5b4cfb6 f065a36 9db6da0 e420f43 f065a36 673fa0a 9db6da0 f065a36 9db6da0 f065a36 5b4cfb6 e420f43 5b4cfb6 673fa0a 5b4cfb6 673fa0a dd53e8d e420f43 9db6da0 dd53e8d 5b4cfb6 e420f43 5b4cfb6 9db6da0 dd53e8d 9db6da0 e420f43 26ca8c8 5b4cfb6 e420f43 5b4cfb6 e420f43 5b4cfb6 9db6da0 dd53e8d 9db6da0 e420f43 26ca8c8 |
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
"""
Square Theory Generator (Markdownโonly, up to 20 ideas)
=====================================================
2025โ05โ28ย v7 โ ๋ฐ์ค ๋ค์ด์ด๊ทธ๋จ ์ ๊ฑฐ & ์ ์ย 20๊ฐ๋ก ํ์ฅ
---------------------------------------------------
๋ณ๊ฒฝ ์์ฝ
---------
1. **์ฌ๊ฐํ ์๊ฐํ ์ ๊ฑฐ** โ ์ถ๋ ฅ/์์กด์์ `matplotlib` ์ ๋ถ ์ญ์ .
2. **์ ์ ๊ฐ์ ์ํฅ** โ LLM์ด ์ต๋ **20๊ฐ** ์์ด๋์ด ๋ฐํ.
3. **์ ์ฐ ๊ธธ์ด ํ์ฉ** โ 1โฏโคโฏNโฏโคโฏ20 ๋ฆฌ์คํธ๋ฉด OK.
4. **UI ๋จ์ํ** โ ์๋ ์
๋ ฅ โ ๋ฒํผ โ ๋งํฌ๋ค์ด ๊ฒฐ๊ณผ.
์คํ๋ฒ
------
```bash
pip install --upgrade gradio openai
export OPENAI_API_KEY="sk-..."
python square_theory_gradio.py
```
"""
import os
import json
import gradio as gr
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 ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค."
)
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):
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": seed},
],
temperature=0.9,
max_tokens=2048,
)
raw = resp.choices[0].message.content
cleaned = clean_json_block(raw)
try:
data = json.loads(cleaned)
if isinstance(data, dict):
data = [data]
if not isinstance(data, list):
raise TypeError("LLM ์๋ต์ด ๋ฆฌ์คํธ๊ฐ ์๋")
if not 1 <= len(data) <= 20:
raise ValueError("์๋ต ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ 1~20 ๋ฒ์๋ฅผ ๋ฒ์ด๋จ")
except Exception as exc:
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ ์ผ๋ถ: {cleaned[:300]} โฆ")
return data
# -------------------------------------------------
# 2. Gradio callback
# -------------------------------------------------
def generate(seed_word: str):
results = call_llm(seed_word)
md_lines = [f"## ์ด {len(results)}๊ฐ ์ ์\n"]
for idx, item in enumerate(results, 1):
md_lines.append(
f"### {idx}. {item['top_phrase']} / {item['bottom_phrase']}\n"
f"- **์ฌ๋ก๊ฑด**: {item['slogan']}\n"
f"- **๋ธ๋๋ ๋ค์**: {item['brand']}\n"
f"- (tl={item['tl']}, tr={item['tr']}, br={item['br']}, bl={item['bl']})\n"
)
return "\n".join(md_lines)
# -------------------------------------------------
# 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(label="์ ์ ๋ชฉ๋ก")
run.click(generate, inputs=seed, outputs=md_out)
if __name__ == "__main__":
demo.launch()
"""
Square Theory Generator (Markdownโonly, up to 20 ideas)
=====================================================
2025โ05โ28ย v7 โ ๋ฐ์ค ๋ค์ด์ด๊ทธ๋จ ์ ๊ฑฐ & ์ ์ย 20๊ฐ๋ก ํ์ฅ
---------------------------------------------------
๋ณ๊ฒฝ ์์ฝ
---------
1. **์ฌ๊ฐํ ์๊ฐํ ์ ๊ฑฐ** โ ์ถ๋ ฅ/์์กด์์ `matplotlib` ์ ๋ถ ์ญ์ .
2. **์ ์ ๊ฐ์ ์ํฅ** โ LLM์ด ์ต๋ **20๊ฐ** ์์ด๋์ด ๋ฐํ.
3. **์ ์ฐ ๊ธธ์ด ํ์ฉ** โ 1โฏโคโฏNโฏโคโฏ20 ๋ฆฌ์คํธ๋ฉด OK.
4. **UI ๋จ์ํ** โ ์๋ ์
๋ ฅ โ ๋ฒํผ โ ๋งํฌ๋ค์ด ๊ฒฐ๊ณผ.
์คํ๋ฒ
------
```bash
pip install --upgrade gradio openai
export OPENAI_API_KEY="sk-..."
python square_theory_gradio.py
```
"""
import os
import json
import gradio as gr
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 ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค."
)
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):
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": seed},
],
temperature=0.9,
max_tokens=2048,
)
raw = resp.choices[0].message.content
cleaned = clean_json_block(raw)
try:
data = json.loads(cleaned)
if isinstance(data, dict):
data = [data]
if not isinstance(data, list):
raise TypeError("LLM ์๋ต์ด ๋ฆฌ์คํธ๊ฐ ์๋")
if not 1 <= len(data) <= 20:
raise ValueError("์๋ต ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ 1~20 ๋ฒ์๋ฅผ ๋ฒ์ด๋จ")
except Exception as exc:
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ ์ผ๋ถ: {cleaned[:300]} โฆ")
return data
# -------------------------------------------------
# 2. Gradio callback
# -------------------------------------------------
def generate(seed_word: str):
results = call_llm(seed_word)
md_lines = [f"## ์ด {len(results)}๊ฐ ์ ์\n"]
for idx, item in enumerate(results, 1):
md_lines.append(
f"### {idx}. {item['top_phrase']} / {item['bottom_phrase']}\n"
f"- **์ฌ๋ก๊ฑด**: {item['slogan']}\n"
f"- **๋ธ๋๋ ๋ค์**: {item['brand']}\n"
f"- (tl={item['tl']}, tr={item['tr']}, br={item['br']}, bl={item['bl']})\n"
)
return "\n".join(md_lines)
# -------------------------------------------------
# 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(label="์ ์ ๋ชฉ๋ก")
run.click(generate, inputs=seed, outputs=md_out)
if __name__ == "__main__":
demo.launch()
|