openfree commited on
Commit
b6e13fb
ยท
verified ยท
1 Parent(s): 9db6da0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -22
app.py CHANGED
@@ -1,22 +1,21 @@
1
  """
2
  Square Theory Generator (10 best variations)
3
  ===========================================
4
- 2025โ€‘05โ€‘28ย v3 โ— ํ•œ ์ž…๋ ฅ โ†’ ์šฐ์ˆ˜๋„ ์ˆœ 10๊ฐ€์ง€ ๊ฒฐ๊ณผ
5
- ------------------------------------------------
6
  ๋ณ€๊ฒฝ ์š”์•ฝ
7
  ---------
8
- 1. **LLM ํ•œย ํšŒ ํ˜ธ์ถœ๋กœ 10๊ฐœ์˜ ์‚ฌ๊ฐํ˜• ์ œ์•ˆ**
9
- * JSON ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜, ํ’ˆ์งˆ์ด ๋†’์€ ์ˆœ์„œ๋กœ ์ •๋ ฌ (1ย โ€“ย 10)
10
- 2. **์ถœ๋ ฅ**
11
- * โ‘  1์œ„ ์ œ์•ˆ์˜ ์‚ฌ๊ฐํ˜• ๋„์‹(Plot)
12
- * โ‘ก 10๊ฐœ ์ œ์•ˆ์„ ๋งˆํฌ๋‹ค์šด ๋ฆฌ์ŠคํŠธ๋กœ ์ •๋ฆฌํ•˜์—ฌ ํ‘œ์‹œ
13
- 3. **UI ๊ฐ„์†Œํ™”** : seed ์ž…๋ ฅย +ย ์‹คํ–‰ ๋ฒ„ํŠผ + Plot + Markdown
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
- for cand in ("Malgun Gothic", "NanumGothic", "AppleGothic", "DejaVu Sans"):
38
- if cand in {f.name for f in font_manager.fontManager.ttflist}:
 
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["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
  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
- markdown_out = "\n".join(md_lines)
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