Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,12 @@
|
|
1 |
"""
|
2 |
Square Theory Generator (Markdownโonly, up to 20 ideas)
|
3 |
=====================================================
|
4 |
-
2025โ05โ28ย
|
5 |
---------------------------------------------------
|
6 |
๋ณ๊ฒฝ ์์ฝ
|
7 |
---------
|
8 |
-
* **๋ฌธ์ **:
|
9 |
-
* **ํด๊ฒฐ**:
|
10 |
-
`except` ์ ์ `openai.OpenAIError` ๋ก ๊ต์ฒด.
|
11 |
|
12 |
์คํ๋ฒ
|
13 |
------
|
@@ -42,10 +41,131 @@ SYSTEM_PROMPT = (
|
|
42 |
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. "
|
43 |
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ ํ๋ฉฐ JSON ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค."
|
44 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
|
46 |
"์ฌ์ฉ์๊ฐ ์
๋ ฅํ ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๋ฐ์ด๋ ์์๋๋ก ์ต๋ 20๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. "
|
|
|
47 |
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. "
|
48 |
-
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ
|
49 |
)
|
50 |
|
51 |
FALLBACK_MODELS = ["gpt-4o-mini", "gpt-4o", "gpt-4o-preview", "gpt-4-turbo"]
|
|
|
1 |
"""
|
2 |
Square Theory Generator (Markdownโonly, up to 20 ideas)
|
3 |
=====================================================
|
4 |
+
2025โ05โ28ย v11 โ IndentationErrorย fix (SYSTEM_PROMPT duplication)
|
5 |
---------------------------------------------------
|
6 |
๋ณ๊ฒฝ ์์ฝ
|
7 |
---------
|
8 |
+
* **๋ฌธ์ **: SYSTEM_PROMPT ์๋์ ์ค๋ณต ๋ฌธ์์ด์ด ์์ด ๋ค์ฌ์ฐ๊ธฐ ์ค๋ฅ ๋ฐ์.
|
9 |
+
* **ํด๊ฒฐ**: ์ค๋ณต ๋ธ๋ก ์ ๊ฑฐ, ์ ์ฒด ์ฝ๋ ์ ๋ฆฌ.
|
|
|
10 |
|
11 |
์คํ๋ฒ
|
12 |
------
|
|
|
41 |
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. "
|
42 |
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ ํ๋ฉฐ JSON ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค."
|
43 |
)
|
44 |
+
|
45 |
+
FALLBACK_MODELS = ["gpt-4o-mini", "gpt-4o", "gpt-4o-preview", "gpt-4-turbo"]
|
46 |
+
|
47 |
+
|
48 |
+
def _clean_json_block(text: str) -> str:
|
49 |
+
text = text.strip()
|
50 |
+
if text.startswith("```"):
|
51 |
+
text = text.split("\n", 1)[1] if "\n" in text else text[3:]
|
52 |
+
if text.endswith("```"):
|
53 |
+
text = text[:-3]
|
54 |
+
return text.strip()
|
55 |
+
|
56 |
+
|
57 |
+
def _call_llm(seed: str):
|
58 |
+
last_exc = None
|
59 |
+
for model in FALLBACK_MODELS:
|
60 |
+
try:
|
61 |
+
resp = client.chat.completions.create(
|
62 |
+
model=model,
|
63 |
+
messages=[
|
64 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
65 |
+
{"role": "user", "content": seed},
|
66 |
+
],
|
67 |
+
temperature=0.9,
|
68 |
+
max_tokens=2048,
|
69 |
+
)
|
70 |
+
cleaned = _clean_json_block(resp.choices[0].message.content)
|
71 |
+
data = json.loads(cleaned) if cleaned else []
|
72 |
+
if isinstance(data, dict):
|
73 |
+
data = [data]
|
74 |
+
if not isinstance(data, list) or not (1 <= len(data) <= 20):
|
75 |
+
raise ValueError("LLM ์๋ต์ด ์ฌ๋ฐ๋ฅธ 1โ20๊ฐ ๋ฆฌ์คํธ๊ฐ ์๋")
|
76 |
+
return data
|
77 |
+
except (openai.OpenAIError, json.JSONDecodeError, ValueError, TypeError) as exc:
|
78 |
+
last_exc = exc
|
79 |
+
continue
|
80 |
+
raise RuntimeError(f"LLM ํธ์ถ ์คํจ: {last_exc}")
|
81 |
+
|
82 |
+
# -------------------------------------------------
|
83 |
+
# 2. Gradio callback
|
84 |
+
# -------------------------------------------------
|
85 |
+
|
86 |
+
def generate(seed_word: str):
|
87 |
+
seed_word = seed_word.strip()
|
88 |
+
if not seed_word:
|
89 |
+
return "โ ๏ธ **์๋ ๋จ์ด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.**"
|
90 |
+
try:
|
91 |
+
results = _call_llm(seed_word)
|
92 |
+
md = [f"## ์ด {len(results)}๊ฐ ์ ์"]
|
93 |
+
for idx, item in enumerate(results, 1):
|
94 |
+
md.append(
|
95 |
+
f"### {idx}. ๋ธ๋๋ ๋ค์: {item['brand']}\n"
|
96 |
+
f"**๋ฉ์ธ ์นดํผ** \
|
97 |
+
โข ์๋จ: {item['top_phrase']} \
|
98 |
+
โข ํ๋จ: {item['bottom_phrase']}\n\n"
|
99 |
+
f"**์ฌ๋ก๊ฑด** \
|
100 |
+
> {item['slogan']}\n\n"
|
101 |
+
f"**์ฌ๊ฐํ ํค์๋** \
|
102 |
+
TL: {item['tl']} ยท TR: {item['tr']} ยท BR: {item['br']} ยท BL: {item['bl']}\n"
|
103 |
+
)
|
104 |
+
return "\n".join(md)
|
105 |
+
except Exception as exc:
|
106 |
+
return f"โ **์ค๋ฅ:** {exc}"
|
107 |
+
|
108 |
+
# -------------------------------------------------
|
109 |
+
# 3. UI
|
110 |
+
# -------------------------------------------------
|
111 |
+
with gr.Blocks(title="Square Theory โ ์ต๋ 20๊ฐ ๐ฐ๐ท") as demo:
|
112 |
+
gr.Markdown("""# ๐ง Square Theory ์ ์ (์ต๋ 20๊ฐ)\n๋จ์ด ํ๋ ์
๋ ฅ โ LLM์ด ์ ๋ ฌํ ์ฌ๊ฐํ/์นดํผ/๋ธ๋๋ ๋ค์""")
|
113 |
+
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ")
|
114 |
+
run = gr.Button("์์ฑ")
|
115 |
+
md_out = gr.Markdown()
|
116 |
+
|
117 |
+
run.click(generate, inputs=seed, outputs=md_out)
|
118 |
+
|
119 |
+
demo.queue().launch()
|
120 |
+
|
121 |
+
# -------------------------------------------------
|
122 |
+
# 4. (Optional) ์ถ๊ฐ ๊ธฐ๋ฅ ์์ด๋์ด
|
123 |
+
# -------------------------------------------------
|
124 |
+
# - JSON์ "why" ํ๋ ์์ฒญ โ ๊ฐ ์ ์์ ์ ์ ์ด์ ๋ฅผ ํ์ํด ์ค๋๋ ฅ ๊ฐํ
|
125 |
+
# - ํ๊น ์ฑ๋๋ณ ํ๊ทธ๋ผ์ธ ๋ณํ ์ต์
(OHHยทSNSยทTV)
|
126 |
+
# - CSV ๋ค์ด๋ก๋ ๋ฒํผ์ผ๋ก ๊ฒฐ๊ณผ ์ผ๊ด ์ ์ฅ
|
127 |
+
"""
|
128 |
+
Square Theory Generator (Markdownโonly, up to 20 ideas)
|
129 |
+
=====================================================
|
130 |
+
2025โ05โ28ย v11 โ IndentationErrorย fix (SYSTEM_PROMPT duplication)
|
131 |
+
---------------------------------------------------
|
132 |
+
๋ณ๊ฒฝ ์์ฝ
|
133 |
+
---------
|
134 |
+
* **๋ฌธ์ **: SYSTEM_PROMPT ์๋์ ์ค๋ณต ๋ฌธ์์ด์ด ์์ด ๋ค์ฌ์ฐ๊ธฐ ์ค๋ฅ ๋ฐ์.
|
135 |
+
* **ํด๊ฒฐ**: ์ค๋ณต ๋ธ๋ก ์ ๊ฑฐ, ์ ์ฒด ์ฝ๋ ์ ๋ฆฌ.
|
136 |
+
|
137 |
+
์คํ๋ฒ
|
138 |
+
------
|
139 |
+
```bash
|
140 |
+
pip install --upgrade gradio openai
|
141 |
+
export OPENAI_API_KEY="sk-..."
|
142 |
+
python square_theory_gradio.py
|
143 |
+
```
|
144 |
+
"""
|
145 |
+
|
146 |
+
import os
|
147 |
+
import json
|
148 |
+
import gradio as gr
|
149 |
+
import openai
|
150 |
+
from openai import OpenAI
|
151 |
+
|
152 |
+
# -------------------------------------------------
|
153 |
+
# 0. OpenAI ํด๋ผ์ด์ธํธ
|
154 |
+
# -------------------------------------------------
|
155 |
+
if not os.getenv("OPENAI_API_KEY"):
|
156 |
+
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
|
157 |
+
|
158 |
+
client = OpenAI()
|
159 |
+
|
160 |
+
# -------------------------------------------------
|
161 |
+
# 1. LLM Prompt & Utilities
|
162 |
+
# -------------------------------------------------
|
163 |
+
SYSTEM_PROMPT = (
|
164 |
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
|
165 |
"์ฌ์ฉ์๊ฐ ์
๋ ฅํ ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๋ฐ์ด๋ ์์๋๋ก ์ต๋ 20๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. "
|
166 |
+
"**์ค์**: tl, tr, br, bl ๊ฐ ํ๋๋ ๋ฐ๋์ ๊ณต๋ฐฑ์ด ์๋ **๋จ์ผ ๋จ์ด**(ํ๊ธ 1~2์ด์ ๋๋ ์์ด 1๋จ์ด)์ฌ์ผ ํ๋ค. "
|
167 |
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. "
|
168 |
+
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ ํ๋ฉฐ JSON ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค."
|
169 |
)
|
170 |
|
171 |
FALLBACK_MODELS = ["gpt-4o-mini", "gpt-4o", "gpt-4o-preview", "gpt-4-turbo"]
|