Update app.py
Browse files
app.py
CHANGED
@@ -1,396 +1,308 @@
|
|
1 |
"""
|
2 |
-
Square Theory Generator
|
3 |
-
|
4 |
-
2025-05-28 |
|
5 |
-
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
- ์๊ฐ์ ํ๋ฆฌ๋ทฐ ๊ธฐ๋ฅ ์ถ๊ฐ
|
10 |
-
- CSV/JSON ๋ด๋ณด๋ด๊ธฐ ๊ธฐ๋ฅ
|
11 |
-
- ์ฌ์์ฑ ๋ฐ ์ฆ๊ฒจ์ฐพ๊ธฐ ๊ธฐ๋ฅ
|
12 |
-
- ํฅ์๋ UI/UX
|
13 |
"""
|
14 |
|
15 |
import os
|
16 |
import json
|
17 |
-
import csv
|
18 |
-
import io
|
19 |
import gradio as gr
|
20 |
import openai
|
21 |
from openai import OpenAI
|
22 |
from datetime import datetime
|
23 |
-
from typing import List, Dict,
|
24 |
|
25 |
-
#
|
26 |
-
# 0. OpenAI ํด๋ผ์ด์ธํธ ์ค์
|
27 |
-
# -------------------------------------------------
|
28 |
if not os.getenv("OPENAI_API_KEY"):
|
29 |
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
|
30 |
|
31 |
client = OpenAI()
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
๋น์ ์ 20๋
๊ฒฝ๋ ฅ์ ํ๊ตญ ์ต๊ณ ์นดํผ๋ผ์ดํฐ์ด์ ๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
38 |
-
Square Theory๋ 4๊ฐ์ ํต์ฌ ๋จ์ด๋ฅผ ์ฌ๊ฐํ ๋ชจ์๋ฆฌ์ ๋ฐฐ์นํ์ฌ ๋ธ๋๋ ์ ์ฒด์ฑ์ ๊ตฌ์ถํ๋ ๋ฐฉ๋ฒ๋ก ์
๋๋ค.
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
- tr(์ค๋ฅธ์ชฝ์๋จ): tl๊ณผ ๋๋น๋๊ฑฐ๋ ๋ณด์ํ๋ ๊ฐ๋
|
47 |
-
- br(์ค๋ฅธ์ชฝํ๋จ): ๊ธ์ ์ ๊ฒฐ๊ณผ๋ ํํ
|
48 |
-
- bl(์ผ์ชฝํ๋จ): ๊ฐ์ฑ์ ๊ฐ์น๋ ๊ฒฝํ
|
49 |
|
50 |
-
|
51 |
-
- top_phrase: ๊ณ ๊ฐ์ ๋์ฆ๋ ๋ฌธ์ ๋ฅผ ์ง๋ ๊ณต๊ฐํ ๋ฌธ๊ตฌ
|
52 |
-
- bottom_phrase: ํด๊ฒฐ์ฑ
์ด๋ ์ฝ์์ ์ ์ํ๋ ํ๋ ์ ๋ํ ๋ฌธ๊ตฌ
|
53 |
-
- slogan: ๋ธ๋๋ ์ ์ฒด์ฑ์ ํ ๋ฌธ์ฅ์ผ๋ก ์์ถ (7-15์ ๊ถ์ฅ)
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
- ์ผ๊ด์ฑ: 4๊ฐ ๋จ์ด๊ฐ ์ ๊ธฐ์ ์ผ๋ก ์ฐ๊ฒฐ
|
63 |
-
- ์ค์ฉ์ฑ: ์ค์ ๋ง์ผํ
์ ํ์ฉ ๊ฐ๋ฅ
|
64 |
-
- ๊ฐ์ฑ์ ํธ์๋ ฅ: ํ๊ฒ ๊ณ ๊ฐ์ ๋ง์์ ์์ง์
|
65 |
-
|
66 |
-
JSON ๋ฐฐ์ด๋ก ๋ฐํํ๋ฉฐ, ํ์ง ์์ผ๋ก ์ ๋ ฌํ์ธ์.
|
67 |
-
๊ฐ ๊ฐ์ฒด: {tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand, concept_score(1-10), target_audience}
|
68 |
"""
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
# ์์ ์๋ ๋จ์ด๋ค
|
73 |
-
EXAMPLE_SEEDS = ["๊ณจ๋ ", "์ค๋งํธ", "๊ทธ๋ฆฐ", "ํ๋ฆฌ๋ฏธ์", "ํ๋ง", "ํ์", "ํจ์ด", "๋๋ฆผ"]
|
74 |
-
|
75 |
-
def _clean_json_response(text: str) -> str:
|
76 |
-
"""JSON ์๋ต ์ ์ """
|
77 |
-
text = text.strip()
|
78 |
-
if text.startswith("```"):
|
79 |
-
lines = text.split("\n")
|
80 |
-
text = "\n".join(lines[1:-1]) if lines[-1] == "```" else "\n".join(lines[1:])
|
81 |
-
return text.strip()
|
82 |
-
|
83 |
-
def _validate_square_item(item: Dict) -> bool:
|
84 |
-
"""Square Theory ํญ๋ชฉ ๊ฒ์ฆ"""
|
85 |
-
required_fields = ["tl", "tr", "br", "bl", "top_phrase", "bottom_phrase", "slogan", "brand"]
|
86 |
-
if not all(field in item for field in required_fields):
|
87 |
-
return False
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
# ์๋ต ํ์ ์ ๊ทํ
|
117 |
-
if "suggestions" in data:
|
118 |
-
results = data["suggestions"]
|
119 |
-
elif "items" in data:
|
120 |
-
results = data["items"]
|
121 |
-
elif isinstance(data, list):
|
122 |
-
results = data
|
123 |
else:
|
124 |
results = [data]
|
|
|
|
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
valid_results.sort(key=lambda x: x.get("concept_score", 0), reverse=True)
|
134 |
-
|
135 |
-
return valid_results[:20] # ์ต๋ 20๊ฐ
|
136 |
-
|
137 |
-
except Exception as e:
|
138 |
-
last_error = e
|
139 |
-
continue
|
140 |
-
|
141 |
-
raise RuntimeError(f"๋ชจ๋ ๋ชจ๋ธ์์ ์คํจ: {last_error}")
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
return f"""
|
149 |
-
<div style="
|
150 |
-
<div style="
|
151 |
-
|
152 |
-
|
|
|
|
|
153 |
</div>
|
154 |
-
<div style="
|
155 |
-
|
156 |
</div>
|
157 |
-
<div style="
|
158 |
-
|
159 |
</div>
|
160 |
-
<div style="
|
161 |
-
|
162 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
</div>
|
164 |
-
|
165 |
-
|
166 |
-
<p style="margin: 5px 0;
|
|
|
|
|
167 |
</div>
|
168 |
</div>
|
169 |
"""
|
170 |
|
171 |
-
|
172 |
-
|
173 |
-
# -------------------------------------------------
|
174 |
-
def generate_squares(seed_word: str, temperature: float = 0.8) -> Tuple[str, List[Dict], str]:
|
175 |
-
"""Square Theory ์ ์ ์์ฑ"""
|
176 |
-
seed_word = seed_word.strip()
|
177 |
-
if not seed_word:
|
178 |
-
return "โ ๏ธ **์๋ ๋จ์ด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.**", [], ""
|
179 |
|
180 |
try:
|
181 |
-
|
182 |
-
|
|
|
|
|
183 |
|
184 |
-
# ๋งํฌ๋ค์ด
|
185 |
markdown_parts = [
|
186 |
-
f"#
|
187 |
-
f"
|
188 |
-
f"\n## ๐ ์ด {len(results)}๊ฐ ์ ์\n"
|
189 |
]
|
190 |
|
191 |
-
# HTML
|
192 |
-
|
193 |
|
194 |
-
for idx,
|
195 |
-
score =
|
196 |
-
target = item.get('target_audience', '์ผ๋ฐ')
|
197 |
|
198 |
markdown_parts.append(f"""
|
199 |
-
### {idx}. {
|
200 |
|
201 |
-
|
202 |
-
|
203 |
-
**๐ ๋ฉ์ธ ์นดํผ**
|
204 |
-
- ์๋จ: *"{item['top_phrase']}"*
|
205 |
-
- ํ๋จ: *"{item['bottom_phrase']}"*
|
206 |
-
|
207 |
-
**๐ฌ ์ฌ๋ก๊ฑด**
|
208 |
-
> {item['slogan']}
|
209 |
-
|
210 |
-
**๐ฒ Square ๊ตฌ์ฑ**
|
211 |
```
|
212 |
-
[{
|
213 |
-
|
214 |
-
|
|
|
|
|
215 |
```
|
216 |
|
|
|
|
|
217 |
---
|
218 |
""")
|
219 |
|
220 |
-
# ์์
|
221 |
-
if idx <=
|
222 |
-
|
223 |
|
224 |
-
return "\n".join(markdown_parts),
|
225 |
|
226 |
except Exception as e:
|
227 |
-
return f"โ
|
228 |
-
|
229 |
-
# -------------------------------------------------
|
230 |
-
# 4. ๋ด๋ณด๋ด๊ธฐ ํจ์
|
231 |
-
# -------------------------------------------------
|
232 |
-
def export_to_csv(results: List[Dict]) -> str:
|
233 |
-
"""CSV ํ์ผ ์์ฑ"""
|
234 |
-
if not results:
|
235 |
-
return None
|
236 |
-
|
237 |
-
output = io.StringIO()
|
238 |
-
fieldnames = ["์์", "๋ธ๋๋๋ช
", "TL", "TR", "BR", "BL",
|
239 |
-
"์๋จ๋ฌธ๊ตฌ", "ํ๋จ๋ฌธ๊ตฌ", "์ฌ๋ก๊ฑด", "์ ์", "ํ๊ฒ"]
|
240 |
-
|
241 |
-
writer = csv.DictWriter(output, fieldnames=fieldnames)
|
242 |
-
writer.writeheader()
|
243 |
-
|
244 |
-
for idx, item in enumerate(results, 1):
|
245 |
-
writer.writerow({
|
246 |
-
"์์": idx,
|
247 |
-
"๋ธ๋๋๋ช
": item['brand'],
|
248 |
-
"TL": item['tl'],
|
249 |
-
"TR": item['tr'],
|
250 |
-
"BR": item['br'],
|
251 |
-
"BL": item['bl'],
|
252 |
-
"์๋จ๋ฌธ๊ตฌ": item['top_phrase'],
|
253 |
-
"ํ๋จ๋ฌธ๊ตฌ": item['bottom_phrase'],
|
254 |
-
"์ฌ๋ก๊ฑด": item['slogan'],
|
255 |
-
"์ ์": item.get('concept_score', 'N/A'),
|
256 |
-
"ํ๊ฒ": item.get('target_audience', '์ผ๋ฐ')
|
257 |
-
})
|
258 |
-
|
259 |
-
return output.getvalue()
|
260 |
|
261 |
-
|
262 |
-
|
263 |
-
if not results:
|
264 |
-
return None
|
265 |
-
|
266 |
-
export_data = {
|
267 |
-
"generated_at": datetime.now().isoformat(),
|
268 |
-
"total_count": len(results),
|
269 |
-
"results": results
|
270 |
-
}
|
271 |
-
|
272 |
-
return json.dumps(export_data, ensure_ascii=False, indent=2)
|
273 |
-
|
274 |
-
# -------------------------------------------------
|
275 |
-
# 5. Gradio UI
|
276 |
-
# -------------------------------------------------
|
277 |
-
# ์ ์ญ ๋ณ์๋ก ๊ฒฐ๊ณผ ์ ์ฅ
|
278 |
-
current_results = []
|
279 |
-
|
280 |
-
def generate_and_update(seed_word, temperature):
|
281 |
-
"""์์ฑ ๋ฐ UI ์
๋ฐ์ดํธ"""
|
282 |
-
global current_results
|
283 |
-
markdown, results, preview = generate_squares(seed_word, temperature)
|
284 |
-
current_results = results
|
285 |
-
|
286 |
-
# ๋ด๋ณด๋ด๊ธฐ ํ์ผ ์์ฑ
|
287 |
-
csv_content = export_to_csv(results) if results else None
|
288 |
-
json_content = export_to_json(results) if results else None
|
289 |
-
|
290 |
-
return markdown, preview, csv_content, json_content
|
291 |
-
|
292 |
-
# UI ๊ตฌ์ฑ
|
293 |
-
with gr.Blocks(title="Square Theory Generator v12", theme=gr.themes.Soft()) as demo:
|
294 |
gr.Markdown("""
|
295 |
-
#
|
296 |
-
###
|
|
|
|
|
|
|
297 |
|
298 |
-
|
|
|
|
|
|
|
|
|
299 |
""")
|
300 |
|
301 |
with gr.Row():
|
302 |
with gr.Column(scale=2):
|
303 |
-
|
304 |
-
label="
|
305 |
-
placeholder="
|
306 |
-
|
307 |
)
|
308 |
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
|
319 |
-
generate_btn = gr.Button("
|
320 |
|
321 |
-
gr.Examples(
|
322 |
-
examples=[[seed] for seed in EXAMPLE_SEEDS],
|
323 |
-
inputs=seed_input,
|
324 |
-
label="์์ ์๋ ๋จ์ด"
|
325 |
-
)
|
326 |
-
|
327 |
with gr.Column(scale=1):
|
328 |
gr.Markdown("""
|
329 |
-
### ๐ก
|
330 |
|
331 |
-
|
332 |
-
-
|
333 |
-
- ๊ฐ์น: ํ์ , ์ ๋ขฐ, ํ์ง
|
334 |
-
- ํน์ฑ: ์ค๋งํธ, ํ๋ฆฌ๋ฏธ์, ์์ฝ
|
335 |
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
```
|
342 |
""")
|
343 |
|
344 |
with gr.Tabs():
|
345 |
-
with gr.Tab("
|
346 |
output_markdown = gr.Markdown()
|
347 |
|
348 |
-
with gr.Tab("๐จ
|
349 |
-
|
350 |
-
|
351 |
-
with gr.Tab("๐พ ๋ด๋ณด๋ด๊ธฐ"):
|
352 |
-
with gr.Row():
|
353 |
-
csv_file = gr.File(label="CSV ๋ค์ด๋ก๋", visible=False)
|
354 |
-
json_file = gr.File(label="JSON ๋ค์ด๋ก๋", visible=False)
|
355 |
-
|
356 |
-
export_info = gr.Markdown("""
|
357 |
-
์์ฑ๋ ๊ฒฐ๊ณผ๋ ์๋์ผ๋ก CSV์ JSON ํ์์ผ๋ก ๋ณํ๋ฉ๋๋ค.
|
358 |
-
์์ฑ ๋ฒํผ์ ํด๋ฆญํ ํ ํ์ผ์ด ๋ํ๋ฉ๋๋ค.
|
359 |
-
""")
|
360 |
|
361 |
-
#
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
if json_content:
|
373 |
-
json_filename = f"square_theory_{seed_word}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
374 |
-
with open(json_filename, 'w', encoding='utf-8') as f:
|
375 |
-
f.write(json_content)
|
376 |
-
|
377 |
-
return markdown, preview, csv_filename, json_filename
|
378 |
|
|
|
379 |
generate_btn.click(
|
380 |
-
fn=
|
381 |
-
inputs=[
|
382 |
-
outputs=[output_markdown,
|
383 |
)
|
384 |
|
385 |
gr.Markdown("""
|
386 |
---
|
387 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
|
389 |
-
|
390 |
-
2. **ํ์
๊ธฐ๋ฅ**: ์์ฑ๋ ๊ฒฐ๊ณผ์ ํ์๋ค์ด ํฌํ/์ฝ๋ฉํธ
|
391 |
-
3. **ํ์คํ ๋ฆฌ ๊ด๋ฆฌ**: ์ด์ ์์ฑ ๊ฒฐ๊ณผ ์ ์ฅ ๋ฐ ๊ฒ์
|
392 |
-
4. **์
์ข
๋ณ ํ
ํ๋ฆฟ**: F&B, IT, ํจ์
๋ฑ ์
์ข
๋ณ ์ต์ ํ
|
393 |
-
5. **๋ค๊ตญ์ด ์ง์**: ์์ด, ์ค๊ตญ์ด, ์ผ๋ณธ์ด Square Theory
|
394 |
""")
|
395 |
|
396 |
if __name__ == "__main__":
|
|
|
1 |
"""
|
2 |
+
Square Theory Generator - True Implementation
|
3 |
+
============================================
|
4 |
+
2025-05-28 | Based on the actual Square Theory concept
|
5 |
+
------------------------------------------------------
|
6 |
|
7 |
+
Square Theory: 4๊ฐ์ ๋จ์ด๊ฐ ์๋ฏธ์ ๊ด๊ณ๋ก ์ฐ๊ฒฐ๋์ด ์ฌ๊ฐํ์ ์ด๋ฃจ๋ ์ธ์ด์ ํฌ
|
8 |
+
์: PUB QUIZ / BAR EXAM (PUB-BAR ๋์์ด, QUIZ-EXAM ๋์์ด)
|
|
|
|
|
|
|
|
|
9 |
"""
|
10 |
|
11 |
import os
|
12 |
import json
|
|
|
|
|
13 |
import gradio as gr
|
14 |
import openai
|
15 |
from openai import OpenAI
|
16 |
from datetime import datetime
|
17 |
+
from typing import List, Dict, Tuple, Optional
|
18 |
|
19 |
+
# OpenAI ํด๋ผ์ด์ธํธ
|
|
|
|
|
20 |
if not os.getenv("OPENAI_API_KEY"):
|
21 |
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
|
22 |
|
23 |
client = OpenAI()
|
24 |
|
25 |
+
# Square Theory ์ ์ฉ ํ๋กฌํํธ
|
26 |
+
SQUARE_THEORY_PROMPT = """
|
27 |
+
๋น์ ์ Square Theory ์ ๋ฌธ๊ฐ์
๋๋ค. Square Theory๋ 4๊ฐ์ ๋จ์ด๊ฐ ์ฌ๊ฐํ์ ๊ผญ์ง์ ์ ์ด๋ฃจ๊ณ ,
|
28 |
+
๊ฐ ๋ณ์ด ์๋ฏธ์ ๊ด๊ณ(๋์์ด, ๊ตฌ๋ฌธ, ์ด์จ ๋ฑ)๋ก ์ฐ๊ฒฐ๋๋ ์ธ์ด ๊ตฌ์กฐ์
๋๋ค.
|
|
|
|
|
29 |
|
30 |
+
ํต์ฌ ์์น:
|
31 |
+
1. 4๊ฐ์ ๋จ์ด๊ฐ ์ฌ๊ฐํ์ ๊ผญ์ง์ ์ ํ์ฑ
|
32 |
+
2. ์ธ์ ํ ๋จ์ด๋ค์ ํน์ ๊ด๊ณ๋ก ์ฐ๊ฒฐ (๋ณ)
|
33 |
+
3. ๋๋ณ์ ๋จ์ด๋ค์ ์ง์ ์ฐ๊ฒฐ๋์ง ์์ง๋ง, ๋ค๋ฅธ ๋ณ๋ค์ ํตํด ๋๋ผ์ด ์ผ์น๋ฅผ ์ด๋ฃธ
|
34 |
+
4. ์ ์ฒด ๊ตฌ์กฐ๊ฐ "์ํ!" ๋ชจ๋จผํธ๋ฅผ ๋ง๋ค์ด์ผ ํจ
|
35 |
|
36 |
+
์์:
|
37 |
+
- PUB QUIZ / BAR EXAM: PUB-BAR(๋์์ด), QUIZ-EXAM(๋์์ด), PUB QUIZ(๊ตฌ๋ฌธ), BAR EXAM(๊ตฌ๋ฌธ)
|
38 |
+
- BOOTY CALL / BUTT DIAL: BOOTY-BUTT(๋์์ด), CALL-DIAL(๋์์ด), BOOTY CALL(๊ตฌ๋ฌธ), BUTT DIAL(๊ตฌ๋ฌธ)
|
39 |
+
- TOP GUN / TSHIRT CANNON: TOP-TSHIRT(์์น), GUN-CANNON(๋ฌด๊ธฐ), TOP GUN(๊ตฌ๋ฌธ), TSHIRT CANNON(๊ตฌ๋ฌธ)
|
|
|
|
|
|
|
40 |
|
41 |
+
์ฌ์ฉ์๊ฐ ์๋ ๋จ์ด๋ ์ปจ์
์ ์ ๊ณตํ๋ฉด, ์ด๋ฅผ ํฌํจํ๋ Square Theory ๊ตฌ์กฐ๋ค์ ์์ฑํ์ธ์.
|
|
|
|
|
|
|
42 |
|
43 |
+
๊ฐ ์ ์์ ๋ค์ ํ์์ JSON์ผ๋ก:
|
44 |
+
{
|
45 |
+
"tl": "์ผ์ชฝ์๋จ ๋จ์ด",
|
46 |
+
"tr": "์ค๋ฅธ์ชฝ์๋จ ๋จ์ด",
|
47 |
+
"bl": "์ผ์ชฝํ๋จ ๋จ์ด",
|
48 |
+
"br": "์ค๋ฅธ์ชฝํ๋จ ๋จ์ด",
|
49 |
+
"top_edge": "์๋จ ๊ด๊ณ ์ค๋ช
(tl-tr)",
|
50 |
+
"bottom_edge": "ํ๋จ ๊ด๊ณ ์ค๋ช
(bl-br)",
|
51 |
+
"left_edge": "์ผ์ชฝ ๊ด๊ณ ์ค๋ช
(tl-bl)",
|
52 |
+
"right_edge": "์ค๋ฅธ์ชฝ ๊ด๊ณ ์ค๋ช
(tr-br)",
|
53 |
+
"top_phrase": "tl + tr ๊ตฌ๋ฌธ",
|
54 |
+
"bottom_phrase": "bl + br ๊ตฌ๋ฌธ",
|
55 |
+
"explanation": "์ ์ด ์ฌ๊ฐํ์ด ํฅ๋ฏธ๋ก์ด์ง",
|
56 |
+
"cleverness_score": 1-10
|
57 |
+
}
|
58 |
|
59 |
+
์ฐฝ์์ ์ด๊ณ ๋๋ผ์ด ์ฐ๊ฒฐ์ ๋ง๋๋ ๊ฒ์ด ํต์ฌ์
๋๋ค. ๋จ์ํ ๋์์ด ๋์ด์ด ์๋,
|
60 |
+
๋๋ณ ์ฌ์ด์ ์จ๊ฒจ์ง ์ฐ๊ฒฐ์ด ๋๋ฌ๋ ๋์ "์ํ!" ๋ชจ๋จผํธ๋ฅผ ๋ชฉํ๋ก ํ์ธ์.
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
"""
|
62 |
|
63 |
+
def call_square_theory_llm(seed: str, mode: str = "word") -> List[Dict]:
|
64 |
+
"""Square Theory ์์ฑ"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
if mode == "word":
|
67 |
+
user_prompt = f"์๋ ๋จ์ด '{seed}'๋ฅผ ํฌํจํ๋ Square Theory ๊ตฌ์กฐ๋ฅผ 5-10๊ฐ ์์ฑํ์ธ์."
|
68 |
+
elif mode == "concept":
|
69 |
+
user_prompt = f"'{seed}' ์ฃผ์ ์ ๊ด๋ จ๋ Square Theory ๊ตฌ์กฐ๋ฅผ 5-10๊ฐ ์์ฑํ์ธ์."
|
70 |
+
else: # pair
|
71 |
+
user_prompt = f"'{seed}'์ ๋น์ทํ ํํ์ Square Theory ๊ตฌ์กฐ๋ฅผ 5-10๊ฐ ์์ฑํ์ธ์."
|
72 |
|
73 |
+
try:
|
74 |
+
response = client.chat.completions.create(
|
75 |
+
model="gpt-4o",
|
76 |
+
messages=[
|
77 |
+
{"role": "system", "content": SQUARE_THEORY_PROMPT},
|
78 |
+
{"role": "user", "content": user_prompt}
|
79 |
+
],
|
80 |
+
temperature=0.8,
|
81 |
+
max_tokens=3000,
|
82 |
+
response_format={"type": "json_object"}
|
83 |
+
)
|
84 |
+
|
85 |
+
content = response.choices[0].message.content
|
86 |
+
data = json.loads(content)
|
87 |
+
|
88 |
+
# ์๋ต ์ ๊ทํ
|
89 |
+
if isinstance(data, dict):
|
90 |
+
if "squares" in data:
|
91 |
+
results = data["squares"]
|
92 |
+
elif "results" in data:
|
93 |
+
results = data["results"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
else:
|
95 |
results = [data]
|
96 |
+
else:
|
97 |
+
results = data
|
98 |
|
99 |
+
# ์ ์์ ์ ๋ ฌ
|
100 |
+
results.sort(key=lambda x: x.get("cleverness_score", 0), reverse=True)
|
101 |
+
|
102 |
+
return results[:10]
|
103 |
+
|
104 |
+
except Exception as e:
|
105 |
+
raise RuntimeError(f"Square Theory ์์ฑ ์คํจ: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
def visualize_square(square: Dict) -> str:
|
108 |
+
"""Square Theory ์๊ฐํ"""
|
109 |
+
|
110 |
+
tl, tr = square.get('tl', '?'), square.get('tr', '?')
|
111 |
+
bl, br = square.get('bl', '?'), square.get('br', '?')
|
112 |
+
|
113 |
+
top_edge = square.get('top_edge', '๊ด๊ณ')
|
114 |
+
bottom_edge = square.get('bottom_edge', '๊ด๊ณ')
|
115 |
+
left_edge = square.get('left_edge', '๊ด๊ณ')
|
116 |
+
right_edge = square.get('right_edge', '๊ด๊ณ')
|
117 |
+
|
118 |
return f"""
|
119 |
+
<div style="max-width: 600px; margin: 20px auto; font-family: 'Courier New', monospace;">
|
120 |
+
<div style="position: relative; width: 100%; height: 300px; background: #f8f9fa; border-radius: 8px; padding: 20px;">
|
121 |
+
|
122 |
+
<!-- ๊ผญ์ง์ (๋จ์ด๋ค) -->
|
123 |
+
<div style="position: absolute; top: 20px; left: 20px; background: #3498db; color: white; padding: 15px 25px; border-radius: 8px; font-weight: bold; font-size: 1.2em; box-shadow: 0 2px 4px rgba(0,0,0,0.2);">
|
124 |
+
{tl}
|
125 |
</div>
|
126 |
+
<div style="position: absolute; top: 20px; right: 20px; background: #e74c3c; color: white; padding: 15px 25px; border-radius: 8px; font-weight: bold; font-size: 1.2em; box-shadow: 0 2px 4px rgba(0,0,0,0.2);">
|
127 |
+
{tr}
|
128 |
</div>
|
129 |
+
<div style="position: absolute; bottom: 20px; left: 20px; background: #f39c12; color: white; padding: 15px 25px; border-radius: 8px; font-weight: bold; font-size: 1.2em; box-shadow: 0 2px 4px rgba(0,0,0,0.2);">
|
130 |
+
{bl}
|
131 |
</div>
|
132 |
+
<div style="position: absolute; bottom: 20px; right: 20px; background: #27ae60; color: white; padding: 15px 25px; border-radius: 8px; font-weight: bold; font-size: 1.2em; box-shadow: 0 2px 4px rgba(0,0,0,0.2);">
|
133 |
+
{br}
|
134 |
</div>
|
135 |
+
|
136 |
+
<!-- ๋ณ (๊ด๊ณ) -->
|
137 |
+
<div style="position: absolute; top: 40px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.8); color: white; padding: 5px 15px; border-radius: 4px; font-size: 0.9em;">
|
138 |
+
{top_edge}
|
139 |
+
</div>
|
140 |
+
<div style="position: absolute; bottom: 40px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.8); color: white; padding: 5px 15px; border-radius: 4px; font-size: 0.9em;">
|
141 |
+
{bottom_edge}
|
142 |
+
</div>
|
143 |
+
<div style="position: absolute; top: 50%; left: 40px; transform: translateY(-50%) rotate(-90deg); background: rgba(0,0,0,0.8); color: white; padding: 5px 15px; border-radius: 4px; font-size: 0.9em;">
|
144 |
+
{left_edge}
|
145 |
+
</div>
|
146 |
+
<div style="position: absolute; top: 50%; right: 40px; transform: translateY(-50%) rotate(90deg); background: rgba(0,0,0,0.8); color: white; padding: 5px 15px; border-radius: 4px; font-size: 0.9em;">
|
147 |
+
{right_edge}
|
148 |
+
</div>
|
149 |
+
|
150 |
+
<!-- ์ฐ๊ฒฐ์ -->
|
151 |
+
<svg style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none;">
|
152 |
+
<!-- ์๋จ -->
|
153 |
+
<line x1="80" y1="40" x2="520" y2="40" stroke="#666" stroke-width="2" stroke-dasharray="5,5"/>
|
154 |
+
<!-- ํ๋จ -->
|
155 |
+
<line x1="80" y1="260" x2="520" y2="260" stroke="#666" stroke-width="2" stroke-dasharray="5,5"/>
|
156 |
+
<!-- ์ผ์ชฝ -->
|
157 |
+
<line x1="50" y1="70" x2="50" y2="230" stroke="#666" stroke-width="2" stroke-dasharray="5,5"/>
|
158 |
+
<!-- ์ค๋ฅธ์ชฝ -->
|
159 |
+
<line x1="550" y1="70" x2="550" y2="230" stroke="#666" stroke-width="2" stroke-dasharray="5,5"/>
|
160 |
+
</svg>
|
161 |
</div>
|
162 |
+
|
163 |
+
<div style="margin-top: 20px; padding: 20px; background: #ecf0f1; border-radius: 8px;">
|
164 |
+
<p style="margin: 5px 0;"><strong>์๋จ ๊ตฌ๋ฌธ:</strong> {square.get('top_phrase', 'N/A')}</p>
|
165 |
+
<p style="margin: 5px 0;"><strong>ํ๋จ ๊ตฌ๋ฌธ:</strong> {square.get('bottom_phrase', 'N/A')}</p>
|
166 |
+
<p style="margin: 15px 0 5px 0; font-style: italic; color: #7f8c8d;">๐ก {square.get('explanation', '')}</p>
|
167 |
</div>
|
168 |
</div>
|
169 |
"""
|
170 |
|
171 |
+
def generate_squares(user_input: str, mode: str) -> Tuple[str, str]:
|
172 |
+
"""Square Theory ์์ฑ ๋ฐ ํ์"""
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
try:
|
175 |
+
squares = call_square_theory_llm(user_input, mode)
|
176 |
+
|
177 |
+
if not squares:
|
178 |
+
return "Square๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.", ""
|
179 |
|
180 |
+
# ๋งํฌ๋ค์ด ๊ฒฐ๊ณผ
|
181 |
markdown_parts = [
|
182 |
+
f"# ๐ฆ Square Theory ๊ฒฐ๊ณผ",
|
183 |
+
f"**์
๋ ฅ**: {user_input} ({mode} ๋ชจ๋)\n"
|
|
|
184 |
]
|
185 |
|
186 |
+
# HTML ์๊ฐํ
|
187 |
+
html_parts = []
|
188 |
|
189 |
+
for idx, square in enumerate(squares, 1):
|
190 |
+
score = square.get('cleverness_score', 0)
|
|
|
191 |
|
192 |
markdown_parts.append(f"""
|
193 |
+
### {idx}. {square.get('top_phrase', '?')} / {square.get('bottom_phrase', '?')} {'โญ' * min(score, 5)}
|
194 |
|
195 |
+
**๊ตฌ์กฐ**:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
```
|
197 |
+
[{square.get('tl', '?')}] โ({square.get('top_edge', '?')})โ [{square.get('tr', '?')}]
|
198 |
+
โ โ
|
199 |
+
({square.get('left_edge', '?')}) ({square.get('right_edge', '?')})
|
200 |
+
โ โ
|
201 |
+
[{square.get('bl', '?')}] โ({square.get('bottom_edge', '?')})โ [{square.get('br', '?')}]
|
202 |
```
|
203 |
|
204 |
+
**์ค๋ช
**: {square.get('explanation', 'N/A')}
|
205 |
+
|
206 |
---
|
207 |
""")
|
208 |
|
209 |
+
# ์์ 5๊ฐ ์๊ฐํ
|
210 |
+
if idx <= 5:
|
211 |
+
html_parts.append(visualize_square(square))
|
212 |
|
213 |
+
return "\n".join(markdown_parts), "\n".join(html_parts)
|
214 |
|
215 |
except Exception as e:
|
216 |
+
return f"โ ์ค๋ฅ: {str(e)}", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
217 |
|
218 |
+
# Gradio UI
|
219 |
+
with gr.Blocks(title="Square Theory Generator", theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
gr.Markdown("""
|
221 |
+
# ๐ฆ Square Theory Generator
|
222 |
+
### ์๋ฏธ์ ๊ด๊ณ๋ก ์ฐ๊ฒฐ๋ 4๋จ์ด ์ฌ๊ฐํ ๊ตฌ์กฐ ์์ฑ๊ธฐ
|
223 |
+
|
224 |
+
[Square Theory](https://www.lesswrong.com/posts/square-theory)๋ 4๊ฐ์ ๋จ์ด๊ฐ ์๋ฏธ์ ๊ด๊ณ๋ก
|
225 |
+
์ฐ๊ฒฐ๋์ด ์ฌ๊ฐํ์ ์ด๋ฃจ๋ ์ธ์ด ๊ตฌ์กฐ์
๋๋ค.
|
226 |
|
227 |
+
**์์**: PUB QUIZ / BAR EXAM
|
228 |
+
- PUB โ BAR (๋์์ด)
|
229 |
+
- QUIZ โ EXAM (๋์์ด)
|
230 |
+
- PUB QUIZ (๊ตฌ๋ฌธ)
|
231 |
+
- BAR EXAM (๊ตฌ๋ฌธ)
|
232 |
""")
|
233 |
|
234 |
with gr.Row():
|
235 |
with gr.Column(scale=2):
|
236 |
+
input_text = gr.Textbox(
|
237 |
+
label="์
๋ ฅ",
|
238 |
+
placeholder="๋จ์ด, ๊ฐ๋
, ๋๋ ์์ ์
๋ ฅ...",
|
239 |
+
lines=1
|
240 |
)
|
241 |
|
242 |
+
mode_radio = gr.Radio(
|
243 |
+
choices=[
|
244 |
+
("๋จ์ด ํฌํจ", "word"),
|
245 |
+
("์ฃผ์ ๊ด๋ จ", "concept"),
|
246 |
+
("์ ์ฌ ํจํด", "pair")
|
247 |
+
],
|
248 |
+
label="์์ฑ ๋ชจ๋",
|
249 |
+
value="word"
|
250 |
+
)
|
251 |
|
252 |
+
generate_btn = gr.Button("๐ฒ Square ์์ฑ", variant="primary")
|
253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
with gr.Column(scale=1):
|
255 |
gr.Markdown("""
|
256 |
+
### ๐ก ์ฌ์ฉ๋ฒ
|
257 |
|
258 |
+
**๋จ์ด ํฌํจ**: ํน์ ๋จ์ด๋ฅผ ํฌํจํ๋ Square
|
259 |
+
- ์: "BLACK" โ JET BLACK / JETBLUE
|
|
|
|
|
260 |
|
261 |
+
**์ฃผ์ ๊ด๋ จ**: ํน์ ์ฃผ์ ์ Square
|
262 |
+
- ์: "์์" โ ๊ด๋ จ Square๋ค
|
263 |
+
|
264 |
+
**์ ์ฌ ํจํด**: ์์์ ๋น์ทํ Square
|
265 |
+
- ์: "PUB QUIZ / BAR EXAM"
|
|
|
266 |
""")
|
267 |
|
268 |
with gr.Tabs():
|
269 |
+
with gr.Tab("๐ ๊ฒฐ๊ณผ"):
|
270 |
output_markdown = gr.Markdown()
|
271 |
|
272 |
+
with gr.Tab("๐จ ์๊ฐํ"):
|
273 |
+
output_visual = gr.HTML()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
|
275 |
+
# ์์
|
276 |
+
gr.Examples(
|
277 |
+
examples=[
|
278 |
+
["BLACK", "word"],
|
279 |
+
["์์", "concept"],
|
280 |
+
["PUB QUIZ / BAR EXAM", "pair"],
|
281 |
+
["CALL", "word"],
|
282 |
+
["๋ธ๋๋", "concept"]
|
283 |
+
],
|
284 |
+
inputs=[input_text, mode_radio]
|
285 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
287 |
+
# ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
288 |
generate_btn.click(
|
289 |
+
fn=generate_squares,
|
290 |
+
inputs=[input_text, mode_radio],
|
291 |
+
outputs=[output_markdown, output_visual]
|
292 |
)
|
293 |
|
294 |
gr.Markdown("""
|
295 |
---
|
296 |
+
### ๐ Square Theory๋?
|
297 |
+
|
298 |
+
Square Theory๋ ์ธ์ด์ ์๋ฏธ์ ๊ด๊ณ๋ฅผ ์ฌ๊ฐํ ๊ตฌ์กฐ๋ก ํํํ๋ ๊ฐ๋
์
๋๋ค.
|
299 |
+
์ข์ Square๋ ๋ค์ ํน์ง์ ๊ฐ์ง๋๋ค:
|
300 |
+
|
301 |
+
1. **์์ฑ๋**: 4๊ฐ ๋ณ์ด ๋ชจ๋ ์๋ฏธ์๋ ๊ด๊ณ
|
302 |
+
2. **๋๋ผ์**: ๋๋ณ ์ฐ๊ฒฐ์ด ์์์น ๋ชปํ ํต์ฐฐ ์ ๊ณต
|
303 |
+
3. **๊ท ํ**: ๋ชจ๋ ๊ด๊ณ๊ฐ ๋น์ทํ ๊ฐ๋
|
304 |
|
305 |
+
์ด๋ ๋ง์ฅ๋, ๋ธ๋๋ ๋ค์ด๋ฐ, ํฌ๋ก์ค์๋ ํผ์ฆ ๋ฑ์์ ์์ฃผ ๋ฐ๊ฒฌ๋ฉ๋๋ค.
|
|
|
|
|
|
|
|
|
306 |
""")
|
307 |
|
308 |
if __name__ == "__main__":
|