Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,15 @@
|
|
1 |
"""
|
2 |
-
Square Theory Generator (
|
3 |
-
|
4 |
-
2025โ05โ28
|
5 |
-
|
6 |
๋ณ๊ฒฝ ์์ฝ
|
7 |
---------
|
8 |
-
* **๋ฌธ์ **:
|
9 |
-
* **ํด๊ฒฐ**:
|
10 |
-
|
|
|
|
|
11 |
|
12 |
์คํ๋ฒ
|
13 |
------
|
@@ -30,7 +32,6 @@ from openai import OpenAI
|
|
30 |
# -------------------------------------------------
|
31 |
# 0. ํ๊ธ ํฐํธ ์ค์ (๋ค์ด๋ก๋ fallback ํฌํจ)
|
32 |
# -------------------------------------------------
|
33 |
-
|
34 |
PREFERRED_FONTS = ["Malgun Gothic", "NanumGothic", "AppleGothic", "DejaVu Sans"]
|
35 |
NANUM_URL = (
|
36 |
"https://github.com/google/fonts/raw/main/ofl/nanumgothic/"
|
@@ -88,21 +89,17 @@ def draw_square(words):
|
|
88 |
# -------------------------------------------------
|
89 |
SYSTEM_PROMPT = (
|
90 |
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
|
91 |
-
"์ฌ์ฉ์๊ฐ ์ค ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด
|
92 |
-
"๊ฐ ๋ฐฐ์ด ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ
|
93 |
-
"์ฌ๊ฐํ ๋ค ๊ผญ์ง์ (tl
|
94 |
-
"
|
95 |
-
"๊ฒฐ๊ณผ๋ JSON ์ธ ๋ค๋ฅธ ํ
์คํธ๋ฅผ ํฌํจํ๋ฉด ์ ๋๋ค."
|
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()
|
@@ -122,10 +119,15 @@ def call_llm(seed: str):
|
|
122 |
cleaned = clean_json_block(raw)
|
123 |
try:
|
124 |
data = json.loads(cleaned)
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
127 |
except Exception as exc:
|
128 |
-
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n
|
129 |
return data
|
130 |
|
131 |
# -------------------------------------------------
|
@@ -135,7 +137,7 @@ def call_llm(seed: str):
|
|
135 |
def generate(seed_word: str):
|
136 |
results = call_llm(seed_word)
|
137 |
fig = draw_square({k: results[0][k] for k in ("tl", "tr", "br", "bl")})
|
138 |
-
md_lines = []
|
139 |
for idx, item in enumerate(results, 1):
|
140 |
md_lines.append(
|
141 |
f"### {idx}. {item['top_phrase']} / {item['bottom_phrase']}\n"
|
@@ -148,12 +150,12 @@ def generate(seed_word: str):
|
|
148 |
# -------------------------------------------------
|
149 |
# 5. UI
|
150 |
# -------------------------------------------------
|
151 |
-
with gr.Blocks(title="Square Theory โ
|
152 |
-
gr.Markdown("""#
|
153 |
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ")
|
154 |
run = gr.Button("์์ฑ")
|
155 |
fig_out = gr.Plot(label="1์ ์ฌ๊ฐํ")
|
156 |
-
md_out = gr.Markdown(label="
|
157 |
|
158 |
run.click(generate, inputs=seed, outputs=[fig_out, md_out])
|
159 |
|
|
|
1 |
"""
|
2 |
+
Square Theory Generator (len-flexible)
|
3 |
+
=====================================
|
4 |
+
2025โ05โ28 v6 โ LLM ๊ฒฐ๊ณผ๊ฐ 10๊ฐ ๋ฏธ๋ง์ผ ๋ ์ ์ฐ ์ฒ๋ฆฌ
|
5 |
+
---------------------------------------------------
|
6 |
๋ณ๊ฒฝ ์์ฝ
|
7 |
---------
|
8 |
+
* **๋ฌธ์ **: LLM์ด 10๊ฐ ๋ฏธ๋ง์ ์ ์์ ๋ฐํํ๋ฉด ๊ธธ์ด ๊ฒ์ฆ ์์ธ ๋ฐ์.
|
9 |
+
* **ํด๊ฒฐ**:
|
10 |
+
1. `call_llm()`์์ JSON์ด dict ๋จ์ผ ๊ฐ์ฒด or 1~9๊ฐ ๋ฆฌ์คํธ์ฌ๋ ํ์ฉ.
|
11 |
+
2. ๋ฆฌ์คํธ๊ฐ 1๊ฐ ๋ฏธ๋ง์ด๋ฉด ์ค๋ฅ, 2~9๊ฐ๋ ๊ฒฝ๊ณ ๋ง ํ๊ธฐ.
|
12 |
+
3. Markdown์ ์ค์ ๊ฐ์๋ก ํค๋ ์ถ๋ ฅ.
|
13 |
|
14 |
์คํ๋ฒ
|
15 |
------
|
|
|
32 |
# -------------------------------------------------
|
33 |
# 0. ํ๊ธ ํฐํธ ์ค์ (๋ค์ด๋ก๋ fallback ํฌํจ)
|
34 |
# -------------------------------------------------
|
|
|
35 |
PREFERRED_FONTS = ["Malgun Gothic", "NanumGothic", "AppleGothic", "DejaVu Sans"]
|
36 |
NANUM_URL = (
|
37 |
"https://github.com/google/fonts/raw/main/ofl/nanumgothic/"
|
|
|
89 |
# -------------------------------------------------
|
90 |
SYSTEM_PROMPT = (
|
91 |
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
|
92 |
+
"์ฌ์ฉ์๊ฐ ์ค ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๋ฐ์ด๋ ์์๋๋ก ์ต๋ 10๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. "
|
93 |
+
"๊ฐ ๋ฐฐ์ด ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. "
|
94 |
+
"์ฌ๊ฐํ ๋ค ๊ผญ์ง์ (tlโtrโbrโbl)์ด ์์ฐ์ค๋ ์ฐ๊ฒฐ๋ผ์ผ ํ๊ณ , ๋ฐฐ์ด ์ฒซ ์์๊ฐ ์ต์ฐ์ ์ ์์ด๋ค. "
|
95 |
+
"๊ฒฐ๊ณผ๋ JSON ์ธ ํ
์คํธ๋ฅผ ํฌํจํ์ง ์์์ผ ํ๋ค."
|
|
|
96 |
)
|
97 |
|
98 |
|
99 |
def clean_json_block(text: str) -> str:
|
|
|
100 |
text = text.strip()
|
101 |
if text.startswith("```"):
|
|
|
102 |
text = text.split("\n", 1)[1] if "\n" in text else text[3:]
|
|
|
103 |
if text.endswith("```"):
|
104 |
text = text[:-3]
|
105 |
return text.strip()
|
|
|
119 |
cleaned = clean_json_block(raw)
|
120 |
try:
|
121 |
data = json.loads(cleaned)
|
122 |
+
# ๋จ์ผ ๊ฐ์ฒด๋ฉด ๋ฆฌ์คํธ๋ก ๋ณํ
|
123 |
+
if isinstance(data, dict):
|
124 |
+
data = [data]
|
125 |
+
if not isinstance(data, list):
|
126 |
+
raise TypeError("LLM ์๋ต์ด ๋ฆฌ์คํธ๊ฐ ์๋")
|
127 |
+
if len(data) == 0:
|
128 |
+
raise ValueError("LLM์ด ๋น ๋ฐฐ์ด์ ๋ฐํ")
|
129 |
except Exception as exc:
|
130 |
+
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ ์ผ๋ถ: {cleaned[:300]} โฆ")
|
131 |
return data
|
132 |
|
133 |
# -------------------------------------------------
|
|
|
137 |
def generate(seed_word: str):
|
138 |
results = call_llm(seed_word)
|
139 |
fig = draw_square({k: results[0][k] for k in ("tl", "tr", "br", "bl")})
|
140 |
+
md_lines = [f"## ์ด {len(results)}๊ฐ ์ ์\n"]
|
141 |
for idx, item in enumerate(results, 1):
|
142 |
md_lines.append(
|
143 |
f"### {idx}. {item['top_phrase']} / {item['bottom_phrase']}\n"
|
|
|
150 |
# -------------------------------------------------
|
151 |
# 5. UI
|
152 |
# -------------------------------------------------
|
153 |
+
with gr.Blocks(title="Square Theory โ ์ต๊ณ ์ ์ ๐ฐ๐ท") as demo:
|
154 |
+
gr.Markdown("""# ๐ง Square Theory ์ ์ (์ต๋ 10๊ฐ)\n๋จ์ด 1๊ฐ ์
๋ ฅ โ LLM์ด ํ๊ฐยท์ ๋ ฌํ ์ฌ๊ฐํ/์นดํผ/๋ธ๋๋ ๋ค์""")
|
155 |
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ")
|
156 |
run = gr.Button("์์ฑ")
|
157 |
fig_out = gr.Plot(label="1์ ์ฌ๊ฐํ")
|
158 |
+
md_out = gr.Markdown(label="์ ๏ฟฝ๏ฟฝ๏ฟฝ ๋ชฉ๋ก")
|
159 |
|
160 |
run.click(generate, inputs=seed, outputs=[fig_out, md_out])
|
161 |
|