XCLiu commited on
Commit
79a0f50
·
1 Parent(s): d1072a9

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -180
app.py DELETED
@@ -1,180 +0,0 @@
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, negative_prompt="", num_inference_steps=1, guidance_scale=0.0):
43
- print('Generate with input seed')
44
- global model
45
- global img
46
- seed = int(seed)
47
- num_inference_steps = int(num_inference_steps)
48
- guidance_scale = float(guidance_scale)
49
- print(seed, num_inference_steps, guidance_scale)
50
-
51
- t_s = time.time()
52
- new_image = model.set_new_latent_and_generate_new_image(int(seed), prompt, negative_prompt, int(num_inference_steps), guidance_scale)
53
- #print('time consumption:', time.time() - t_s)
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
- #print('time consumption:', time.time() - t_s)
72
- inf_time = time.time() - t_s
73
-
74
- return new_image[0], inf_time
75
-
76
-
77
- def set_new_latent_and_generate_new_image_and_random_seed(seed, prompt, negative_prompt="", num_inference_steps=1, guidance_scale=0.0):
78
- print('Generate with a random seed')
79
- global model
80
- global img
81
- seed = np.random.randint(0, 2**32)
82
- num_inference_steps = int(num_inference_steps)
83
- guidance_scale = float(guidance_scale)
84
- print(seed, num_inference_steps, guidance_scale)
85
-
86
- t_s = time.time()
87
- new_image = model.set_new_latent_and_generate_new_image(int(seed), prompt, negative_prompt, int(num_inference_steps), guidance_scale)
88
- #print('time consumption:', time.time() - t_s)
89
- inf_time = time.time() - t_s
90
-
91
- img = copy.copy(new_image[0])
92
-
93
- return new_image[0], seed, inf_time
94
-
95
-
96
- def refine_image_512(prompt):
97
- print('Refine with SDXL-Refiner (512)')
98
- global img
99
-
100
- t_s = time.time()
101
- img = torch.tensor(img).unsqueeze(0).permute(0, 3, 1, 2)
102
- img = img.permute(0, 2, 3, 1).squeeze(0).cpu().numpy()
103
- new_image = pipe(prompt, image=img).images[0]
104
- print('time consumption:', time.time() - t_s)
105
- new_image = np.array(new_image) * 1.0 / 255.
106
-
107
- img = new_image
108
-
109
- return new_image
110
-
111
- def refine_image_1024(prompt):
112
- print('Refine with SDXL-Refiner (1024)')
113
- global img
114
-
115
- t_s = time.time()
116
- img = torch.tensor(img).unsqueeze(0).permute(0, 3, 1, 2)
117
- img = torch.nn.functional.interpolate(img, size=1024, mode='bilinear')
118
- img = img.permute(0, 2, 3, 1).squeeze(0).cpu().numpy()
119
- new_image = pipe(prompt, image=img).images[0]
120
- print('time consumption:', time.time() - t_s)
121
- new_image = np.array(new_image) * 1.0 / 255.
122
-
123
- img = new_image
124
-
125
- return new_image
126
-
127
- set_model('InstaFlow-0.9B')
128
- set_base_model("runwayml/stable-diffusion-v1-5")
129
-
130
- with gr.Blocks() as gradio_gui:
131
- gr.Markdown(
132
- """
133
- # InstaFlow! One-Step Stable Diffusion with Rectified Flow
134
- ## This Huggingface Space provides a demo of one-step InstaFlow-0.9B and measures the inference time.
135
- ## For fair comparison, Stable Difusion 1.5 is shown in parallel.
136
- ##
137
- ##
138
- """)
139
- gr.Markdown("Set Input Seed and Text Prompts Here")
140
- with gr.Row():
141
- with gr.Column(scale=0.4):
142
- seed_input = gr.Textbox(value='101098274', label="Random Seed")
143
- with gr.Column(scale=0.4):
144
- prompt_input = gr.Textbox(value='A high-resolution photograph of a waterfall in autumn; muted tone', label="Prompt")
145
-
146
- with gr.Row():
147
- with gr.Column(scale=0.4):
148
- with gr.Group():
149
- gr.Markdown("Generation from InstaFlow-0.9B")
150
- im = gr.Image()
151
-
152
- gr.Markdown("Model ID: One-Step InstaFlow-0.9B")
153
- inference_time_output = gr.Textbox(value='0.0', label='Inference Time with One-Step Model (Second)')
154
- new_image_button = gr.Button(value="One-Step Generation with the Input Seed")
155
- new_image_button.click(set_new_latent_and_generate_new_image, inputs=[seed_input, prompt_input], outputs=[im, inference_time_output])
156
-
157
- next_image_button = gr.Button(value="One-Step Generation with a New Random Seed")
158
- next_image_button.click(set_new_latent_and_generate_new_image_and_random_seed, inputs=[seed_input, prompt_input], outputs=[im, seed_input, inference_time_output])
159
-
160
- refine_button_512 = gr.Button(value="Refine One-Step Generation with SDXL Refiner (Resolution: 512)")
161
- refine_button_512.click(refine_image_512, inputs=[prompt_input], outputs=[im])
162
-
163
- refine_button_1024 = gr.Button(value="Refine One-Step Generation with SDXL Refiner (Resolution: 1024)")
164
- refine_button_1024.click(refine_image_1024, inputs=[prompt_input], outputs=[im])
165
-
166
- with gr.Column(scale=0.4):
167
- with gr.Group():
168
- gr.Markdown("Generation from Stable Diffusion 1.5")
169
- im_base = gr.Image()
170
-
171
- gr.Markdown("Model ID: Multi-Step Stable Diffusion 1.5")
172
- base_model_inference_time_output = gr.Textbox(value='0.0', label='Inference Time with Multi-Step Stable Diffusion (Second)')
173
-
174
- num_inference_steps = gr.Textbox(value='25', label="Number of Inference Steps for Stable Diffusion")
175
- guidance_scale = gr.Textbox(value='5.0', label="Guidance Scale for Stable Diffusion")
176
-
177
- base_new_image_button = gr.Button(value="Multi-Step Generation with Stable Diffusion and the Input Seed")
178
- base_new_image_button.click(set_new_latent_and_generate_new_image_with_base_model, inputs=[seed_input, prompt_input, num_inference_steps, guidance_scale], outputs=[im_base, base_model_inference_time_output])
179
-
180
- gradio_gui.launch()