|
""" |
|
Square Theory Generator (Markdownโonly, up to 20 ideas) |
|
===================================================== |
|
2025โ05โ28ย v7 โ ๋ฐ์ค ๋ค์ด์ด๊ทธ๋จ ์ ๊ฑฐ & ์ ์ย 20๊ฐ๋ก ํ์ฅ |
|
--------------------------------------------------- |
|
๋ณ๊ฒฝ ์์ฝ |
|
--------- |
|
1. **์ฌ๊ฐํ ์๊ฐํ ์ ๊ฑฐ** โ ์ถ๋ ฅ/์์กด์์ `matplotlib` ์ ๋ถ ์ญ์ . |
|
2. **์ ์ ๊ฐ์ ์ํฅ** โ LLM์ด ์ต๋ **20๊ฐ** ์์ด๋์ด ๋ฐํ. |
|
3. **์ ์ฐ ๊ธธ์ด ํ์ฉ** โ 1โฏโคโฏNโฏโคโฏ20 ๋ฆฌ์คํธ๋ฉด OK. |
|
4. **UI ๋จ์ํ** โ ์๋ ์
๋ ฅ โ ๋ฒํผ โ ๋งํฌ๋ค์ด ๊ฒฐ๊ณผ. |
|
|
|
์คํ๋ฒ |
|
------ |
|
```bash |
|
pip install --upgrade gradio openai |
|
export OPENAI_API_KEY="sk-..." |
|
python square_theory_gradio.py |
|
``` |
|
""" |
|
|
|
import os |
|
import json |
|
import gradio as gr |
|
from openai import OpenAI |
|
|
|
|
|
|
|
|
|
if not os.getenv("OPENAI_API_KEY"): |
|
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.") |
|
|
|
client = OpenAI() |
|
|
|
|
|
|
|
|
|
SYSTEM_PROMPT = ( |
|
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. " |
|
"์ฌ์ฉ์๊ฐ ์
๋ ฅํ ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๋ฐ์ด๋ ์์๋๋ก ์ต๋ 20๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. " |
|
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. " |
|
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ ํ๋ค. JSON ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค." |
|
) |
|
|
|
|
|
def clean_json_block(text: str) -> str: |
|
text = text.strip() |
|
if text.startswith("```"): |
|
text = text.split("\n", 1)[1] if "\n" in text else text[3:] |
|
if text.endswith("```"): |
|
text = text[:-3] |
|
return text.strip() |
|
|
|
|
|
def call_llm(seed: str): |
|
resp = client.chat.completions.create( |
|
model="gpt-4o-mini", |
|
messages=[ |
|
{"role": "system", "content": SYSTEM_PROMPT}, |
|
{"role": "user", "content": seed}, |
|
], |
|
temperature=0.9, |
|
max_tokens=2048, |
|
) |
|
raw = resp.choices[0].message.content |
|
cleaned = clean_json_block(raw) |
|
try: |
|
data = json.loads(cleaned) |
|
if isinstance(data, dict): |
|
data = [data] |
|
if not isinstance(data, list): |
|
raise TypeError("LLM ์๋ต์ด ๋ฆฌ์คํธ๊ฐ ์๋") |
|
if not 1 <= len(data) <= 20: |
|
raise ValueError("์๋ต ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ 1~20 ๋ฒ์๋ฅผ ๋ฒ์ด๋จ") |
|
except Exception as exc: |
|
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ ์ผ๋ถ: {cleaned[:300]} โฆ") |
|
return data |
|
|
|
|
|
|
|
|
|
|
|
def generate(seed_word: str): |
|
results = call_llm(seed_word) |
|
md_lines = [f"## ์ด {len(results)}๊ฐ ์ ์\n"] |
|
for idx, item in enumerate(results, 1): |
|
md_lines.append( |
|
f"### {idx}. {item['top_phrase']} / {item['bottom_phrase']}\n" |
|
f"- **์ฌ๋ก๊ฑด**: {item['slogan']}\n" |
|
f"- **๋ธ๋๋ ๋ค์**: {item['brand']}\n" |
|
f"- (tl={item['tl']}, tr={item['tr']}, br={item['br']}, bl={item['bl']})\n" |
|
) |
|
return "\n".join(md_lines) |
|
|
|
|
|
|
|
|
|
with gr.Blocks(title="Square Theory โ ์ต๋ 20๊ฐ ๐ฐ๐ท") as demo: |
|
gr.Markdown("""# ๐ง Square Theory ์ ์ (์ต๋ 20๊ฐ)\n๋จ์ด ํ๋ ์
๋ ฅ โ LLM์ด ์ ์ํยท์ ๋ ฌํ ์ฌ๊ฐํ/์นดํผ/๋ธ๋๋ ๋ค์""") |
|
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ") |
|
run = gr.Button("์์ฑ") |
|
md_out = gr.Markdown(label="์ ์ ๋ชฉ๋ก") |
|
|
|
run.click(generate, inputs=seed, outputs=md_out) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
""" |
|
Square Theory Generator (Markdownโonly, up to 20 ideas) |
|
===================================================== |
|
2025โ05โ28ย v7 โ ๋ฐ์ค ๋ค์ด์ด๊ทธ๋จ ์ ๊ฑฐ & ์ ์ย 20๊ฐ๋ก ํ์ฅ |
|
--------------------------------------------------- |
|
๋ณ๊ฒฝ ์์ฝ |
|
--------- |
|
1. **์ฌ๊ฐํ ์๊ฐํ ์ ๊ฑฐ** โ ์ถ๋ ฅ/์์กด์์ `matplotlib` ์ ๋ถ ์ญ์ . |
|
2. **์ ์ ๊ฐ์ ์ํฅ** โ LLM์ด ์ต๋ **20๊ฐ** ์์ด๋์ด ๋ฐํ. |
|
3. **์ ์ฐ ๊ธธ์ด ํ์ฉ** โ 1โฏโคโฏNโฏโคโฏ20 ๋ฆฌ์คํธ๋ฉด OK. |
|
4. **UI ๋จ์ํ** โ ์๋ ์
๋ ฅ โ ๋ฒํผ โ ๋งํฌ๋ค์ด ๊ฒฐ๊ณผ. |
|
|
|
์คํ๋ฒ |
|
------ |
|
```bash |
|
pip install --upgrade gradio openai |
|
export OPENAI_API_KEY="sk-..." |
|
python square_theory_gradio.py |
|
``` |
|
""" |
|
|
|
import os |
|
import json |
|
import gradio as gr |
|
from openai import OpenAI |
|
|
|
|
|
|
|
|
|
if not os.getenv("OPENAI_API_KEY"): |
|
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.") |
|
|
|
client = OpenAI() |
|
|
|
|
|
|
|
|
|
SYSTEM_PROMPT = ( |
|
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. " |
|
"์ฌ์ฉ์๊ฐ ์
๋ ฅํ ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๋ฐ์ด๋ ์์๋๋ก ์ต๋ 20๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. " |
|
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. " |
|
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ ํ๋ค. JSON ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค." |
|
) |
|
|
|
|
|
def clean_json_block(text: str) -> str: |
|
text = text.strip() |
|
if text.startswith("```"): |
|
text = text.split("\n", 1)[1] if "\n" in text else text[3:] |
|
if text.endswith("```"): |
|
text = text[:-3] |
|
return text.strip() |
|
|
|
|
|
def call_llm(seed: str): |
|
resp = client.chat.completions.create( |
|
model="gpt-4o-mini", |
|
messages=[ |
|
{"role": "system", "content": SYSTEM_PROMPT}, |
|
{"role": "user", "content": seed}, |
|
], |
|
temperature=0.9, |
|
max_tokens=2048, |
|
) |
|
raw = resp.choices[0].message.content |
|
cleaned = clean_json_block(raw) |
|
try: |
|
data = json.loads(cleaned) |
|
if isinstance(data, dict): |
|
data = [data] |
|
if not isinstance(data, list): |
|
raise TypeError("LLM ์๋ต์ด ๋ฆฌ์คํธ๊ฐ ์๋") |
|
if not 1 <= len(data) <= 20: |
|
raise ValueError("์๋ต ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ 1~20 ๋ฒ์๋ฅผ ๋ฒ์ด๋จ") |
|
except Exception as exc: |
|
raise ValueError(f"LLM JSON ํ์ฑ ์คํจ: {exc}\n์๋ฌธ ์ผ๋ถ: {cleaned[:300]} โฆ") |
|
return data |
|
|
|
|
|
|
|
|
|
|
|
def generate(seed_word: str): |
|
results = call_llm(seed_word) |
|
md_lines = [f"## ์ด {len(results)}๊ฐ ์ ์\n"] |
|
for idx, item in enumerate(results, 1): |
|
md_lines.append( |
|
f"### {idx}. {item['top_phrase']} / {item['bottom_phrase']}\n" |
|
f"- **์ฌ๋ก๊ฑด**: {item['slogan']}\n" |
|
f"- **๋ธ๋๋ ๋ค์**: {item['brand']}\n" |
|
f"- (tl={item['tl']}, tr={item['tr']}, br={item['br']}, bl={item['bl']})\n" |
|
) |
|
return "\n".join(md_lines) |
|
|
|
|
|
|
|
|
|
with gr.Blocks(title="Square Theory โ ์ต๋ 20๊ฐ ๐ฐ๐ท") as demo: |
|
gr.Markdown("""# ๐ง Square Theory ์ ์ (์ต๋ 20๊ฐ)\n๋จ์ด ํ๋ ์
๋ ฅ โ LLM์ด ์ ์ํยท์ ๋ ฌํ ์ฌ๊ฐํ/์นดํผ/๋ธ๋๋ ๋ค์""") |
|
seed = gr.Textbox(label="์๋ ๋จ์ด(TL)", placeholder="์: ๊ณจ๋ ") |
|
run = gr.Button("์์ฑ") |
|
md_out = gr.Markdown(label="์ ์ ๋ชฉ๋ก") |
|
|
|
run.click(generate, inputs=seed, outputs=md_out) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|