Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,21 @@
|
|
1 |
"""
|
2 |
Square Theory Generator (10 best variations)
|
3 |
===========================================
|
4 |
-
2025โ05โ28ย
|
5 |
-
|
6 |
๋ณ๊ฒฝ ์์ฝ
|
7 |
---------
|
8 |
-
1.
|
9 |
-
*
|
10 |
-
2.
|
11 |
-
*
|
12 |
-
|
13 |
-
|
14 |
-
4. **์๋ฌ ์ฒ๋ฆฌ ๊ฐํ**
|
15 |
|
16 |
์คํ๋ฒ
|
17 |
------
|
18 |
```bash
|
19 |
-
pip install --upgrade gradio matplotlib openai
|
20 |
export OPENAI_API_KEY="sk-..."
|
21 |
python square_theory_gradio.py
|
22 |
```
|
@@ -24,20 +23,44 @@ python square_theory_gradio.py
|
|
24 |
|
25 |
import os
|
26 |
import json
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
def _set_korean_font():
|
37 |
-
|
38 |
-
|
|
|
39 |
rcParams["font.family"] = cand
|
40 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
rcParams["axes.unicode_minus"] = False
|
42 |
|
43 |
_set_korean_font()
|
@@ -57,10 +80,10 @@ client = OpenAI()
|
|
57 |
def draw_square(words):
|
58 |
fig, ax = plt.subplots(figsize=(4, 4))
|
59 |
ax.add_patch(patches.Rectangle((0, 0), 1, 1, fill=False, linewidth=2))
|
60 |
-
ax.text(-0.05, 1.05, words
|
61 |
-
ax.text(1.05, 1.05, words
|
62 |
-
ax.text(1.05, -0.05, words
|
63 |
-
ax.text(-0.05, -0.05, words
|
64 |
ax.set_xticks([])
|
65 |
ax.set_yticks([])
|
66 |
ax.set_xlim(-0.2, 1.2)
|
@@ -106,10 +129,7 @@ def call_llm(seed: str):
|
|
106 |
|
107 |
def generate(seed_word: str):
|
108 |
results = call_llm(seed_word)
|
109 |
-
# 1์ ๋์
|
110 |
fig = draw_square({k: results[0][k] for k in ("tl", "tr", "br", "bl")})
|
111 |
-
|
112 |
-
# ๋ฆฌ์คํธ ๋งํฌ๋ค์ด
|
113 |
md_lines = []
|
114 |
for idx, item in enumerate(results, 1):
|
115 |
md_lines.append(
|
@@ -118,8 +138,7 @@ def generate(seed_word: str):
|
|
118 |
f"- **๋ธ๋๋ ๋ค์**: {item['brand']}\n"
|
119 |
f"- (tl={item['tl']}, tr={item['tr']}, br={item['br']}, bl={item['bl']})\n"
|
120 |
)
|
121 |
-
|
122 |
-
return fig, markdown_out
|
123 |
|
124 |
# -------------------------------------------------
|
125 |
# 5. UI
|
|
|
1 |
"""
|
2 |
Square Theory Generator (10 best variations)
|
3 |
===========================================
|
4 |
+
2025โ05โ28ย v4 โ ํ๊ธ ๊นจ์ง ์์ ํด๊ฒฐ + Topย 10 ๊ฒฐ๊ณผ
|
5 |
+
-------------------------------------------------
|
6 |
๋ณ๊ฒฝ ์์ฝ
|
7 |
---------
|
8 |
+
1. **ํ๊ธ ํฐํธ ๊ฐ์ ๋ฑ๋ก ๋ก์ง ๊ฐํ**
|
9 |
+
* ์์คํ
์ โMalgun/Nanum/Appleโ ํฐํธ๊ฐ ์์ผ๋ฉด โ Google Fonts์์ **NanumGothic TTF**๋ฅผ ๋ค์ด๋ก๋ํด ๋ฐํ์ ๋ฑ๋ก.
|
10 |
+
2. **draw_square ์์ ์ฑ ๊ฐํ**
|
11 |
+
* ๊ฐ ์ฝ๋ ๋ฌธ์์ด์ `str()`ย ์บ์คํ
, ๋ ๊ฐ ๋ฐฉ์ง.
|
12 |
+
3. **๊ธฐํ**
|
13 |
+
* ์ฝ๋ ์ฃผ์ยท์ ๋ฆฌ.
|
|
|
14 |
|
15 |
์คํ๋ฒ
|
16 |
------
|
17 |
```bash
|
18 |
+
pip install --upgrade gradio matplotlib openai requests
|
19 |
export OPENAI_API_KEY="sk-..."
|
20 |
python square_theory_gradio.py
|
21 |
```
|
|
|
23 |
|
24 |
import os
|
25 |
import json
|
26 |
+
import tempfile
|
27 |
+
import urllib.request
|
28 |
+
import requests
|
29 |
import gradio as gr
|
30 |
import matplotlib.pyplot as plt
|
31 |
from matplotlib import patches, font_manager, rcParams
|
32 |
from openai import OpenAI
|
33 |
|
34 |
# -------------------------------------------------
|
35 |
+
# 0. ํ๊ธ ํฐํธ ์ค์ (๋ค์ด๋ก๋ fallback ํฌํจ)
|
36 |
# -------------------------------------------------
|
37 |
|
38 |
+
PREFERRED_FONTS = ["Malgun Gothic", "NanumGothic", "AppleGothic", "DejaVu Sans"]
|
39 |
+
NANUM_URL = (
|
40 |
+
"https://github.com/google/fonts/raw/main/ofl/nanumgothic/"
|
41 |
+
"NanumGothic-Regular.ttf"
|
42 |
+
)
|
43 |
+
|
44 |
def _set_korean_font():
|
45 |
+
available = {f.name for f in font_manager.fontManager.ttflist}
|
46 |
+
for cand in PREFERRED_FONTS:
|
47 |
+
if cand in available:
|
48 |
rcParams["font.family"] = cand
|
49 |
break
|
50 |
+
else:
|
51 |
+
# ํฐํธ๊ฐ ํ๋๋ ์์ผ๋ฉด NanumGothic ๋ค์ด๋ก๋ ํ ๋ฑ๋ก
|
52 |
+
try:
|
53 |
+
tmp_dir = tempfile.gettempdir()
|
54 |
+
font_path = os.path.join(tmp_dir, "NanumGothic-Regular.ttf")
|
55 |
+
if not os.path.exists(font_path):
|
56 |
+
print("[INFO] Downloading NanumGothic fontโฆ")
|
57 |
+
urllib.request.urlretrieve(NANUM_URL, font_path)
|
58 |
+
font_manager.fontManager.addfont(font_path)
|
59 |
+
rcParams["font.family"] = font_manager.FontProperties(fname=font_path).get_name()
|
60 |
+
print(f"[INFO] Registered fallback font: {rcParams['font.family']}")
|
61 |
+
except Exception as e:
|
62 |
+
print("[WARN] Font download failed, Korean text may break:", e)
|
63 |
+
# ๊ทธ๋ํ ์์ ๋ถํธ ๊นจ์ง ๋ฐฉ์ง
|
64 |
rcParams["axes.unicode_minus"] = False
|
65 |
|
66 |
_set_korean_font()
|
|
|
80 |
def draw_square(words):
|
81 |
fig, ax = plt.subplots(figsize=(4, 4))
|
82 |
ax.add_patch(patches.Rectangle((0, 0), 1, 1, fill=False, linewidth=2))
|
83 |
+
ax.text(-0.05, 1.05, str(words.get("tl", "")), ha="right", va="bottom", fontsize=14, fontweight="bold")
|
84 |
+
ax.text(1.05, 1.05, str(words.get("tr", "")), ha="left", va="bottom", fontsize=14, fontweight="bold")
|
85 |
+
ax.text(1.05, -0.05, str(words.get("br", "")), ha="left", va="top", fontsize=14, fontweight="bold")
|
86 |
+
ax.text(-0.05, -0.05, str(words.get("bl", "")), ha="right", va="top", fontsize=14, fontweight="bold")
|
87 |
ax.set_xticks([])
|
88 |
ax.set_yticks([])
|
89 |
ax.set_xlim(-0.2, 1.2)
|
|
|
129 |
|
130 |
def generate(seed_word: str):
|
131 |
results = call_llm(seed_word)
|
|
|
132 |
fig = draw_square({k: results[0][k] for k in ("tl", "tr", "br", "bl")})
|
|
|
|
|
133 |
md_lines = []
|
134 |
for idx, item in enumerate(results, 1):
|
135 |
md_lines.append(
|
|
|
138 |
f"- **๋ธ๋๋ ๋ค์**: {item['brand']}\n"
|
139 |
f"- (tl={item['tl']}, tr={item['tr']}, br={item['br']}, bl={item['bl']})\n"
|
140 |
)
|
141 |
+
return fig, "\n".join(md_lines)
|
|
|
142 |
|
143 |
# -------------------------------------------------
|
144 |
# 5. UI
|