HBDing commited on
Commit
007662f
·
1 Parent(s): 4d1c37d

优化app.py文件,移除演示模式相关代码,简化图像生成逻辑,确保在GPU模式下正常初始化DreamRenderer管道。同时更新README.md中的sdk版本至5.31.0,并添加硬件配置说明。

Browse files
Files changed (2) hide show
  1. README.md +2 -1
  2. app.py +56 -92
README.md CHANGED
@@ -4,10 +4,11 @@ emoji: 🎨
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
11
  ---
12
 
13
  # DreamRenderer: Multi-Instance Attribute Control 🎨
 
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 5.31.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
+ hardware: zero-gpu-medium
12
  ---
13
 
14
  # DreamRenderer: Multi-Instance Attribute Control 🎨
app.py CHANGED
@@ -6,13 +6,8 @@ from PIL import Image, ImageDraw
6
  import json
7
  import warnings
8
  from typing import Optional
9
-
10
- try:
11
- from dream_renderer import DreamRendererPipeline
12
- DEMO_MODE = False
13
- except ImportError:
14
- print("Warning: dream_renderer 模块未找到。将使用演示模式。")
15
- DEMO_MODE = True
16
 
17
  warnings.filterwarnings("ignore")
18
 
@@ -53,96 +48,65 @@ def create_demo_image(prompt: str, bbox_data: list, width: int = 512, height: in
53
 
54
  return image
55
 
56
- # 根据是否是演示模式决定是否使用GPU装饰器
57
- if DEMO_MODE:
58
- def initialize_pipeline():
59
- """初始化DreamRenderer管道(演示模式)"""
60
- return "✅ 演示模式已启动!(未加载实际模型)"
61
-
62
- def generate_image_with_bbox(prompt: str, negative_prompt: str,
63
- num_inference_steps: int, guidance_scale: float,
64
- width: int, height: int, seed: int, use_seed: bool):
65
- """使用边界框生成图像(演示模式)"""
66
- global current_bbox_data
67
-
68
- if not prompt.strip():
69
- return None, "❌ 请输入提示词!"
70
-
71
- try:
72
- # 创建演示图像
73
- image = create_demo_image(prompt, current_bbox_data, width, height)
74
-
75
- info = f"✅ 演示图像生成成功!\n"
76
- info += f"🔸 使用边界框: {len(current_bbox_data)}个\n"
77
- info += f"🔸 推理步数: {num_inference_steps}\n"
78
- info += f"🔸 引导强度: {guidance_scale}\n"
79
- info += f"🔸 图像尺寸: {width}×{height}\n"
80
- info += f"🔸 模式: 演示模式(非实际AI生成)\n"
81
- if use_seed:
82
- info += f"🔸 随机种子: {seed}"
83
-
84
- return image, info
85
- except Exception as e:
86
- return None, f"❌ 生成演示图像时出错: {str(e)}"
87
- else:
88
- @spaces.GPU
89
- def initialize_pipeline():
90
- """初始化DreamRenderer管道"""
91
- global pipeline
92
- try:
93
- if pipeline is None:
94
- pipeline = DreamRendererPipeline()
95
- # 预加载模型以节省时间
96
- success = pipeline.load_model()
97
- if success:
98
- return "✅ DreamRenderer管道已成功初始化并加载模型!"
99
- else:
100
- return "⚠️ DreamRenderer管道已初始化,但模型加载失败。将使用演示模式。"
101
  else:
102
- return " DreamRenderer管道已经初始化完成!"
103
- except Exception as e:
104
- return f" 初始化失败: {str(e)}"
 
 
105
 
106
- @spaces.GPU
107
- def generate_image_with_bbox(prompt: str, negative_prompt: str,
108
- num_inference_steps: int, guidance_scale: float,
109
- width: int, height: int, seed: int, use_seed: bool):
110
- """使用边界框生成图像"""
111
- global pipeline, current_bbox_data
 
 
 
 
 
 
 
 
 
 
112
 
113
- if pipeline is None:
114
- return None, "❌ 请先初始化DreamRenderer管道!"
 
 
 
 
 
 
 
 
 
115
 
116
- if not prompt.strip():
117
- return None, " 请输入提示词!"
 
 
 
 
 
118
 
119
- try:
120
- # 设置种子
121
- actual_seed = seed if use_seed else None
122
-
123
- # 生成图像
124
- image = pipeline.generate_image(
125
- prompt=prompt,
126
- bbox_data=current_bbox_data,
127
- negative_prompt=negative_prompt,
128
- num_inference_steps=num_inference_steps,
129
- guidance_scale=guidance_scale,
130
- width=width,
131
- height=height,
132
- seed=actual_seed
133
- )
134
-
135
- info = f"✅ 图像生成成功!\n"
136
- info += f"🔸 使用边界框: {len(current_bbox_data)}个\n"
137
- info += f"🔸 推理步数: {num_inference_steps}\n"
138
- info += f"🔸 引导强度: {guidance_scale}\n"
139
- info += f"🔸 图像尺寸: {width}×{height}\n"
140
- if actual_seed is not None:
141
- info += f"🔸 随机种子: {actual_seed}"
142
-
143
- return image, info
144
- except Exception as e:
145
- return None, f"❌ 生成图像时出错: {str(e)}"
146
 
147
  def load_bbox_component():
148
  """加载边界框绘制组件"""
 
6
  import json
7
  import warnings
8
  from typing import Optional
9
+ from dream_renderer import DreamRendererPipeline
10
+ DEMO_MODE = False
 
 
 
 
 
11
 
12
  warnings.filterwarnings("ignore")
13
 
 
48
 
49
  return image
50
 
51
+
52
+ @spaces.GPU
53
+ def initialize_pipeline():
54
+ """初始化DreamRenderer管道"""
55
+ global pipeline
56
+ try:
57
+ if pipeline is None:
58
+ pipeline = DreamRendererPipeline()
59
+ # 预加载模型以节省时间
60
+ success = pipeline.load_model()
61
+ if success:
62
+ return "✅ DreamRenderer管道已成功初始化并加载模型!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  else:
64
+ return "⚠️ DreamRenderer管道已初始化,但模型加载失败。将使用演示模式。"
65
+ else:
66
+ return " DreamRenderer管道已经初始化完成!"
67
+ except Exception as e:
68
+ return f"❌ 初始化失败: {str(e)}"
69
 
70
+ @spaces.GPU
71
+ def generate_image_with_bbox(prompt: str, negative_prompt: str,
72
+ num_inference_steps: int, guidance_scale: float,
73
+ width: int, height: int, seed: int, use_seed: bool):
74
+ """使用边界框生成图像"""
75
+ global pipeline, current_bbox_data
76
+
77
+ if pipeline is None:
78
+ return None, "❌ 请先初始化DreamRenderer管道!"
79
+
80
+ if not prompt.strip():
81
+ return None, "❌ 请输入提示词!"
82
+
83
+ try:
84
+ # 设置种子
85
+ actual_seed = seed if use_seed else None
86
 
87
+ # 生成图像
88
+ image = pipeline.generate_image(
89
+ prompt=prompt,
90
+ bbox_data=current_bbox_data,
91
+ negative_prompt=negative_prompt,
92
+ num_inference_steps=num_inference_steps,
93
+ guidance_scale=guidance_scale,
94
+ width=width,
95
+ height=height,
96
+ seed=actual_seed
97
+ )
98
 
99
+ info = f"✅ 图像生成成功!\n"
100
+ info += f"🔸 使用边界框: {len(current_bbox_data)}个\n"
101
+ info += f"🔸 推理步数: {num_inference_steps}\n"
102
+ info += f"🔸 引导强度: {guidance_scale}\n"
103
+ info += f"🔸 图像尺寸: {width}×{height}\n"
104
+ if actual_seed is not None:
105
+ info += f"🔸 随机种子: {actual_seed}"
106
 
107
+ return image, info
108
+ except Exception as e:
109
+ return None, f"❌ 生成图像时出错: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  def load_bbox_component():
112
  """加载边界框绘制组件"""