pva22 commited on
Commit
d24c692
·
1 Parent(s): 3ea7b53

new lora and app

Browse files
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
app.py CHANGED
@@ -57,21 +57,17 @@ def align_embeddings(prompt_embeds, negative_prompt_embeds):
57
  torch.nn.functional.pad(negative_prompt_embeds, (0, 0, 0, max_length - negative_prompt_embeds.shape[1]))
58
 
59
  device = "cuda" if torch.cuda.is_available() else "cpu"
60
- #model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
61
  model_id_default = "sd-legacy/stable-diffusion-v1-5"
62
- model_dropdown = ['stabilityai/sdxl-turbo', 'CompVis/stable-diffusion-v1-4', 'sd-legacy/stable-diffusion-v1-5' ]
63
 
64
  model_lora_default = "lora"
65
- model_lora_dropdown = ['lora', 'lora']
66
 
67
  if torch.cuda.is_available():
68
  torch_dtype = torch.float16
69
  else:
70
  torch_dtype = torch.float32
71
 
72
- # pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
73
- # pipe = pipe.to(device)
74
-
75
  MAX_SEED = np.iinfo(np.int32).max
76
  MAX_IMAGE_SIZE = 1024
77
 
@@ -96,10 +92,6 @@ def infer(
96
  seed = random.randint(0, MAX_SEED)
97
 
98
  generator = torch.Generator().manual_seed(seed)
99
-
100
- # убираем обновление pipe всегда
101
- #pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
102
- #pipe = pipe.to(device)
103
 
104
  # добавляем обновление pipe по условию
105
  if model_repo_id != model_id_default:
@@ -109,7 +101,6 @@ def infer(
109
  prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
110
  else:
111
  # добавляем lora
112
- #pipe = get_lora_sd_pipeline(ckpt_dir='./lora_lady_and_cats_logos', base_model_name_or_path=model_id_default, dtype=torch_dtype).to(device)
113
  pipe = get_lora_sd_pipeline(ckpt_dir='./' + model_lora_id, base_model_name_or_path=model_id_default, dtype=torch_dtype).to(device)
114
  prompt_embeds = process_prompt(prompt, pipe.tokenizer, pipe.text_encoder)
115
  negative_prompt_embeds = process_prompt(negative_prompt, pipe.tokenizer, pipe.text_encoder)
@@ -118,19 +109,6 @@ def infer(
118
  print(f"LoRA scale applied: {lora_scale}")
119
  pipe.fuse_lora(lora_scale=lora_scale)
120
 
121
-
122
- # заменяем просто вызов pipe с промптом
123
- #image = pipe(
124
- # prompt=prompt,
125
- # negative_prompt=negative_prompt,
126
- # guidance_scale=guidance_scale,
127
- # num_inference_steps=num_inference_steps,
128
- # width=width,
129
- # height=height,
130
- # generator=generator,
131
- #).images[0]
132
-
133
-
134
  # на вызов pipe с эмбеддингами
135
  params = {
136
  'prompt_embeds': prompt_embeds,
@@ -144,17 +122,12 @@ def infer(
144
 
145
  return pipe(**params).images[0], seed
146
 
147
- # return image, seed
148
-
149
 
150
  examples = [
151
- "Puss in Boots wearing a sombrero crosses the Grand Canyon on a tightrope with a guitar.",
152
- "A cat is playing a song called ""About the Cat"" on an accordion by the sea at sunset. The sun is quickly setting behind the horizon, and the light is fading.",
153
- "A cat walks through the grass on the streets of an abandoned city. The camera view is always focused on the cat's face.",
154
- "A young lady in a Russian embroidered kaftan is sitting on a beautiful carved veranda, holding a cup to her mouth and drinking tea from the cup. With her other hand, the girl holds a saucer. The cup and saucer are painted with gzhel. Next to the girl on the table stands a samovar, and steam can be seen above it.",
155
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
156
- "An astronaut riding a green horse",
157
- "A delicious ceviche cheesecake slice",
158
  ]
159
 
160
  css = """
@@ -166,7 +139,7 @@ css = """
166
 
167
  with gr.Blocks(css=css) as demo:
168
  with gr.Column(elem_id="col-container"):
169
- gr.Markdown(" # Text-to-Image SemaSci Template")
170
 
171
  with gr.Row():
172
  prompt = gr.Text(
@@ -181,112 +154,5 @@ with gr.Blocks(css=css) as demo:
181
 
182
  result = gr.Image(label="Result", show_label=False)
183
 
184
- with gr.Accordion("Advanced Settings", open=False):
185
- # model_repo_id = gr.Text(
186
- # label="Model Id",
187
- # max_lines=1,
188
- # placeholder="Choose model",
189
- # visible=True,
190
- # value=model_repo_id,
191
- # )
192
- model_repo_id = gr.Dropdown(
193
- label="Model Id",
194
- choices=model_dropdown,
195
- info="Choose model",
196
- visible=True,
197
- allow_custom_value=True,
198
- # value=model_repo_id,
199
- value=model_id_default,
200
- )
201
-
202
- negative_prompt = gr.Text(
203
- label="Negative prompt",
204
- max_lines=1,
205
- placeholder="Enter a negative prompt",
206
- visible=True,
207
- )
208
-
209
- seed = gr.Slider(
210
- label="Seed",
211
- minimum=0,
212
- maximum=MAX_SEED,
213
- step=1,
214
- value=42,
215
- )
216
-
217
- randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
218
-
219
- with gr.Row():
220
- width = gr.Slider(
221
- label="Width",
222
- minimum=256,
223
- maximum=MAX_IMAGE_SIZE,
224
- step=32,
225
- value=512, # Replace with defaults that work for your model
226
- )
227
-
228
- height = gr.Slider(
229
- label="Height",
230
- minimum=256,
231
- maximum=MAX_IMAGE_SIZE,
232
- step=32,
233
- value=512, # Replace with defaults that work for your model
234
- )
235
-
236
- with gr.Row():
237
- guidance_scale = gr.Slider(
238
- label="Guidance scale",
239
- minimum=0.0,
240
- maximum=10.0,
241
- step=0.1,
242
- value=7.0, # Replace with defaults that work for your model
243
- )
244
-
245
- num_inference_steps = gr.Slider(
246
- label="Number of inference steps",
247
- minimum=1,
248
- maximum=50,
249
- step=1,
250
- value=20, # Replace with defaults that work for your model
251
- )
252
-
253
- with gr.Row():
254
- model_lora_id = gr.Dropdown(
255
- label="Lora Id",
256
- choices=model_lora_dropdown,
257
- info="Choose LoRA model",
258
- visible=True,
259
- allow_custom_value=True,
260
- value=model_lora_default,
261
- )
262
-
263
- lora_scale = gr.Slider(
264
- label="LoRA scale",
265
- minimum=0.0,
266
- maximum=1.0,
267
- step=0.1,
268
- value=0.5,
269
- )
270
-
271
- gr.Examples(examples=examples, inputs=[prompt])
272
- gr.on(
273
- triggers=[run_button.click, prompt.submit],
274
- fn=infer,
275
- inputs=[
276
- prompt,
277
- negative_prompt,
278
- randomize_seed,
279
- width,
280
- height,
281
- model_repo_id,
282
- seed,
283
- guidance_scale,
284
- num_inference_steps,
285
- model_lora_id,
286
- lora_scale,
287
- ],
288
- outputs=[result, seed],
289
- )
290
-
291
  if __name__ == "__main__":
292
  demo.launch()
 
57
  torch.nn.functional.pad(negative_prompt_embeds, (0, 0, 0, max_length - negative_prompt_embeds.shape[1]))
58
 
59
  device = "cuda" if torch.cuda.is_available() else "cpu"
60
+
61
  model_id_default = "sd-legacy/stable-diffusion-v1-5"
62
+ model_dropdown = ['stabilityai/sdxl-turbo', 'CompVis/stable-diffusion-v1-4', 'sd-legacy/stable-diffusion-v1-5']
63
 
64
  model_lora_default = "lora"
 
65
 
66
  if torch.cuda.is_available():
67
  torch_dtype = torch.float16
68
  else:
69
  torch_dtype = torch.float32
70
 
 
 
 
71
  MAX_SEED = np.iinfo(np.int32).max
72
  MAX_IMAGE_SIZE = 1024
73
 
 
92
  seed = random.randint(0, MAX_SEED)
93
 
94
  generator = torch.Generator().manual_seed(seed)
 
 
 
 
95
 
96
  # добавляем обновление pipe по условию
97
  if model_repo_id != model_id_default:
 
101
  prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
102
  else:
103
  # добавляем lora
 
104
  pipe = get_lora_sd_pipeline(ckpt_dir='./' + model_lora_id, base_model_name_or_path=model_id_default, dtype=torch_dtype).to(device)
105
  prompt_embeds = process_prompt(prompt, pipe.tokenizer, pipe.text_encoder)
106
  negative_prompt_embeds = process_prompt(negative_prompt, pipe.tokenizer, pipe.text_encoder)
 
109
  print(f"LoRA scale applied: {lora_scale}")
110
  pipe.fuse_lora(lora_scale=lora_scale)
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  # на вызов pipe с эмбеддингами
113
  params = {
114
  'prompt_embeds': prompt_embeds,
 
122
 
123
  return pipe(**params).images[0], seed
124
 
 
 
125
 
126
  examples = [
127
+ "A Elon Mask lady in a Russian embroidered kaftan is sitting on a beautiful carved veranda, holding a cup to her mouth and drinking tea from the cup. With her other hand, the girl holds a saucer. The cup and saucer are painted with gzhel. Next to the girl on the table stands a samovar, and steam can be seen above it.",
128
+ "Elon Mask in a jungle, cold color palette, muted colors, detailed, 8k",
129
+ "An Elon Mask astronaut riding a green horse",
130
+ "A delicious Elon Mask ceviche cheesecake slice",
 
 
 
131
  ]
132
 
133
  css = """
 
139
 
140
  with gr.Blocks(css=css) as demo:
141
  with gr.Column(elem_id="col-container"):
142
+ gr.Markdown(" # Text-to-Image")
143
 
144
  with gr.Row():
145
  prompt = gr.Text(
 
154
 
155
  result = gr.Image(label="Result", show_label=False)
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  if __name__ == "__main__":
158
  demo.launch()
lora/.DS_Store CHANGED
Binary files a/lora/.DS_Store and b/lora/.DS_Store differ
 
lora/unet/.DS_Store DELETED
Binary file (6.15 kB)
 
lora/unet/adapter_config.json CHANGED
@@ -27,10 +27,10 @@
27
  "rank_pattern": {},
28
  "revision": null,
29
  "target_modules": [
30
- "query",
31
- "to_v",
32
  "to_q",
33
- "value"
 
 
34
  ],
35
  "task_type": null,
36
  "use_dora": false,
 
27
  "rank_pattern": {},
28
  "revision": null,
29
  "target_modules": [
 
 
30
  "to_q",
31
+ "value",
32
+ "query",
33
+ "to_v"
34
  ],
35
  "task_type": null,
36
  "use_dora": false,
lora/unet/adapter_model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:63b44b516a91a81711d3b5950e6a43adc23144a7d4c84bf94198e6c6c59240e7
3
  size 6397528
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:abd77708f6b47df2914c974a88d6f15a8f468a2f998446c987314d0c0311e8d2
3
  size 6397528
lora/unet/config.json DELETED
@@ -1,68 +0,0 @@
1
- {
2
- "_class_name": "UNet2DConditionModel",
3
- "_diffusers_version": "0.32.2",
4
- "_name_or_path": "sd-legacy/stable-diffusion-v1-5",
5
- "act_fn": "silu",
6
- "addition_embed_type": null,
7
- "addition_embed_type_num_heads": 64,
8
- "addition_time_embed_dim": null,
9
- "attention_head_dim": 8,
10
- "attention_type": "default",
11
- "block_out_channels": [
12
- 320,
13
- 640,
14
- 1280,
15
- 1280
16
- ],
17
- "center_input_sample": false,
18
- "class_embed_type": null,
19
- "class_embeddings_concat": false,
20
- "conv_in_kernel": 3,
21
- "conv_out_kernel": 3,
22
- "cross_attention_dim": 768,
23
- "cross_attention_norm": null,
24
- "down_block_types": [
25
- "CrossAttnDownBlock2D",
26
- "CrossAttnDownBlock2D",
27
- "CrossAttnDownBlock2D",
28
- "DownBlock2D"
29
- ],
30
- "downsample_padding": 1,
31
- "dropout": 0.0,
32
- "dual_cross_attention": false,
33
- "encoder_hid_dim": null,
34
- "encoder_hid_dim_type": null,
35
- "flip_sin_to_cos": true,
36
- "freq_shift": 0,
37
- "in_channels": 4,
38
- "layers_per_block": 2,
39
- "mid_block_only_cross_attention": null,
40
- "mid_block_scale_factor": 1,
41
- "mid_block_type": "UNetMidBlock2DCrossAttn",
42
- "norm_eps": 1e-05,
43
- "norm_num_groups": 32,
44
- "num_attention_heads": null,
45
- "num_class_embeds": null,
46
- "only_cross_attention": false,
47
- "out_channels": 4,
48
- "projection_class_embeddings_input_dim": null,
49
- "resnet_out_scale_factor": 1.0,
50
- "resnet_skip_time_act": false,
51
- "resnet_time_scale_shift": "default",
52
- "reverse_transformer_layers_per_block": null,
53
- "sample_size": 64,
54
- "time_cond_proj_dim": null,
55
- "time_embedding_act_fn": null,
56
- "time_embedding_dim": null,
57
- "time_embedding_type": "positional",
58
- "timestep_post_act": null,
59
- "transformer_layers_per_block": 1,
60
- "up_block_types": [
61
- "UpBlock2D",
62
- "CrossAttnUpBlock2D",
63
- "CrossAttnUpBlock2D",
64
- "CrossAttnUpBlock2D"
65
- ],
66
- "upcast_attention": false,
67
- "use_linear_projection": false
68
- }