Ephemeral182 commited on
Commit
07ec0a5
·
verified ·
1 Parent(s): 8136fd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -73
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, "❌ 错误:未找到 HF_TOKEN,请配置认证。", ""
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
- # 使用预加载的 FLUX 管道生成图像
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"✅ 生成完成!种子:{actual_seed}"
218
  return image, status_log, final_prompt
219
 
220
  except Exception as e:
221
- logging.error(f"生成失败:{e}")
222
- return None, f"❌ 生成失败:{str(e)}", ""
223
 
224
  # ─────────────────────────────────────────────
225
- # Gradio 界面
226
  # ─────────────────────────────────────────────
227
  def create_interface():
228
- """创建 Gradio 界面"""
229
 
230
- with gr.Blocks(
231
- title="PosterCraft-v1.0",
232
- theme=gr.themes.Soft(),
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="启用提示词增强 (Qwen3-8B)",
269
  value=True,
270
- info="使用 AI 增强和扩展您的提示词"
271
  )
272
 
273
  with gr.Row():
274
- # 默认宽度 832,高度 1216
275
- width_input = gr.Slider(label="宽度", minimum=256, maximum=MAX_IMAGE_SIZE, value=832, step=32)
276
- height_input = gr.Slider(label="高度", minimum=256, maximum=MAX_IMAGE_SIZE, value=1216, step=32)
277
 
278
- # 默认步数 28
279
- num_inference_steps_input = gr.Slider(label="推理步数", minimum=1, maximum=100, value=28, step=1)
280
- # 默认 CFG 28,为了能取到 28,将最大值提高到 40
281
- guidance_scale_input = gr.Slider(label="引导强度", minimum=0.0, maximum=40.0, value=28, step=0.1)
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="生成的海报", type="pil", height=600)
288
- status_output = gr.Textbox(label="生成状态", lines=2, interactive=False)
289
- recapped_prompt_output = gr.Textbox(label="增强后的提示词", lines=5, interactive=False, info="用于生成的最终提示词")
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, status_output, recapped_prompt_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