XCLiu commited on
Commit
22f6cf2
1 Parent(s): 7717449

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -137
app.py CHANGED
@@ -1,139 +1,9 @@
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
- else:
29
- raise NotImplementedError
30
- print('Finished Loading Model!')
31
-
32
- def set_base_model(model_id):
33
- global base_model
34
- if model_id == "runwayml/stable-diffusion-v1-5":
35
- base_model = SD_model("runwayml/stable-diffusion-v1-5")
36
- else:
37
- raise NotImplementedError
38
- print('Finished Loading Base Model!')
39
-
40
- def set_new_latent_and_generate_new_image(seed, prompt, num_inference_steps=1, guidance_scale=0.0):
41
- print('Generate with input seed')
42
- global model
43
- global img
44
- negative_prompt=""
45
- seed = int(seed)
46
- num_inference_steps = int(num_inference_steps)
47
- guidance_scale = float(guidance_scale)
48
- print(seed, num_inference_steps, guidance_scale)
49
-
50
- t_s = time.time()
51
- new_image = model.set_new_latent_and_generate_new_image(int(seed), prompt, negative_prompt, int(num_inference_steps), guidance_scale)
52
- inf_time = time.time() - t_s
53
-
54
- img = copy.copy(new_image[0])
55
-
56
- return new_image[0], inf_time
57
-
58
- def set_new_latent_and_generate_new_image_with_base_model(seed, prompt, num_inference_steps=1, guidance_scale=0.0):
59
- print('Generate with input seed')
60
- global base_model
61
- negative_prompt=""
62
- seed = int(seed)
63
- num_inference_steps = int(num_inference_steps)
64
- guidance_scale = float(guidance_scale)
65
- print(seed, num_inference_steps, guidance_scale)
66
-
67
- t_s = time.time()
68
- new_image = base_model.set_new_latent_and_generate_new_image(int(seed), prompt, negative_prompt, int(num_inference_steps), guidance_scale)
69
- inf_time = time.time() - t_s
70
-
71
- return new_image[0], inf_time
72
-
73
-
74
- def refine_image_512(prompt):
75
- print('Refine with SDXL-Refiner (512)')
76
- global img
77
-
78
- t_s = time.time()
79
- img = torch.tensor(img).unsqueeze(0).permute(0, 3, 1, 2)
80
- img = img.permute(0, 2, 3, 1).squeeze(0).cpu().numpy()
81
- new_image = pipe(prompt, image=img).images[0]
82
- print('time consumption:', time.time() - t_s)
83
- new_image = np.array(new_image) * 1.0 / 255.
84
-
85
- img = new_image
86
-
87
- return new_image
88
-
89
-
90
- set_model('InstaFlow-0.9B')
91
- set_base_model("runwayml/stable-diffusion-v1-5")
92
-
93
- with gr.Blocks() as gradio_gui:
94
- gr.Markdown(
95
- """
96
- # InstaFlow! One-Step Stable Diffusion with Rectified Flow
97
- ## This Huggingface Space provides a demo of one-step InstaFlow-0.9B and measures the inference time.
98
- ## For fair comparison, Stable Diffusion 1.5 is shown in parallel, running on the same GPU.
99
- ##
100
- """)
101
- gr.Markdown("Set Input Seed and Text Prompts Here")
102
- with gr.Row():
103
- with gr.Column(scale=0.4):
104
- seed_input = gr.Textbox(value='101098274', label="Random Seed")
105
- with gr.Column(scale=0.4):
106
- prompt_input = gr.Textbox(value='A high-resolution photograph of a waterfall in autumn; muted tone', label="Prompt")
107
-
108
- with gr.Row():
109
- with gr.Column(scale=0.4):
110
- with gr.Group():
111
- gr.Markdown("Generation from InstaFlow-0.9B")
112
- im = gr.Image()
113
-
114
- gr.Markdown("Model ID: One-Step InstaFlow-0.9B")
115
- inference_time_output = gr.Textbox(value='0.0', label='Inference Time with One-Step InstaFlow (Second)')
116
- num_inference_steps = gr.Textbox(value='1', label="Number of Inference Steps for InstaFlow (can only be 1)")
117
- guidance_scale = gr.Textbox(value='0.0', label="Guidance Scale for InstaFlow (can only be 0.0)")
118
-
119
- new_image_button = gr.Button(value="One-Step Generation with InstaFlow and the Input Seed")
120
- 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])
121
-
122
- refine_button_512 = gr.Button(value="Refine One-Step Generation with SDXL Refiner (Resolution: 512)")
123
- refine_button_512.click(refine_image_512, inputs=[prompt_input], outputs=[im])
124
-
125
- with gr.Column(scale=0.4):
126
- with gr.Group():
127
- gr.Markdown("Generation from Stable Diffusion 1.5")
128
- im_base = gr.Image()
129
-
130
- gr.Markdown("Model ID: Multi-Step Stable Diffusion 1.5")
131
- base_model_inference_time_output = gr.Textbox(value='0.0', label='Inference Time with Multi-Step Stable Diffusion (Second)')
132
-
133
- base_num_inference_steps = gr.Textbox(value='25', label="Number of Inference Steps for Stable Diffusion")
134
- base_guidance_scale = gr.Textbox(value='5.0', label="Guidance Scale for Stable Diffusion")
135
-
136
- base_new_image_button = gr.Button(value="Multi-Step Generation with Stable Diffusion and the Input Seed")
137
- 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])
138
-
139
- gradio_gui.launch()
 
1
+ import shlex
2
+ import subprocess
3
 
4
+ from huggingface_hub import HfApi
 
5
 
6
+ api = HfApi()
7
+ api.snapshot_download(repo_id="XCLiu/InstaFlow_hidden", repo_type="space", local_dir=".")
8
+ subprocess.run(shlex.split("pip install -r requirements.txt"))
9
+ subprocess.run(shlex.split("python app.py"))