Spaces:
Sleeping
Sleeping
File size: 11,542 Bytes
091b91e aee5e75 c8bdb0b aee5e75 2b391bb aee5e75 2b391bb aee5e75 2b391bb aee5e75 c8bdb0b 2b391bb aee5e75 2b391bb aee5e75 58b38c0 aee5e75 2b391bb c8bdb0b aee5e75 2211dd0 2b391bb aee5e75 2b391bb 7a1aa5e 2b391bb 7a1aa5e 2b391bb 7a1aa5e 2b391bb 7a1aa5e 2b391bb 7a1aa5e 2b391bb 7a1aa5e 2b391bb 7a1aa5e 2b391bb aee5e75 7a1aa5e 2b391bb 7a1aa5e aee5e75 7a1aa5e 2b391bb 7a1aa5e 2b391bb 7a1aa5e 2b391bb 7a1aa5e aee5e75 2b391bb c8bdb0b 2b391bb c8bdb0b 2b391bb c8bdb0b 2b391bb c8bdb0b 2b391bb c8bdb0b 2b391bb c8bdb0b 2b391bb c8bdb0b 2b391bb aee5e75 2b391bb aee5e75 c8bdb0b aee5e75 c8bdb0b aee5e75 c8bdb0b aee5e75 c8bdb0b aee5e75 2b391bb c8bdb0b aee5e75 c8bdb0b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | from pathlib import Path
import gradio as gr
from canvas_editor import (
HTML_TEMPLATE,
CSS_TEMPLATE,
JS_ON_LOAD,
payload_append,
payload_reset,
empty_canvas_payload,
)
from shape_e_service import (
DEVICE,
clear_saved_assets,
generate_and_add_asset,
next_view,
prev_view,
select_asset,
selected_view_path,
set_prompt,
)
from sd_service import generate_with_depth_from_scene
ROOT_DIR = Path(__file__).resolve().parent
DATA_DIR = ROOT_DIR / "data"
DATA_DIR.mkdir(parents=True, exist_ok=True)
CSS = """
#title-wrap {
text-align: center;
margin-bottom: 14px;
}
#subtitle {
color: #9ca3af;
font-size: 15px;
max-width: 900px;
margin: 0 auto;
line-height: 1.45;
}
#device-badge {
display: inline-block;
margin-top: 10px;
padding: 6px 10px;
border-radius: 999px;
background: #111827;
border: 1px solid #374151;
font-size: 13px;
}
#scene_png_data {
display: none;
}
.story-box {
border: 1px solid #374151;
border-radius: 12px;
padding: 14px 16px;
background: #0f172a;
margin-bottom: 14px;
}
.story-box h3 {
margin: 0 0 8px 0;
font-size: 16px;
}
.story-box p {
margin: 0;
color: #cbd5e1;
line-height: 1.5;
}
"""
ASSET_PROMPTS = {
"Princess": "A stylized fantasy princess figurine in a long dress",
"Dragon": "A small fantasy dragon figurine with wings",
"Medieval house": "A low-poly medieval house",
"Forest tree": "A stylized fantasy forest tree",
"Treasure chest": "A fantasy treasure chest with gold trim",
"Stone well": "A medieval stone well",
}
DEFAULT_ASSET_PROMPT = ASSET_PROMPTS["Princess"]
DEFAULT_SD_PROMPT = (
"A fantasy storybook scene: a princess in a forest near a medieval house, "
"with a small dragon nearby, cinematic lighting, detailed environment, "
"cozy magical atmosphere, highly detailed"
)
DEFAULT_NEGATIVE_PROMPT = (
"low quality, blurry, cropped, deformed, duplicate objects, floating objects, "
"extra limbs, bad anatomy, text, watermark, white background"
)
def add_selected_view_to_canvas(saved_assets, selected_asset_index):
path = selected_view_path(saved_assets, selected_asset_index)
if not path:
raise gr.Error("Select an asset first.")
return payload_append([path])
def reset_canvas_with_selected_view(saved_assets, selected_asset_index):
path = selected_view_path(saved_assets, selected_asset_index)
if not path:
raise gr.Error("Select an asset first.")
return payload_reset([path])
def clear_canvas():
return empty_canvas_payload()
with gr.Blocks(title="Asset2Scene") as demo:
saved_assets_state = gr.State([])
selected_asset_index_state = gr.State(None)
# Hidden field: canvas JS writes exported PNG composition here as data URL
scene_png_data = gr.Textbox(elem_id="scene_png_data", value="", lines=1)
gr.HTML(
f"""
<div id="title-wrap">
<h1>Asset2Scene</h1>
<div id="subtitle">
Build a scene from separate Shap-E assets, place them on a canvas,
then use that composition only to estimate depth. Stable Diffusion
receives your text prompt plus the depth map as ControlNet guidance,
so it follows the spatial idea without being forced to copy the raw collage literally.
</div>
<div id="device-badge">Device: {DEVICE}</div>
</div>
"""
)
gr.HTML(
"""
<div class="story-box">
<h3>Suggested story</h3>
<p>
Try building a simple fairy-tale setup: a princess in a forest near a medieval house,
with a dragon nearby. Generate the pieces one by one, place them on the canvas,
and then let SD reinterpret the layout into a richer final image.
</p>
</div>
"""
)
with gr.Row():
with gr.Column(scale=1):
prompt = gr.Textbox(
label="Asset prompt",
lines=3,
value=DEFAULT_ASSET_PROMPT,
placeholder="Describe one object or character to generate as a Shap-E asset...",
)
gr.Markdown("**Quick asset ideas**")
with gr.Row():
princess_btn = gr.Button("Princess")
dragon_btn = gr.Button("Dragon")
house_btn = gr.Button("House")
with gr.Row():
tree_btn = gr.Button("Tree")
chest_btn = gr.Button("Chest")
well_btn = gr.Button("Well")
with gr.Row():
steps = gr.Slider(
minimum=8,
maximum=64,
step=1,
value=24 if DEVICE == "cpu" else 32,
label="Shap-E steps",
)
guidance_scale = gr.Slider(
minimum=1.0,
maximum=20.0,
step=0.5,
value=12.0,
label="Shap-E guidance",
)
with gr.Row():
frame_size = gr.Slider(
minimum=64,
maximum=256,
step=32,
value=64 if DEVICE == "cpu" else 256,
label="Frame size",
)
seed = gr.Number(
label="Shap-E seed",
value=42,
precision=0,
)
with gr.Row():
generate_btn = gr.Button("Generate asset and add to gallery", variant="primary")
clear_saved_btn = gr.Button("Clear gallery")
gr.Markdown(
"Generate one object at a time, pick the best view, and send it to the canvas."
)
with gr.Column(scale=1):
current_view = gr.Image(label="Selected asset view", type="filepath")
view_text = gr.Markdown("No asset selected.")
with gr.Row():
prev_btn = gr.Button("← Prev view", interactive=False)
next_btn = gr.Button("Next view →", interactive=False)
with gr.Row():
add_to_canvas_btn = gr.Button("Add selected view to canvas", variant="primary")
reset_canvas_btn = gr.Button("Reset canvas with selected view")
saved_gallery = gr.Gallery(
label="Saved assets",
columns=3,
height="auto",
preview=False,
)
gr.Markdown("## Scene canvas")
gr.Markdown(
"Place the generated assets here. Move them around, resize them, and build the rough composition of the scene."
)
editor = gr.HTML(
value='{"render_id": null, "mode": "append", "items": []}',
html_template=HTML_TEMPLATE,
css_template=CSS_TEMPLATE,
js_on_load=JS_ON_LOAD,
)
clear_canvas_btn = gr.Button("Clear canvas")
gr.Markdown("## Stable Diffusion + ControlNet depth")
gr.Markdown(
"The canvas composition is used only to estimate depth. SD receives your text prompt and the depth map, so it can reinterpret the scene more freely."
)
sd_prompt = gr.Textbox(
label="Scene prompt for SD",
lines=4,
value=DEFAULT_SD_PROMPT,
)
sd_negative_prompt = gr.Textbox(
label="Negative prompt",
lines=2,
value=DEFAULT_NEGATIVE_PROMPT,
)
with gr.Row():
sd_steps = gr.Slider(
label="SD steps",
minimum=10,
maximum=50,
step=1,
value=30,
)
sd_guidance = gr.Slider(
label="Guidance scale",
minimum=1.0,
maximum=12.0,
step=0.5,
value=7.5,
)
controlnet_scale = gr.Slider(
label="Depth control scale",
minimum=0.1,
maximum=2.0,
step=0.1,
value=0.9,
)
sd_seed = gr.Number(label="SD seed", value=1234, precision=0)
render_sd_btn = gr.Button("Render scene with SD + depth", variant="primary")
with gr.Row():
scene_preview = gr.Image(label="Canvas composition used for depth", type="filepath")
depth_preview = gr.Image(label="Depth map used by ControlNet", type="filepath")
sd_result = gr.Image(label="Final SD result", type="filepath")
# Quick asset buttons
princess_btn.click(fn=lambda: set_prompt(ASSET_PROMPTS["Princess"]), outputs=prompt)
dragon_btn.click(fn=lambda: set_prompt(ASSET_PROMPTS["Dragon"]), outputs=prompt)
house_btn.click(fn=lambda: set_prompt(ASSET_PROMPTS["Medieval house"]), outputs=prompt)
tree_btn.click(fn=lambda: set_prompt(ASSET_PROMPTS["Forest tree"]), outputs=prompt)
chest_btn.click(fn=lambda: set_prompt(ASSET_PROMPTS["Treasure chest"]), outputs=prompt)
well_btn.click(fn=lambda: set_prompt(ASSET_PROMPTS["Stone well"]), outputs=prompt)
# Shap-E generation
generate_btn.click(
fn=generate_and_add_asset,
inputs=[prompt, steps, guidance_scale, frame_size, seed, saved_assets_state],
outputs=[
saved_assets_state,
selected_asset_index_state,
saved_gallery,
current_view,
view_text,
prev_btn,
next_btn,
],
)
# Gallery selection
saved_gallery.select(
fn=select_asset,
inputs=[saved_assets_state],
outputs=[
selected_asset_index_state,
saved_gallery,
current_view,
view_text,
prev_btn,
next_btn,
],
)
# View switching
prev_btn.click(
fn=prev_view,
inputs=[saved_assets_state, selected_asset_index_state],
outputs=[
saved_assets_state,
saved_gallery,
current_view,
view_text,
prev_btn,
next_btn,
],
)
next_btn.click(
fn=next_view,
inputs=[saved_assets_state, selected_asset_index_state],
outputs=[
saved_assets_state,
saved_gallery,
current_view,
view_text,
prev_btn,
next_btn,
],
)
# Clear gallery
clear_saved_btn.click(
fn=clear_saved_assets,
outputs=[
saved_assets_state,
selected_asset_index_state,
saved_gallery,
current_view,
view_text,
prev_btn,
next_btn,
],
)
# Canvas actions
add_to_canvas_btn.click(
fn=add_selected_view_to_canvas,
inputs=[saved_assets_state, selected_asset_index_state],
outputs=editor,
)
reset_canvas_btn.click(
fn=reset_canvas_with_selected_view,
inputs=[saved_assets_state, selected_asset_index_state],
outputs=editor,
)
clear_canvas_btn.click(
fn=clear_canvas,
outputs=editor,
)
# SD render: prompt + depth only
render_sd_btn.click(
fn=generate_with_depth_from_scene,
inputs=[
scene_png_data,
sd_prompt,
sd_negative_prompt,
sd_steps,
sd_guidance,
controlnet_scale,
sd_seed,
],
outputs=[scene_preview, depth_preview, sd_result],
)
if __name__ == "__main__":
demo.launch(
css=CSS,
allowed_paths=[str(DATA_DIR)],
) |