File size: 5,185 Bytes
26ca8c8 9db6da0 f065a36 26ca8c8 5b4cfb6 26ca8c8 5b4cfb6 9db6da0 5b4cfb6 9db6da0 5b4cfb6 f065a36 5b4cfb6 9db6da0 5b4cfb6 f065a36 5b4cfb6 9db6da0 5b4cfb6 26ca8c8 f065a36 5b4cfb6 26ca8c8 5b4cfb6 9db6da0 5b4cfb6 f065a36 9db6da0 f065a36 9db6da0 f065a36 9db6da0 f065a36 5b4cfb6 9db6da0 5b4cfb6 f065a36 5b4cfb6 9db6da0 5b4cfb6 9db6da0 5b4cfb6 9db6da0 26ca8c8 5b4cfb6 9db6da0 5b4cfb6 9db6da0 5b4cfb6 9db6da0 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 |
"""
Square Theory Generator (10 best variations)
===========================================
2025โ05โ28ย v3 โ ํ ์
๋ ฅ โ ์ฐ์๋ ์ 10๊ฐ์ง ๊ฒฐ๊ณผ
------------------------------------------------
๋ณ๊ฒฝ ์์ฝ
---------
1. **LLM ํย ํ ํธ์ถ๋ก 10๊ฐ์ ์ฌ๊ฐํ ์ ์**
* JSON ๋ฐฐ์ด ํํ๋ก ๋ฐํ, ํ์ง์ด ๋์ ์์๋ก ์ ๋ ฌ (1ย โย 10)
2. **์ถ๋ ฅ**
* โ 1์ ์ ์์ ์ฌ๊ฐํ ๋์(Plot)
* โก 10๊ฐ ์ ์์ ๋งํฌ๋ค์ด ๋ฆฌ์คํธ๋ก ์ ๋ฆฌํ์ฌ ํ์
3. **UI ๊ฐ์ํ** : seed ์
๋ ฅย +ย ์คํ ๋ฒํผ + Plot + Markdown
4. **์๋ฌ ์ฒ๋ฆฌ ๊ฐํ**
์คํ๋ฒ
------
```bash
pip install --upgrade gradio matplotlib openai
export OPENAI_API_KEY="sk-..."
python square_theory_gradio.py
```
"""
import os
import json
import gradio as gr
import matplotlib.pyplot as plt
from matplotlib import patches, font_manager, rcParams
from openai import OpenAI
# -------------------------------------------------
# 0. ํ๊ธ ํฐํธ ์ค์
# -------------------------------------------------
def _set_korean_font():
for cand in ("Malgun Gothic", "NanumGothic", "AppleGothic", "DejaVu Sans"):
if cand in {f.name for f in font_manager.fontManager.ttflist}:
rcParams["font.family"] = cand
break
rcParams["axes.unicode_minus"] = False
_set_korean_font()
# -------------------------------------------------
# 1. OpenAI ํด๋ผ์ด์ธํธ
# -------------------------------------------------
if not os.getenv("OPENAI_API_KEY"):
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
client = OpenAI()
# -------------------------------------------------
# 2. Square Diagram
# -------------------------------------------------
def draw_square(words):
fig, ax = plt.subplots(figsize=(4, 4))
ax.add_patch(patches.Rectangle((0, 0), 1, 1, fill=False, linewidth=2))
ax.text(-0.05, 1.05, words["tl"], ha="right", va="bottom", fontsize=14, fontweight="bold")
ax.text(1.05, 1.05, words["tr"], ha="left", va="bottom", fontsize=14, fontweight="bold")
ax.text(1.05, -0.05, words["br"], ha="left", va="top", fontsize=14, fontweight="bold")
ax.text(-0.05, -0.05, words["bl"], ha="right", va="top", fontsize=14, fontweight="bold")
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlim(-0.2, 1.2)
ax.set_ylim(-0.2, 1.2)
ax.set_aspect("equal")
return fig
# -------------------------------------------------
# 3. LLM Prompt & Call
# -------------------------------------------------
SYSTEM_PROMPT = (
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
"์ฌ์ฉ์๊ฐ ์ค ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๊ฐ์ฅ ๋ฐ์ด๋ ๊ฒ๋ถํฐ 10๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. "
"๊ฐ ๋ฐฐ์ด ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ฉฐ, "
"์ฌ๊ฐํ ๋ค ๊ผญ์ง์ (tl>tr>br>bl)๊ณผ ๋ ํํยท์ฌ๋ก๊ฑดยท๋ธ๋๋ ๋ค์์ด ์์ฐ์ค๋ฝ๊ฒ ์ฐ๊ฒฐ๋ผ์ผ ํ๋ค. "
"๋ฐฐ์ด์ ์ต๊ณ ์ ์ ์์ด index 0, ๊ทธ๋ค์์ด index 1 โฆ 9 ์์์ฌ์ผ ํ๋ค. "
"๊ฒฐ๊ณผ๋ JSON ์ธ ๋ค๋ฅธ ํ
์คํธ๋ฅผ ํฌํจํ๋ฉด ์ ๋๋ค."
)
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=1024,
)
raw = resp.choices[0].message.content.strip()
try:
data = json.loads(raw)
if not isinstance(data, list) or len(data) != 10:
raise ValueError("JSON ๋ฐฐ์ด ๊ธธ์ด๊ฐ 10์ด ์๋")
except Exception as exc:
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ: {raw[:300]} โฆ")
return data
# -------------------------------------------------
# 4. Gradio callback
# -------------------------------------------------
def generate(seed_word: str):
results = call_llm(seed_word)
# 1์ ๋์
fig = draw_square({k: results[0][k] for k in ("tl", "tr", "br", "bl")})
# ๋ฆฌ์คํธ ๋งํฌ๋ค์ด
md_lines = []
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"
)
markdown_out = "\n".join(md_lines)
return fig, markdown_out
# -------------------------------------------------
# 5. UI
# -------------------------------------------------
with gr.Blocks(title="Square Theory โ Topย 10 ๐ฐ๐ท") as demo:
gr.Markdown("""# ๐ Square Theory ์ ์ Topย 10\n๋จ์ด 1๊ฐ โ LLM์ด ํ๊ฐยท์ ๋ ฌํ 10๊ฐ ์ฌ๊ฐํ/์นดํผ/๋ธ๋๋ ๋ค์""")
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ")
run = gr.Button("์์ฑ")
fig_out = gr.Plot(label="1์ ์ฌ๊ฐํ")
md_out = gr.Markdown(label="Topย 10 ์ ์")
run.click(generate, inputs=seed, outputs=[fig_out, md_out])
if __name__ == "__main__":
demo.launch()
|