Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,4 @@
|
|
| 1 |
from pathlib import Path
|
| 2 |
-
import gradio as gr
|
| 3 |
-
|
| 4 |
-
ROOT_DIR = Path(__file__).resolve().parent
|
| 5 |
-
DATA_DIR = ROOT_DIR / "data"
|
| 6 |
-
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
|
|
@@ -26,6 +21,11 @@ from shape_e_service import (
|
|
| 26 |
selected_view_path,
|
| 27 |
set_prompt,
|
| 28 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
CSS = """
|
| 31 |
#title-wrap {
|
|
@@ -45,6 +45,9 @@ CSS = """
|
|
| 45 |
border: 1px solid #374151;
|
| 46 |
font-size: 13px;
|
| 47 |
}
|
|
|
|
|
|
|
|
|
|
| 48 |
"""
|
| 49 |
|
| 50 |
|
|
@@ -66,15 +69,18 @@ def clear_canvas():
|
|
| 66 |
return empty_canvas_payload()
|
| 67 |
|
| 68 |
|
| 69 |
-
with gr.Blocks(
|
| 70 |
saved_assets_state = gr.State([])
|
| 71 |
selected_asset_index_state = gr.State(None)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
| 73 |
gr.HTML(
|
| 74 |
f"""
|
| 75 |
<div id="title-wrap">
|
| 76 |
<h1>Depthpaint Studio</h1>
|
| 77 |
-
<div id="subtitle">Generate Shap-E assets, browse their views,
|
| 78 |
<div id="device-badge">Device: {DEVICE}</div>
|
| 79 |
</div>
|
| 80 |
"""
|
|
@@ -168,6 +174,40 @@ with gr.Blocks(css=CSS, title="Depthpaint Studio") as demo:
|
|
| 168 |
|
| 169 |
clear_canvas_btn = gr.Button("Clear canvas")
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
ex1.click(fn=lambda: set_prompt(EXAMPLES[0]), outputs=prompt)
|
| 172 |
ex2.click(fn=lambda: set_prompt(EXAMPLES[1]), outputs=prompt)
|
| 173 |
ex3.click(fn=lambda: set_prompt(EXAMPLES[2]), outputs=prompt)
|
|
@@ -175,6 +215,7 @@ with gr.Blocks(css=CSS, title="Depthpaint Studio") as demo:
|
|
| 175 |
ex5.click(fn=lambda: set_prompt(EXAMPLES[4]), outputs=prompt)
|
| 176 |
ex6.click(fn=lambda: set_prompt(EXAMPLES[5]), outputs=prompt)
|
| 177 |
|
|
|
|
| 178 |
generate_btn.click(
|
| 179 |
fn=generate_and_add_asset,
|
| 180 |
inputs=[prompt, steps, guidance_scale, frame_size, seed, saved_assets_state],
|
|
@@ -189,6 +230,7 @@ with gr.Blocks(css=CSS, title="Depthpaint Studio") as demo:
|
|
| 189 |
],
|
| 190 |
)
|
| 191 |
|
|
|
|
| 192 |
saved_gallery.select(
|
| 193 |
fn=select_asset,
|
| 194 |
inputs=[saved_assets_state],
|
|
@@ -202,6 +244,7 @@ with gr.Blocks(css=CSS, title="Depthpaint Studio") as demo:
|
|
| 202 |
],
|
| 203 |
)
|
| 204 |
|
|
|
|
| 205 |
prev_btn.click(
|
| 206 |
fn=prev_view,
|
| 207 |
inputs=[saved_assets_state, selected_asset_index_state],
|
|
@@ -228,6 +271,7 @@ with gr.Blocks(css=CSS, title="Depthpaint Studio") as demo:
|
|
| 228 |
],
|
| 229 |
)
|
| 230 |
|
|
|
|
| 231 |
clear_saved_btn.click(
|
| 232 |
fn=clear_saved_assets,
|
| 233 |
outputs=[
|
|
@@ -241,6 +285,7 @@ with gr.Blocks(css=CSS, title="Depthpaint Studio") as demo:
|
|
| 241 |
],
|
| 242 |
)
|
| 243 |
|
|
|
|
| 244 |
add_to_canvas_btn.click(
|
| 245 |
fn=add_selected_view_to_canvas,
|
| 246 |
inputs=[saved_assets_state, selected_asset_index_state],
|
|
@@ -258,5 +303,24 @@ with gr.Blocks(css=CSS, title="Depthpaint Studio") as demo:
|
|
| 258 |
outputs=editor,
|
| 259 |
)
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
if __name__ == "__main__":
|
| 262 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
|
|
|
|
| 21 |
selected_view_path,
|
| 22 |
set_prompt,
|
| 23 |
)
|
| 24 |
+
from sd_service import generate_with_depth_from_scene
|
| 25 |
+
|
| 26 |
+
ROOT_DIR = Path(__file__).resolve().parent
|
| 27 |
+
DATA_DIR = ROOT_DIR / "data"
|
| 28 |
+
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 29 |
|
| 30 |
CSS = """
|
| 31 |
#title-wrap {
|
|
|
|
| 45 |
border: 1px solid #374151;
|
| 46 |
font-size: 13px;
|
| 47 |
}
|
| 48 |
+
#scene_png_data {
|
| 49 |
+
display: none;
|
| 50 |
+
}
|
| 51 |
"""
|
| 52 |
|
| 53 |
|
|
|
|
| 69 |
return empty_canvas_payload()
|
| 70 |
|
| 71 |
|
| 72 |
+
with gr.Blocks(title="Depthpaint Studio") as demo:
|
| 73 |
saved_assets_state = gr.State([])
|
| 74 |
selected_asset_index_state = gr.State(None)
|
| 75 |
|
| 76 |
+
# Скрытое поле, в которое canvas JS складывает PNG композиции в виде data URL
|
| 77 |
+
scene_png_data = gr.Textbox(elem_id="scene_png_data", value="", lines=1)
|
| 78 |
+
|
| 79 |
gr.HTML(
|
| 80 |
f"""
|
| 81 |
<div id="title-wrap">
|
| 82 |
<h1>Depthpaint Studio</h1>
|
| 83 |
+
<div id="subtitle">Generate Shap-E assets, browse their views, place them on the canvas, then render with SD + ControlNet depth</div>
|
| 84 |
<div id="device-badge">Device: {DEVICE}</div>
|
| 85 |
</div>
|
| 86 |
"""
|
|
|
|
| 174 |
|
| 175 |
clear_canvas_btn = gr.Button("Clear canvas")
|
| 176 |
|
| 177 |
+
gr.Markdown("## SD + ControlNet depth")
|
| 178 |
+
gr.Markdown("Current canvas composition will be sent into SD as init image, and its depth will be used as ControlNet guidance.")
|
| 179 |
+
|
| 180 |
+
sd_prompt = gr.Textbox(
|
| 181 |
+
label="SD prompt",
|
| 182 |
+
lines=3,
|
| 183 |
+
value="A cozy fantasy tabletop scene, detailed, cinematic lighting, highly detailed",
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
sd_negative_prompt = gr.Textbox(
|
| 187 |
+
label="Negative prompt",
|
| 188 |
+
lines=2,
|
| 189 |
+
value="low quality, blurry, cropped, deformed, duplicate objects, floating objects, white background",
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
with gr.Row():
|
| 193 |
+
sd_steps = gr.Slider(label="SD steps", minimum=10, maximum=50, step=1, value=30)
|
| 194 |
+
sd_guidance = gr.Slider(label="Guidance scale", minimum=1.0, maximum=12.0, step=0.5, value=7.5)
|
| 195 |
+
|
| 196 |
+
with gr.Row():
|
| 197 |
+
sd_strength = gr.Slider(label="Img2Img strength", minimum=0.1, maximum=1.0, step=0.05, value=0.65)
|
| 198 |
+
controlnet_scale = gr.Slider(label="Depth control scale", minimum=0.1, maximum=2.0, step=0.1, value=0.9)
|
| 199 |
+
|
| 200 |
+
sd_seed = gr.Number(label="SD seed", value=1234, precision=0)
|
| 201 |
+
|
| 202 |
+
render_sd_btn = gr.Button("Render canvas with SD + depth", variant="primary")
|
| 203 |
+
|
| 204 |
+
with gr.Row():
|
| 205 |
+
scene_preview = gr.Image(label="Canvas composition sent to SD", type="filepath")
|
| 206 |
+
depth_preview = gr.Image(label="Depth used by ControlNet", type="filepath")
|
| 207 |
+
|
| 208 |
+
sd_result = gr.Image(label="SD result", type="filepath")
|
| 209 |
+
|
| 210 |
+
# Prompt example buttons
|
| 211 |
ex1.click(fn=lambda: set_prompt(EXAMPLES[0]), outputs=prompt)
|
| 212 |
ex2.click(fn=lambda: set_prompt(EXAMPLES[1]), outputs=prompt)
|
| 213 |
ex3.click(fn=lambda: set_prompt(EXAMPLES[2]), outputs=prompt)
|
|
|
|
| 215 |
ex5.click(fn=lambda: set_prompt(EXAMPLES[4]), outputs=prompt)
|
| 216 |
ex6.click(fn=lambda: set_prompt(EXAMPLES[5]), outputs=prompt)
|
| 217 |
|
| 218 |
+
# Shape-E generation
|
| 219 |
generate_btn.click(
|
| 220 |
fn=generate_and_add_asset,
|
| 221 |
inputs=[prompt, steps, guidance_scale, frame_size, seed, saved_assets_state],
|
|
|
|
| 230 |
],
|
| 231 |
)
|
| 232 |
|
| 233 |
+
# Gallery selection
|
| 234 |
saved_gallery.select(
|
| 235 |
fn=select_asset,
|
| 236 |
inputs=[saved_assets_state],
|
|
|
|
| 244 |
],
|
| 245 |
)
|
| 246 |
|
| 247 |
+
# View switching
|
| 248 |
prev_btn.click(
|
| 249 |
fn=prev_view,
|
| 250 |
inputs=[saved_assets_state, selected_asset_index_state],
|
|
|
|
| 271 |
],
|
| 272 |
)
|
| 273 |
|
| 274 |
+
# Clear gallery
|
| 275 |
clear_saved_btn.click(
|
| 276 |
fn=clear_saved_assets,
|
| 277 |
outputs=[
|
|
|
|
| 285 |
],
|
| 286 |
)
|
| 287 |
|
| 288 |
+
# Canvas actions
|
| 289 |
add_to_canvas_btn.click(
|
| 290 |
fn=add_selected_view_to_canvas,
|
| 291 |
inputs=[saved_assets_state, selected_asset_index_state],
|
|
|
|
| 303 |
outputs=editor,
|
| 304 |
)
|
| 305 |
|
| 306 |
+
# SD render
|
| 307 |
+
render_sd_btn.click(
|
| 308 |
+
fn=generate_with_depth_from_scene,
|
| 309 |
+
inputs=[
|
| 310 |
+
scene_png_data,
|
| 311 |
+
sd_prompt,
|
| 312 |
+
sd_negative_prompt,
|
| 313 |
+
sd_steps,
|
| 314 |
+
sd_guidance,
|
| 315 |
+
sd_strength,
|
| 316 |
+
controlnet_scale,
|
| 317 |
+
sd_seed,
|
| 318 |
+
],
|
| 319 |
+
outputs=[scene_preview, depth_preview, sd_result],
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
if __name__ == "__main__":
|
| 323 |
+
demo.launch(
|
| 324 |
+
css=CSS,
|
| 325 |
+
allowed_paths=[str(DATA_DIR)],
|
| 326 |
+
)
|