ruslanmv commited on
Commit
92717ee
·
1 Parent(s): d2a27fd
Files changed (1) hide show
  1. app.py +216 -73
app.py CHANGED
@@ -1,6 +1,44 @@
 
 
1
 
2
- #!/usr/bin/env python
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
 
 
 
 
 
4
  import os
5
  import gradio as gr
6
  import numpy as np
@@ -8,122 +46,151 @@ import PIL
8
  import base64
9
  import io
10
  import torch
11
-
12
- # SSD-1B
13
- #from diffusers import LCMScheduler, AutoPipelineForText2Image
14
 
15
  # SDXL
16
  from diffusers import UNet2DConditionModel, DiffusionPipeline, LCMScheduler
17
 
 
 
 
 
 
18
  MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = int(os.getenv('MAX_IMAGE_SIZE', '1024'))
20
- SECRET_TOKEN = os.getenv('SECRET_TOKEN', 'default_secret')
 
 
 
 
 
 
 
 
 
 
21
 
22
- #device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
23
  if torch.cuda.is_available():
 
 
 
24
 
25
- #pipe = AutoPipelineForText2Image.from_pretrained("segmind/SSD-1B", torch_dtype=torch.float16, variant="fp16")
26
- #pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
27
- #pipe.to("cuda")
28
 
29
- # load and fuse
30
- #pipe.load_lora_weights("latent-consistency/lcm-lora-ssd-1b")
31
- #pipe.fuse_lora()
32
 
33
- unet = UNet2DConditionModel.from_pretrained("latent-consistency/lcm-sdxl", torch_dtype=torch.float16, variant="fp16")
34
- pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", unet=unet, torch_dtype=torch.float16, variant="fp16")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
37
- pipe.to('cuda')
38
-
39
  else:
40
  pipe = None
41
 
42
- def generate(prompt: str,
43
- negative_prompt: str = '',
44
- seed: int = 0,
45
- width: int = 1024,
46
- height: int = 1024,
47
- guidance_scale: float = 0.0,
48
- num_inference_steps: int = 4,
49
- secret_token: str = '') -> PIL.Image.Image:
 
 
 
50
  if secret_token != SECRET_TOKEN:
51
  raise gr.Error(
52
- f'Invalid secret token. Please fork the original space if you want to use it for yourself.')
53
-
 
54
  generator = torch.Generator().manual_seed(seed)
55
 
56
- image = pipe(prompt=prompt,
57
- negative_prompt=negative_prompt,
58
- width=width,
59
- height=height,
60
- guidance_scale=guidance_scale,
61
- num_inference_steps=num_inference_steps,
62
- generator=generator,
63
- output_type='pil').images[0]
64
-
 
65
  return image
66
 
67
- with gr.Blocks() as demo:
68
- gr.HTML("""
69
- <div style="z-index: 100; position: fixed; top: 0px; right: 0px; left: 0px; bottom: 0px; width: 100%; height: 100%; background: white; display: flex; align-items: center; justify-content: center; color: black;">
70
- <div style="text-align: center; color: black;">
71
- <p style="color: black;">This space is a REST API to programmatically generate images using LCM LoRA SSD-1B.</p>
72
- <p style="color: black;">It is not meant to be directly used through a user interface, but using code and an access key.</p>
73
- </div>
74
- </div>""")
75
  secret_token = gr.Text(
76
- label='Secret Token',
77
  max_lines=1,
78
- placeholder='Enter your secret token',
79
  )
80
  prompt = gr.Text(
81
- label='Prompt',
82
  show_label=False,
83
  max_lines=1,
84
- placeholder='Enter your prompt',
85
  container=False,
86
  )
87
- result = gr.Image(label='Result', show_label=False)
88
  negative_prompt = gr.Text(
89
- label='Negative prompt',
90
  max_lines=1,
91
- placeholder='Enter a negative prompt',
92
  visible=True,
93
  )
94
- seed = gr.Slider(label='Seed',
95
- minimum=0,
96
- maximum=MAX_SEED,
97
- step=1,
98
- value=0)
99
 
100
  width = gr.Slider(
101
- label='Width',
102
  minimum=256,
103
  maximum=MAX_IMAGE_SIZE,
104
  step=32,
105
  value=1024,
106
  )
107
  height = gr.Slider(
108
- label='Height',
109
  minimum=256,
110
  maximum=MAX_IMAGE_SIZE,
111
  step=32,
112
  value=1024,
113
  )
114
  guidance_scale = gr.Slider(
115
- label='Guidance scale',
116
- minimum=0,
117
- maximum=2,
118
- step=0.1,
119
- value=0.0)
120
  num_inference_steps = gr.Slider(
121
- label='Number of inference steps',
122
- minimum=1,
123
- maximum=8,
124
- step=1,
125
- value=4)
126
-
127
  inputs = [
128
  prompt,
129
  negative_prompt,
@@ -134,11 +201,87 @@ with gr.Blocks() as demo:
134
  num_inference_steps,
135
  secret_token,
136
  ]
137
- prompt.submit(
138
  fn=generate,
139
  inputs=inputs,
140
  outputs=result,
141
- api_name='run',
 
142
  )
143
 
144
- demo.queue(max_size=32).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ run_api = False
2
+ SSD_1B = False
3
 
4
+ import os
5
+
6
+ # Use GPU
7
+ gpu_info = os.popen("nvidia-smi").read()
8
+ if "failed" in gpu_info:
9
+ print("Not connected to a GPU")
10
+ is_gpu = False
11
+ else:
12
+ print(gpu_info)
13
+ is_gpu = True
14
+ print(is_gpu)
15
+
16
+ from IPython.display import clear_output
17
+
18
+
19
+ def check_enviroment():
20
+ try:
21
+ import torch
22
+
23
+ print("Enviroment is already installed.")
24
+ except ImportError:
25
+ print("Enviroment not found. Installing...")
26
+ # Install requirements from requirements.txt
27
+ os.system("pip install -r requirements.txt")
28
+ # Install gradio version 3.48.0
29
+ os.system("pip install gradio==3.39.0")
30
+ # Install python-dotenv
31
+ os.system("pip install python-dotenv")
32
+ # Clear the output
33
+ clear_output()
34
+
35
+ print("Enviroment installed successfully.")
36
 
37
+
38
+ # Call the function to check and install Packages if necessary
39
+ check_enviroment()
40
+
41
+ from IPython.display import clear_output
42
  import os
43
  import gradio as gr
44
  import numpy as np
 
46
  import base64
47
  import io
48
  import torch
49
+ from diffusers import UNet2DConditionModel, DiffusionPipeline, LCMScheduler
 
 
50
 
51
  # SDXL
52
  from diffusers import UNet2DConditionModel, DiffusionPipeline, LCMScheduler
53
 
54
+ # Get the current directory
55
+ current_dir = os.getcwd()
56
+ model_path = os.path.join(current_dir)
57
+ # Set the cache path
58
+ cache_path = os.path.join(current_dir, "cache")
59
  MAX_SEED = np.iinfo(np.int32).max
60
+ MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
61
+ SECRET_TOKEN = os.getenv("SECRET_TOKEN", "default_secret")
62
+
63
+ # Uncomment the following line if you are using PyTorch 1.10 or later
64
+ # os.environ["TORCH_USE_CUDA_DSA"] = "1"
65
+
66
+ if is_gpu:
67
+ # Uncomment the following line if you want to enable CUDA launch blocking
68
+ os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
69
+ else:
70
+ # Uncomment the following line if you want to use CPU instead of GPU
71
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
72
 
 
73
  if torch.cuda.is_available():
74
+ # Get the current directory
75
+ current_dir = os.getcwd()
76
+ model_path = os.path.join(current_dir)
77
 
78
+ # Set the cache path
79
+ cache_path = os.path.join(current_dir, "cache")
 
80
 
81
+ if not SSD_1B:
 
 
82
 
83
+ unet = UNet2DConditionModel.from_pretrained(
84
+ "latent-consistency/lcm-sdxl",
85
+ torch_dtype=torch.float16,
86
+ variant="fp16",
87
+ cache_dir=cache_path,
88
+ )
89
+ pipe = DiffusionPipeline.from_pretrained(
90
+ "stabilityai/stable-diffusion-xl-base-1.0",
91
+ unet=unet,
92
+ torch_dtype=torch.float16,
93
+ variant="fp16",
94
+ cache_dir=cache_path,
95
+ )
96
+
97
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
98
+ pipe.to("cuda")
99
+ else:
100
+ # SSD-1B
101
+ from diffusers import LCMScheduler, AutoPipelineForText2Image
102
+
103
+ pipe = AutoPipelineForText2Image.from_pretrained(
104
+ "segmind/SSD-1B",
105
+ torch_dtype=torch.float16,
106
+ variant="fp16",
107
+ cache_dir=cache_path,
108
+ )
109
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
110
+ pipe.to("cuda")
111
+
112
+ # load and fuse
113
+ pipe.load_lora_weights("latent-consistency/lcm-lora-ssd-1b")
114
+ pipe.fuse_lora()
115
 
 
 
 
116
  else:
117
  pipe = None
118
 
119
+
120
+ def generate(
121
+ prompt: str,
122
+ negative_prompt: str = "",
123
+ seed: int = 0,
124
+ width: int = 1024,
125
+ height: int = 1024,
126
+ guidance_scale: float = 0.0,
127
+ num_inference_steps: int = 4,
128
+ secret_token: str = "",
129
+ ) -> PIL.Image.Image:
130
  if secret_token != SECRET_TOKEN:
131
  raise gr.Error(
132
+ f"Invalid secret token. Please fork the original space if you want to use it for yourself."
133
+ )
134
+
135
  generator = torch.Generator().manual_seed(seed)
136
 
137
+ image = pipe(
138
+ prompt=prompt,
139
+ negative_prompt=negative_prompt,
140
+ width=width,
141
+ height=height,
142
+ guidance_scale=guidance_scale,
143
+ num_inference_steps=num_inference_steps,
144
+ generator=generator,
145
+ output_type="pil",
146
+ ).images[0]
147
  return image
148
 
149
+
150
+ clear_output()
151
+
152
+ if not run_api:
 
 
 
 
153
  secret_token = gr.Text(
154
+ label="Secret Token",
155
  max_lines=1,
156
+ placeholder="Enter your secret token",
157
  )
158
  prompt = gr.Text(
159
+ label="Prompt",
160
  show_label=False,
161
  max_lines=1,
162
+ placeholder="Enter your prompt",
163
  container=False,
164
  )
165
+ result = gr.Image(label="Result", show_label=False)
166
  negative_prompt = gr.Text(
167
+ label="Negative prompt",
168
  max_lines=1,
169
+ placeholder="Enter a negative prompt",
170
  visible=True,
171
  )
172
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
 
 
 
 
173
 
174
  width = gr.Slider(
175
+ label="Width",
176
  minimum=256,
177
  maximum=MAX_IMAGE_SIZE,
178
  step=32,
179
  value=1024,
180
  )
181
  height = gr.Slider(
182
+ label="Height",
183
  minimum=256,
184
  maximum=MAX_IMAGE_SIZE,
185
  step=32,
186
  value=1024,
187
  )
188
  guidance_scale = gr.Slider(
189
+ label="Guidance scale", minimum=0, maximum=2, step=0.1, value=0.0
190
+ )
 
 
 
191
  num_inference_steps = gr.Slider(
192
+ label="Number of inference steps", minimum=1, maximum=8, step=1, value=4
193
+ )
 
 
 
 
194
  inputs = [
195
  prompt,
196
  negative_prompt,
 
201
  num_inference_steps,
202
  secret_token,
203
  ]
204
+ iface = gr.Interface(
205
  fn=generate,
206
  inputs=inputs,
207
  outputs=result,
208
+ title="Image Generator",
209
+ description="Generate images based on prompts.",
210
  )
211
 
212
+ iface.launch()
213
+
214
+
215
+ if run_api:
216
+ with gr.Blocks() as demo:
217
+ gr.HTML(
218
+ """
219
+ <div style="z-index: 100; position: fixed; top: 0px; right: 0px; left: 0px; bottom: 0px; width: 100%; height: 100%; background: white; display: flex; align-items: center; justify-content: center; color: black;">
220
+ <div style="text-align: center; color: black;">
221
+ <p style="color: black;">This space is a REST API to programmatically generate images using LCM LoRA SSD-1B.</p>
222
+ <p style="color: black;">It is not meant to be directly used through a user interface, but using code and an access key.</p>
223
+ </div>
224
+ </div>"""
225
+ )
226
+ secret_token = gr.Text(
227
+ label="Secret Token",
228
+ max_lines=1,
229
+ placeholder="Enter your secret token",
230
+ )
231
+ prompt = gr.Text(
232
+ label="Prompt",
233
+ show_label=False,
234
+ max_lines=1,
235
+ placeholder="Enter your prompt",
236
+ container=False,
237
+ )
238
+ result = gr.Image(label="Result", show_label=False)
239
+ negative_prompt = gr.Text(
240
+ label="Negative prompt",
241
+ max_lines=1,
242
+ placeholder="Enter a negative prompt",
243
+ visible=True,
244
+ )
245
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
246
+
247
+ width = gr.Slider(
248
+ label="Width",
249
+ minimum=256,
250
+ maximum=MAX_IMAGE_SIZE,
251
+ step=32,
252
+ value=1024,
253
+ )
254
+ height = gr.Slider(
255
+ label="Height",
256
+ minimum=256,
257
+ maximum=MAX_IMAGE_SIZE,
258
+ step=32,
259
+ value=1024,
260
+ )
261
+ guidance_scale = gr.Slider(
262
+ label="Guidance scale", minimum=0, maximum=2, step=0.1, value=0.0
263
+ )
264
+ num_inference_steps = gr.Slider(
265
+ label="Number of inference steps", minimum=1, maximum=8, step=1, value=4
266
+ )
267
+
268
+ inputs = [
269
+ prompt,
270
+ negative_prompt,
271
+ seed,
272
+ width,
273
+ height,
274
+ guidance_scale,
275
+ num_inference_steps,
276
+ secret_token,
277
+ ]
278
+ prompt.submit(
279
+ fn=generate,
280
+ inputs=inputs,
281
+ outputs=result,
282
+ api_name="run",
283
+ )
284
+
285
+ # demo.queue(max_size=32).launch()
286
+ # Launch the Gradio app with multiple workers and debug mode enabled
287
+ demo.queue(max_size=32).launch(debug=True)