Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import torch | |
| import numpy as np | |
| import random | |
| from diffusers import DiffusionPipeline | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| if torch.cuda.is_available(): | |
| pipe = DiffusionPipeline.from_pretrained("civit-ai/wanostyle_2_offset", use_safetensors=True) | |
| pipe = pipe.to(device) | |
| else: | |
| pipe = DiffusionPipeline.from_pretrained("civit-ai/wanostyle_2_offset", use_safetensors=True) | |
| pipe = pipe.to(device) | |
| MAX_SEED = np.iinfo(np.int32).max | |
| MAX_IMAGE_SIZE = 1024 | |
| def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps): | |
| if randomize_seed: | |
| seed = random.randint(0, MAX_SEED) | |
| generator = torch.Generator().manual_seed(seed) | |
| image = pipe( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| guidance_scale=guidance_scale, | |
| num_inference_steps=num_inference_steps, | |
| width=width, | |
| height=height, | |
| generator=generator | |
| ).images[0] | |
| return image | |
| 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: 520px; | |
| } | |
| """ | |
| if torch.cuda.is_available(): | |
| power_device = "GPU" | |
| else: | |
| power_device = "CPU" | |
| gr.Interface( | |
| fn=infer, | |
| inputs=[ | |
| gr.inputs.Text(label="Prompt", placeholder="Enter your prompt"), | |
| gr.inputs.Text(label="Negative Prompt", visible=False), | |
| gr.inputs.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, default=0), | |
| gr.inputs.Checkbox(label="Randomize Seed", default=True), | |
| gr.inputs.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, default=512), | |
| gr.inputs.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, default=512), | |
| gr.inputs.Slider(label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, default=0.0), | |
| gr.inputs.Slider(label="Number of Inference Steps", minimum=1, maximum=12, step=1, default=2) | |
| ], | |
| outputs=gr.outputs.Image(label="Result"), | |
| title="Text-to-Image Gradio Template", | |
| css=css, | |
| examples=examples | |
| ).launch() | |