Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,20 @@
|
|
| 1 |
"""
|
| 2 |
-
Square Theory Generator (Korean
|
| 3 |
-----------------------------------------------------
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
โ ๏ธ **OPENAI_API_KEY** ํ๊ฒฝ ๋ณ์๋ฅผ ๋ฏธ๋ฆฌ ์ค์ ํด ๋์ธ์.
|
| 11 |
"""
|
| 12 |
|
| 13 |
import os
|
|
@@ -18,30 +25,24 @@ from matplotlib import patches, font_manager, rcParams
|
|
| 18 |
from openai import OpenAI
|
| 19 |
|
| 20 |
# -------------------------------------------------
|
| 21 |
-
# 0. ํ๊ธ ํฐํธ ์๋ ํ์ & ์ค์
|
| 22 |
# -------------------------------------------------
|
| 23 |
|
| 24 |
def _set_korean_font():
|
| 25 |
-
candidates = [
|
| 26 |
-
"Malgun Gothic", # Windows
|
| 27 |
-
"NanumGothic", # Linux (apt install fonts-nanum)
|
| 28 |
-
"AppleGothic", # macOS
|
| 29 |
-
"DejaVu Sans", # fallback (๊ธฐ๋ณธ ํฐํธ์ง๋ง ํ๊ธ ์ผ๋ถ ์ง์)
|
| 30 |
-
]
|
| 31 |
available = {f.name for f in font_manager.fontManager.ttflist}
|
| 32 |
for cand in candidates:
|
| 33 |
if cand in available:
|
| 34 |
rcParams["font.family"] = cand
|
| 35 |
break
|
| 36 |
-
rcParams["axes.unicode_minus"] = False
|
| 37 |
|
| 38 |
_set_korean_font()
|
| 39 |
|
| 40 |
# -------------------------------------------------
|
| 41 |
# 1. OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 42 |
# -------------------------------------------------
|
| 43 |
-
|
| 44 |
-
if not OPENAI_API_KEY:
|
| 45 |
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
|
| 46 |
|
| 47 |
client = OpenAI()
|
|
@@ -51,17 +52,12 @@ client = OpenAI()
|
|
| 51 |
# -------------------------------------------------
|
| 52 |
|
| 53 |
def draw_square(words):
|
| 54 |
-
"""words = dict with keys tl, tr, br, bl"""
|
| 55 |
fig, ax = plt.subplots(figsize=(4, 4))
|
| 56 |
-
|
| 57 |
-
square = patches.Rectangle((0, 0), 1, 1, fill=False, linewidth=2)
|
| 58 |
-
ax.add_patch(square)
|
| 59 |
-
|
| 60 |
ax.text(-0.05, 1.05, words["tl"], ha="right", va="bottom", fontsize=14, fontweight="bold")
|
| 61 |
ax.text(1.05, 1.05, words["tr"], ha="left", va="bottom", fontsize=14, fontweight="bold")
|
| 62 |
ax.text(1.05, -0.05, words["br"], ha="left", va="top", fontsize=14, fontweight="bold")
|
| 63 |
ax.text(-0.05, -0.05, words["bl"], ha="right", va="top", fontsize=14, fontweight="bold")
|
| 64 |
-
|
| 65 |
ax.set_xticks([])
|
| 66 |
ax.set_yticks([])
|
| 67 |
ax.set_xlim(-0.2, 1.2)
|
|
@@ -72,27 +68,32 @@ def draw_square(words):
|
|
| 72 |
# -------------------------------------------------
|
| 73 |
# 3. LLM ํ๋กฌํํธ & ์๋ต ํ์ฑ
|
| 74 |
# -------------------------------------------------
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
],
|
| 87 |
-
text={"format": {"type": "text"}},
|
| 88 |
temperature=0.9,
|
| 89 |
-
|
| 90 |
)
|
| 91 |
-
raw =
|
|
|
|
|
|
|
| 92 |
try:
|
| 93 |
data = json.loads(raw)
|
| 94 |
-
except json.JSONDecodeError as
|
| 95 |
-
raise ValueError(f"LLM
|
| 96 |
return data
|
| 97 |
|
| 98 |
# -------------------------------------------------
|
|
@@ -101,39 +102,23 @@ def call_llm(seed):
|
|
| 101 |
|
| 102 |
def generate(seed_word):
|
| 103 |
data = call_llm(seed_word)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
"tl": data["tl"],
|
| 107 |
-
"tr": data["tr"],
|
| 108 |
-
"br": data["br"],
|
| 109 |
-
"bl": data["bl"],
|
| 110 |
-
}
|
| 111 |
-
fig = draw_square(words)
|
| 112 |
-
return (
|
| 113 |
-
fig,
|
| 114 |
-
data.get("top_phrase", ""),
|
| 115 |
-
data.get("bottom_phrase", ""),
|
| 116 |
-
data.get("slogan", ""),
|
| 117 |
-
data.get("brand", ""),
|
| 118 |
-
)
|
| 119 |
|
| 120 |
# -------------------------------------------------
|
| 121 |
# 5. Gradio UI
|
| 122 |
# -------------------------------------------------
|
| 123 |
with gr.Blocks(title="Square Theory Generator โ LLM Powered ๐ฐ๐ท") as demo:
|
| 124 |
-
gr.Markdown("""# ๐ง Square Theory Generator\n๋จ์ด ํ๋๋ง
|
| 125 |
-
|
| 126 |
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ")
|
| 127 |
-
|
| 128 |
-
|
| 129 |
fig_out = gr.Plot(label="์ฌ๊ฐํ ๋ค์ด์ด๊ทธ๋จ")
|
| 130 |
top_out = gr.Textbox(label="์๋ณ ํํ")
|
| 131 |
bottom_out = gr.Textbox(label="์๋ซ๋ณ ํํ")
|
| 132 |
slogan_out = gr.Textbox(label="๋ ์ค ์ฌ๋ก๊ฑด")
|
| 133 |
-
brand_out = gr.Textbox(label="๋ธ๋๋ ๋ค์
|
| 134 |
|
| 135 |
-
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
-
# launch(): share=True ํ๋ฉด ์ธ๋ถ์์ ์ ์ ๊ฐ๋ฅ
|
| 139 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
Square Theory Generator (Korean-friendly, LLM-powered)
|
| 3 |
-----------------------------------------------------
|
| 4 |
+
2025โ05โ28 Hotโpatch โ
์ค๋ฅ ์์
|
| 5 |
+
================================
|
| 6 |
+
* **๋ฒ๊ทธ ์์ธ**: `openai` Python SDK v1.x์ `client.responses.create()` ๋ฐํ ๊ฐ์ฒด์๋ `.choices` ์์ฑ์ด ์์ โ `AttributeError: 'Response' object has no attribute 'choices'` ๋ฐ์.
|
| 7 |
+
* **ํด๊ฒฐ**: `client.chat.completions.create()` ์๋ํฌ์ธํธ๋ก ๊ต์ฒด โ ์ต์ํ `.choices[0].message.content` ๊ตฌ์กฐ ์ฌ์ฉ.
|
| 8 |
+
* ์ถ๊ฐ: ์์ธ ์ฒ๋ฆฌยท๋๋ฒ๊ทธ ํ๋ฆฐํธ ์ต์
.
|
| 9 |
+
|
| 10 |
+
์ฌ์ฉ ๋ฐฉ๋ฒ
|
| 11 |
+
=========
|
| 12 |
+
```bash
|
| 13 |
+
pip install --upgrade gradio matplotlib openai
|
| 14 |
+
export OPENAI_API_KEY="sk-..."
|
| 15 |
+
python square_theory_gradio.py
|
| 16 |
+
```
|
| 17 |
|
|
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
import os
|
|
|
|
| 25 |
from openai import OpenAI
|
| 26 |
|
| 27 |
# -------------------------------------------------
|
| 28 |
+
# 0. ํ๊ธ ํฐํธ ์๋ ํ์ & ์ค์
|
| 29 |
# -------------------------------------------------
|
| 30 |
|
| 31 |
def _set_korean_font():
|
| 32 |
+
candidates = ["Malgun Gothic", "NanumGothic", "AppleGothic", "DejaVu Sans"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
available = {f.name for f in font_manager.fontManager.ttflist}
|
| 34 |
for cand in candidates:
|
| 35 |
if cand in available:
|
| 36 |
rcParams["font.family"] = cand
|
| 37 |
break
|
| 38 |
+
rcParams["axes.unicode_minus"] = False
|
| 39 |
|
| 40 |
_set_korean_font()
|
| 41 |
|
| 42 |
# -------------------------------------------------
|
| 43 |
# 1. OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 44 |
# -------------------------------------------------
|
| 45 |
+
if not os.getenv("OPENAI_API_KEY"):
|
|
|
|
| 46 |
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
|
| 47 |
|
| 48 |
client = OpenAI()
|
|
|
|
| 52 |
# -------------------------------------------------
|
| 53 |
|
| 54 |
def draw_square(words):
|
|
|
|
| 55 |
fig, ax = plt.subplots(figsize=(4, 4))
|
| 56 |
+
ax.add_patch(patches.Rectangle((0, 0), 1, 1, fill=False, linewidth=2))
|
|
|
|
|
|
|
|
|
|
| 57 |
ax.text(-0.05, 1.05, words["tl"], ha="right", va="bottom", fontsize=14, fontweight="bold")
|
| 58 |
ax.text(1.05, 1.05, words["tr"], ha="left", va="bottom", fontsize=14, fontweight="bold")
|
| 59 |
ax.text(1.05, -0.05, words["br"], ha="left", va="top", fontsize=14, fontweight="bold")
|
| 60 |
ax.text(-0.05, -0.05, words["bl"], ha="right", va="top", fontsize=14, fontweight="bold")
|
|
|
|
| 61 |
ax.set_xticks([])
|
| 62 |
ax.set_yticks([])
|
| 63 |
ax.set_xlim(-0.2, 1.2)
|
|
|
|
| 68 |
# -------------------------------------------------
|
| 69 |
# 3. LLM ํ๋กฌํํธ & ์๋ต ํ์ฑ
|
| 70 |
# -------------------------------------------------
|
| 71 |
+
SYSTEM_PROMPT = (
|
| 72 |
+
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory(์ฌ๊ฐํ ์ด๋ก ) ๋์ฐ๋ฏธ๋ค.\n"
|
| 73 |
+
"์ฌ์ฉ์๊ฐ ์ ์ํ ํ๋์ ๋จ์ด(TL)๋ก ๋ค์ JSON์ ์์ฑํด๋ผ: \n"
|
| 74 |
+
"{\n \"tl\": str, \"tr\": str, \"br\": str, \"bl\": str,\n \"top_phrase\": str, \"bottom_phrase\": str, \n \"slogan\": str, \"brand\": str\n}\n"
|
| 75 |
+
"๊ฐ corner ๋ ์์ ํ ์ฌ๊ฐํ์ ์ด๋ฃจ๋๋ก ์๋ฏธยท์์ด ์์ฐ์ค๋ฝ๊ฒ ์ฐ๊ฒฐ๋ผ์ผ ํ๋ค. ๊ฒฐ๊ณผ๋ **JSON ๋ฌธ์์ด**๋ง ์ถ๋ ฅํ๋ค."
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def call_llm(seed: str, debug: bool = False):
|
| 80 |
+
"""LLM ํธ์ถ โ JSON dict ๋ฐํ"""
|
| 81 |
+
resp = client.chat.completions.create(
|
| 82 |
+
model="gpt-4o-mini", # ๋๋ gpt-4-turbo (์กฐ์ ๊ฐ๋ฅ)
|
| 83 |
+
messages=[
|
| 84 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 85 |
+
{"role": "user", "content": seed},
|
| 86 |
],
|
|
|
|
| 87 |
temperature=0.9,
|
| 88 |
+
max_tokens=512,
|
| 89 |
)
|
| 90 |
+
raw = resp.choices[0].message.content.strip()
|
| 91 |
+
if debug:
|
| 92 |
+
print("LLM raw:", raw)
|
| 93 |
try:
|
| 94 |
data = json.loads(raw)
|
| 95 |
+
except json.JSONDecodeError as exc:
|
| 96 |
+
raise ValueError(f"LLM ์๋ต JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ: {raw}")
|
| 97 |
return data
|
| 98 |
|
| 99 |
# -------------------------------------------------
|
|
|
|
| 102 |
|
| 103 |
def generate(seed_word):
|
| 104 |
data = call_llm(seed_word)
|
| 105 |
+
fig = draw_square({k: data[k] for k in ("tl", "tr", "br", "bl")})
|
| 106 |
+
return fig, data["top_phrase"], data["bottom_phrase"], data["slogan"], data["brand"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# -------------------------------------------------
|
| 109 |
# 5. Gradio UI
|
| 110 |
# -------------------------------------------------
|
| 111 |
with gr.Blocks(title="Square Theory Generator โ LLM Powered ๐ฐ๐ท") as demo:
|
| 112 |
+
gr.Markdown("""# ๐ง Square Theory Generator\n๋จ์ด ํ๋๋ง ์
๋ ฅํ๋ฉด LLM์ด ์ฌ๊ฐํ ๊ตฌ์กฐยท์นดํผยท๋ธ๋๋ ๋ค์์ ์๋ ์์ฑํฉ๋๋ค.""")
|
|
|
|
| 113 |
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ")
|
| 114 |
+
run = gr.Button("์ฌ๊ฐํ ์์ฑ")
|
|
|
|
| 115 |
fig_out = gr.Plot(label="์ฌ๊ฐํ ๋ค์ด์ด๊ทธ๋จ")
|
| 116 |
top_out = gr.Textbox(label="์๋ณ ํํ")
|
| 117 |
bottom_out = gr.Textbox(label="์๋ซ๋ณ ํํ")
|
| 118 |
slogan_out = gr.Textbox(label="๋ ์ค ์ฌ๋ก๊ฑด")
|
| 119 |
+
brand_out = gr.Textbox(label="๋ธ๋๋ ๋ค์")
|
| 120 |
|
| 121 |
+
run.click(generate, inputs=seed, outputs=[fig_out, top_out, bottom_out, slogan_out, brand_out])
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
|
|
|
| 124 |
demo.launch()
|