Spaces:
Running
on
Zero
Running
on
Zero
kayfahaarukku
commited on
Commit
•
b45db27
1
Parent(s):
2edaaf1
Update app.py
Browse files
app.py
CHANGED
@@ -1,99 +1,99 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import
|
7 |
-
|
8 |
-
# Enable TQDM progress tracking
|
9 |
-
tqdm.monitor_interval = 0
|
10 |
-
|
11 |
-
# Load the diffusion pipeline
|
12 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
13 |
-
"kayfahaarukku/UrangDiffusion-1.0",
|
14 |
-
torch_dtype=torch.float16,
|
15 |
-
custom_pipeline="lpw_stable_diffusion_xl",
|
16 |
-
use_safetensors=True,
|
17 |
-
)
|
18 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
19 |
-
|
20 |
-
# Function to generate an image
|
21 |
-
@spaces.GPU(duration=120) # Adjust the duration as needed
|
22 |
-
def generate_image(prompt, negative_prompt, use_defaults, width, height, guidance_scale, num_inference_steps, seed, randomize_seed, progress=gr.Progress()):
|
23 |
-
pipe.to('cuda') # Move the model to GPU when the function is called
|
24 |
-
|
25 |
-
if randomize_seed:
|
26 |
-
seed = random.randint(0, 99999999)
|
27 |
-
if use_defaults:
|
28 |
-
prompt = f"{prompt}, masterpiece, best quality"
|
29 |
-
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}"
|
30 |
-
generator = torch.manual_seed(seed)
|
31 |
-
|
32 |
-
def callback(step, timestep, latents):
|
33 |
-
progress(step / num_inference_steps)
|
34 |
-
return
|
35 |
-
|
36 |
-
image = pipe(
|
37 |
-
prompt,
|
38 |
-
negative_prompt=negative_prompt,
|
39 |
-
width=width,
|
40 |
-
height=height,
|
41 |
-
guidance_scale=guidance_scale,
|
42 |
-
num_inference_steps=num_inference_steps,
|
43 |
-
generator=generator,
|
44 |
-
callback=callback,
|
45 |
-
callback_steps=1
|
46 |
-
).images[0]
|
47 |
-
|
48 |
-
torch.cuda.empty_cache()
|
49 |
-
pipe.to('cpu') # Move the model back to CPU after generation
|
50 |
-
|
51 |
-
return image, seed
|
52 |
-
|
53 |
-
# Define Gradio interface
|
54 |
-
def interface_fn(prompt, negative_prompt, use_defaults, width, height, guidance_scale, num_inference_steps, seed, randomize_seed, progress=gr.Progress()):
|
55 |
-
image, seed = generate_image(prompt, negative_prompt, use_defaults, width, height, guidance_scale, num_inference_steps, seed, randomize_seed, progress)
|
56 |
-
return image, seed, gr.update(value=seed)
|
57 |
-
|
58 |
-
def reset_inputs():
|
59 |
-
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)
|
60 |
-
|
61 |
-
with gr.Blocks(title="UrangDiffusion 1.0 Demo", theme="NoCrypt/[email protected]") as demo:
|
62 |
-
gr.HTML(
|
63 |
-
"<h1>UrangDiffusion 1.0 Demo</h1>"
|
64 |
-
)
|
65 |
-
with gr.Row():
|
66 |
-
with gr.Column():
|
67 |
-
prompt_input = gr.Textbox(lines=2, placeholder="Enter prompt here", label="Prompt")
|
68 |
-
negative_prompt_input = gr.Textbox(lines=2, placeholder="Enter negative prompt here", label="Negative Prompt")
|
69 |
-
use_defaults_input = gr.Checkbox(label="Use Default Quality Tags and Negative Prompt", value=True)
|
70 |
-
width_input = gr.Slider(minimum=256, maximum=2048, step=32, label="Width", value=832)
|
71 |
-
height_input = gr.Slider(minimum=256, maximum=2048, step=32, label="Height", value=1216)
|
72 |
-
guidance_scale_input = gr.Slider(minimum=1, maximum=20, step=0.5, label="Guidance Scale", value=7)
|
73 |
-
num_inference_steps_input = gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=28)
|
74 |
-
seed_input = gr.Slider(minimum=0, maximum=99999999, step=1, label="Seed", value=0, interactive=True)
|
75 |
-
randomize_seed_input = gr.Checkbox(label="Randomize Seed", value=False)
|
76 |
-
generate_button = gr.Button("Generate")
|
77 |
-
reset_button = gr.Button("Reset")
|
78 |
-
|
79 |
-
with gr.Column():
|
80 |
-
output_image = gr.Image(type="pil", label="Generated Image")
|
81 |
-
output_seed = gr.Number(label="Seed", interactive=False)
|
82 |
-
|
83 |
-
generate_button.click(
|
84 |
-
interface_fn,
|
85 |
-
inputs=[
|
86 |
-
prompt_input, negative_prompt_input, use_defaults_input, width_input, height_input, guidance_scale_input, num_inference_steps_input, seed_input, randomize_seed_input
|
87 |
-
],
|
88 |
-
outputs=[output_image, output_seed, seed_input]
|
89 |
-
)
|
90 |
-
|
91 |
-
reset_button.click(
|
92 |
-
reset_inputs,
|
93 |
-
inputs=[],
|
94 |
-
outputs=[
|
95 |
-
prompt_input, negative_prompt_input, use_defaults_input, width_input, height_input, guidance_scale_input, num_inference_steps_input, seed_input, randomize_seed_input
|
96 |
-
]
|
97 |
-
)
|
98 |
-
|
99 |
demo.queue(max_size=20).launch(share=True)
|
|
|
1 |
+
import spaces
|
2 |
+
import torch
|
3 |
+
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
4 |
+
import gradio as gr
|
5 |
+
import random
|
6 |
+
import tqdm
|
7 |
+
|
8 |
+
# Enable TQDM progress tracking
|
9 |
+
tqdm.monitor_interval = 0
|
10 |
+
|
11 |
+
# Load the diffusion pipeline
|
12 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
13 |
+
"kayfahaarukku/UrangDiffusion-1.0",
|
14 |
+
torch_dtype=torch.float16,
|
15 |
+
custom_pipeline="lpw_stable_diffusion_xl",
|
16 |
+
use_safetensors=True,
|
17 |
+
)
|
18 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
19 |
+
|
20 |
+
# Function to generate an image
|
21 |
+
@spaces.GPU(duration=120) # Adjust the duration as needed
|
22 |
+
def generate_image(prompt, negative_prompt, use_defaults, width, height, guidance_scale, num_inference_steps, seed, randomize_seed, progress=gr.Progress()):
|
23 |
+
pipe.to('cuda') # Move the model to GPU when the function is called
|
24 |
+
|
25 |
+
if randomize_seed:
|
26 |
+
seed = random.randint(0, 99999999)
|
27 |
+
if use_defaults:
|
28 |
+
prompt = f"{prompt}, masterpiece, best quality"
|
29 |
+
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}"
|
30 |
+
generator = torch.manual_seed(seed)
|
31 |
+
|
32 |
+
def callback(step, timestep, latents):
|
33 |
+
progress(step / num_inference_steps)
|
34 |
+
return
|
35 |
+
|
36 |
+
image = pipe(
|
37 |
+
prompt,
|
38 |
+
negative_prompt=negative_prompt,
|
39 |
+
width=width,
|
40 |
+
height=height,
|
41 |
+
guidance_scale=guidance_scale,
|
42 |
+
num_inference_steps=num_inference_steps,
|
43 |
+
generator=generator,
|
44 |
+
callback=callback,
|
45 |
+
callback_steps=1
|
46 |
+
).images[0]
|
47 |
+
|
48 |
+
torch.cuda.empty_cache()
|
49 |
+
pipe.to('cpu') # Move the model back to CPU after generation
|
50 |
+
|
51 |
+
return image, seed
|
52 |
+
|
53 |
+
# Define Gradio interface
|
54 |
+
def interface_fn(prompt, negative_prompt, use_defaults, width, height, guidance_scale, num_inference_steps, seed, randomize_seed, progress=gr.Progress()):
|
55 |
+
image, seed = generate_image(prompt, negative_prompt, use_defaults, width, height, guidance_scale, num_inference_steps, seed, randomize_seed, progress)
|
56 |
+
return image, seed, gr.update(value=seed)
|
57 |
+
|
58 |
+
def reset_inputs():
|
59 |
+
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)
|
60 |
+
|
61 |
+
with gr.Blocks(title="UrangDiffusion 1.0 Demo", theme="NoCrypt/[email protected]") as demo:
|
62 |
+
gr.HTML(
|
63 |
+
"<h1>UrangDiffusion 1.0 Demo</h1>"
|
64 |
+
)
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Column():
|
67 |
+
prompt_input = gr.Textbox(lines=2, placeholder="Enter prompt here", label="Prompt")
|
68 |
+
negative_prompt_input = gr.Textbox(lines=2, placeholder="Enter negative prompt here", label="Negative Prompt")
|
69 |
+
use_defaults_input = gr.Checkbox(label="Use Default Quality Tags and Negative Prompt", value=True)
|
70 |
+
width_input = gr.Slider(minimum=256, maximum=2048, step=32, label="Width", value=832)
|
71 |
+
height_input = gr.Slider(minimum=256, maximum=2048, step=32, label="Height", value=1216)
|
72 |
+
guidance_scale_input = gr.Slider(minimum=1, maximum=20, step=0.5, label="Guidance Scale", value=7)
|
73 |
+
num_inference_steps_input = gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=28)
|
74 |
+
seed_input = gr.Slider(minimum=0, maximum=99999999, step=1, label="Seed", value=0, interactive=True)
|
75 |
+
randomize_seed_input = gr.Checkbox(label="Randomize Seed", value=False)
|
76 |
+
generate_button = gr.Button("Generate")
|
77 |
+
reset_button = gr.Button("Reset")
|
78 |
+
|
79 |
+
with gr.Column():
|
80 |
+
output_image = gr.Image(type="pil", label="Generated Image")
|
81 |
+
output_seed = gr.Number(label="Seed", interactive=False)
|
82 |
+
|
83 |
+
generate_button.click(
|
84 |
+
interface_fn,
|
85 |
+
inputs=[
|
86 |
+
prompt_input, negative_prompt_input, use_defaults_input, width_input, height_input, guidance_scale_input, num_inference_steps_input, seed_input, randomize_seed_input
|
87 |
+
],
|
88 |
+
outputs=[output_image, output_seed, seed_input]
|
89 |
+
)
|
90 |
+
|
91 |
+
reset_button.click(
|
92 |
+
reset_inputs,
|
93 |
+
inputs=[],
|
94 |
+
outputs=[
|
95 |
+
prompt_input, negative_prompt_input, use_defaults_input, width_input, height_input, guidance_scale_input, num_inference_steps_input, seed_input, randomize_seed_input
|
96 |
+
]
|
97 |
+
)
|
98 |
+
|
99 |
demo.queue(max_size=20).launch(share=True)
|