Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,19 @@
|
|
1 |
"""
|
2 |
-
Square Theory Generator (
|
3 |
-
|
4 |
-
2025โ05โ28
|
5 |
---------------------------------------------------
|
6 |
๋ณ๊ฒฝ ์์ฝ
|
7 |
---------
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
3. Markdown์ ์ค์ ๊ฐ์๋ก ํค๋ ์ถ๋ ฅ.
|
13 |
|
14 |
์คํ๋ฒ
|
15 |
------
|
16 |
```bash
|
17 |
-
pip install --upgrade gradio
|
18 |
export OPENAI_API_KEY="sk-..."
|
19 |
python square_theory_gradio.py
|
20 |
```
|
@@ -22,44 +21,11 @@ python square_theory_gradio.py
|
|
22 |
|
23 |
import os
|
24 |
import json
|
25 |
-
import tempfile
|
26 |
-
import urllib.request
|
27 |
import gradio as gr
|
28 |
-
import matplotlib.pyplot as plt
|
29 |
-
from matplotlib import patches, font_manager, rcParams
|
30 |
from openai import OpenAI
|
31 |
|
32 |
# -------------------------------------------------
|
33 |
-
# 0.
|
34 |
-
# -------------------------------------------------
|
35 |
-
PREFERRED_FONTS = ["Malgun Gothic", "NanumGothic", "AppleGothic", "DejaVu Sans"]
|
36 |
-
NANUM_URL = (
|
37 |
-
"https://github.com/google/fonts/raw/main/ofl/nanumgothic/"
|
38 |
-
"NanumGothic-Regular.ttf"
|
39 |
-
)
|
40 |
-
|
41 |
-
def _set_korean_font():
|
42 |
-
available = {f.name for f in font_manager.fontManager.ttflist}
|
43 |
-
for cand in PREFERRED_FONTS:
|
44 |
-
if cand in available:
|
45 |
-
rcParams["font.family"] = cand
|
46 |
-
break
|
47 |
-
else:
|
48 |
-
try:
|
49 |
-
tmp_dir = tempfile.gettempdir()
|
50 |
-
font_path = os.path.join(tmp_dir, "NanumGothic-Regular.ttf")
|
51 |
-
if not os.path.exists(font_path):
|
52 |
-
urllib.request.urlretrieve(NANUM_URL, font_path)
|
53 |
-
font_manager.fontManager.addfont(font_path)
|
54 |
-
rcParams["font.family"] = font_manager.FontProperties(fname=font_path).get_name()
|
55 |
-
except Exception as e:
|
56 |
-
print("[WARN] Font download failed, Korean text may break:", e)
|
57 |
-
rcParams["axes.unicode_minus"] = False
|
58 |
-
|
59 |
-
_set_korean_font()
|
60 |
-
|
61 |
-
# -------------------------------------------------
|
62 |
-
# 1. OpenAI ํด๋ผ์ด์ธํธ
|
63 |
# -------------------------------------------------
|
64 |
if not os.getenv("OPENAI_API_KEY"):
|
65 |
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
|
@@ -67,32 +33,120 @@ if not os.getenv("OPENAI_API_KEY"):
|
|
67 |
client = OpenAI()
|
68 |
|
69 |
# -------------------------------------------------
|
70 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
# -------------------------------------------------
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
fig, ax = plt.subplots(figsize=(4, 4))
|
75 |
-
ax.add_patch(patches.Rectangle((0, 0), 1, 1, fill=False, linewidth=2))
|
76 |
-
ax.text(-0.05, 1.05, str(words.get("tl", "")), ha="right", va="bottom", fontsize=14, fontweight="bold")
|
77 |
-
ax.text(1.05, 1.05, str(words.get("tr", "")), ha="left", va="bottom", fontsize=14, fontweight="bold")
|
78 |
-
ax.text(1.05, -0.05, str(words.get("br", "")), ha="left", va="top", fontsize=14, fontweight="bold")
|
79 |
-
ax.text(-0.05, -0.05, str(words.get("bl", "")), ha="right", va="top", fontsize=14, fontweight="bold")
|
80 |
-
ax.set_xticks([])
|
81 |
-
ax.set_yticks([])
|
82 |
-
ax.set_xlim(-0.2, 1.2)
|
83 |
-
ax.set_ylim(-0.2, 1.2)
|
84 |
-
ax.set_aspect("equal")
|
85 |
-
return fig
|
86 |
|
87 |
# -------------------------------------------------
|
88 |
-
#
|
89 |
# -------------------------------------------------
|
90 |
SYSTEM_PROMPT = (
|
91 |
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
|
92 |
-
"์ฌ์ฉ์๊ฐ
|
93 |
-
"๊ฐ
|
94 |
-
"
|
95 |
-
"๊ฒฐ๊ณผ๋ JSON ์ธ ํ
์คํธ๋ฅผ ํฌํจํ์ง ์์์ผ ํ๋ค."
|
96 |
)
|
97 |
|
98 |
|
@@ -113,30 +167,28 @@ def call_llm(seed: str):
|
|
113 |
{"role": "user", "content": seed},
|
114 |
],
|
115 |
temperature=0.9,
|
116 |
-
max_tokens=
|
117 |
)
|
118 |
raw = resp.choices[0].message.content
|
119 |
cleaned = clean_json_block(raw)
|
120 |
try:
|
121 |
data = json.loads(cleaned)
|
122 |
-
# ๋จ์ผ ๊ฐ์ฒด๋ฉด ๋ฆฌ์คํธ๋ก ๋ณํ
|
123 |
if isinstance(data, dict):
|
124 |
data = [data]
|
125 |
if not isinstance(data, list):
|
126 |
raise TypeError("LLM ์๋ต์ด ๋ฆฌ์คํธ๊ฐ ์๋")
|
127 |
-
if len(data)
|
128 |
-
raise ValueError("
|
129 |
except Exception as exc:
|
130 |
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ ์ผ๋ถ: {cleaned[:300]} โฆ")
|
131 |
return data
|
132 |
|
133 |
# -------------------------------------------------
|
134 |
-
#
|
135 |
# -------------------------------------------------
|
136 |
|
137 |
def generate(seed_word: str):
|
138 |
results = call_llm(seed_word)
|
139 |
-
fig = draw_square({k: results[0][k] for k in ("tl", "tr", "br", "bl")})
|
140 |
md_lines = [f"## ์ด {len(results)}๊ฐ ์ ์\n"]
|
141 |
for idx, item in enumerate(results, 1):
|
142 |
md_lines.append(
|
@@ -145,19 +197,18 @@ def generate(seed_word: str):
|
|
145 |
f"- **๋ธ๋๋ ๋ค์**: {item['brand']}\n"
|
146 |
f"- (tl={item['tl']}, tr={item['tr']}, br={item['br']}, bl={item['bl']})\n"
|
147 |
)
|
148 |
-
return
|
149 |
|
150 |
# -------------------------------------------------
|
151 |
-
#
|
152 |
# -------------------------------------------------
|
153 |
-
with gr.Blocks(title="Square Theory โ
|
154 |
-
gr.Markdown("""# ๐ง Square Theory ์ ์ (์ต๋
|
155 |
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ")
|
156 |
run = gr.Button("์์ฑ")
|
157 |
-
fig_out = gr.Plot(label="1์ ์ฌ๊ฐํ")
|
158 |
md_out = gr.Markdown(label="์ ์ ๋ชฉ๋ก")
|
159 |
|
160 |
-
run.click(generate, inputs=seed, outputs=
|
161 |
|
162 |
if __name__ == "__main__":
|
163 |
demo.launch()
|
|
|
1 |
"""
|
2 |
+
Square Theory Generator (Markdownโonly, up to 20 ideas)
|
3 |
+
=====================================================
|
4 |
+
2025โ05โ28ย v7 โ ๋ฐ์ค ๋ค์ด์ด๊ทธ๋จ ์ ๊ฑฐ & ์ ์ย 20๊ฐ๋ก ํ์ฅ
|
5 |
---------------------------------------------------
|
6 |
๋ณ๊ฒฝ ์์ฝ
|
7 |
---------
|
8 |
+
1. **์ฌ๊ฐํ ์๊ฐํ ์ ๊ฑฐ** โ ์ถ๋ ฅ/์์กด์์ `matplotlib` ์ ๋ถ ์ญ์ .
|
9 |
+
2. **์ ์ ๊ฐ์ ์ํฅ** โ LLM์ด ์ต๋ **20๊ฐ** ์์ด๋์ด ๋ฐํ.
|
10 |
+
3. **์ ์ฐ ๊ธธ์ด ํ์ฉ** โ 1โฏโคโฏNโฏโคโฏ20 ๋ฆฌ์คํธ๋ฉด OK.
|
11 |
+
4. **UI ๋จ์ํ** โ ์๋ ์
๋ ฅ โ ๋ฒํผ โ ๋งํฌ๋ค์ด ๊ฒฐ๊ณผ.
|
|
|
12 |
|
13 |
์คํ๋ฒ
|
14 |
------
|
15 |
```bash
|
16 |
+
pip install --upgrade gradio openai
|
17 |
export OPENAI_API_KEY="sk-..."
|
18 |
python square_theory_gradio.py
|
19 |
```
|
|
|
21 |
|
22 |
import os
|
23 |
import json
|
|
|
|
|
24 |
import gradio as gr
|
|
|
|
|
25 |
from openai import OpenAI
|
26 |
|
27 |
# -------------------------------------------------
|
28 |
+
# 0. OpenAI ํด๋ผ์ด์ธํธ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# -------------------------------------------------
|
30 |
if not os.getenv("OPENAI_API_KEY"):
|
31 |
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
|
|
|
33 |
client = OpenAI()
|
34 |
|
35 |
# -------------------------------------------------
|
36 |
+
# 1. LLM Prompt & Utilities
|
37 |
+
# -------------------------------------------------
|
38 |
+
SYSTEM_PROMPT = (
|
39 |
+
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
|
40 |
+
"์ฌ์ฉ์๊ฐ ์
๋ ฅํ ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๋ฐ์ด๋ ์์๋๋ก ์ต๋ 20๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. "
|
41 |
+
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. "
|
42 |
+
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ ํ๋ค. JSON ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค."
|
43 |
+
)
|
44 |
+
|
45 |
+
|
46 |
+
def clean_json_block(text: str) -> str:
|
47 |
+
text = text.strip()
|
48 |
+
if text.startswith("```"):
|
49 |
+
text = text.split("\n", 1)[1] if "\n" in text else text[3:]
|
50 |
+
if text.endswith("```"):
|
51 |
+
text = text[:-3]
|
52 |
+
return text.strip()
|
53 |
+
|
54 |
+
|
55 |
+
def call_llm(seed: str):
|
56 |
+
resp = client.chat.completions.create(
|
57 |
+
model="gpt-4o-mini",
|
58 |
+
messages=[
|
59 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
60 |
+
{"role": "user", "content": seed},
|
61 |
+
],
|
62 |
+
temperature=0.9,
|
63 |
+
max_tokens=2048,
|
64 |
+
)
|
65 |
+
raw = resp.choices[0].message.content
|
66 |
+
cleaned = clean_json_block(raw)
|
67 |
+
try:
|
68 |
+
data = json.loads(cleaned)
|
69 |
+
if isinstance(data, dict):
|
70 |
+
data = [data]
|
71 |
+
if not isinstance(data, list):
|
72 |
+
raise TypeError("LLM ์๋ต์ด ๋ฆฌ์คํธ๊ฐ ์๋")
|
73 |
+
if not 1 <= len(data) <= 20:
|
74 |
+
raise ValueError("์๋ต ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ 1~20 ๋ฒ์๋ฅผ ๋ฒ์ด๋จ")
|
75 |
+
except Exception as exc:
|
76 |
+
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ ์ผ๋ถ: {cleaned[:300]} โฆ")
|
77 |
+
return data
|
78 |
+
|
79 |
+
# -------------------------------------------------
|
80 |
+
# 2. Gradio callback
|
81 |
+
# -------------------------------------------------
|
82 |
+
|
83 |
+
def generate(seed_word: str):
|
84 |
+
results = call_llm(seed_word)
|
85 |
+
md_lines = [f"## ์ด {len(results)}๊ฐ ์ ์\n"]
|
86 |
+
for idx, item in enumerate(results, 1):
|
87 |
+
md_lines.append(
|
88 |
+
f"### {idx}. {item['top_phrase']} / {item['bottom_phrase']}\n"
|
89 |
+
f"- **์ฌ๋ก๊ฑด**: {item['slogan']}\n"
|
90 |
+
f"- **๋ธ๋๋ ๋ค์**: {item['brand']}\n"
|
91 |
+
f"- (tl={item['tl']}, tr={item['tr']}, br={item['br']}, bl={item['bl']})\n"
|
92 |
+
)
|
93 |
+
return "\n".join(md_lines)
|
94 |
+
|
95 |
+
# -------------------------------------------------
|
96 |
+
# 3. UI
|
97 |
+
# -------------------------------------------------
|
98 |
+
with gr.Blocks(title="Square Theory โ ์ต๋ 20๊ฐ ๐ฐ๐ท") as demo:
|
99 |
+
gr.Markdown("""# ๐ง Square Theory ์ ์ (์ต๋ 20๊ฐ)\n๋จ์ด ํ๋ ์
๋ ฅ โ LLM์ด ์ ์ํยท์ ๋ ฌํ ์ฌ๊ฐํ/์นดํผ/๋ธ๋๋ ๋ค์""")
|
100 |
+
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ")
|
101 |
+
run = gr.Button("์์ฑ")
|
102 |
+
md_out = gr.Markdown(label="์ ์ ๋ชฉ๋ก")
|
103 |
+
|
104 |
+
run.click(generate, inputs=seed, outputs=md_out)
|
105 |
+
|
106 |
+
if __name__ == "__main__":
|
107 |
+
demo.launch()
|
108 |
+
"""
|
109 |
+
Square Theory Generator (Markdownโonly, up to 20 ideas)
|
110 |
+
=====================================================
|
111 |
+
2025โ05โ28ย v7 โ ๋ฐ์ค ๋ค์ด์ด๊ทธ๋จ ์ ๊ฑฐ & ์ ์ย 20๊ฐ๋ก ํ์ฅ
|
112 |
+
---------------------------------------------------
|
113 |
+
๋ณ๊ฒฝ ์์ฝ
|
114 |
+
---------
|
115 |
+
1. **์ฌ๊ฐํ ์๊ฐํ ์ ๊ฑฐ** โ ์ถ๋ ฅ/์์กด์์ `matplotlib` ์ ๋ถ ์ญ์ .
|
116 |
+
2. **์ ์ ๊ฐ์ ์ํฅ** โ LLM์ด ์ต๋ **20๊ฐ** ์์ด๋์ด ๋ฐํ.
|
117 |
+
3. **์ ์ฐ ๊ธธ์ด ํ์ฉ** โ 1โฏโคโฏNโฏโคโฏ20 ๋ฆฌ์คํธ๋ฉด OK.
|
118 |
+
4. **UI ๋จ์ํ** โ ์๋ ์
๋ ฅ โ ๋ฒํผ โ ๋งํฌ๋ค์ด ๊ฒฐ๊ณผ.
|
119 |
+
|
120 |
+
์คํ๋ฒ
|
121 |
+
------
|
122 |
+
```bash
|
123 |
+
pip install --upgrade gradio openai
|
124 |
+
export OPENAI_API_KEY="sk-..."
|
125 |
+
python square_theory_gradio.py
|
126 |
+
```
|
127 |
+
"""
|
128 |
+
|
129 |
+
import os
|
130 |
+
import json
|
131 |
+
import gradio as gr
|
132 |
+
from openai import OpenAI
|
133 |
+
|
134 |
+
# -------------------------------------------------
|
135 |
+
# 0. OpenAI ํด๋ผ์ด์ธํธ
|
136 |
# -------------------------------------------------
|
137 |
+
if not os.getenv("OPENAI_API_KEY"):
|
138 |
+
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
|
139 |
|
140 |
+
client = OpenAI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
# -------------------------------------------------
|
143 |
+
# 1. LLM Prompt & Utilities
|
144 |
# -------------------------------------------------
|
145 |
SYSTEM_PROMPT = (
|
146 |
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
|
147 |
+
"์ฌ์ฉ์๊ฐ ์
๋ ฅํ ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๋ฐ์ด๋ ์์๋๋ก ์ต๋ 20๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. "
|
148 |
+
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. "
|
149 |
+
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ ํ๋ค. JSON ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค."
|
|
|
150 |
)
|
151 |
|
152 |
|
|
|
167 |
{"role": "user", "content": seed},
|
168 |
],
|
169 |
temperature=0.9,
|
170 |
+
max_tokens=2048,
|
171 |
)
|
172 |
raw = resp.choices[0].message.content
|
173 |
cleaned = clean_json_block(raw)
|
174 |
try:
|
175 |
data = json.loads(cleaned)
|
|
|
176 |
if isinstance(data, dict):
|
177 |
data = [data]
|
178 |
if not isinstance(data, list):
|
179 |
raise TypeError("LLM ์๋ต์ด ๋ฆฌ์คํธ๊ฐ ์๋")
|
180 |
+
if not 1 <= len(data) <= 20:
|
181 |
+
raise ValueError("์๋ต ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ 1~20 ๋ฒ์๋ฅผ ๋ฒ์ด๋จ")
|
182 |
except Exception as exc:
|
183 |
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ ์ผ๋ถ: {cleaned[:300]} โฆ")
|
184 |
return data
|
185 |
|
186 |
# -------------------------------------------------
|
187 |
+
# 2. Gradio callback
|
188 |
# -------------------------------------------------
|
189 |
|
190 |
def generate(seed_word: str):
|
191 |
results = call_llm(seed_word)
|
|
|
192 |
md_lines = [f"## ์ด {len(results)}๊ฐ ์ ์\n"]
|
193 |
for idx, item in enumerate(results, 1):
|
194 |
md_lines.append(
|
|
|
197 |
f"- **๋ธ๋๋ ๋ค์**: {item['brand']}\n"
|
198 |
f"- (tl={item['tl']}, tr={item['tr']}, br={item['br']}, bl={item['bl']})\n"
|
199 |
)
|
200 |
+
return "\n".join(md_lines)
|
201 |
|
202 |
# -------------------------------------------------
|
203 |
+
# 3. UI
|
204 |
# -------------------------------------------------
|
205 |
+
with gr.Blocks(title="Square Theory โ ์ต๋ 20๊ฐ ๐ฐ๐ท") as demo:
|
206 |
+
gr.Markdown("""# ๐ง Square Theory ์ ์ (์ต๋ 20๊ฐ)\n๋จ์ด ํ๋ ์
๋ ฅ โ LLM์ด ์ ์ํยท์ ๋ ฌํ ์ฌ๊ฐํ/์นดํผ/๋ธ๋๋ ๋ค์""")
|
207 |
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ")
|
208 |
run = gr.Button("์์ฑ")
|
|
|
209 |
md_out = gr.Markdown(label="์ ์ ๋ชฉ๋ก")
|
210 |
|
211 |
+
run.click(generate, inputs=seed, outputs=md_out)
|
212 |
|
213 |
if __name__ == "__main__":
|
214 |
demo.launch()
|