Spaces:
Running
on
Zero
Running
on
Zero
| #!/usr/bin/env python | |
| import pathlib | |
| import shlex | |
| import subprocess | |
| import gradio as gr | |
| import PIL.Image | |
| import spaces | |
| from model import Model | |
| from settings import MAX_SEED | |
| from utils import randomize_seed_fn | |
| def create_demo(model: Model) -> gr.Blocks: | |
| if not pathlib.Path("corgi.png").exists(): | |
| subprocess.run( # noqa: S603 | |
| shlex.split( | |
| "wget https://raw.githubusercontent.com/openai/shap-e/d99cedaea18e0989e340163dbaeb4b109fa9e8ec/shap_e/examples/example_data/corgi.png -O corgi.png" | |
| ), | |
| check=True, | |
| ) | |
| examples = ["corgi.png"] | |
| def process_example_fn(image_path: str) -> str: | |
| return model.run_image(image_path) | |
| def run(image: PIL.Image.Image, seed: int, guidance_scale: float = 3.0, num_inference_steps: int = 64) -> str: | |
| """Generate a 3D model from an image. | |
| Args: | |
| image (PIL.Image.Image): The input image. | |
| seed (int): The seed for the random number generator. | |
| guidance_scale (float): The guidance scale for the model. Defaults to 3.0. | |
| num_inference_steps (int): The number of inference steps for the model. Defaults to 64. | |
| Returns: | |
| str: The path to the 3D model. | |
| """ | |
| return model.run_image(image, seed, guidance_scale, num_inference_steps) | |
| with gr.Blocks() as demo: | |
| with gr.Group(): | |
| image = gr.Image(label="Input image", show_label=False, type="pil") | |
| run_button = gr.Button("Run") | |
| result = gr.Model3D(label="Result", show_label=False) | |
| with gr.Accordion("Advanced options", open=False): | |
| seed = gr.Slider( | |
| label="Seed", | |
| minimum=0, | |
| maximum=MAX_SEED, | |
| step=1, | |
| value=0, | |
| ) | |
| randomize_seed = gr.Checkbox(label="Randomize seed", value=True) | |
| guidance_scale = gr.Slider( | |
| label="Guidance scale", | |
| minimum=1, | |
| maximum=20, | |
| step=0.1, | |
| value=3.0, | |
| ) | |
| num_inference_steps = gr.Slider( | |
| label="Number of inference steps", | |
| minimum=2, | |
| maximum=100, | |
| step=1, | |
| value=64, | |
| ) | |
| gr.Examples( | |
| examples=examples, | |
| inputs=image, | |
| outputs=result, | |
| fn=process_example_fn, | |
| ) | |
| run_button.click( | |
| fn=randomize_seed_fn, | |
| inputs=[seed, randomize_seed], | |
| outputs=seed, | |
| api_name=False, | |
| ).then( | |
| fn=run, | |
| inputs=[ | |
| image, | |
| seed, | |
| guidance_scale, | |
| num_inference_steps, | |
| ], | |
| outputs=result, | |
| api_name="image-to-3d", | |
| ) | |
| return demo | |