Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -179,28 +179,28 @@ def generate_poster(
|
|
| 179 |
seed_input,
|
| 180 |
progress=gr.Progress(track_tqdm=True),
|
| 181 |
):
|
| 182 |
-
"""
|
| 183 |
if not original_prompt or not original_prompt.strip():
|
| 184 |
-
return None, "❌
|
| 185 |
|
| 186 |
try:
|
| 187 |
if not HF_TOKEN:
|
| 188 |
-
return None, "❌
|
| 189 |
|
| 190 |
-
progress(0.1, desc="
|
| 191 |
|
| 192 |
-
#
|
| 193 |
final_prompt = original_prompt
|
| 194 |
if enable_recap:
|
| 195 |
-
progress(0.2, desc="
|
| 196 |
final_prompt = enhance_prompt_with_qwen(original_prompt)
|
| 197 |
|
| 198 |
-
#
|
| 199 |
actual_seed = int(seed_input) if seed_input and seed_input != -1 else random.randint(1, 2**32 - 1)
|
| 200 |
|
| 201 |
-
progress(0.3, desc="
|
| 202 |
|
| 203 |
-
#
|
| 204 |
generator = torch.Generator("cuda").manual_seed(actual_seed)
|
| 205 |
|
| 206 |
with torch.inference_mode():
|
|
@@ -213,99 +213,62 @@ def generate_poster(
|
|
| 213 |
height=int(height)
|
| 214 |
).images[0]
|
| 215 |
|
| 216 |
-
progress(1.0, desc="
|
| 217 |
-
status_log = f"✅
|
| 218 |
return image, status_log, final_prompt
|
| 219 |
|
| 220 |
except Exception as e:
|
| 221 |
-
logging.error(f"
|
| 222 |
-
return None, f"❌
|
| 223 |
|
| 224 |
# ─────────────────────────────────────────────
|
| 225 |
-
# Gradio
|
| 226 |
# ─────────────────────────────────────────────
|
| 227 |
def create_interface():
|
| 228 |
-
"""
|
| 229 |
|
| 230 |
-
with gr.Blocks(
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
css="""
|
| 234 |
-
.main-container { max-width: 1200px; margin: 0 auto; }
|
| 235 |
-
.status-box { padding: 10px; border-radius: 5px; margin: 10px 0; }
|
| 236 |
-
"""
|
| 237 |
-
) as demo:
|
| 238 |
-
|
| 239 |
-
gr.HTML("""
|
| 240 |
-
<div class="main-container">
|
| 241 |
-
<h1 style="text-align: center; margin-bottom: 20px;">🎨 PosterCraft-v1.0</h1>
|
| 242 |
-
<p style="text-align: center; color: #666; margin-bottom: 30px;">
|
| 243 |
-
专业海报生成工具,基于 FLUX.1-dev 和定制微调权重
|
| 244 |
-
</p>
|
| 245 |
-
</div>
|
| 246 |
-
""")
|
| 247 |
-
|
| 248 |
-
with gr.Row():
|
| 249 |
-
gr.Markdown(f"**基础模型:** `{DEFAULT_PIPELINE_PATH}`")
|
| 250 |
-
gr.Markdown(f"**认证状态:** {auth_status}")
|
| 251 |
-
|
| 252 |
-
gr.HTML("""
|
| 253 |
-
<div class="status-box">
|
| 254 |
-
<p><strong>⚡ 首次生成需要加载模型(5-10分钟),后续生成会非常快!</strong></p>
|
| 255 |
-
</div>
|
| 256 |
-
""")
|
| 257 |
|
| 258 |
with gr.Row():
|
| 259 |
with gr.Column(scale=1):
|
| 260 |
-
gr.Markdown("### 1.
|
| 261 |
prompt_input = gr.Textbox(
|
| 262 |
-
label="
|
| 263 |
lines=3,
|
| 264 |
-
placeholder="
|
| 265 |
-
value="
|
| 266 |
)
|
| 267 |
enable_recap_checkbox = gr.Checkbox(
|
| 268 |
-
label="
|
| 269 |
value=True,
|
| 270 |
-
info="
|
| 271 |
)
|
| 272 |
|
| 273 |
with gr.Row():
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
seed_number_input = gr.Number(label="种子 (-1 随机)", value=-1, minimum=-1, step=1)
|
| 283 |
-
generate_button = gr.Button("🎨 生成海报", variant="primary", size="lg")
|
| 284 |
|
| 285 |
with gr.Column(scale=1):
|
| 286 |
-
gr.Markdown("### 2.
|
| 287 |
-
image_output = gr.Image(label="
|
| 288 |
-
|
| 289 |
-
|
| 290 |
|
| 291 |
inputs_list = [
|
| 292 |
prompt_input, enable_recap_checkbox, height_input, width_input,
|
| 293 |
num_inference_steps_input, guidance_scale_input, seed_number_input
|
| 294 |
]
|
| 295 |
-
outputs_list = [image_output,
|
| 296 |
|
| 297 |
generate_button.click(fn=generate_poster, inputs=inputs_list, outputs=outputs_list)
|
| 298 |
-
|
| 299 |
-
# 示例
|
| 300 |
-
gr.Examples(
|
| 301 |
-
examples=[
|
| 302 |
-
["复古科幻电影海报,霓虹色彩和飞行汽车"],
|
| 303 |
-
["优雅的装饰艺术风格豪华酒店海报"],
|
| 304 |
-
["简约音乐会海报,粗体字体"],
|
| 305 |
-
["有机咖啡的复古广告"],
|
| 306 |
-
],
|
| 307 |
-
inputs=[prompt_input]
|
| 308 |
-
)
|
| 309 |
|
| 310 |
return demo
|
| 311 |
|
|
|
|
| 179 |
seed_input,
|
| 180 |
progress=gr.Progress(track_tqdm=True),
|
| 181 |
):
|
| 182 |
+
"""Generate poster using preloaded models"""
|
| 183 |
if not original_prompt or not original_prompt.strip():
|
| 184 |
+
return None, "❌ Prompt cannot be empty!", ""
|
| 185 |
|
| 186 |
try:
|
| 187 |
if not HF_TOKEN:
|
| 188 |
+
return None, "❌ Error: HF_TOKEN not found, please configure authentication.", ""
|
| 189 |
|
| 190 |
+
progress(0.1, desc="Starting generation...")
|
| 191 |
|
| 192 |
+
# Determine final prompt
|
| 193 |
final_prompt = original_prompt
|
| 194 |
if enable_recap:
|
| 195 |
+
progress(0.2, desc="Enhancing prompt...")
|
| 196 |
final_prompt = enhance_prompt_with_qwen(original_prompt)
|
| 197 |
|
| 198 |
+
# Determine seed
|
| 199 |
actual_seed = int(seed_input) if seed_input and seed_input != -1 else random.randint(1, 2**32 - 1)
|
| 200 |
|
| 201 |
+
progress(0.3, desc="Generating image...")
|
| 202 |
|
| 203 |
+
# Use preloaded FLUX pipeline to generate image
|
| 204 |
generator = torch.Generator("cuda").manual_seed(actual_seed)
|
| 205 |
|
| 206 |
with torch.inference_mode():
|
|
|
|
| 213 |
height=int(height)
|
| 214 |
).images[0]
|
| 215 |
|
| 216 |
+
progress(1.0, desc="Complete!")
|
| 217 |
+
status_log = f"✅ Generation complete! Seed: {actual_seed}"
|
| 218 |
return image, status_log, final_prompt
|
| 219 |
|
| 220 |
except Exception as e:
|
| 221 |
+
logging.error(f"Generation failed: {e}")
|
| 222 |
+
return None, f"❌ Generation failed: {str(e)}", ""
|
| 223 |
|
| 224 |
# ─────────────────────────────────────────────
|
| 225 |
+
# Gradio Interface
|
| 226 |
# ─────────────────────────────────────────────
|
| 227 |
def create_interface():
|
| 228 |
+
"""Create Gradio interface"""
|
| 229 |
|
| 230 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 231 |
+
gr.Markdown("# PosterCraft-v1.0")
|
| 232 |
+
gr.Markdown(f"Running on: **{device if 'device' in globals() else 'cuda' if torch.cuda.is_available() else 'cpu'}** | Base Pipeline: **{DEFAULT_PIPELINE_PATH}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
with gr.Row():
|
| 235 |
with gr.Column(scale=1):
|
| 236 |
+
gr.Markdown("### 1. Configuration")
|
| 237 |
prompt_input = gr.Textbox(
|
| 238 |
+
label="Prompt",
|
| 239 |
lines=3,
|
| 240 |
+
placeholder="Enter your creative prompt...",
|
| 241 |
+
value="Retro sci-fi movie poster with neon colors and flying cars"
|
| 242 |
)
|
| 243 |
enable_recap_checkbox = gr.Checkbox(
|
| 244 |
+
label="Enable Prompt Recap",
|
| 245 |
value=True,
|
| 246 |
+
info=f"Uses {DEFAULT_QWEN_MODEL_PATH} for rewriting."
|
| 247 |
)
|
| 248 |
|
| 249 |
with gr.Row():
|
| 250 |
+
width_input = gr.Slider(label="Width", minimum=256, maximum=2048, value=832, step=64)
|
| 251 |
+
height_input = gr.Slider(label="Height", minimum=256, maximum=2048, value=1216, step=64)
|
| 252 |
+
gr.Markdown("Tip: Recommended size is 832x1216 for best results.")
|
| 253 |
|
| 254 |
+
num_inference_steps_input = gr.Slider(label="Inference Steps", minimum=1, maximum=100, value=28, step=1)
|
| 255 |
+
guidance_scale_input = gr.Slider(label="Guidance Scale (CFG)", minimum=0.0, maximum=20.0, value=3.5, step=0.1)
|
| 256 |
+
seed_number_input = gr.Number(label="Seed", value=None, minimum=-1, step=1, info="Leave blank or set to -1 for a random seed.")
|
| 257 |
+
generate_button = gr.Button("Generate Image", variant="primary")
|
|
|
|
|
|
|
| 258 |
|
| 259 |
with gr.Column(scale=1):
|
| 260 |
+
gr.Markdown("### 2. Results")
|
| 261 |
+
image_output = gr.Image(label="Generated Image", type="pil", show_download_button=True, height=512)
|
| 262 |
+
recapped_prompt_output = gr.Textbox(label="Final Prompt Used", lines=5, interactive=False)
|
| 263 |
+
status_output = gr.Textbox(label="Status Log", lines=4, interactive=False)
|
| 264 |
|
| 265 |
inputs_list = [
|
| 266 |
prompt_input, enable_recap_checkbox, height_input, width_input,
|
| 267 |
num_inference_steps_input, guidance_scale_input, seed_number_input
|
| 268 |
]
|
| 269 |
+
outputs_list = [image_output, recapped_prompt_output, status_output]
|
| 270 |
|
| 271 |
generate_button.click(fn=generate_poster, inputs=inputs_list, outputs=outputs_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
return demo
|
| 274 |
|