File size: 5,397 Bytes
26ca8c8 e420f43 4be365e dd53e8d 9db6da0 4be365e 9db6da0 f065a36 e420f43 f065a36 26ca8c8 5b4cfb6 26ca8c8 4be365e 5b4cfb6 e420f43 5b4cfb6 f065a36 5b4cfb6 e420f43 c953917 e420f43 7f0c2bb e420f43 7f0c2bb e420f43 7f0c2bb 4be365e 7f0c2bb e420f43 7f0c2bb e420f43 7f0c2bb e420f43 7f0c2bb e420f43 7f0c2bb e420f43 7f0c2bb 5b4cfb6 4be365e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
"""
Square Theory Generator (Markdownโonly, up to 20 ideas)
=====================================================
2025โ05โ28ย v10 โ ImportErrorย fix (OpenAI error module)
---------------------------------------------------
๋ณ๊ฒฝ ์์ฝ
---------
* **๋ฌธ์ **: `from openai import OpenAI, error as oai_err` โ v1ย SDK์๋ `error` ์๋ธ๋ชจ๋์ด ์์ โ `ImportError`.
* **ํด๊ฒฐ**: `import openai` ํ `openai.OpenAIError` ์ฐธ์กฐ.
`except` ์ ์ `openai.OpenAIError` ๋ก ๊ต์ฒด.
์คํ๋ฒ
------
```bash
pip install --upgrade gradio openai
export OPENAI_API_KEY="sk-..."
python square_theory_gradio.py
```
"""
import os
import json
import gradio as gr
import openai
from openai import OpenAI
# -------------------------------------------------
# 0. OpenAI ํด๋ผ์ด์ธํธ
# -------------------------------------------------
if not os.getenv("OPENAI_API_KEY"):
raise EnvironmentError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์.")
client = OpenAI()
# -------------------------------------------------
# 1. LLM Prompt & Utilities
# -------------------------------------------------
SYSTEM_PROMPT = (
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
"์ฌ์ฉ์๊ฐ ์
๋ ฅํ ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๋ฐ์ด๋ ์์๋๋ก ์ต๋ 20๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. "
"**์ค์**: tl, tr, br, bl ๊ฐ ํ๋๋ ๋ฐ๋์ ๊ณต๋ฐฑ์ด ์๋ **๋จ์ผ ๋จ์ด**(ํ๊ธ 1~2์ด์ ๋๋ ์์ด 1๋จ์ด)์ฌ์ผ ํ๋ค. "
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. "
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ ํ๋ฉฐ JSON ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค."
)
"๋๋ ํ๊ตญ์ด ์นดํผยท๋ธ๋๋ ๋ค์ด๋ฐ ์ ๋ฌธ๊ฐ์ด์ Square Theory ๋์ฐ๋ฏธ๋ค. "
"์ฌ์ฉ์๊ฐ ์
๋ ฅํ ํ๋์ ๋จ์ด(tl)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง์ด ๋ฐ์ด๋ ์์๋๋ก ์ต๋ 20๊ฐ์ ์ ์์ JSON ๋ฐฐ์ด๋ก ๋ฐํํด๋ผ. "
"๊ฐ ์์๋ tl, tr, br, bl, top_phrase, bottom_phrase, slogan, brand ํ๋๋ฅผ ๊ฐ์ง๋ค. "
"๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์๊ฐ ๊ฐ์ฅ ์ฐ์ํด์ผ ํ๋ค. JSON ์ธ ๋ฌธ์๋ ๊ธ์งํ๋ค."
)
FALLBACK_MODELS = ["gpt-4o-mini", "gpt-4o", "gpt-4o-preview", "gpt-4-turbo"]
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):
last_exc = None
for model in FALLBACK_MODELS:
try:
resp = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": seed},
],
temperature=0.9,
max_tokens=2048,
)
cleaned = _clean_json_block(resp.choices[0].message.content)
data = json.loads(cleaned) if cleaned else []
if isinstance(data, dict):
data = [data]
if not isinstance(data, list) or not (1 <= len(data) <= 20):
raise ValueError("LLM ์๋ต์ด ์ฌ๋ฐ๋ฅธ 1โ20๊ฐ ๋ฆฌ์คํธ๊ฐ ์๋")
return data
except (openai.OpenAIError, json.JSONDecodeError, ValueError, TypeError) as exc:
last_exc = exc
continue
raise RuntimeError(f"LLM ํธ์ถ ์คํจ: {last_exc}")
# -------------------------------------------------
# 2. Gradio callback
# -------------------------------------------------
def generate(seed_word: str):
seed_word = seed_word.strip()
if not seed_word:
return "โ ๏ธ **์๋ ๋จ์ด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.**"
try:
results = _call_llm(seed_word)
md = [f"## ์ด {len(results)}๊ฐ ์ ์"]
for idx, item in enumerate(results, 1):
md.append(
f"### {idx}. ๋ธ๋๋ ๋ค์: {item['brand']}\n"
f"**๋ฉ์ธ ์นดํผ** \
โข ์๋จ: {item['top_phrase']} \
โข ํ๋จ: {item['bottom_phrase']}\n\n"
f"**์ฌ๋ก๊ฑด** \
> {item['slogan']}\n\n"
f"**์ฌ๊ฐํ ํค์๋** \
TL: {item['tl']} ยท TR: {item['tr']} ยท BR: {item['br']} ยท BL: {item['bl']}\n"
)
return "\n".join(md)
except Exception as exc:
return f"โ **์ค๋ฅ:** {exc}"
# -------------------------------------------------
# 3. UI
# -------------------------------------------------
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()
run.click(generate, inputs=seed, outputs=md_out)
demo.queue().launch()
# -------------------------------------------------
# 4. (Optional) ์ถ๊ฐ ๊ธฐ๋ฅ ์์ด๋์ด
# -------------------------------------------------
# - JSON์ "why" ํ๋ ์์ฒญ โ ๊ฐ ์ ์์ ์ ์ ์ด์ ๋ฅผ ํ์ํด ์ค๋๋ ฅ ๊ฐํ
# - ํ๊น ์ฑ๋๋ณ ํ๊ทธ๋ผ์ธ ๋ณํ ์ต์
(OHHยทSNSยทTV)
# - CSV ๋ค์ด๋ก๋ ๋ฒํผ์ผ๋ก ๊ฒฐ๊ณผ ์ผ๊ด ์ ์ฅ
|