| import gradio as gr |
| import numpy as np |
| import random |
|
|
| import spaces |
| |
| from sid import SiDFluxPipeline, SiDSD3Pipeline, SiDSanaPipeline |
| import torch |
| import os |
| os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1" |
| os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1" |
|
|
| device = "cuda" if torch.cuda.is_available() else "cpu" |
| torch_dtype = torch.float16 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| MODEL_OPTIONS = { |
| |
|
|
| |
| "SiD-DiT-SANA-0.6B-RectifiedFlow": "YGu1998/SiD-DiT-SANA-0.6B-RectifiedFlow", |
| "SiD-adversarial-DiT-SANA-0.6B-RectifiedFlow": "YGu1998/SiD-adversarial-DiT-SANA-0.6B-RectifiedFlow", |
| "YGu1998/SiD-DiT-SANA-1.6B-RectifiedFlow": "YGu1998/SiD-DiT-SANA-1.6B-RectifiedFlow", |
| "YGu1998/SiD-adversarial-DiT-SANA-1.6B-RectifiedFlow": "YGu1998/SiD-adversarial-DiT-SANA-1.6B-RectifiedFlow", |
|
|
| |
| "SiD-DiT-SANA-0.6B-TrigFlow": "YGu1998/SiD-DiT-SANA-0.6B-TrigFlow", |
| "SiD-adversarial-DiT-SANA-0.6B-TrigFlow": "YGu1998/SiD-adversarial-DiT-SANA-0.6B-TrigFlow", |
| "SiD-DiT-SANA-1.6B-TrigFlow": "YGu1998/SiD-DiT-SANA-1.6B-TrigFlow", |
| "SiD-adversarial-DiT-SANA-1.6B-TrigFlow": "YGu1998/SiD-adversarial-DiT-SANA-1.6B-TrigFlow", |
|
|
| } |
|
|
|
|
|
|
| def load_model(model_choice, progress=None): |
| if progress is not None: |
| progress(0.1, desc=f"Loading {model_choice}...") |
|
|
| model_repo_id = MODEL_OPTIONS[model_choice] |
| time_scale = 1000.0 |
|
|
| if "Sana" in model_choice: |
| pipe = SiDSanaPipeline.from_pretrained(model_repo_id, torch_dtype=torch.bfloat16) |
| if "Sprint" in model_choice: |
| time_scale = 1.0 |
| elif "SD3" in model_choice: |
| pipe = SiDSD3Pipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype) |
| elif "Flux" in model_choice: |
| pipe = SiDFluxPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype) |
| else: |
| raise ValueError(f"Unknown model type for: {model_choice}") |
|
|
| if progress is not None: |
| progress(0.5, desc=f"{model_choice} loaded") |
|
|
| pipe = pipe.to(device) |
| return pipe, time_scale |
|
|
|
|
| MAX_SEED = np.iinfo(np.int32).max |
| MAX_IMAGE_SIZE = 1024 |
|
|
|
|
| @spaces.GPU |
| def infer( |
| prompt, |
| seed, |
| randomize_seed, |
| width, |
| height, |
| num_inference_steps, |
| model_choice, |
| progress=gr.Progress(track_tqdm=False), |
| ): |
| if randomize_seed: |
| seed = random.randint(0, MAX_SEED) |
|
|
| generator = torch.Generator().manual_seed(seed) |
| progress(0.0, desc="Preparing model...") |
| pipe, time_scale = load_model(model_choice) |
|
|
|
|
| progress(0.7, desc="Running inference...") |
| image = pipe( |
| prompt=prompt, |
| guidance_scale=1, |
| num_inference_steps=num_inference_steps, |
| width=width, |
| height=height, |
| generator=generator, |
| time_scale=time_scale, |
| ).images[0] |
|
|
|
|
| progress(1.0, desc="Done") |
|
|
| pipe.maybe_free_model_hooks() |
| del pipe |
| torch.cuda.empty_cache() |
|
|
| return image, seed |
|
|
|
|
| examples = [ |
| "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", |
| "An astronaut riding a green horse", |
| "A delicious ceviche cheesecake slice", |
| ] |
|
|
| css = """ |
| #col-container { |
| margin: 0 auto; |
| max-width: 640px; |
| } |
| """ |
|
|
| with gr.Blocks(css=css) as demo: |
| with gr.Column(elem_id="col-container"): |
| gr.Markdown(" # SiD-DiT demo") |
|
|
| with gr.Row(): |
| prompt = gr.Text( |
| label="Prompt", |
| show_label=False, |
| max_lines=1, |
| placeholder="Enter your prompt", |
| container=False, |
| ) |
|
|
| run_button = gr.Button("Run", scale=0, variant="primary") |
|
|
| model_choice = gr.Dropdown( |
| label="Model Choice", |
| choices=list(MODEL_OPTIONS.keys()), |
| value="SiD-Flow-SD3-medium", |
| ) |
|
|
| result = gr.Image(label="Result", show_label=False) |
|
|
| with gr.Accordion("Advanced Settings", 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) |
|
|
| with gr.Row(): |
| width = gr.Slider( |
| label="Width", |
| minimum=256, |
| maximum=MAX_IMAGE_SIZE, |
| step=32, |
| value=1024, |
| ) |
|
|
| height = gr.Slider( |
| label="Height", |
| minimum=256, |
| maximum=MAX_IMAGE_SIZE, |
| step=32, |
| value=1024, |
| ) |
|
|
| with gr.Row(): |
| |
| |
| |
| |
| |
| |
| |
|
|
| num_inference_steps = gr.Slider( |
| label="Number of inference steps", |
| minimum=4, |
| maximum=4, |
| step=1, |
| value=4, |
| ) |
|
|
| gr.Examples(examples=examples, inputs=[prompt]) |
| gr.on( |
| triggers=[run_button.click, prompt.submit], |
| fn=infer, |
| inputs=[ |
| prompt, |
| seed, |
| randomize_seed, |
| width, |
| height, |
| num_inference_steps, |
| model_choice, |
| ], |
| outputs=[result, seed], |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|