XCLiu commited on
Commit
8af8710
1 Parent(s): 79a0f50

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +174 -0
app.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from rf_models import RF_model
4
+ from sd_models import SD_model
5
+
6
+ import torch
7
+ from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize
8
+ import torch.nn.functional as F
9
+
10
+ from diffusers import StableDiffusionXLImg2ImgPipeline
11
+ import time
12
+ import copy
13
+ import numpy as np
14
+
15
+ pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
16
+ "stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
17
+ )
18
+ pipe = pipe.to("cuda")
19
+
20
+ global model
21
+ global base_model
22
+ global img
23
+
24
+ def set_model(model_id):
25
+ global model
26
+ if model_id == "InstaFlow-0.9B":
27
+ model = RF_model("./instaflow_09b.pt")
28
+ elif model_id == "InstaFlow-1.7B":
29
+ model = RF_model("./instaflow_17b.pt")
30
+ else:
31
+ raise NotImplementedError
32
+ print('Finished Loading Model!')
33
+
34
+ def set_base_model(model_id):
35
+ global base_model
36
+ if model_id == "runwayml/stable-diffusion-v1-5":
37
+ base_model = SD_model("runwayml/stable-diffusion-v1-5")
38
+ else:
39
+ raise NotImplementedError
40
+ print('Finished Loading Base Model!')
41
+
42
+ def set_new_latent_and_generate_new_image(seed, prompt, num_inference_steps=1, guidance_scale=0.0):
43
+ print('Generate with input seed')
44
+ global model
45
+ global img
46
+ negative_prompt=""
47
+ seed = int(seed)
48
+ num_inference_steps = int(num_inference_steps)
49
+ guidance_scale = float(guidance_scale)
50
+ print(seed, num_inference_steps, guidance_scale)
51
+
52
+ t_s = time.time()
53
+ new_image = model.set_new_latent_and_generate_new_image(int(seed), prompt, negative_prompt, int(num_inference_steps), guidance_scale)
54
+ inf_time = time.time() - t_s
55
+
56
+ img = copy.copy(new_image[0])
57
+
58
+ return new_image[0], inf_time
59
+
60
+ def set_new_latent_and_generate_new_image_with_base_model(seed, prompt, num_inference_steps=1, guidance_scale=0.0):
61
+ print('Generate with input seed')
62
+ global base_model
63
+ negative_prompt=""
64
+ seed = int(seed)
65
+ num_inference_steps = int(num_inference_steps)
66
+ guidance_scale = float(guidance_scale)
67
+ print(seed, num_inference_steps, guidance_scale)
68
+
69
+ t_s = time.time()
70
+ new_image = base_model.set_new_latent_and_generate_new_image(int(seed), prompt, negative_prompt, int(num_inference_steps), guidance_scale)
71
+ inf_time = time.time() - t_s
72
+
73
+ return new_image[0], inf_time
74
+
75
+
76
+ def set_new_latent_and_generate_new_image_and_random_seed(seed, prompt, negative_prompt="", num_inference_steps=1, guidance_scale=0.0):
77
+ print('Generate with a random seed')
78
+ global model
79
+ global img
80
+ seed = np.random.randint(0, 2**32)
81
+ num_inference_steps = int(num_inference_steps)
82
+ guidance_scale = float(guidance_scale)
83
+ print(seed, num_inference_steps, guidance_scale)
84
+
85
+ t_s = time.time()
86
+ new_image = model.set_new_latent_and_generate_new_image(int(seed), prompt, negative_prompt, int(num_inference_steps), guidance_scale)
87
+ inf_time = time.time() - t_s
88
+
89
+ img = copy.copy(new_image[0])
90
+
91
+ return new_image[0], seed, inf_time
92
+
93
+
94
+ def refine_image_512(prompt):
95
+ print('Refine with SDXL-Refiner (512)')
96
+ global img
97
+
98
+ t_s = time.time()
99
+ img = torch.tensor(img).unsqueeze(0).permute(0, 3, 1, 2)
100
+ img = img.permute(0, 2, 3, 1).squeeze(0).cpu().numpy()
101
+ new_image = pipe(prompt, image=img).images[0]
102
+ print('time consumption:', time.time() - t_s)
103
+ new_image = np.array(new_image) * 1.0 / 255.
104
+
105
+ img = new_image
106
+
107
+ return new_image
108
+
109
+ def refine_image_1024(prompt):
110
+ print('Refine with SDXL-Refiner (1024)')
111
+ global img
112
+
113
+ t_s = time.time()
114
+ img = torch.tensor(img).unsqueeze(0).permute(0, 3, 1, 2)
115
+ img = torch.nn.functional.interpolate(img, size=1024, mode='bilinear')
116
+ img = img.permute(0, 2, 3, 1).squeeze(0).cpu().numpy()
117
+ new_image = pipe(prompt, image=img).images[0]
118
+ print('time consumption:', time.time() - t_s)
119
+ new_image = np.array(new_image) * 1.0 / 255.
120
+
121
+ img = new_image
122
+
123
+ return new_image
124
+
125
+ set_model('InstaFlow-0.9B')
126
+ set_base_model("runwayml/stable-diffusion-v1-5")
127
+
128
+ with gr.Blocks() as gradio_gui:
129
+ gr.Markdown(
130
+ """
131
+ # InstaFlow! One-Step Stable Diffusion with Rectified Flow
132
+ ## This Huggingface Space provides a demo of one-step InstaFlow-0.9B and measures the inference time.
133
+ ## For fair comparison, Stable Difusion 1.5 is shown in parallel.
134
+ ##
135
+ """)
136
+ gr.Markdown("Set Input Seed and Text Prompts Here")
137
+ with gr.Row():
138
+ with gr.Column(scale=0.4):
139
+ seed_input = gr.Textbox(value='101098274', label="Random Seed")
140
+ with gr.Column(scale=0.4):
141
+ prompt_input = gr.Textbox(value='A high-resolution photograph of a waterfall in autumn; muted tone', label="Prompt")
142
+
143
+ with gr.Row():
144
+ with gr.Column(scale=0.4):
145
+ with gr.Group():
146
+ gr.Markdown("Generation from InstaFlow-0.9B")
147
+ im = gr.Image()
148
+
149
+ gr.Markdown("Model ID: One-Step InstaFlow-0.9B")
150
+ inference_time_output = gr.Textbox(value='0.0', label='Inference Time with One-Step Model (Second)')
151
+ num_inference_steps = gr.Textbox(value='1', label="Number of Inference Steps (can only be 1)")
152
+ guidance_scale = gr.Textbox(value='0.0', label="Guidance Scale for InstaFlow (can only be 0.0)")
153
+
154
+ new_image_button = gr.Button(value="One-Step Generation with InstaFlow and the Input Seed")
155
+ new_image_button.click(set_new_latent_and_generate_new_image, inputs=[seed_input, prompt_input, num_inference_steps, guidance_scale], outputs=[im, inference_time_output])
156
+
157
+ refine_button_512 = gr.Button(value="Refine One-Step Generation with SDXL Refiner (Resolution: 512)")
158
+ refine_button_512.click(refine_image_512, inputs=[prompt_input], outputs=[im])
159
+
160
+ with gr.Column(scale=0.4):
161
+ with gr.Group():
162
+ gr.Markdown("Generation from Stable Diffusion 1.5")
163
+ im_base = gr.Image()
164
+
165
+ gr.Markdown("Model ID: Multi-Step Stable Diffusion 1.5")
166
+ base_model_inference_time_output = gr.Textbox(value='0.0', label='Inference Time with Multi-Step Stable Diffusion (Second)')
167
+
168
+ base_num_inference_steps = gr.Textbox(value='25', label="Number of Inference Steps for Stable Diffusion")
169
+ base_guidance_scale = gr.Textbox(value='5.0', label="Guidance Scale for Stable Diffusion")
170
+
171
+ base_new_image_button = gr.Button(value="Multi-Step Generation with Stable Diffusion and the Input Seed")
172
+ base_new_image_button.click(set_new_latent_and_generate_new_image_with_base_model, inputs=[seed_input, prompt_input, base_num_inference_steps, base_guidance_scale], outputs=[im_base, base_model_inference_time_output])
173
+
174
+ gradio_gui.launch()