helblazer811 commited on
Commit
4b30dce
·
1 Parent(s): 8fd23f8

app changes

Browse files
app.py CHANGED
@@ -28,12 +28,11 @@ EXAMPLES = [
28
  # ["Portrait of a young woman", "monkey.png", "face, hair, eyes", 456],
29
  ]
30
 
31
-
32
  pipeline = ConceptAttentionFluxPipeline(model_name="flux-schnell", device="cuda")
33
 
34
-
35
  @spaces.GPU(duration=60)
36
  def process_inputs(prompt, input_image, word_list, seed):
 
37
  prompt = prompt.strip()
38
  if not word_list.strip():
39
  return None, "Please enter comma-separated words"
 
28
  # ["Portrait of a young woman", "monkey.png", "face, hair, eyes", 456],
29
  ]
30
 
 
31
  pipeline = ConceptAttentionFluxPipeline(model_name="flux-schnell", device="cuda")
32
 
 
33
  @spaces.GPU(duration=60)
34
  def process_inputs(prompt, input_image, word_list, seed):
35
+ print("Processing inputs")
36
  prompt = prompt.strip()
37
  if not word_list.strip():
38
  return None, "Please enter comma-separated words"
concept_attention/binary_segmentation_baselines/__pycache__/raw_cross_attention.cpython-310.pyc CHANGED
Binary files a/concept_attention/binary_segmentation_baselines/__pycache__/raw_cross_attention.cpython-310.pyc and b/concept_attention/binary_segmentation_baselines/__pycache__/raw_cross_attention.cpython-310.pyc differ
 
concept_attention/binary_segmentation_baselines/__pycache__/raw_output_space.cpython-310.pyc CHANGED
Binary files a/concept_attention/binary_segmentation_baselines/__pycache__/raw_output_space.cpython-310.pyc and b/concept_attention/binary_segmentation_baselines/__pycache__/raw_output_space.cpython-310.pyc differ
 
concept_attention/flux/src/flux/util.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  from dataclasses import dataclass
3
 
4
  import torch
 
5
  from einops import rearrange
6
  from huggingface_hub import hf_hub_download
7
  from imwatermark import WatermarkEncoder
@@ -126,6 +127,34 @@ def load_flow_model(name: str, device: str | torch.device = "cuda", hf_download:
126
 
127
  return model
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  def load_t5(device: str | torch.device = "cuda", max_length: int = 512) -> HFEmbedder:
130
  # Download each of the files
131
  config_file = hf_hub_download(configs["flux-schnell"].repo_id, "text_encoder_2/config.json") # File 1: config.json
@@ -139,13 +168,20 @@ def load_t5(device: str | torch.device = "cuda", max_length: int = 512) -> HFEmb
139
  state_dict.update(load_sft(safe_tensor_1, device=str(device)))
140
  state_dict.update(load_sft(safe_tensor_2, device=str(device)))
141
  # Load the state dict
142
- t5_encoder = T5EncoderModel(config=model_config)
143
  t5_encoder.load_state_dict(state_dict, strict=False)
144
 
145
  # Load the tokenizer
146
  tokenizer = T5Tokenizer.from_pretrained("google/t5-v1_1-xxl")
147
  t5_encoder.tokenizer = tokenizer
148
 
 
 
 
 
 
 
 
149
  # max length 64, 128, 256 and 512 should work (if your sequence is short enough)
150
  # Load the safe tensors model
151
  # ckpt_path = hf_hub_download(configs["name"].repo_id, configs["name"].repo_flow)
@@ -156,8 +192,8 @@ def load_t5(device: str | torch.device = "cuda", max_length: int = 512) -> HFEmb
156
  # max_length=max_length,
157
  # torch_dtype=torch.bfloat16,
158
  # ).to(device)
159
-
160
- return t5_encoder
161
  # return HFEmbedder("google/t5-v1_1-xxl", max_length=max_length, torch_dtype=torch.bfloat16).to(device)
162
 
163
  def load_clip(device: str | torch.device = "cuda") -> HFEmbedder:
 
2
  from dataclasses import dataclass
3
 
4
  import torch
5
+ from torch import nn
6
  from einops import rearrange
7
  from huggingface_hub import hf_hub_download
8
  from imwatermark import WatermarkEncoder
 
127
 
128
  return model
129
 
130
+
131
+ class T5Embedder(nn.Module):
132
+ def __init__(self, hf_module, tokenizer, max_length=512, output_key="last_hidden_state"):
133
+ super().__init__()
134
+ self.max_length = max_length
135
+ self.output_key = output_key
136
+ self.hf_module = hf_module
137
+ self.tokenizer = tokenizer
138
+
139
+ def forward(self, text: list[str]) -> torch.Tensor:
140
+ batch_encoding = self.tokenizer(
141
+ text,
142
+ truncation=True,
143
+ max_length=self.max_length,
144
+ return_length=False,
145
+ return_overflowing_tokens=False,
146
+ padding="max_length",
147
+ return_tensors="pt",
148
+ )
149
+
150
+ outputs = self.hf_module(
151
+ input_ids=batch_encoding["input_ids"].to(self.hf_module.device),
152
+ attention_mask=None,
153
+ output_hidden_states=False,
154
+ )
155
+ return outputs[self.output_key]
156
+
157
+
158
  def load_t5(device: str | torch.device = "cuda", max_length: int = 512) -> HFEmbedder:
159
  # Download each of the files
160
  config_file = hf_hub_download(configs["flux-schnell"].repo_id, "text_encoder_2/config.json") # File 1: config.json
 
168
  state_dict.update(load_sft(safe_tensor_1, device=str(device)))
169
  state_dict.update(load_sft(safe_tensor_2, device=str(device)))
170
  # Load the state dict
171
+ t5_encoder = T5EncoderModel(config=model_config).to(torch.bfloat16)
172
  t5_encoder.load_state_dict(state_dict, strict=False)
173
 
174
  # Load the tokenizer
175
  tokenizer = T5Tokenizer.from_pretrained("google/t5-v1_1-xxl")
176
  t5_encoder.tokenizer = tokenizer
177
 
178
+ # Now make t5 a custom model that tokenizes the input and then passes it through the model
179
+ return T5Embedder(
180
+ t5_encoder,
181
+ tokenizer,
182
+ max_length=max_length,
183
+ output_key="last_hidden_state"
184
+ ).to(device)
185
  # max length 64, 128, 256 and 512 should work (if your sequence is short enough)
186
  # Load the safe tensors model
187
  # ckpt_path = hf_hub_download(configs["name"].repo_id, configs["name"].repo_flow)
 
192
  # max_length=max_length,
193
  # torch_dtype=torch.bfloat16,
194
  # ).to(device)
195
+ #
196
+ # return t5_encoder
197
  # return HFEmbedder("google/t5-v1_1-xxl", max_length=max_length, torch_dtype=torch.bfloat16).to(device)
198
 
199
  def load_clip(device: str | torch.device = "cuda") -> HFEmbedder: