Keltezaa commited on
Commit
d683517
·
verified ·
1 Parent(s): 3891a4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -84
app.py CHANGED
@@ -19,70 +19,6 @@ import pandas as pd
19
  # Disable tokenizer parallelism
20
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
21
 
22
- # Summarization Function (Option 5)
23
- def summarize_prompt(input_text, max_length=77):
24
- """
25
- Summarizes the input text to fit within the CLIP token limit.
26
- Basic implementation uses the first `max_length` tokens.
27
- """
28
- summarized_text = " ".join(input_text.split()[:max_length]) # Simple summarization: First 77 words
29
- print(f"Summarized prompt: {summarized_text}")
30
- return summarized_text
31
-
32
- def process_summarized_input(input_text):
33
- """
34
- Prepares summarized text for CLIP processing.
35
- """
36
- summarized_text = summarize_prompt(input_text, max_length=77)
37
- inputs = clip_processor(text=summarized_text, return_tensors="pt", padding=True, truncation=True, max_length=77)
38
- return inputs
39
-
40
- def split_prompt_with_overlap(prompt, chunk_size=77, overlap=10):
41
- tokens = clip_processor.tokenizer(prompt, return_tensors="pt")["input_ids"][0]
42
- chunks = [
43
- tokens[i:max(i + chunk_size, len(tokens))]
44
- for i in range(0, len(tokens), chunk_size - overlap)
45
- ]
46
- return chunks
47
-
48
- def split_prompt(prompt, chunk_size=77):
49
- """Splits a long prompt into chunks of the specified token size."""
50
- tokens = clip_processor.tokenizer(prompt, return_tensors="pt")["input_ids"][0]
51
- chunks = [tokens[i:i + chunk_size] for i in range(0, len(tokens), chunk_size)]
52
- return chunks
53
-
54
- def process_clip_chunks(input_text):
55
- """
56
- Tokenizes and processes a long input text in chunks for CLIP.
57
- Each chunk respects the model's 77-token limit.
58
- """
59
- chunks = split_prompt(input_text)
60
- processed_chunks = []
61
- for chunk in chunks:
62
- chunk_text = clip_processor.tokenizer.decode(chunk, skip_special_tokens=True)
63
- inputs = clip_processor(text=chunk_text, return_tensors="pt", padding=True, truncation=True, max_length=77)
64
- processed_chunks.append(inputs)
65
- return processed_chunks # Return processed chunks for downstream usage
66
-
67
- def preprocess_prompt(input_text, max_clip_tokens=77):
68
- """
69
- Preprocess the input prompt based on its length:
70
- - If the prompt is <= max_clip_tokens, summarize it.
71
- - If the prompt is > max_clip_tokens, split and process it.
72
- """
73
- # Tokenize the prompt to determine its token length
74
- tokens = clip_processor.tokenizer(input_text, return_tensors="pt")["input_ids"][0]
75
- token_count = len(tokens)
76
-
77
- if token_count <= max_clip_tokens:
78
- # Use summarization for shorter prompts
79
- print("Using summarization (Option 5) as the prompt is short.")
80
- return process_summarized_input(input_text)
81
- else:
82
- # Use split-and-process for longer prompts
83
- print("Using chunking (Option 3) as the prompt exceeds 77 tokens.")
84
- return process_clip_chunks(input_text)
85
-
86
  # Initialize the CLIP tokenizer and model
87
  clip_tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch16")
88
  clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
@@ -92,10 +28,6 @@ clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
92
  longformer_tokenizer = LongformerTokenizer.from_pretrained("allenai/longformer-base-4096")
93
  longformer_model = LongformerModel.from_pretrained("allenai/longformer-base-4096")
94
 
95
- # Example usage
96
- input_text = "Your long prompt goes here..."
97
- inputs = preprocess_prompt(input_text)
98
-
99
  # Load prompts for randomization
100
  df = pd.read_csv('prompts.csv', header=None)
101
  prompt_values = df.values.flatten()
@@ -113,26 +45,19 @@ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).
113
  good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype).to(device)
114
  pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1).to(device)
115
 
116
-
117
- # Gradio interface function
118
- taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
119
- good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype).to(device)
120
- pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1).to(device)
121
- pipe_i2i = AutoPipelineForImage2Image.from_pretrained(
122
- base_model,
123
- vae=good_vae,
124
- transformer=pipe.transformer,
125
- text_encoder=pipe.text_encoder,
126
- tokenizer=pipe.tokenizer,
127
- text_encoder_2=pipe.text_encoder_2,
128
- tokenizer_2=pipe.tokenizer_2,
129
- torch_dtype=dtype
130
- )
131
-
132
  MAX_SEED = 2**32 - 1
133
 
134
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
135
 
 
 
 
 
 
 
 
 
 
136
  class calculateDuration:
137
  def __init__(self, activity_name=""):
138
  self.activity_name = activity_name
 
19
  # Disable tokenizer parallelism
20
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Initialize the CLIP tokenizer and model
23
  clip_tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch16")
24
  clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
 
28
  longformer_tokenizer = LongformerTokenizer.from_pretrained("allenai/longformer-base-4096")
29
  longformer_model = LongformerModel.from_pretrained("allenai/longformer-base-4096")
30
 
 
 
 
 
31
  # Load prompts for randomization
32
  df = pd.read_csv('prompts.csv', header=None)
33
  prompt_values = df.values.flatten()
 
45
  good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype).to(device)
46
  pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1).to(device)
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  MAX_SEED = 2**32 - 1
49
 
50
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
51
 
52
+ def process_input(input_text):
53
+ # Tokenize and truncate input
54
+ inputs = clip_processor(text=input_text, return_tensors="pt", padding=True, truncation=True, max_length=77)
55
+ return inputs
56
+
57
+ # Example usage
58
+ input_text = "Your long prompt goes here..."
59
+ inputs = process_input(input_text)
60
+
61
  class calculateDuration:
62
  def __init__(self, activity_name=""):
63
  self.activity_name = activity_name