Spaces:
Sleeping
Sleeping
添加演示模式支持,优化图像生成逻辑,更新边界框绘制功能,调整requirements.txt以启用必要的依赖项。
Browse files- app.py +195 -83
- requirements.txt +8 -8
app.py
CHANGED
|
@@ -1,12 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
|
|
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
import warnings
|
|
|
|
|
|
|
| 5 |
try:
|
| 6 |
from dream_renderer import DreamRendererPipeline
|
|
|
|
| 7 |
except ImportError:
|
| 8 |
print("Warning: dream_renderer 模块未找到。将使用演示模式。")
|
| 9 |
-
|
| 10 |
|
| 11 |
warnings.filterwarnings("ignore")
|
| 12 |
|
|
@@ -14,23 +20,129 @@ warnings.filterwarnings("ignore")
|
|
| 14 |
pipeline = None
|
| 15 |
current_bbox_data = []
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def load_bbox_component():
|
| 36 |
"""加载边界框绘制组件"""
|
|
@@ -154,47 +266,6 @@ def update_bbox_data(bbox_json: str):
|
|
| 154 |
current_bbox_data = []
|
| 155 |
return f"❌ 处理边界框数据时出错: {str(e)}", ""
|
| 156 |
|
| 157 |
-
@spaces.GPU
|
| 158 |
-
def generate_image_with_bbox(prompt: str, negative_prompt: str,
|
| 159 |
-
num_inference_steps: int, guidance_scale: float,
|
| 160 |
-
width: int, height: int, seed: int, use_seed: bool):
|
| 161 |
-
"""使用边界框生成图像"""
|
| 162 |
-
global pipeline, current_bbox_data
|
| 163 |
-
|
| 164 |
-
if pipeline is None:
|
| 165 |
-
return None, "❌ 请先初始化DreamRenderer管道!"
|
| 166 |
-
|
| 167 |
-
if not prompt.strip():
|
| 168 |
-
return None, "❌ 请输入提示词!"
|
| 169 |
-
|
| 170 |
-
try:
|
| 171 |
-
# 设置种子
|
| 172 |
-
actual_seed = seed if use_seed else None
|
| 173 |
-
|
| 174 |
-
# 生成图像
|
| 175 |
-
image = pipeline.generate_image(
|
| 176 |
-
prompt=prompt,
|
| 177 |
-
bbox_data=current_bbox_data,
|
| 178 |
-
negative_prompt=negative_prompt,
|
| 179 |
-
num_inference_steps=num_inference_steps,
|
| 180 |
-
guidance_scale=guidance_scale,
|
| 181 |
-
width=width,
|
| 182 |
-
height=height,
|
| 183 |
-
seed=actual_seed
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
info = f"✅ 图像生成成功!\n"
|
| 187 |
-
info += f"🔸 使用边界框: {len(current_bbox_data)}个\n"
|
| 188 |
-
info += f"🔸 推理步数: {num_inference_steps}\n"
|
| 189 |
-
info += f"🔸 引导强度: {guidance_scale}\n"
|
| 190 |
-
info += f"🔸 图像尺寸: {width}×{height}\n"
|
| 191 |
-
if actual_seed is not None:
|
| 192 |
-
info += f"🔸 随机种子: {actual_seed}"
|
| 193 |
-
|
| 194 |
-
return image, info
|
| 195 |
-
except Exception as e:
|
| 196 |
-
return None, f"❌ 生成图像时出错: {str(e)}"
|
| 197 |
-
|
| 198 |
def create_interface():
|
| 199 |
"""创建Gradio界面"""
|
| 200 |
|
|
@@ -237,34 +308,72 @@ def create_interface():
|
|
| 237 |
"""
|
| 238 |
|
| 239 |
with gr.Blocks(css=css, title="DreamRenderer - Multi-Instance Control", theme=gr.themes.Soft()) as demo:
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
# 使用说明
|
| 253 |
with gr.Accordion("📖 使用说明", open=False):
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
with gr.Row():
|
| 270 |
# 左侧:边界框绘制和控制
|
|
@@ -272,7 +381,10 @@ def create_interface():
|
|
| 272 |
# 初始化部分
|
| 273 |
with gr.Group():
|
| 274 |
gr.Markdown("### 🚀 模型初始化")
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
| 276 |
init_status = gr.Textbox(label="初始化状态", interactive=False, lines=2)
|
| 277 |
|
| 278 |
# 边界框绘制区域
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
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 |
|
|
|
|
| 20 |
pipeline = None
|
| 21 |
current_bbox_data = []
|
| 22 |
|
| 23 |
+
def create_demo_image(prompt: str, bbox_data: list, width: int = 512, height: int = 512):
|
| 24 |
+
"""创建演示图像"""
|
| 25 |
+
# 创建一个简单的演示图像
|
| 26 |
+
image = Image.new('RGB', (width, height), color='lightblue')
|
| 27 |
+
draw = ImageDraw.Draw(image)
|
| 28 |
+
|
| 29 |
+
# 绘制背景文字
|
| 30 |
try:
|
| 31 |
+
# 尝试绘制提示词
|
| 32 |
+
draw.text((10, 10), f"演示模式: {prompt[:50]}", fill='darkblue')
|
| 33 |
+
draw.text((10, 30), f"边界框数量: {len(bbox_data)}", fill='darkblue')
|
| 34 |
+
|
| 35 |
+
# 绘制边界框
|
| 36 |
+
for i, bbox in enumerate(bbox_data):
|
| 37 |
+
x = int(bbox['x'] * width)
|
| 38 |
+
y = int(bbox['y'] * height)
|
| 39 |
+
w = int(bbox['width'] * width)
|
| 40 |
+
h = int(bbox['height'] * height)
|
| 41 |
+
|
| 42 |
+
# 绘制边界框
|
| 43 |
+
color = f"hsl({i * 60}, 70%, 50%)"
|
| 44 |
+
# 简单的颜色映射
|
| 45 |
+
colors = ['red', 'green', 'blue', 'yellow', 'purple', 'orange']
|
| 46 |
+
bbox_color = colors[i % len(colors)]
|
| 47 |
+
|
| 48 |
+
draw.rectangle([x, y, x+w, y+h], outline=bbox_color, width=2)
|
| 49 |
+
draw.text((x+5, y+5), bbox.get('label', f'区域{i+1}'), fill=bbox_color)
|
| 50 |
+
|
| 51 |
except Exception as e:
|
| 52 |
+
draw.text((10, 50), f"绘制错误: {str(e)}", fill='red')
|
| 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 |
"""加载边界框绘制组件"""
|
|
|
|
| 266 |
current_bbox_data = []
|
| 267 |
return f"❌ 处理边界框数据时出错: {str(e)}", ""
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
def create_interface():
|
| 270 |
"""创建Gradio界面"""
|
| 271 |
|
|
|
|
| 308 |
"""
|
| 309 |
|
| 310 |
with gr.Blocks(css=css, title="DreamRenderer - Multi-Instance Control", theme=gr.themes.Soft()) as demo:
|
| 311 |
+
# 根据模式显示不同的标题
|
| 312 |
+
if DEMO_MODE:
|
| 313 |
+
gr.HTML("""
|
| 314 |
+
<div style="text-align: center; padding: 20px;">
|
| 315 |
+
<h1 style="background: linear-gradient(45deg, #FF6B6B, #4ECDC4); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-size: 3em; margin-bottom: 10px;">
|
| 316 |
+
🎨 DreamRenderer (演示模式)
|
| 317 |
+
</h1>
|
| 318 |
+
<h2 style="color: #666; margin-bottom: 20px;">Multi-Instance Attribute Control</h2>
|
| 319 |
+
<div style="background: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; padding: 15px; margin: 20px auto; max-width: 800px;">
|
| 320 |
+
<p style="margin: 0; color: #856404; font-size: 1.1em;">
|
| 321 |
+
⚠️ <strong>当前运行在演示模式下</strong><br>
|
| 322 |
+
由于缺少实际的AI模型,系统将生成简单的演示图像来展示界面功能。<br>
|
| 323 |
+
您仍然可以测试边界框绘制和参数设置功能。
|
| 324 |
+
</p>
|
| 325 |
+
</div>
|
| 326 |
+
</div>
|
| 327 |
+
""")
|
| 328 |
+
else:
|
| 329 |
+
gr.HTML("""
|
| 330 |
+
<div style="text-align: center; padding: 20px;">
|
| 331 |
+
<h1 style="background: linear-gradient(45deg, #FF6B6B, #4ECDC4); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-size: 3em; margin-bottom: 10px;">
|
| 332 |
+
🎨 DreamRenderer
|
| 333 |
+
</h1>
|
| 334 |
+
<h2 style="color: #666; margin-bottom: 20px;">Multi-Instance Attribute Control</h2>
|
| 335 |
+
<p style="font-size: 1.2em; color: #888; max-width: 800px; margin: 0 auto;">
|
| 336 |
+
基于ZeroGPU的高质量多实例属性控制文本到图像生成工具
|
| 337 |
+
</p>
|
| 338 |
+
</div>
|
| 339 |
+
""")
|
| 340 |
|
| 341 |
# 使用说明
|
| 342 |
with gr.Accordion("📖 使用说明", open=False):
|
| 343 |
+
if DEMO_MODE:
|
| 344 |
+
gr.Markdown("""
|
| 345 |
+
### 🚀 演示模式说明:
|
| 346 |
+
1. **功能测试**: 点击"初始化"按钮启动演示模式
|
| 347 |
+
2. **绘制区域**: 在画布上拖拽鼠标绘制边界框
|
| 348 |
+
3. **添加描述**: 为每个边界框输入描述文本
|
| 349 |
+
4. **设置参数**: 调整生成参数(用于演示)
|
| 350 |
+
5. **生成图像**: 输入主提示词并点击生成演示图像
|
| 351 |
+
|
| 352 |
+
### ⚠️ 演示模式限制:
|
| 353 |
+
- 🎯 **界面功能**: 所有界面功能都可以正常使用
|
| 354 |
+
- 🖼️ **图像生成**: 生成的是简单的演示图像,非AI生成
|
| 355 |
+
- 📦 **边界框**: 边界框绘制和编辑功能完全正常
|
| 356 |
+
- 🔧 **参数调节**: 参数设置功能正常,但不影响实际生成
|
| 357 |
+
|
| 358 |
+
### 📌 完整功能需要:
|
| 359 |
+
- 安装完整的AI模型(dream_renderer模块)
|
| 360 |
+
- 配置ZeroGPU环境
|
| 361 |
+
""")
|
| 362 |
+
else:
|
| 363 |
+
gr.Markdown("""
|
| 364 |
+
### 🚀 快速开始:
|
| 365 |
+
1. **初始化**: 点击"初始化DreamRenderer"按钮加载模型
|
| 366 |
+
2. **绘制区域**: 在画布上拖拽鼠标绘制边界框
|
| 367 |
+
3. **添加描述**: 为每个边界框输入描述文本
|
| 368 |
+
4. **设置参数**: 调整生成参数(可选)
|
| 369 |
+
5. **生成图像**: 输入主提示词并点击生成
|
| 370 |
+
|
| 371 |
+
### ✨ 功能特点:
|
| 372 |
+
- 🎯 **精确控制**: 通过边界框精确控制每个实例的位置和属性
|
| 373 |
+
- 🚀 **ZeroGPU加速**: 利用Hugging Face的ZeroGPU实现快速推理
|
| 374 |
+
- 🎨 **高质量生成**: 基于FLUX模型的高质量图像生成
|
| 375 |
+
- 🔧 **灵活参数**: 丰富的参数调节选项
|
| 376 |
+
""")
|
| 377 |
|
| 378 |
with gr.Row():
|
| 379 |
# 左侧:边界框绘制和控制
|
|
|
|
| 381 |
# 初始化部分
|
| 382 |
with gr.Group():
|
| 383 |
gr.Markdown("### 🚀 模型初始化")
|
| 384 |
+
if DEMO_MODE:
|
| 385 |
+
init_btn = gr.Button("🚀 启动演示模式", variant="primary")
|
| 386 |
+
else:
|
| 387 |
+
init_btn = gr.Button("🚀 初始化DreamRenderer", variant="primary")
|
| 388 |
init_status = gr.Textbox(label="初始化状态", interactive=False, lines=2)
|
| 389 |
|
| 390 |
# 边界框绘制区域
|
requirements.txt
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
gradio==5.31.0
|
| 2 |
spaces>=0.28.0
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 1 |
gradio==5.31.0
|
| 2 |
spaces>=0.28.0
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
torchvision
|
| 5 |
+
diffusers>=0.21.0
|
| 6 |
+
transformers>=4.30.0
|
| 7 |
+
accelerate
|
| 8 |
+
pillow
|
| 9 |
+
numpy
|
| 10 |
+
opencv-python-headless
|