Update app.py
Browse files
app.py
CHANGED
|
@@ -30,6 +30,8 @@ import trimesh
|
|
| 30 |
import argparse
|
| 31 |
import numpy as np
|
| 32 |
import gradio as gr
|
|
|
|
|
|
|
| 33 |
from step1x3d_geometry.models.pipelines.pipeline import Step1X3DGeometryPipeline
|
| 34 |
from step1x3d_texture.pipelines.step1x_3d_texture_synthesis_pipeline import (
|
| 35 |
Step1X3DTexturePipeline,
|
|
@@ -55,9 +57,46 @@ geometry_model = Step1X3DGeometryPipeline.from_pretrained(
|
|
| 55 |
|
| 56 |
texture_model = Step1X3DTexturePipeline.from_pretrained("stepfun-ai/Step1X-3D", subfolder=args.texture_model)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
@spaces.GPU(duration=240)
|
| 60 |
-
def
|
| 61 |
input_image_path, guidance_scale, inference_steps, max_facenum, symmetry, edge_type
|
| 62 |
):
|
| 63 |
# geometry_model = geometry_model.to("cuda")
|
|
@@ -96,14 +135,46 @@ def generate_func(
|
|
| 96 |
return geometry_save_path, textured_save_path
|
| 97 |
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
with gr.Blocks(title="Step1X-3D demo") as demo:
|
| 100 |
-
gr.Markdown("# Step1X-3D")
|
|
|
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column(scale=2):
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
inference_steps = gr.Slider(
|
| 106 |
-
label="
|
| 107 |
)
|
| 108 |
max_facenum = gr.Number(label="Max Face Num", value="400000")
|
| 109 |
symmetry = gr.Radio(
|
|
@@ -118,10 +189,12 @@ with gr.Blocks(title="Step1X-3D demo") as demo:
|
|
| 118 |
value="sharp",
|
| 119 |
type="value",
|
| 120 |
)
|
| 121 |
-
|
|
|
|
| 122 |
with gr.Column(scale=4):
|
| 123 |
textured_preview = gr.Model3D(label="Textured", height=380)
|
| 124 |
geometry_preview = gr.Model3D(label="Geometry", height=380)
|
|
|
|
| 125 |
with gr.Column(scale=1):
|
| 126 |
gr.Examples(
|
| 127 |
examples=[
|
|
@@ -134,14 +207,58 @@ with gr.Blocks(title="Step1X-3D demo") as demo:
|
|
| 134 |
["examples/images/061.png"],
|
| 135 |
["examples/images/107.png"],
|
| 136 |
],
|
| 137 |
-
inputs=[
|
| 138 |
cache_examples=False,
|
|
|
|
| 139 |
)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
inputs=[
|
| 144 |
-
|
| 145 |
guidance_scale,
|
| 146 |
inference_steps,
|
| 147 |
max_facenum,
|
|
@@ -151,4 +268,4 @@ with gr.Blocks(title="Step1X-3D demo") as demo:
|
|
| 151 |
outputs=[geometry_preview, textured_preview],
|
| 152 |
)
|
| 153 |
|
| 154 |
-
demo.launch(ssr_mode=False)
|
|
|
|
| 30 |
import argparse
|
| 31 |
import numpy as np
|
| 32 |
import gradio as gr
|
| 33 |
+
from gradio_client import Client
|
| 34 |
+
from PIL import Image
|
| 35 |
from step1x3d_geometry.models.pipelines.pipeline import Step1X3DGeometryPipeline
|
| 36 |
from step1x3d_texture.pipelines.step1x_3d_texture_synthesis_pipeline import (
|
| 37 |
Step1X3DTexturePipeline,
|
|
|
|
| 57 |
|
| 58 |
texture_model = Step1X3DTexturePipeline.from_pretrained("stepfun-ai/Step1X-3D", subfolder=args.texture_model)
|
| 59 |
|
| 60 |
+
# Initialize text-to-image client
|
| 61 |
+
t2i_client = Client("http://211.233.58.201:7971/")
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def generate_image_from_text(prompt, height, width, steps, scales, seed):
|
| 65 |
+
"""Generate image from text using the external API"""
|
| 66 |
+
try:
|
| 67 |
+
result = t2i_client.predict(
|
| 68 |
+
height=height,
|
| 69 |
+
width=width,
|
| 70 |
+
steps=steps,
|
| 71 |
+
scales=scales,
|
| 72 |
+
prompt=prompt,
|
| 73 |
+
seed=seed if seed != -1 else None,
|
| 74 |
+
api_name="/process_and_save_image"
|
| 75 |
+
)
|
| 76 |
+
# Result contains a dict with 'path' key pointing to the generated image
|
| 77 |
+
if isinstance(result, dict) and 'path' in result:
|
| 78 |
+
return result['path']
|
| 79 |
+
elif isinstance(result, str):
|
| 80 |
+
return result
|
| 81 |
+
else:
|
| 82 |
+
raise Exception("Unexpected result format from text-to-image API")
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"Error generating image from text: {e}")
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def get_random_seed():
|
| 89 |
+
"""Get a random seed from the external API"""
|
| 90 |
+
try:
|
| 91 |
+
result = t2i_client.predict(api_name="/update_random_seed")
|
| 92 |
+
return result
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(f"Error getting random seed: {e}")
|
| 95 |
+
return -1
|
| 96 |
+
|
| 97 |
|
| 98 |
@spaces.GPU(duration=240)
|
| 99 |
+
def generate_3d_func(
|
| 100 |
input_image_path, guidance_scale, inference_steps, max_facenum, symmetry, edge_type
|
| 101 |
):
|
| 102 |
# geometry_model = geometry_model.to("cuda")
|
|
|
|
| 135 |
return geometry_save_path, textured_save_path
|
| 136 |
|
| 137 |
|
| 138 |
+
def update_image_display(uploaded_image, generated_image):
|
| 139 |
+
"""Update the displayed image based on which source has content"""
|
| 140 |
+
if generated_image is not None:
|
| 141 |
+
return generated_image
|
| 142 |
+
elif uploaded_image is not None:
|
| 143 |
+
return uploaded_image
|
| 144 |
+
else:
|
| 145 |
+
return None
|
| 146 |
+
|
| 147 |
+
|
| 148 |
with gr.Blocks(title="Step1X-3D demo") as demo:
|
| 149 |
+
gr.Markdown("# Step1X-3D with Text-to-Image Generation")
|
| 150 |
+
|
| 151 |
with gr.Row():
|
| 152 |
with gr.Column(scale=2):
|
| 153 |
+
gr.Markdown("## Image Input")
|
| 154 |
+
with gr.Tab("Upload Image"):
|
| 155 |
+
uploaded_image = gr.Image(label="Upload Image", type="filepath")
|
| 156 |
+
|
| 157 |
+
with gr.Tab("Generate from Text"):
|
| 158 |
+
text_prompt = gr.Textbox(label="Image Description", placeholder="Enter your image description here...")
|
| 159 |
+
with gr.Row():
|
| 160 |
+
t2i_height = gr.Slider(label="Height", minimum=512, maximum=2048, value=1024, step=64)
|
| 161 |
+
t2i_width = gr.Slider(label="Width", minimum=512, maximum=2048, value=1024, step=64)
|
| 162 |
+
with gr.Row():
|
| 163 |
+
t2i_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, value=8, step=1)
|
| 164 |
+
t2i_scales = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, value=3.5, step=0.5)
|
| 165 |
+
with gr.Row():
|
| 166 |
+
t2i_seed = gr.Number(label="Seed (optional, -1 for random)", value=-1)
|
| 167 |
+
random_seed_btn = gr.Button("Get Random Seed", scale=0)
|
| 168 |
+
generate_image_btn = gr.Button("Generate Image", variant="primary")
|
| 169 |
+
|
| 170 |
+
# Display the current working image
|
| 171 |
+
current_image = gr.Image(label="Current Image (for 3D generation)", type="filepath", interactive=False)
|
| 172 |
+
generated_image_path = gr.State(value=None)
|
| 173 |
+
|
| 174 |
+
gr.Markdown("## 3D Generation Settings")
|
| 175 |
+
guidance_scale = gr.Number(label="3D Guidance Scale", value="7.5")
|
| 176 |
inference_steps = gr.Slider(
|
| 177 |
+
label="3D Inference Steps", minimum=1, maximum=100, value=50
|
| 178 |
)
|
| 179 |
max_facenum = gr.Number(label="Max Face Num", value="400000")
|
| 180 |
symmetry = gr.Radio(
|
|
|
|
| 189 |
value="sharp",
|
| 190 |
type="value",
|
| 191 |
)
|
| 192 |
+
btn_3d = gr.Button("Generate 3D", variant="primary")
|
| 193 |
+
|
| 194 |
with gr.Column(scale=4):
|
| 195 |
textured_preview = gr.Model3D(label="Textured", height=380)
|
| 196 |
geometry_preview = gr.Model3D(label="Geometry", height=380)
|
| 197 |
+
|
| 198 |
with gr.Column(scale=1):
|
| 199 |
gr.Examples(
|
| 200 |
examples=[
|
|
|
|
| 207 |
["examples/images/061.png"],
|
| 208 |
["examples/images/107.png"],
|
| 209 |
],
|
| 210 |
+
inputs=[uploaded_image],
|
| 211 |
cache_examples=False,
|
| 212 |
+
label="Example Images"
|
| 213 |
)
|
| 214 |
|
| 215 |
+
# Event handlers
|
| 216 |
+
def on_generate_image(prompt, height, width, steps, scales, seed):
|
| 217 |
+
if not prompt:
|
| 218 |
+
gr.Warning("Please enter a text prompt")
|
| 219 |
+
return None, None
|
| 220 |
+
|
| 221 |
+
generated_path = generate_image_from_text(prompt, height, width, steps, scales, seed)
|
| 222 |
+
if generated_path:
|
| 223 |
+
return generated_path, generated_path
|
| 224 |
+
else:
|
| 225 |
+
gr.Warning("Failed to generate image from text")
|
| 226 |
+
return None, None
|
| 227 |
+
|
| 228 |
+
def on_upload_image(image_path):
|
| 229 |
+
return image_path
|
| 230 |
+
|
| 231 |
+
def get_current_image(uploaded, generated):
|
| 232 |
+
if generated is not None:
|
| 233 |
+
return generated
|
| 234 |
+
elif uploaded is not None:
|
| 235 |
+
return uploaded
|
| 236 |
+
else:
|
| 237 |
+
return None
|
| 238 |
+
|
| 239 |
+
# Connect event handlers
|
| 240 |
+
generate_image_btn.click(
|
| 241 |
+
on_generate_image,
|
| 242 |
+
inputs=[text_prompt, t2i_height, t2i_width, t2i_steps, t2i_scales, t2i_seed],
|
| 243 |
+
outputs=[generated_image_path, current_image]
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
random_seed_btn.click(
|
| 247 |
+
get_random_seed,
|
| 248 |
+
inputs=[],
|
| 249 |
+
outputs=[t2i_seed]
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
uploaded_image.change(
|
| 253 |
+
on_upload_image,
|
| 254 |
+
inputs=[uploaded_image],
|
| 255 |
+
outputs=[current_image]
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
btn_3d.click(
|
| 259 |
+
lambda img, gs, is_, mf, sym, et: generate_3d_func(img, gs, is_, mf, sym, et) if img else (None, None),
|
| 260 |
inputs=[
|
| 261 |
+
current_image,
|
| 262 |
guidance_scale,
|
| 263 |
inference_steps,
|
| 264 |
max_facenum,
|
|
|
|
| 268 |
outputs=[geometry_preview, textured_preview],
|
| 269 |
)
|
| 270 |
|
| 271 |
+
demo.launch(ssr_mode=False)
|