UrangDiffusion / app.py
kayfahaarukku's picture
Upload 2 files
465cab6 verified
raw
history blame
4.61 kB
import torch
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
import gradio as gr
import random
import tqdm
import spaces
# Enable TQDM progress tracking
tqdm.monitor_interval = 0
# Load the diffusion pipeline
pipe = StableDiffusionXLPipeline.from_pretrained(
"kayfahaarukku/UrangDiffusion-1.0",
torch_dtype=torch.float16,
custom_pipeline="lpw_stable_diffusion_xl",
use_safetensors=True,
)
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
# Function to generate an image
@spaces.GPU(duration=120) # Adjust the duration as needed
def generate_image(prompt, negative_prompt, use_defaults, width, height, guidance_scale, num_inference_steps, seed, randomize_seed, progress=gr.Progress()):
pipe.to('cuda') # Move the model to GPU when the function is called
if randomize_seed:
seed = random.randint(0, 99999999)
if use_defaults:
prompt = f"{prompt}, masterpiece, best quality"
negative_prompt = f"lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, artist name, {negative_prompt}"
generator = torch.manual_seed(seed)
def callback(step, timestep, latents):
progress(step / num_inference_steps)
return
image = pipe(
prompt,
negative_prompt=negative_prompt,
width=width,
height=height,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
generator=generator,
callback=callback,
callback_steps=1
).images[0]
torch.cuda.empty_cache()
pipe.to('cpu') # Move the model back to CPU after generation
return image, seed
# Define Gradio interface
def interface_fn(prompt, negative_prompt, use_defaults, width, height, guidance_scale, num_inference_steps, seed, randomize_seed, progress=gr.Progress()):
image, seed = generate_image(prompt, negative_prompt, use_defaults, width, height, guidance_scale, num_inference_steps, seed, randomize_seed, progress)
return image, seed, gr.update(value=seed)
def reset_inputs():
return gr.update(value=''), gr.update(value=''), gr.update(value=True), gr.update(value=832), gr.update(value=1216), gr.update(value=7), gr.update(value=28), gr.update(value=0), gr.update(value=False)
with gr.Blocks(title="UrangDiffusion 1.0 Demo", theme="NoCrypt/[email protected]") as demo:
gr.HTML(
"<h1>UrangDiffusion 1.0 Demo</h1>"
)
with gr.Row():
with gr.Column():
prompt_input = gr.Textbox(lines=2, placeholder="Enter prompt here", label="Prompt")
negative_prompt_input = gr.Textbox(lines=2, placeholder="Enter negative prompt here", label="Negative Prompt")
use_defaults_input = gr.Checkbox(label="Use Default Quality Tags and Negative Prompt", value=True)
width_input = gr.Slider(minimum=256, maximum=2048, step=32, label="Width", value=832)
height_input = gr.Slider(minimum=256, maximum=2048, step=32, label="Height", value=1216)
guidance_scale_input = gr.Slider(minimum=1, maximum=20, step=0.5, label="Guidance Scale", value=7)
num_inference_steps_input = gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=28)
seed_input = gr.Slider(minimum=0, maximum=99999999, step=1, label="Seed", value=0, interactive=True)
randomize_seed_input = gr.Checkbox(label="Randomize Seed", value=False)
generate_button = gr.Button("Generate")
reset_button = gr.Button("Reset")
with gr.Column():
output_image = gr.Image(type="pil", label="Generated Image")
output_seed = gr.Number(label="Seed", interactive=False)
generate_button.click(
interface_fn,
inputs=[
prompt_input, negative_prompt_input, use_defaults_input, width_input, height_input, guidance_scale_input, num_inference_steps_input, seed_input, randomize_seed_input
],
outputs=[output_image, output_seed, seed_input]
)
reset_button.click(
reset_inputs,
inputs=[],
outputs=[
prompt_input, negative_prompt_input, use_defaults_input, width_input, height_input, guidance_scale_input, num_inference_steps_input, seed_input, randomize_seed_input
]
)
demo.queue(max_size=20).launch(share=True)