multimodalart HF staff commited on
Commit
6a4b741
1 Parent(s): 3ce1e06

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionXLPipeline, AutoencoderKL
4
+ from huggingface_hub import hf_hub_download
5
+ import lora
6
+ from time import sleep
7
+ import copy
8
+
9
+ sdxl_loras = [
10
+ ("pixel-art-xl.jpeg", "Pixel Art XL", "nerijs/pixel-art-xl", "pixel art", "pixel-art-xl.safetensors", True),
11
+ ("papercut_SDXL.jpeg", "Papercut SDXL", "TheLastBen/Papercut_SDXL", "papercut", "papercut.safetensors", False),
12
+ ("lego-minifig-xl.jpeg", "Lego Minifig XL", "nerijs/lego-minifig-xl", "lego minifig", "legominifig-v1.0-000003.safetensors", True),
13
+ ("3d_style_4.jpeg", "3D Render Style", "goofyai/3d_render_style_xl", "3d style", "3d_render_style_xl.safetensors", True),
14
+ ("LogoRedmond-LogoLoraForSDXL.jpeg","Logo.Redmond", "artificialguybr/LogoRedmond-LogoLoraForSDXL", "LogoRedAF", "LogoRedmond_LogoRedAF.safetensors", False),
15
+ ("LineAni.Redmond.png", "LinearManga.Redmond", "artificialguybr/LineAniRedmond-LinearMangaSDXL", "LineAniAF", "LineAniRedmond-LineAniAF.safetensors", True),
16
+ ("embroid.png","Embroidery Style","ostris/embroidery_style_lora_sdxl","","embroidered_style_v1_sdxl.safetensors",False),
17
+ ("watercolor.png","Watercolor Style","ostris/watercolor_style_lora_sdxl","","watercolor_v1_sdxl.safetensors",False),
18
+ ("crayon.png","Crayon Style","ostris/crayon_style_lora_sdxl","","crayons_v1_sdxl.safetensors",False),
19
+ ("dog.png", "Cyborg Style", "goofyai/cyborg_style_xl", "cyborg style", "cyborg_style_xl-off.safetensors", True),
20
+ ("ToyRedmond-ToyLoraForSDXL10.png","Toy.Redmond", "artificialguybr/ToyRedmond-ToyLoraForSDXL10", "FnkRedmAF", "ToyRedmond-FnkRedmAF.safetensors", True),
21
+ ("voxel-xl-lora.png", "Voxel XL", "Fictiverse/Voxel_XL_Lora", "voxel style", "VoxelXL_v1.safetensors", True),
22
+ ("pikachu.webp", "Pikachu XL", "TheLastBen/Pikachu_SDXL", "pikachu", "pikachu.safetensors", False),
23
+ ("william_eggleston.webp", "William Eggleston Style", "TheLastBen/William_Eggleston_Style_SDXL", "by william eggleston", "wegg.safetensors", False),
24
+ ("josef_koudelka.webp", "Josef Koudelka Style", "TheLastBen/Josef_Koudelka_Style_SDXL", "by josef koudelka", "koud.safetensors", False),
25
+ ("corgi_brick.jpeg", "Lego BrickHeadz", "nerijs/lego-brickheadz-xl", "lego brickheadz", "legobrickheadz-v1.0-000004.safetensors", True)
26
+ ]
27
+
28
+ saved_names = [hf_hub_download(repo_id, filename) for _, _, repo_id, _, filename, _ in sdxl_loras]
29
+
30
+ def update_selection(selected_state: gr.SelectData):
31
+ sleep(60)
32
+ lora_repo = sdxl_loras[selected_state.index][2]
33
+ instance_prompt = sdxl_loras[selected_state.index][3]
34
+ updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo})"
35
+ return updated_text, instance_prompt, selected_state
36
+
37
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
38
+ mutable_pipe = StableDiffusionXLPipeline.from_pretrained(
39
+ "stabilityai/stable-diffusion-xl-base-1.0",
40
+ vae=vae,
41
+ torch_dtype=torch.float16,
42
+ ).to("cpu")
43
+ original_pipe = copy.deepcopy(mutable_pipe)
44
+ mutable_pipe.to("cuda")
45
+
46
+ last_lora = ""
47
+ last_merged = False
48
+
49
+ def run_lora(prompt, negative, weight, selected_state):
50
+ global last_lora, last_merged
51
+ pipe = mutable_pipe
52
+ if(not selected_state):
53
+ raise gr.Error("You must select a LoRA")
54
+ repo_name = sdxl_loras[selected_state.index][2]
55
+ weight_name = sdxl_loras[selected_state.index][4]
56
+ full_path_lora = saved_names[selected_state.index]
57
+ cross_attention_kwargs = None
58
+ if(last_lora != repo_name):
59
+ if(last_merged):
60
+ pipe = copy.deepcopy(original_pipe)
61
+ pipe.to("cuda")
62
+ else:
63
+ pipe.unload_lora_weights()
64
+ is_compatible = sdxl_loras[selected_state.index][5]
65
+ if(is_compatible):
66
+ pipe.load_lora_weights(full_path_lora)
67
+ cross_attention_kwargs={"scale": weight}
68
+ else:
69
+ for weights_file in [full_path_lora]:
70
+ if ";" in weights_file:
71
+ weights_file, multiplier = weights_file.split(";")
72
+ multiplier = float(weight)
73
+ else:
74
+ multiplier = 1.0
75
+
76
+ lora_model, weights_sd = lora.create_network_from_weights(
77
+ multiplier, full_path_lora, pipe.vae, pipe.text_encoder, pipe.unet, for_inference=True
78
+ )
79
+ lora_model.merge_to(pipe.text_encoder, pipe.unet, weights_sd, torch.float16, "cuda")
80
+ last_merged = True
81
+ image = pipe(
82
+ prompt=prompt, negative_prompt=negative, num_inference_steps=20, guidance_scale=7.5, cross_attention_kwargs=cross_attention_kwargs).images[0]
83
+ last_lora = repo_name
84
+ return image
85
+
86
+ css = '''
87
+ #prompt textarea{width: calc(100% - 160px);border-top-right-radius: 0px;border-bottom-right-radius: 0px;}
88
+ #run_button{position:absolute;margin-top: 38px;right: 0;margin-right: 0.8em;border-bottom-left-radius: 0px;
89
+ border-top-left-radius: 0px;}
90
+ #gallery{display:flex}
91
+ #gallery .grid-wrap{min-height: 100%;}
92
+ '''
93
+
94
+ with gr.Blocks(css=css) as demo:
95
+ title = gr.Markdown("# Lora The Explorer XL")
96
+ with gr.Row():
97
+ gallery = gr.Gallery(value=[(a, b) for a, b, _, _, _, _ in sdxl_loras],
98
+ label="LoRA Gallery",
99
+ allow_preview=False,
100
+ columns=3,
101
+ elem_id="gallery"
102
+ )
103
+ with gr.Column():
104
+ prompt_title = gr.Markdown(value="### Click on a LoRA in the gallery to select it", visible=True)
105
+ with gr.Row():
106
+ prompt = gr.Textbox(label="Prompt", elem_id="prompt")
107
+ button = gr.Button("Run", elem_id="run_button")
108
+ result = gr.Image(interactive=False, label="result")
109
+ with gr.Accordion("Advanced options", open=False):
110
+ negative = gr.Textbox(label="Negative Prompt")
111
+ weight = gr.Slider(0, 1, value=1, step=0.1, label="LoRA weight")
112
+ with gr.Column():
113
+ gr.Markdown("Use it with:")
114
+ with gr.Row():
115
+ with gr.Accordion("🧨 diffusers", open=False):
116
+ gr.Markdown("")
117
+ with gr.Accordion("ComfyUI", open=False):
118
+ gr.Markdown("")
119
+ with gr.Accordion("Invoke AI", open=False):
120
+ gr.Markdown("")
121
+ with gr.Accordion("SD.Next (AUTO1111 fork)", open=False):
122
+ gr.Markdown("")
123
+ selected_state = gr.State()
124
+ gallery.select(update_selection, outputs=[prompt_title, prompt, selected_state], queue=False, show_progress=False)
125
+ button.click(fn=run_lora, inputs=[prompt, negative, weight, selected_state], outputs=result)
126
+
127
+ demo.launch()