Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py - 完整连接版本
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import sys
|
|
@@ -316,6 +315,21 @@ def generate_designs(selected_suggestion, progress=gr.Progress()):
|
|
| 316 |
design_images = []
|
| 317 |
design_choices = []
|
| 318 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
for i in range(3): # 生成3个设计方案
|
| 320 |
try:
|
| 321 |
progress(0.2 + i*0.25, desc=f"生成设计方案 {i+1}/3...")
|
|
@@ -323,15 +337,22 @@ def generate_designs(selected_suggestion, progress=gr.Progress()):
|
|
| 323 |
# 为每个设计添加变化
|
| 324 |
variant_prompt = f"{base_prompt}, design variation {i+1}, unique style, high quality fashion design"
|
| 325 |
|
| 326 |
-
# 使用模型生成图像
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
if image:
|
| 337 |
design_images.append(image)
|
|
@@ -442,14 +463,19 @@ def generate_3d_fitting(selected_design, progress=gr.Progress()):
|
|
| 442 |
|
| 443 |
# 回退到普通SD模型
|
| 444 |
progress(0.5, desc="使用标准模型生成...")
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
|
| 454 |
progress(0.9, desc="完成3D渲染")
|
| 455 |
return image
|
|
@@ -561,7 +587,9 @@ def create_gradio_interface():
|
|
| 561 |
3. **生成设计** → 点击"生成设计方案"查看AI设计的服装
|
| 562 |
4. **3D试穿** → 选择喜欢的设计,点击"生成3D试穿效果"
|
| 563 |
|
| 564 |
-
💡 **提示**:
|
|
|
|
|
|
|
| 565 |
""")
|
| 566 |
|
| 567 |
return demo
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import sys
|
|
|
|
| 315 |
design_images = []
|
| 316 |
design_choices = []
|
| 317 |
|
| 318 |
+
# 检查模型是否可用
|
| 319 |
+
if not hasattr(model_manager, 'sd_pipeline') or model_manager.sd_pipeline is None:
|
| 320 |
+
logger.error("无法生成设计:Stable Diffusion 模型未加载")
|
| 321 |
+
# 创建占位图像
|
| 322 |
+
for i in range(3):
|
| 323 |
+
color = (random.randint(120, 200), random.randint(120, 200), random.randint(120, 200)
|
| 324 |
+
img = Image.new('RGB', (512, 512), color=color)
|
| 325 |
+
design_images.append(img)
|
| 326 |
+
design_choices.append(f"{selected_suggestion} - 方案 {i+1} (模型未加载)")
|
| 327 |
+
|
| 328 |
+
return (
|
| 329 |
+
gr.Gallery(value=design_images),
|
| 330 |
+
gr.Radio(choices=design_choices, value=design_choices[0] if design_choices else None)
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
for i in range(3): # 生成3个设计方案
|
| 334 |
try:
|
| 335 |
progress(0.2 + i*0.25, desc=f"生成设计方案 {i+1}/3...")
|
|
|
|
| 337 |
# 为每个设计添加变化
|
| 338 |
variant_prompt = f"{base_prompt}, design variation {i+1}, unique style, high quality fashion design"
|
| 339 |
|
| 340 |
+
# 使用模型生成图像 - 添加错误处理
|
| 341 |
+
try:
|
| 342 |
+
image = model_manager.generate_image(
|
| 343 |
+
prompt=variant_prompt,
|
| 344 |
+
negative_prompt="blurry, low quality, distorted, text, watermark, ugly, deformed",
|
| 345 |
+
num_inference_steps=25,
|
| 346 |
+
width=512,
|
| 347 |
+
height=512,
|
| 348 |
+
guidance_scale=7.5
|
| 349 |
+
)
|
| 350 |
+
except Exception as gen_e:
|
| 351 |
+
logger.error(f"生成设计 {i+1} 失败: {gen_e}")
|
| 352 |
+
# 创建占位图像
|
| 353 |
+
width, height = 512, 512
|
| 354 |
+
color = (random.randint(120, 200), random.randint(120, 200), random.randint(120, 200)
|
| 355 |
+
image = Image.new('RGB', (width, height), color=color)
|
| 356 |
|
| 357 |
if image:
|
| 358 |
design_images.append(image)
|
|
|
|
| 463 |
|
| 464 |
# 回退到普通SD模型
|
| 465 |
progress(0.5, desc="使用标准模型生成...")
|
| 466 |
+
try:
|
| 467 |
+
image = model_manager.generate_image(
|
| 468 |
+
prompt=fitting_prompt,
|
| 469 |
+
negative_prompt="blurry, distorted, low quality, unrealistic, extra limbs, deformed, bad anatomy",
|
| 470 |
+
num_inference_steps=30,
|
| 471 |
+
width=512,
|
| 472 |
+
height=768, # 适合全身图像
|
| 473 |
+
guidance_scale=7.5
|
| 474 |
+
)
|
| 475 |
+
except Exception as e:
|
| 476 |
+
logger.error(f"3D试穿生成失败: {e}")
|
| 477 |
+
# 创建占位图像
|
| 478 |
+
image = Image.new('RGB', (512, 768), color=(180, 180, 180))
|
| 479 |
|
| 480 |
progress(0.9, desc="完成3D渲染")
|
| 481 |
return image
|
|
|
|
| 587 |
3. **生成设计** → 点击"生成设计方案"查看AI设计的服装
|
| 588 |
4. **3D试穿** → 选择喜欢的设计,点击"生成3D试穿效果"
|
| 589 |
|
| 590 |
+
💡 **提示**:
|
| 591 |
+
- 每一步都会调用相应的AI模型,请耐心等待生成完成
|
| 592 |
+
- 如果遇到模型加载问题,请检查网络连接或预下载模型
|
| 593 |
""")
|
| 594 |
|
| 595 |
return demo
|