ktrndy commited on
Commit
2ca97fb
·
verified ·
1 Parent(s): 116989c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -24,7 +24,8 @@ def get_lora_sd_pipeline(
24
  base_model_name_or_path=model_id_default,
25
  dtype=torch_dtype,
26
  device=device,
27
- adapter_name="default"
 
28
  ):
29
  unet_sub_dir = os.path.join(ckpt_dir, "unet")
30
  text_encoder_sub_dir = os.path.join(ckpt_dir, "text_encoder")
@@ -38,6 +39,7 @@ def get_lora_sd_pipeline(
38
  pipe = StableDiffusionPipeline.from_pretrained(base_model_name_or_path, torch_dtype=dtype).to(device)
39
  pipe.unet = PeftModel.from_pretrained(pipe.unet, unet_sub_dir, adapter_name=adapter_name)
40
  pipe.unet.set_adapter(adapter_name)
 
41
 
42
  if os.path.exists(text_encoder_sub_dir):
43
  pipe.text_encoder = PeftModel.from_pretrained(
@@ -52,30 +54,30 @@ def get_lora_sd_pipeline(
52
  return pipe
53
 
54
 
55
- def encode_prompt(prompt, tokenizer, text_encoder):
56
- text_inputs = tokenizer(
57
- prompt,
58
- padding="max_length",
59
- max_length=tokenizer.model_max_length,
60
- return_tensors="pt",
61
- )
62
- with torch.no_grad():
63
- if len(text_inputs.input_ids[0]) < tokenizer.model_max_length:
64
- prompt_embeds = text_encoder(text_inputs.input_ids.to(text_encoder.device))[0]
65
- else:
66
- embeds = []
67
- start = 0
68
- while start < tokenizer.model_max_length:
69
- end = start + tokenizer.model_max_length
70
- part_of_text_inputs = text_inputs.input_ids[0][start:end]
71
- if len(part_of_text_inputs) < tokenizer.model_max_length:
72
- part_of_text_inputs = torch.cat([part_of_text_inputs, torch.tensor([tokenizer.pad_token_id] * (tokenizer.model_max_length - len(part_of_text_inputs)))])
73
- embeds.append(text_encoder(part_of_text_inputs.to(text_encoder.device).unsqueeze(0))[0])
74
- start += int((8/
75
 
76
- 11)*tokenizer.model_max_length)
77
- prompt_embeds = torch.mean(torch.stack(embeds, dim=0), dim=0)
78
- return prompt_embeds
79
 
80
 
81
  # @spaces.GPU #[uncomment to use ZeroGPU]
@@ -95,13 +97,12 @@ def infer(
95
  pipe = get_lora_sd_pipeline(base_model_name_or_path=model_id,
96
  adapter_name="sticker_of_funny_cat_Pusheen")
97
  pipe = pipe.to(device)
98
- prompt_embeds = encode_prompt(prompt, pipe.tokenizer, pipe.text_encoder)
99
- negative_prompt_embeds = encode_prompt(negative_prompt, pipe.tokenizer, pipe.text_encoder)
100
- # pipe.fuse_lora(lora_scale=lora_scale)
101
 
102
  image = pipe(
103
- prompt_embeds=prompt_embeds,
104
- negative_prompt_embeds=negative_prompt_embeds,
105
  guidance_scale=guidance_scale,
106
  num_inference_steps=num_inference_steps,
107
  width=width,
 
24
  base_model_name_or_path=model_id_default,
25
  dtype=torch_dtype,
26
  device=device,
27
+ adapter_name="default",
28
+ lora_scale=1.0
29
  ):
30
  unet_sub_dir = os.path.join(ckpt_dir, "unet")
31
  text_encoder_sub_dir = os.path.join(ckpt_dir, "text_encoder")
 
39
  pipe = StableDiffusionPipeline.from_pretrained(base_model_name_or_path, torch_dtype=dtype).to(device)
40
  pipe.unet = PeftModel.from_pretrained(pipe.unet, unet_sub_dir, adapter_name=adapter_name)
41
  pipe.unet.set_adapter(adapter_name)
42
+ pipe.fuse_lora(lora_scale=lora_scale)
43
 
44
  if os.path.exists(text_encoder_sub_dir):
45
  pipe.text_encoder = PeftModel.from_pretrained(
 
54
  return pipe
55
 
56
 
57
+ # def encode_prompt(prompt, tokenizer, text_encoder):
58
+ # text_inputs = tokenizer(
59
+ # prompt,
60
+ # padding="max_length",
61
+ # max_length=tokenizer.model_max_length,
62
+ # return_tensors="pt",
63
+ # )
64
+ # with torch.no_grad():
65
+ # if len(text_inputs.input_ids[0]) < tokenizer.model_max_length:
66
+ # prompt_embeds = text_encoder(text_inputs.input_ids.to(text_encoder.device))[0]
67
+ # else:
68
+ # embeds = []
69
+ # start = 0
70
+ # while start < tokenizer.model_max_length:
71
+ # end = start + tokenizer.model_max_length
72
+ # part_of_text_inputs = text_inputs.input_ids[0][start:end]
73
+ # if len(part_of_text_inputs) < tokenizer.model_max_length:
74
+ # part_of_text_inputs = torch.cat([part_of_text_inputs, torch.tensor([tokenizer.pad_token_id] * (tokenizer.model_max_length - len(part_of_text_inputs)))])
75
+ # embeds.append(text_encoder(part_of_text_inputs.to(text_encoder.device).unsqueeze(0))[0])
76
+ # start += int((8/
77
 
78
+ # 11)*tokenizer.model_max_length)
79
+ # prompt_embeds = torch.mean(torch.stack(embeds, dim=0), dim=0)
80
+ # return prompt_embeds
81
 
82
 
83
  # @spaces.GPU #[uncomment to use ZeroGPU]
 
97
  pipe = get_lora_sd_pipeline(base_model_name_or_path=model_id,
98
  adapter_name="sticker_of_funny_cat_Pusheen")
99
  pipe = pipe.to(device)
100
+ # prompt_embeds = encode_prompt(prompt, pipe.tokenizer, pipe.text_encoder)
101
+ # negative_prompt_embeds = encode_prompt(negative_prompt, pipe.tokenizer, pipe.text_encoder)
 
102
 
103
  image = pipe(
104
+ prompt=prompt,
105
+ negative_prompt=negative_prompt,
106
  guidance_scale=guidance_scale,
107
  num_inference_steps=num_inference_steps,
108
  width=width,