ClaireOzzz commited on
Commit
d941650
1 Parent(s): 963316e

Upload 6 files

Browse files
Files changed (6) hide show
  1. README.md +6 -6
  2. app.py +461 -0
  3. requirements.txt +8 -0
  4. shop1.jpg +0 -0
  5. shop2.jpg +0 -0
  6. shop3.jpg +0 -0
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: ShopGenV2
3
- emoji: 🏃
4
- colorFrom: purple
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 4.7.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: SD-XL + Control LoRas
3
+ emoji: 🦀
4
+ colorFrom: green
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 3.44.4
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,461 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ # os.system('pip install pip==23.3.0')
3
+ # os.system('pip uninstall spaces -y')
4
+ # os.system('pip install spaces==0.18.0')
5
+ # os.system('pip install gradio==4.0.2')
6
+
7
+
8
+ import gradio as gr
9
+ from huggingface_hub import login, HfFileSystem, HfApi, ModelCard
10
+ import os
11
+ import spaces
12
+ import random
13
+ import torch
14
+
15
+ from transformers import GLPNFeatureExtractor, GLPNForDepthEstimation
16
+ from transformers import AutoFeatureExtractor, AutoModelForDepthEstimation
17
+ feature_extractor = AutoFeatureExtractor.from_pretrained("Intel/dpt-large")
18
+ modeld = AutoModelForDepthEstimation.from_pretrained("Intel/dpt-large")
19
+
20
+ # from depthGAN.app import create_visual_demo
21
+
22
+ is_shared_ui = False
23
+ hf_token = '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'
24
+ login(token=hf_token)
25
+
26
+ fs = HfFileSystem(token=hf_token)
27
+ api = HfApi()
28
+
29
+ device="cuda" if torch.cuda.is_available() else "cpu"
30
+
31
+ from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
32
+ from diffusers.utils import load_image
33
+ from PIL import Image
34
+ import torch
35
+ import numpy as np
36
+ import cv2
37
+
38
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
39
+
40
+ controlnet = ControlNetModel.from_pretrained(
41
+ "diffusers/controlnet-canny-sdxl-1.0",
42
+ torch_dtype=torch.float16
43
+ )
44
+
45
+ def check_use_custom_or_no(value):
46
+ if value is True:
47
+ return gr.update(visible=True)
48
+ else:
49
+ return gr.update(visible=False)
50
+
51
+ def get_files(file_paths):
52
+ last_files = {} # Dictionary to store the last file for each path
53
+
54
+ for file_path in file_paths:
55
+ # Split the file path into directory and file components
56
+ directory, file_name = file_path.rsplit('/', 1)
57
+
58
+ # Update the last file for the current path
59
+ last_files[directory] = file_name
60
+
61
+ # Extract the last files from the dictionary
62
+ result = list(last_files.values())
63
+
64
+ return result
65
+
66
+ def load_model(model_name):
67
+
68
+ if model_name == "":
69
+ gr.Warning("If you want to use a private model, you need to duplicate this space on your personal account.")
70
+ raise gr.Error("You forgot to define Model ID.")
71
+
72
+ # Get instance_prompt a.k.a trigger word
73
+ card = ModelCard.load(model_name)
74
+ repo_data = card.data.to_dict()
75
+ instance_prompt = repo_data.get("instance_prompt")
76
+
77
+ if instance_prompt is not None:
78
+ print(f"Trigger word: {instance_prompt}")
79
+ else:
80
+ instance_prompt = "no trigger word needed"
81
+ print(f"Trigger word: no trigger word needed")
82
+
83
+ # List all ".safetensors" files in repo
84
+ sfts_available_files = fs.glob(f"{model_name}/*safetensors")
85
+ sfts_available_files = get_files(sfts_available_files)
86
+
87
+ if sfts_available_files == []:
88
+ sfts_available_files = ["NO SAFETENSORS FILE"]
89
+
90
+ print(f"Safetensors available: {sfts_available_files}")
91
+
92
+ return model_name, "Model Ready", gr.update(choices=sfts_available_files, value=sfts_available_files[0], visible=True), gr.update(value=instance_prompt, visible=True)
93
+
94
+ def custom_model_changed(model_name, previous_model):
95
+ if model_name == "" and previous_model == "" :
96
+ status_message = ""
97
+ elif model_name != previous_model:
98
+ status_message = "model changed, please reload before any new run"
99
+ else:
100
+ status_message = "model ready"
101
+ return status_message
102
+
103
+ def resize_image(input_path, output_path, target_height):
104
+ # Open the input image
105
+ img = Image.open(input_path)
106
+
107
+ # Calculate the aspect ratio of the original image
108
+ original_width, original_height = img.size
109
+ original_aspect_ratio = original_width / original_height
110
+
111
+ # Calculate the new width while maintaining the aspect ratio and the target height
112
+ new_width = int(target_height * original_aspect_ratio)
113
+
114
+ # Resize the image while maintaining the aspect ratio and fixing the height
115
+ img = img.resize((new_width, target_height), Image.LANCZOS)
116
+
117
+ # Save the resized image
118
+ img.save(output_path)
119
+
120
+ return output_path
121
+
122
+ def predict(image):
123
+ inputs = feature_extractor(images=image, return_tensors="pt")
124
+ with torch.no_grad():
125
+ outputs = modeld(**inputs)
126
+ predicted_depth = outputs.predicted_depth
127
+ # interpolate to original size
128
+ prediction = torch.nn.functional.interpolate(
129
+ predicted_depth.unsqueeze(1),
130
+ size=image.size[::-1],
131
+ mode="bicubic",
132
+ align_corners=False,
133
+ )
134
+ # visualize the prediction
135
+ output = prediction.squeeze().cpu().numpy()
136
+ formatted = (output * 255 / np.max(output)).astype("uint8")
137
+ depth_image = Image.fromarray(formatted)
138
+ depth_image.save(f"depth.png")
139
+ return depth_image
140
+
141
+
142
+ @spaces.GPU
143
+ def infer(use_custom_model, model_name, weight_name, custom_lora_weight, image_in, prompt, negative_prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, inf_steps, seed, progress=gr.Progress(track_tqdm=True)):
144
+
145
+ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
146
+ "stabilityai/stable-diffusion-xl-base-1.0",
147
+ controlnet=controlnet,
148
+ vae=vae,
149
+ torch_dtype=torch.float16,
150
+ variant="fp16",
151
+ use_safetensors=True
152
+ )
153
+
154
+ pipe.to(device)
155
+
156
+ prompt = prompt
157
+ negative_prompt = negative_prompt
158
+
159
+ if seed < 0 :
160
+ seed = random.randint(0, 423538377342)
161
+
162
+ generator = torch.Generator(device=device).manual_seed(seed)
163
+
164
+ if image_in == None:
165
+ raise gr.Error("You forgot to upload a source image.")
166
+
167
+ image_in = resize_image(image_in, "resized_input.jpg", 1024)
168
+
169
+ if preprocessor == "canny":
170
+
171
+ image = load_image(image_in)
172
+
173
+ image = np.array(image)
174
+ image = cv2.Canny(image, 100, 200)
175
+ image = image[:, :, None]
176
+ image = np.concatenate([image, image, image], axis=2)
177
+ image = Image.fromarray(image)
178
+
179
+ if use_custom_model:
180
+
181
+ if model_name == "":
182
+ raise gr.Error("you forgot to set a custom model name.")
183
+
184
+ custom_model = model_name
185
+
186
+ # This is where you load your trained weights
187
+ if weight_name == "NO SAFETENSORS FILE":
188
+ pipe.load_lora_weights(
189
+ custom_model,
190
+ low_cpu_mem_usage = True,
191
+ use_auth_token = True
192
+ )
193
+
194
+ else:
195
+ pipe.load_lora_weights(
196
+ custom_model,
197
+ weight_name = weight_name,
198
+ low_cpu_mem_usage = True,
199
+ use_auth_token = True
200
+ )
201
+
202
+ lora_scale=custom_lora_weight
203
+
204
+ images = pipe(
205
+ prompt,
206
+ negative_prompt=negative_prompt,
207
+ image=image,
208
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale),
209
+ guidance_scale = float(guidance_scale),
210
+ num_inference_steps=inf_steps,
211
+ generator=generator,
212
+ cross_attention_kwargs={"scale": lora_scale}
213
+ ).images
214
+ else:
215
+ images = pipe(
216
+ prompt,
217
+ negative_prompt=negative_prompt,
218
+ image=image,
219
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale),
220
+ guidance_scale = float(guidance_scale),
221
+ num_inference_steps=inf_steps,
222
+ generator=generator,
223
+ ).images
224
+
225
+ images[0].save(f"result.png")
226
+ print("HELP")
227
+ predict(images[0])
228
+ # create_visual_demo();
229
+ return f"result.png", seed
230
+
231
+
232
+ css="""
233
+ .{
234
+ height: 20%;
235
+ }
236
+ #col-container{
237
+ margin: 0 auto;
238
+ max-width: 720px;
239
+ text-align: left;
240
+ }
241
+ div#warning-duplicate {
242
+ background-color: #ebf5ff;
243
+ padding: 0 10px 5px;
244
+ margin: 20px 0;
245
+ }
246
+ div#warning-duplicate > .gr-prose > h2, div#warning-duplicate > .gr-prose > p {
247
+ color: #0f4592!important;
248
+ }
249
+ div#warning-duplicate strong {
250
+ color: #0f4592;
251
+ }
252
+ p.actions {
253
+ display: flex;
254
+ align-items: center;
255
+ margin: 20px 0;
256
+ }
257
+ div#warning-duplicate .actions a {
258
+ display: inline-block;
259
+ margin-right: 10px;
260
+ }
261
+ button#load_model_btn{
262
+ height: 45px !important;
263
+ border: none;
264
+ background-color: #99F6E4; !important;
265
+ border-radius: 10px !important;
266
+ padding: 10px !important;
267
+ cursor: pointer;
268
+ display: block;
269
+ position: relative;
270
+ top: -20px;
271
+ z-index: 100;
272
+ }
273
+ #status_info{
274
+ font-size: 0.9em;
275
+ }
276
+ """
277
+
278
+ theme = gr.themes.Soft(
279
+ primary_hue="teal",
280
+ secondary_hue="gray",
281
+ ).set(
282
+ body_text_color_dark='*neutral_800',
283
+ background_fill_primary_dark='*neutral_50',
284
+ background_fill_secondary_dark='*neutral_50',
285
+ border_color_accent_dark='*primary_300',
286
+ border_color_primary_dark='*neutral_200',
287
+ color_accent_soft_dark='*neutral_50',
288
+ link_text_color_dark='*secondary_600',
289
+ link_text_color_active_dark='*secondary_600',
290
+ link_text_color_hover_dark='*secondary_700',
291
+ link_text_color_visited_dark='*secondary_500',
292
+ code_background_fill_dark='*neutral_100',
293
+ shadow_spread_dark='6px',
294
+ block_background_fill_dark='white',
295
+ block_label_background_fill_dark='*primary_100',
296
+ block_label_text_color_dark='*primary_500',
297
+ block_title_text_color_dark='*primary_500',
298
+ checkbox_background_color_dark='*background_fill_primary',
299
+ checkbox_background_color_selected_dark='*primary_600',
300
+ checkbox_border_color_dark='*neutral_100',
301
+ checkbox_border_color_focus_dark='*primary_500',
302
+ checkbox_border_color_hover_dark='*neutral_300',
303
+ checkbox_border_color_selected_dark='*primary_600',
304
+ checkbox_label_background_fill_selected_dark='*primary_500',
305
+ checkbox_label_text_color_selected_dark='white',
306
+ error_background_fill_dark='#fef2f2',
307
+ error_border_color_dark='#b91c1c',
308
+ error_text_color_dark='#b91c1c',
309
+ error_icon_color_dark='#b91c1c',
310
+ input_background_fill_dark='white',
311
+ input_background_fill_focus_dark='*secondary_500',
312
+ input_border_color_dark='*neutral_50',
313
+ input_border_color_focus_dark='*secondary_300',
314
+ input_placeholder_color_dark='*neutral_400',
315
+ slider_color_dark='*primary_500',
316
+ stat_background_fill_dark='*primary_300',
317
+ table_border_color_dark='*neutral_300',
318
+ table_even_background_fill_dark='white',
319
+ table_odd_background_fill_dark='*neutral_50',
320
+ button_primary_background_fill_dark='*primary_500',
321
+ button_primary_background_fill_hover_dark='*primary_400',
322
+ button_primary_border_color_dark='*primary_00',
323
+ button_secondary_background_fill_dark='whiite',
324
+ button_secondary_background_fill_hover_dark='*neutral_100',
325
+ button_secondary_border_color_dark='*neutral_200',
326
+ button_secondary_text_color_dark='*neutral_800'
327
+ )
328
+
329
+ #examples = [["examples/" + img] for img in os.listdir("examples/")]
330
+ im = gr.Image(visible=False)
331
+
332
+ with gr.Blocks(theme=theme, css=css) as demo:
333
+ with gr.Row():
334
+ with gr.Column(elem_id="col-container"):
335
+
336
+ gr.HTML("""
337
+ <h2 style="text-align: left;">Choose a Style</h2>
338
+ <p style="text-align: left;">Our Pretrained Models can be found on Huggingface</p>
339
+ """)
340
+
341
+ use_custom_model = gr.Checkbox(label="Use a custom pre-trained LoRa model ? (optional)", visible = False, value=False, info="To use a private model, you'll need to duplicate the space with your own access token.")
342
+
343
+ with gr.Blocks(visible=False) as custom_model_box:
344
+ with gr.Row():
345
+ with gr.Column():
346
+ if not is_shared_ui:
347
+ your_username = api.whoami()["name"]
348
+ my_models = api.list_models(author=your_username, filter=["diffusers", "stable-diffusion-xl", 'lora'])
349
+ model_names = [item.modelId for item in my_models]
350
+
351
+ if not is_shared_ui:
352
+ custom_model = gr.Dropdown(
353
+ label = "Your custom model ID",
354
+ info="You can pick one of your private models",
355
+ choices = model_names,
356
+ allow_custom_value = True
357
+ #placeholder = "username/model_id"
358
+ )
359
+ else:
360
+ custom_model = gr.Textbox(
361
+ label="Your custom model ID",
362
+ placeholder="your_username/your_trained_model_name",
363
+ info="Make sure your model is set to PUBLIC"
364
+ )
365
+
366
+ weight_name = gr.Dropdown(
367
+ label="Safetensors file",
368
+ #value="pytorch_lora_weights.safetensors",
369
+ info="specify which one if model has several .safetensors files",
370
+ allow_custom_value=True,
371
+ visible = False
372
+ )
373
+ with gr.Column():
374
+ with gr.Group():
375
+ # load_model_btn = gr.Button("Load my model", elem_id="load_model_btn")
376
+ previous_model = gr.Textbox(
377
+ visible = False
378
+ )
379
+
380
+ model_status = gr.Textbox(
381
+ label = "model status",
382
+ show_label = False,
383
+ elem_id = "status_info"
384
+ )
385
+ trigger_word = gr.Textbox(label="Trigger word", interactive=False, visible=False)
386
+
387
+ load_model_btn = gr.Button("Load my model", elem_id="load_model_btn")
388
+ image_in = gr.Image(sources="upload", type="filepath", value=( "shop1.jpg"))
389
+ gr.Examples(
390
+ examples=[[os.path.join(os.path.dirname(__file__), "shop2.jpg")],[os.path.join(os.path.dirname(__file__), "shop3.jpg")]], inputs=im)
391
+
392
+
393
+ with gr.Column(elem_id="col-container"):
394
+ gr.HTML("""
395
+ <h2 style="text-align: left;">Input a Prompt!</h2>
396
+ <p style="text-align: left;">Negative prompts and other settings can be found in advanced options</p>
397
+ """)
398
+
399
+ with gr.Row():
400
+
401
+ with gr.Column():
402
+ # with gr.Group():
403
+ prompt = gr.Textbox(label="Prompt", placeholder="Add your trigger word here + prompt")
404
+
405
+ with gr.Accordion(label="Advanced Options", open=False):
406
+ # with gr.Group():
407
+ negative_prompt = gr.Textbox(label="Negative prompt", value="extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured")
408
+ guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=8.8)
409
+ inf_steps = gr.Slider(label="Inference Steps", minimum="25", maximum="50", step=1, value=25)
410
+ custom_lora_weight = gr.Slider(label="Custom model weights", minimum=0.1, maximum=0.9, step=0.1, value=0.7)
411
+ preprocessor = gr.Dropdown(label="Preprocessor", choices=["canny"], value="canny", interactive=False, info="For the moment, only canny is available")
412
+ controlnet_conditioning_scale = gr.Slider(label="Controlnet conditioning Scale", minimum=0.1, maximum=0.9, step=0.01, value=0.3)
413
+ seed = gr.Slider(
414
+ label="Seed",
415
+ info = "-1 denotes a random seed",
416
+ minimum=-1,
417
+ maximum=423538377342,
418
+ step=1,
419
+ value=-1
420
+ )
421
+ last_used_seed = gr.Number(
422
+ label = "Last used seed",
423
+ info = "the seed used in the last generation",
424
+ )
425
+
426
+ submit_btn = gr.Button("Submit")
427
+
428
+ # label = gr.Label(label="Loader")
429
+ # submit_btn.click(infer, outputs=[label])
430
+
431
+ result = gr.Image(label="Result", visible=True)
432
+
433
+ use_custom_model.change(
434
+ fn = check_use_custom_or_no,
435
+ inputs =[use_custom_model],
436
+ outputs = [custom_model_box],
437
+ queue = False
438
+ )
439
+ custom_model.blur(
440
+ fn=custom_model_changed,
441
+ inputs = [custom_model, previous_model],
442
+ outputs = [model_status],
443
+ queue = False
444
+ )
445
+ load_model_btn.click(
446
+ fn = load_model,
447
+ inputs=[custom_model],
448
+ outputs = [previous_model, model_status, weight_name, trigger_word],
449
+ queue = False
450
+ )
451
+ submit_btn.click(
452
+ fn = infer,
453
+ inputs = [use_custom_model,custom_model, weight_name, custom_lora_weight, image_in, prompt, negative_prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, inf_steps, seed],
454
+ outputs = [result, last_used_seed]
455
+ )
456
+
457
+
458
+ # return demo
459
+
460
+
461
+ demo.queue().launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ torch==2.0.1
2
+ torchvision==0.15.2
3
+ invisible_watermark
4
+ accelerate
5
+ transformers
6
+ safetensors
7
+ opencv-python
8
+ git+https://github.com/huggingface/diffusers.git
shop1.jpg ADDED
shop2.jpg ADDED
shop3.jpg ADDED