openfree commited on
Commit
673fa0a
ยท
verified ยท
1 Parent(s): b6e13fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -1,16 +1,13 @@
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
  ------
@@ -25,7 +22,6 @@ 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
@@ -48,19 +44,15 @@ def _set_korean_font():
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()
@@ -104,6 +96,18 @@ SYSTEM_PROMPT = (
104
  )
105
 
106
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  def call_llm(seed: str):
108
  resp = client.chat.completions.create(
109
  model="gpt-4o-mini",
@@ -114,13 +118,14 @@ def call_llm(seed: str):
114
  temperature=0.9,
115
  max_tokens=1024,
116
  )
117
- raw = resp.choices[0].message.content.strip()
 
118
  try:
119
- data = json.loads(raw)
120
  if not isinstance(data, list) or len(data) != 10:
121
  raise ValueError("JSON ๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ 10์ด ์•„๋‹˜")
122
  except Exception as exc:
123
- raise ValueError(f"LLM JSON ํŒŒ์‹ฑ ์‹คํŒจ: {exc}\n์›๋ฌธ: {raw[:300]} โ€ฆ")
124
  return data
125
 
126
  # -------------------------------------------------
 
1
  """
2
  Square Theory Generator (10 best variations)
3
  ===========================================
4
+ 2025โ€‘05โ€‘28ย v5 โ— ์ฝ”๋“œ๋ธ”๋ก JSON ํŒŒ์‹ฑ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ + ํ•œ๊ธ€ ํฐํŠธย OK
5
  -------------------------------------------------
6
  ๋ณ€๊ฒฝ ์š”์•ฝ
7
  ---------
8
+ * **๋ฌธ์ œ**: ์ผ๋ถ€ LLM ์‘๋‹ต์ด ```json โ€ฆ ``` ์ฝ”๋“œํŽœ์Šค๋กœ ๊ฐ์‹ธ์ ธ `json.loads()` ์‹คํŒจ โ†’ ์˜ˆ์™ธ.
9
+ * **ํ•ด๊ฒฐ**: ์ฝ”๋“œํŽœ์Šค ์ œ๊ฑฐ `clean_json_block()` ํ•จ์ˆ˜ ์ถ”๊ฐ€.
10
+ * ๊ธฐํƒ€: ํฐํŠธ ๋กœ์ง ๊ทธ๋Œ€๋กœ ์œ ์ง€.
 
 
 
11
 
12
  ์‹คํ–‰๋ฒ•
13
  ------
 
22
  import json
23
  import tempfile
24
  import urllib.request
 
25
  import gradio as gr
26
  import matplotlib.pyplot as plt
27
  from matplotlib import patches, font_manager, rcParams
 
44
  rcParams["font.family"] = cand
45
  break
46
  else:
 
47
  try:
48
  tmp_dir = tempfile.gettempdir()
49
  font_path = os.path.join(tmp_dir, "NanumGothic-Regular.ttf")
50
  if not os.path.exists(font_path):
 
51
  urllib.request.urlretrieve(NANUM_URL, font_path)
52
  font_manager.fontManager.addfont(font_path)
53
  rcParams["font.family"] = font_manager.FontProperties(fname=font_path).get_name()
 
54
  except Exception as e:
55
  print("[WARN] Font download failed, Korean text may break:", e)
 
56
  rcParams["axes.unicode_minus"] = False
57
 
58
  _set_korean_font()
 
96
  )
97
 
98
 
99
+ def clean_json_block(text: str) -> str:
100
+ """Remove ```json ... ``` or ``` ... ``` fences."""
101
+ text = text.strip()
102
+ if text.startswith("```"):
103
+ # remove first ``` line
104
+ text = text.split("\n", 1)[1] if "\n" in text else text[3:]
105
+ # strip trailing fence
106
+ if text.endswith("```"):
107
+ text = text[:-3]
108
+ return text.strip()
109
+
110
+
111
  def call_llm(seed: str):
112
  resp = client.chat.completions.create(
113
  model="gpt-4o-mini",
 
118
  temperature=0.9,
119
  max_tokens=1024,
120
  )
121
+ raw = resp.choices[0].message.content
122
+ cleaned = clean_json_block(raw)
123
  try:
124
+ data = json.loads(cleaned)
125
  if not isinstance(data, list) or len(data) != 10:
126
  raise ValueError("JSON ๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ 10์ด ์•„๋‹˜")
127
  except Exception as exc:
128
+ raise ValueError(f"LLM JSON ํŒŒ์‹ฑ ์‹คํŒจ: {exc}\n์›๋ฌธ: {cleaned[:300]} โ€ฆ")
129
  return data
130
 
131
  # -------------------------------------------------