dezzman commited on
Commit
1f7518e
·
verified ·
1 Parent(s): 3ca9089

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -2
app.py CHANGED
@@ -1,12 +1,44 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
 
5
  # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
 
7
  import torch
8
  from typing import Optional
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
  model_id_default = "CompVis/stable-diffusion-v1-4"
12
 
@@ -15,7 +47,13 @@ if torch.cuda.is_available():
15
  else:
16
  torch_dtype = torch.float32
17
 
18
- pipe_default = DiffusionPipeline.from_pretrained(model_id_default, torch_dtype=torch_dtype)
 
 
 
 
 
 
19
  pipe_default = pipe_default.to(device)
20
 
21
  MAX_SEED = np.iinfo(np.int32).max
@@ -32,6 +70,7 @@ def infer(
32
  model_id: Optional[str] = 'CompVis/stable-diffusion-v1-4',
33
  seed: Optional[int] = 42,
34
  guidance_scale: Optional[float] = 7.0,
 
35
  progress=gr.Progress(track_tqdm=True),
36
  ):
37
  generator = torch.Generator().manual_seed(seed)
@@ -49,8 +88,10 @@ def infer(
49
  if model_id != model_id_default:
50
  pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
51
  pipe = pipe.to(device)
 
52
  image = pipe(**params).images[0]
53
  else:
 
54
  image = pipe_default(**params).images[0]
55
 
56
  return image
@@ -105,6 +146,15 @@ with gr.Blocks(css=css) as demo:
105
  value=7.0,
106
  )
107
 
 
 
 
 
 
 
 
 
 
108
  with gr.Row():
109
  num_inference_steps = gr.Slider(
110
  label="Number of inference steps",
@@ -148,6 +198,7 @@ with gr.Blocks(css=css) as demo:
148
  model_id,
149
  seed,
150
  guidance_scale,
 
151
  ],
152
  outputs=[result],
153
  )
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
+ import os
5
 
6
  # import spaces #[uncomment to use ZeroGPU]
7
+ from diffusers import DiffusionPipeline, StableDiffusionPipeline
8
+ from peft import PeftModel, LoraConfig
9
  import torch
10
  from typing import Optional
11
 
12
+
13
+ def get_lora_sd_pipeline(
14
+ ckpt_dir='./lora_logos',
15
+ base_model_name_or_path=None,
16
+ dtype=torch.float16,
17
+ adapter_name="default"
18
+ ):
19
+ unet_sub_dir = os.path.join(ckpt_dir, "unet")
20
+ text_encoder_sub_dir = os.path.join(ckpt_dir, "text_encoder")
21
+ if os.path.exists(text_encoder_sub_dir) and base_model_name_or_path is None:
22
+ config = LoraConfig.from_pretrained(text_encoder_sub_dir)
23
+ base_model_name_or_path = config.base_model_name_or_path
24
+
25
+ if base_model_name_or_path is None:
26
+ raise ValueError("Please specify the base model name or path")
27
+
28
+ pipe = StableDiffusionPipeline.from_pretrained(base_model_name_or_path, torch_dtype=dtype).to(device)
29
+ pipe.unet = PeftModel.from_pretrained(pipe.unet, unet_sub_dir, adapter_name=adapter_name)
30
+
31
+ if os.path.exists(text_encoder_sub_dir):
32
+ pipe.text_encoder = PeftModel.from_pretrained(
33
+ pipe.text_encoder, text_encoder_sub_dir, adapter_name=adapter_name
34
+ )
35
+
36
+ if dtype in (torch.float16, torch.bfloat16):
37
+ pipe.unet.half()
38
+ pipe.text_encoder.half()
39
+
40
+ return pipe
41
+
42
  device = "cuda" if torch.cuda.is_available() else "cpu"
43
  model_id_default = "CompVis/stable-diffusion-v1-4"
44
 
 
47
  else:
48
  torch_dtype = torch.float32
49
 
50
+
51
+ pipe_default = get_lora_sd_pipeline(
52
+ ckpt_dir='./lora_logos',
53
+ base_model_name_or_path=model_id_default,
54
+ dtype=torch_dtype,
55
+ )
56
+ # pipe_default = DiffusionPipeline.from_pretrained(model_id_default, torch_dtype=torch_dtype)
57
  pipe_default = pipe_default.to(device)
58
 
59
  MAX_SEED = np.iinfo(np.int32).max
 
70
  model_id: Optional[str] = 'CompVis/stable-diffusion-v1-4',
71
  seed: Optional[int] = 42,
72
  guidance_scale: Optional[float] = 7.0,
73
+ lora_scale: Optional[float] = 0.5,
74
  progress=gr.Progress(track_tqdm=True),
75
  ):
76
  generator = torch.Generator().manual_seed(seed)
 
88
  if model_id != model_id_default:
89
  pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
90
  pipe = pipe.to(device)
91
+ pipe.fuse_lora(lora_scale=0.4)
92
  image = pipe(**params).images[0]
93
  else:
94
+ pipe_default.fuse_lora(lora_scale=0.4)
95
  image = pipe_default(**params).images[0]
96
 
97
  return image
 
146
  value=7.0,
147
  )
148
 
149
+ with gr.Row():
150
+ lora_scale = gr.Slider(
151
+ label="LoRA scale",
152
+ minimum=0.0,
153
+ maximum=1.0,
154
+ step=0.1,
155
+ value=0.5,
156
+ )
157
+
158
  with gr.Row():
159
  num_inference_steps = gr.Slider(
160
  label="Number of inference steps",
 
198
  model_id,
199
  seed,
200
  guidance_scale,
201
+ lora_scale,
202
  ],
203
  outputs=[result],
204
  )