Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,146 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
from diffusers import DiffusionPipeline
|
5 |
import torch
|
|
|
6 |
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
pipe.enable_xformers_memory_efficient_attention()
|
13 |
-
pipe = pipe.to(device)
|
14 |
-
else:
|
15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
16 |
-
pipe = pipe.to(device)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
20 |
|
21 |
-
def
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
generator = torch.Generator().manual_seed(seed)
|
27 |
-
|
28 |
-
image = pipe(
|
29 |
-
prompt = prompt,
|
30 |
-
negative_prompt = negative_prompt,
|
31 |
-
guidance_scale = guidance_scale,
|
32 |
-
num_inference_steps = num_inference_steps,
|
33 |
-
width = width,
|
34 |
-
height = height,
|
35 |
-
generator = generator
|
36 |
-
).images[0]
|
37 |
-
|
38 |
-
return image
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
]
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
with gr.Column(elem_id="col-container"):
|
61 |
-
gr.Markdown(f"""
|
62 |
-
# Text-to-Image Gradio Template
|
63 |
-
Currently running on {power_device}.
|
64 |
-
""")
|
65 |
-
|
66 |
-
with gr.Row():
|
67 |
-
|
68 |
-
prompt = gr.Text(
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
negative_prompt = gr.Text(
|
83 |
-
label="Negative prompt",
|
84 |
-
max_lines=1,
|
85 |
-
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
87 |
-
)
|
88 |
-
|
89 |
-
seed = gr.Slider(
|
90 |
-
label="Seed",
|
91 |
-
minimum=0,
|
92 |
-
maximum=MAX_SEED,
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
-
)
|
96 |
-
|
97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
|
101 |
-
width = gr.Slider(
|
102 |
-
label="Width",
|
103 |
-
minimum=256,
|
104 |
-
maximum=MAX_IMAGE_SIZE,
|
105 |
-
step=32,
|
106 |
-
value=512,
|
107 |
-
)
|
108 |
-
|
109 |
-
height = gr.Slider(
|
110 |
-
label="Height",
|
111 |
-
minimum=256,
|
112 |
-
maximum=MAX_IMAGE_SIZE,
|
113 |
-
step=32,
|
114 |
-
value=512,
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
|
119 |
-
guidance_scale = gr.Slider(
|
120 |
-
label="Guidance scale",
|
121 |
-
minimum=0.0,
|
122 |
-
maximum=10.0,
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
-
)
|
126 |
-
|
127 |
-
num_inference_steps = gr.Slider(
|
128 |
-
label="Number of inference steps",
|
129 |
-
minimum=1,
|
130 |
-
maximum=12,
|
131 |
-
step=1,
|
132 |
-
value=2,
|
133 |
-
)
|
134 |
-
|
135 |
-
gr.Examples(
|
136 |
-
examples = examples,
|
137 |
-
inputs = [prompt]
|
138 |
-
)
|
139 |
|
140 |
-
|
141 |
-
fn = infer,
|
142 |
-
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
143 |
-
outputs = [result]
|
144 |
-
)
|
145 |
|
146 |
-
|
|
|
|
1 |
+
import spaces
|
2 |
+
import argparse
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
from os import path
|
6 |
+
from safetensors.torch import load_file
|
7 |
+
from huggingface_hub import hf_hub_download
|
8 |
+
|
9 |
+
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
10 |
+
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
11 |
+
os.environ["HF_HUB_CACHE"] = cache_path
|
12 |
+
os.environ["HF_HOME"] = cache_path
|
13 |
+
|
14 |
import gradio as gr
|
|
|
|
|
|
|
15 |
import torch
|
16 |
+
from diffusers import FluxPipeline
|
17 |
|
18 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
19 |
|
20 |
+
class timer:
|
21 |
+
def __init__(self, method_name="timed process"):
|
22 |
+
self.method = method_name
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
def __enter__(self):
|
25 |
+
self.start = time.time()
|
26 |
+
print(f"{self.method} starts")
|
27 |
|
28 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
29 |
+
end = time.time()
|
30 |
+
print(f"{self.method} took {str(round(end - self.start, 2))}s")
|
31 |
|
32 |
+
if not path.exists(cache_path):
|
33 |
+
os.makedirs(cache_path, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
|
36 |
+
pipe.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"))
|
37 |
+
pipe.fuse_lora(lora_scale=0.125)
|
38 |
+
pipe.to(device="cuda", dtype=torch.bfloat16)
|
|
|
39 |
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
with gr.Column():
|
42 |
+
with gr.Row():
|
43 |
+
with gr.Column():
|
44 |
+
num_images = gr.Slider(label="Number of Images", minimum=1, maximum=8, step=1, value=4, interactive=True)
|
45 |
+
height = gr.Number(label="Image Height", value=1024, interactive=True)
|
46 |
+
width = gr.Number(label="Image Width", value=1024, interactive=True)
|
47 |
+
# steps = gr.Slider(label="Inference Steps", minimum=1, maximum=8, step=1, value=1, interactive=True)
|
48 |
+
# eta = gr.Number(label="Eta (Corresponds to parameter eta (η) in the DDIM paper, i.e. 0.0 eqauls DDIM, 1.0 equals LCM)", value=1., interactive=True)
|
49 |
+
prompt = gr.Text(label="Prompt", value="a photo of a cat", interactive=True)
|
50 |
+
seed = gr.Number(label="Seed", value=3413, interactive=True)
|
51 |
+
btn = gr.Button(value="run")
|
52 |
+
with gr.Column():
|
53 |
+
output = gr.Gallery(height=1024)
|
54 |
|
55 |
+
@spaces.GPU
|
56 |
+
def process_image(num_images, height, width, prompt, seed):
|
57 |
+
global pipe
|
58 |
+
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
59 |
+
return pipe(
|
60 |
+
prompt=[prompt]*num_images,
|
61 |
+
generator=torch.Generator().manual_seed(int(seed)),
|
62 |
+
num_inference_steps=8,
|
63 |
+
guidance_scale=3.5,
|
64 |
+
height=int(height),
|
65 |
+
width=int(width)
|
66 |
+
).images
|
67 |
|
68 |
+
reactive_controls = [num_images, height, width, prompt, seed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
# for control in reactive_controls:
|
71 |
+
# control.change(fn=process_image, inputs=reactive_controls, outputs=[output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
btn.click(process_image, inputs=reactive_controls, outputs=[output])
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
if __name__ == "__main__":
|
76 |
+
demo.launch()
|