Update app.py
Browse files
app.py
CHANGED
|
@@ -108,14 +108,63 @@ def openai_translate(text: str, retries: int = 3) -> str:
|
|
| 108 |
time.sleep(2)
|
| 109 |
return text # ๋ฒ์ญ ์คํจ ์ ์๋ฌธ ๊ทธ๋๋ก
|
| 110 |
|
| 111 |
-
def
|
| 112 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
prompt_en = openai_translate(user_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
style_suffix = STYLE_PRESETS.get(style_key, "")
|
| 115 |
if style_suffix:
|
| 116 |
final_prompt = f"{prompt_en}, {style_suffix}"
|
| 117 |
else:
|
| 118 |
final_prompt = prompt_en
|
|
|
|
| 119 |
return final_prompt
|
| 120 |
|
| 121 |
# ===== ์ด๋ฏธ์ง ์ ์ฅ =====
|
|
@@ -157,6 +206,7 @@ def run_pipeline(prompt: str, seed: int, width: int, height: int, guidance_scale
|
|
| 157 |
def generate_image(
|
| 158 |
user_prompt: str,
|
| 159 |
style_key: str,
|
|
|
|
| 160 |
seed: int = 42,
|
| 161 |
randomize_seed: bool = True,
|
| 162 |
width: int = 1024,
|
|
@@ -171,7 +221,7 @@ def generate_image(
|
|
| 171 |
seed = random.randint(0, MAX_SEED)
|
| 172 |
|
| 173 |
# 1) ๋ฒ์ญ + ์ฆ๊ฐ
|
| 174 |
-
final_prompt = prepare_prompt(user_prompt, style_key)
|
| 175 |
print(f"Final prompt: {final_prompt}")
|
| 176 |
|
| 177 |
# 2) ํ์ดํ๋ผ์ธ ํธ์ถ
|
|
@@ -224,6 +274,38 @@ button:hover{transform:translateY(-2px); box-shadow:0 5px 15px rgba(0,0,0,.1);}
|
|
| 224 |
.input-container{border-radius:10px; box-shadow:0 2px 8px rgba(0,0,0,.05); background:rgba(255,255,255,.6); padding:20px; margin-bottom:1rem;}
|
| 225 |
.advanced-settings{margin-top:1rem; padding:1rem; border-radius:10px; background:rgba(255,255,255,.6);}
|
| 226 |
.example-region{background:rgba(255,255,255,.5); border-radius:10px; padding:1rem; margin-top:1rem;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
"""
|
| 228 |
|
| 229 |
# ===== Gradio UI =====
|
|
@@ -244,9 +326,36 @@ def create_interface():
|
|
| 244 |
# ===== ๋ฉ์ธ ์
๋ ฅ =====
|
| 245 |
with gr.Column():
|
| 246 |
with gr.Row(elem_classes="input-container"):
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
result_image = gr.Image(label="Generated Image")
|
| 252 |
seed_output = gr.Number(label="Seed")
|
|
@@ -274,6 +383,7 @@ def create_interface():
|
|
| 274 |
inputs=[
|
| 275 |
user_prompt,
|
| 276 |
style_select,
|
|
|
|
| 277 |
seed,
|
| 278 |
randomize_seed,
|
| 279 |
width,
|
|
|
|
| 108 |
time.sleep(2)
|
| 109 |
return text # ๋ฒ์ญ ์คํจ ์ ์๋ฌธ ๊ทธ๋๋ก
|
| 110 |
|
| 111 |
+
def enhance_prompt(text: str, retries: int = 3) -> str:
|
| 112 |
+
"""OpenAI๋ฅผ ํตํด ํ๋กฌํํธ๋ฅผ ์ฆ๊ฐํ์ฌ ๊ณ ํ์ง ์ด๋ฏธ์ง ์์ฑ์ ์ํ ์์ธํ ์ค๋ช
์ผ๋ก ๋ณํ."""
|
| 113 |
+
if client is None:
|
| 114 |
+
print("Warning: OpenAI client not available, returning original text")
|
| 115 |
+
return text
|
| 116 |
+
|
| 117 |
+
for attempt in range(retries):
|
| 118 |
+
try:
|
| 119 |
+
res = client.chat.completions.create(
|
| 120 |
+
model="gpt-4o-mini",
|
| 121 |
+
messages=[
|
| 122 |
+
{
|
| 123 |
+
"role": "system",
|
| 124 |
+
"content": """You are an expert prompt engineer for image generation models. Enhance the given prompt to create high-quality, detailed images.
|
| 125 |
+
|
| 126 |
+
Guidelines:
|
| 127 |
+
- Add specific visual details (lighting, composition, colors, textures)
|
| 128 |
+
- Include technical photography terms (depth of field, focal length, etc.)
|
| 129 |
+
- Add atmosphere and mood descriptors
|
| 130 |
+
- Specify image quality terms (4K, ultra-detailed, professional, etc.)
|
| 131 |
+
- Keep the core subject and meaning intact
|
| 132 |
+
- Make it comprehensive but not overly long
|
| 133 |
+
- Focus on visual elements that will improve image generation quality
|
| 134 |
+
|
| 135 |
+
Example:
|
| 136 |
+
Input: "A man giving a speech"
|
| 137 |
+
Output: "A professional man giving an inspiring speech at a podium, dramatic lighting with warm spotlights, confident posture and gestures, high-resolution 4K photography, sharp focus, cinematic composition, bokeh background with audience silhouettes, professional event setting, detailed facial expressions, realistic skin texture"
|
| 138 |
+
"""
|
| 139 |
+
},
|
| 140 |
+
{"role": "user", "content": f"Enhance this prompt for high-quality image generation: {text}"}
|
| 141 |
+
],
|
| 142 |
+
temperature=0.7,
|
| 143 |
+
max_tokens=512,
|
| 144 |
+
)
|
| 145 |
+
return res.choices[0].message.content.strip()
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f"[enhance] attempt {attempt + 1} failed: {e}")
|
| 148 |
+
time.sleep(2)
|
| 149 |
+
return text # ์ฆ๊ฐ ์คํจ ์ ์๋ฌธ ๊ทธ๋๋ก
|
| 150 |
+
|
| 151 |
+
def prepare_prompt(user_prompt: str, style_key: str, enhance_prompt_enabled: bool = False) -> str:
|
| 152 |
+
"""ํ๊ธ์ด๋ฉด ๋ฒ์ญํ๊ณ , ํ๋กฌํํธ ์ฆ๊ฐ ์ต์
์ด ํ์ฑํ๋๋ฉด ์ฆ๊ฐํ๊ณ , ์ ํํ ์คํ์ผ ํ๋ฆฌ์
์ ๋ถ์ฌ์ ์ต์ข
ํ๋กฌํํธ๋ฅผ ๋ง๋ ๋ค."""
|
| 153 |
+
# 1. ๋ฒ์ญ (ํ๊ธ์ธ ๊ฒฝ์ฐ)
|
| 154 |
prompt_en = openai_translate(user_prompt)
|
| 155 |
+
|
| 156 |
+
# 2. ํ๋กฌํํธ ์ฆ๊ฐ (ํ์ฑํ๋ ๊ฒฝ์ฐ)
|
| 157 |
+
if enhance_prompt_enabled:
|
| 158 |
+
prompt_en = enhance_prompt(prompt_en)
|
| 159 |
+
print(f"Enhanced prompt: {prompt_en}")
|
| 160 |
+
|
| 161 |
+
# 3. ์คํ์ผ ํ๋ฆฌ์
์ ์ฉ
|
| 162 |
style_suffix = STYLE_PRESETS.get(style_key, "")
|
| 163 |
if style_suffix:
|
| 164 |
final_prompt = f"{prompt_en}, {style_suffix}"
|
| 165 |
else:
|
| 166 |
final_prompt = prompt_en
|
| 167 |
+
|
| 168 |
return final_prompt
|
| 169 |
|
| 170 |
# ===== ์ด๋ฏธ์ง ์ ์ฅ =====
|
|
|
|
| 206 |
def generate_image(
|
| 207 |
user_prompt: str,
|
| 208 |
style_key: str,
|
| 209 |
+
enhance_prompt_enabled: bool = False,
|
| 210 |
seed: int = 42,
|
| 211 |
randomize_seed: bool = True,
|
| 212 |
width: int = 1024,
|
|
|
|
| 221 |
seed = random.randint(0, MAX_SEED)
|
| 222 |
|
| 223 |
# 1) ๋ฒ์ญ + ์ฆ๊ฐ
|
| 224 |
+
final_prompt = prepare_prompt(user_prompt, style_key, enhance_prompt_enabled)
|
| 225 |
print(f"Final prompt: {final_prompt}")
|
| 226 |
|
| 227 |
# 2) ํ์ดํ๋ผ์ธ ํธ์ถ
|
|
|
|
| 274 |
.input-container{border-radius:10px; box-shadow:0 2px 8px rgba(0,0,0,.05); background:rgba(255,255,255,.6); padding:20px; margin-bottom:1rem;}
|
| 275 |
.advanced-settings{margin-top:1rem; padding:1rem; border-radius:10px; background:rgba(255,255,255,.6);}
|
| 276 |
.example-region{background:rgba(255,255,255,.5); border-radius:10px; padding:1rem; margin-top:1rem;}
|
| 277 |
+
|
| 278 |
+
/* ํ๋กฌํํธ ์
๋ ฅ์นธ ํฌ๊ธฐ 2๋ฐฐ ์ฆ๊ฐ */
|
| 279 |
+
.large-prompt textarea {
|
| 280 |
+
min-height: 120px !important;
|
| 281 |
+
font-size: 16px !important;
|
| 282 |
+
line-height: 1.5 !important;
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
/* ์์ฑ ๋ฒํผ ์๊ฒ ๋ง๋ค๊ธฐ */
|
| 286 |
+
.small-generate-btn {
|
| 287 |
+
max-width: 120px !important;
|
| 288 |
+
height: 40px !important;
|
| 289 |
+
font-size: 14px !important;
|
| 290 |
+
padding: 8px 16px !important;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
/* ํ๋กฌํํธ ์ฆ๊ฐ ์น์
์คํ์ผ */
|
| 294 |
+
.prompt-enhance-section {
|
| 295 |
+
background: rgba(255,255,255,.7);
|
| 296 |
+
border-radius: 8px;
|
| 297 |
+
padding: 15px;
|
| 298 |
+
margin-top: 10px;
|
| 299 |
+
border-left: 3px solid var(--color-primary);
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
/* ์คํ์ผ ํ๋ฆฌ์
์น์
*/
|
| 303 |
+
.style-preset-section {
|
| 304 |
+
background: rgba(255,255,255,.6);
|
| 305 |
+
border-radius: 8px;
|
| 306 |
+
padding: 15px;
|
| 307 |
+
margin-top: 10px;
|
| 308 |
+
}
|
| 309 |
"""
|
| 310 |
|
| 311 |
# ===== Gradio UI =====
|
|
|
|
| 326 |
# ===== ๋ฉ์ธ ์
๋ ฅ =====
|
| 327 |
with gr.Column():
|
| 328 |
with gr.Row(elem_classes="input-container"):
|
| 329 |
+
with gr.Column(scale=4):
|
| 330 |
+
user_prompt = gr.Text(
|
| 331 |
+
label="Prompt",
|
| 332 |
+
max_lines=5,
|
| 333 |
+
value=examples[0],
|
| 334 |
+
elem_classes="large-prompt"
|
| 335 |
+
)
|
| 336 |
+
with gr.Column(scale=1):
|
| 337 |
+
run_button = gr.Button(
|
| 338 |
+
"์์ฑ",
|
| 339 |
+
variant="primary",
|
| 340 |
+
elem_classes="small-generate-btn"
|
| 341 |
+
)
|
| 342 |
+
|
| 343 |
+
# ํ๋กฌํํธ ์ฆ๊ฐ ์ต์
(์์ฑ ๋ฒํผ ์๋)
|
| 344 |
+
with gr.Group(elem_classes="prompt-enhance-section"):
|
| 345 |
+
enhance_prompt_checkbox = gr.Checkbox(
|
| 346 |
+
label="๐ ํ๋กฌํํธ ์ฆ๊ฐ (AI๋ก ํ๋กฌํํธ๋ฅผ ์๋์ผ๋ก ๊ฐ์ ํ์ฌ ๊ณ ํ์ง ์ด๋ฏธ์ง ์์ฑ)",
|
| 347 |
+
value=False,
|
| 348 |
+
info="OpenAI API๋ฅผ ์ฌ์ฉํ์ฌ ์
๋ ฅํ ํ๋กฌํํธ๋ฅผ ๋์ฑ ์์ธํ๊ณ ๊ณ ํ์ง์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ ์ ์๋๋ก ์๋์ผ๋ก ์ฆ๊ฐํฉ๋๋ค."
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
# ์คํ์ผ ํ๋ฆฌ์
์น์
|
| 352 |
+
with gr.Group(elem_classes="style-preset-section"):
|
| 353 |
+
style_select = gr.Radio(
|
| 354 |
+
label="๐จ Style Preset",
|
| 355 |
+
choices=list(STYLE_PRESETS.keys()),
|
| 356 |
+
value="None",
|
| 357 |
+
interactive=True
|
| 358 |
+
)
|
| 359 |
|
| 360 |
result_image = gr.Image(label="Generated Image")
|
| 361 |
seed_output = gr.Number(label="Seed")
|
|
|
|
| 383 |
inputs=[
|
| 384 |
user_prompt,
|
| 385 |
style_select,
|
| 386 |
+
enhance_prompt_checkbox,
|
| 387 |
seed,
|
| 388 |
randomize_seed,
|
| 389 |
width,
|