Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,13 @@
|
|
1 |
"""
|
2 |
Square Theory Generator (10 best variations)
|
3 |
===========================================
|
4 |
-
2025โ05โ28ย
|
5 |
-------------------------------------------------
|
6 |
๋ณ๊ฒฝ ์์ฝ
|
7 |
---------
|
8 |
-
|
9 |
-
|
10 |
-
|
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
|
|
|
118 |
try:
|
119 |
-
data = json.loads(
|
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์๋ฌธ: {
|
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 |
# -------------------------------------------------
|