Yaron Koresh commited on
Commit
8fc171e
·
verified ·
1 Parent(s): 4c255b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -162
app.py CHANGED
@@ -58,167 +58,6 @@ working = False
58
  model = T5ForConditionalGeneration.from_pretrained("t5-base")
59
  tokenizer = T5Tokenizer.from_pretrained("t5-base")
60
 
61
- def calculate_shift(
62
- image_seq_len,
63
- base_seq_len: int = 256,
64
- max_seq_len: int = 4096,
65
- base_shift: float = 0.5,
66
- max_shift: float = 1.16,
67
- ):
68
- m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
69
- b = base_shift - m * base_seq_len
70
- mu = image_seq_len * m + b
71
- return mu
72
-
73
- def retrieve_timesteps(
74
- scheduler,
75
- num_inference_steps: Optional[int] = None,
76
- device: Optional[Union[str, torch.device]] = None,
77
- timesteps: Optional[List[int]] = None,
78
- sigmas: Optional[List[float]] = None,
79
- **kwargs,
80
- ):
81
- if timesteps is not None and sigmas is not None:
82
- raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
83
- if timesteps is not None:
84
- scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
85
- timesteps = scheduler.timesteps
86
- num_inference_steps = len(timesteps)
87
- elif sigmas is not None:
88
- scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
89
- timesteps = scheduler.timesteps
90
- num_inference_steps = len(timesteps)
91
- else:
92
- scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
93
- timesteps = scheduler.timesteps
94
- return timesteps, num_inference_steps
95
-
96
- # FLUX pipeline function
97
- @torch.inference_mode()
98
- def flux_pipe_call_that_returns_an_iterable_of_images(
99
- self,
100
- prompt: Union[str, List[str]] = None,
101
- prompt_2: Optional[Union[str, List[str]]] = None,
102
- height: Optional[int] = None,
103
- width: Optional[int] = None,
104
- num_inference_steps: int = 28,
105
- timesteps: List[int] = None,
106
- guidance_scale: float = 3.5,
107
- num_images_per_prompt: Optional[int] = 1,
108
- generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
109
- latents: Optional[torch.FloatTensor] = None,
110
- prompt_embeds: Optional[torch.FloatTensor] = None,
111
- pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
112
- output_type: Optional[str] = "pil",
113
- return_dict: bool = True,
114
- joint_attention_kwargs: Optional[Dict[str, Any]] = None,
115
- max_sequence_length: int = 512,
116
- good_vae: Optional[Any] = None,
117
- ):
118
- height = height or self.default_sample_size * self.vae_scale_factor
119
- width = width or self.default_sample_size * self.vae_scale_factor
120
-
121
- # 1. Check inputs
122
- self.check_inputs(
123
- prompt,
124
- prompt_2,
125
- height,
126
- width,
127
- prompt_embeds=prompt_embeds,
128
- pooled_prompt_embeds=pooled_prompt_embeds,
129
- max_sequence_length=max_sequence_length,
130
- )
131
-
132
- self._guidance_scale = guidance_scale
133
- self._joint_attention_kwargs = joint_attention_kwargs
134
- self._interrupt = False
135
-
136
- # 2. Define call parameters
137
- batch_size = 1 if isinstance(prompt, str) else len(prompt)
138
- device = self._execution_device
139
-
140
- # 3. Encode prompt
141
- lora_scale = joint_attention_kwargs.get("scale", None) if joint_attention_kwargs is not None else None
142
- prompt_embeds, pooled_prompt_embeds, text_ids = self.encode_prompt(
143
- prompt=prompt,
144
- prompt_2=prompt_2,
145
- prompt_embeds=prompt_embeds,
146
- pooled_prompt_embeds=pooled_prompt_embeds,
147
- device=device,
148
- num_images_per_prompt=num_images_per_prompt,
149
- max_sequence_length=max_sequence_length,
150
- lora_scale=lora_scale,
151
- )
152
- # 4. Prepare latent variables
153
- num_channels_latents = self.transformer.config.in_channels // 4
154
- latents, latent_image_ids = self.prepare_latents(
155
- batch_size * num_images_per_prompt,
156
- num_channels_latents,
157
- height,
158
- width,
159
- prompt_embeds.dtype,
160
- device,
161
- generator,
162
- latents,
163
- )
164
- # 5. Prepare timesteps
165
- sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps)
166
- image_seq_len = latents.shape[1]
167
- mu = calculate_shift(
168
- image_seq_len,
169
- self.scheduler.config.base_image_seq_len,
170
- self.scheduler.config.max_image_seq_len,
171
- self.scheduler.config.base_shift,
172
- self.scheduler.config.max_shift,
173
- )
174
- timesteps, num_inference_steps = retrieve_timesteps(
175
- self.scheduler,
176
- num_inference_steps,
177
- device,
178
- timesteps,
179
- sigmas,
180
- mu=mu,
181
- )
182
- self._num_timesteps = len(timesteps)
183
-
184
- # Handle guidance
185
- guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32).expand(latents.shape[0]) if self.transformer.config.guidance_embeds else None
186
-
187
- # 6. Denoising loop
188
- for i, t in enumerate(timesteps):
189
- if self.interrupt:
190
- continue
191
-
192
- timestep = t.expand(latents.shape[0]).to(latents.dtype)
193
-
194
- noise_pred = self.transformer(
195
- hidden_states=latents,
196
- timestep=timestep / 1000,
197
- guidance=guidance,
198
- pooled_projections=pooled_prompt_embeds,
199
- encoder_hidden_states=prompt_embeds,
200
- txt_ids=text_ids,
201
- img_ids=latent_image_ids,
202
- joint_attention_kwargs=self.joint_attention_kwargs,
203
- return_dict=False,
204
- )[0]
205
- # Yield intermediate result
206
- latents_for_image = self._unpack_latents(latents, height, width, self.vae_scale_factor)
207
- latents_for_image = (latents_for_image / self.vae.config.scaling_factor) + self.vae.config.shift_factor
208
- image = self.vae.decode(latents_for_image, return_dict=False)[0]
209
- yield self.image_processor.postprocess(image, output_type=output_type)[0]
210
-
211
- latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
212
- torch.cuda.empty_cache()
213
-
214
- # Final image using good_vae
215
- latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
216
- latents = (latents / good_vae.config.scaling_factor) + good_vae.config.shift_factor
217
- image = good_vae.decode(latents, return_dict=False)[0]
218
- self.maybe_free_model_hooks()
219
- torch.cuda.empty_cache()
220
- yield self.image_processor.postprocess(image, output_type=output_type)[0]
221
-
222
  def log(msg):
223
  print(f'{datetime.now().time()} {msg}')
224
 
@@ -1577,6 +1416,5 @@ if __name__ == "__main__":
1577
  inputs=[cover,top,bottom],
1578
  outputs=[cover]
1579
  )
1580
-
1581
 
1582
  demo.queue().launch()
 
58
  model = T5ForConditionalGeneration.from_pretrained("t5-base")
59
  tokenizer = T5Tokenizer.from_pretrained("t5-base")
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  def log(msg):
62
  print(f'{datetime.now().time()} {msg}')
63
 
 
1416
  inputs=[cover,top,bottom],
1417
  outputs=[cover]
1418
  )
 
1419
 
1420
  demo.queue().launch()