Update app.py

#1
by linoyts HF Staff - opened
Files changed (1) hide show
  1. app.py +29 -6
app.py CHANGED
@@ -76,7 +76,8 @@ def encode_prompt(
76
  prompt: str,
77
  enhance_prompt: bool = True,
78
  input_image = None,
79
- seed: int = 42
 
80
  ):
81
  """
82
  Encode a text prompt using Gemma text encoder.
@@ -86,6 +87,7 @@ def encode_prompt(
86
  enhance_prompt: Whether to use AI to enhance the prompt
87
  input_image: Optional image for image-to-video enhancement
88
  seed: Random seed for prompt enhancement
 
89
 
90
  Returns:
91
  tuple: (file_path, enhanced_prompt_text, status_message)
@@ -120,21 +122,35 @@ def encode_prompt(
120
  seed=seed
121
  )
122
 
123
- # Encode the prompt using the pre-loaded text encoder
124
  video_context, audio_context = encode_text_simple(text_encoder, final_prompt)
125
 
 
 
 
 
 
 
126
  # Save embeddings to file
127
  output_dir = Path("embeddings")
128
  output_dir.mkdir(exist_ok=True)
129
  output_path = output_dir / f"embedding_{int(time.time())}.pt"
130
 
131
- # Save both video and audio contexts
132
- torch.save({
133
  'video_context': video_context.cpu(),
134
  'audio_context': audio_context.cpu(),
135
  'prompt': final_prompt,
136
  'original_prompt': prompt if enhance_prompt else final_prompt,
137
- }, output_path)
 
 
 
 
 
 
 
 
138
 
139
  # Get memory stats
140
  elapsed_time = time.time() - start_time
@@ -171,6 +187,13 @@ with gr.Blocks(title="LTX-2 Gemma Text Encoder") as demo:
171
  value="An astronaut hatches from a fragile egg on the surface of the Moon"
172
  )
173
 
 
 
 
 
 
 
 
174
  enhance_checkbox = gr.Checkbox(
175
  label="Enhance Prompt",
176
  value=True,
@@ -204,7 +227,7 @@ with gr.Blocks(title="LTX-2 Gemma Text Encoder") as demo:
204
 
205
  encode_btn.click(
206
  fn=encode_prompt,
207
- inputs=[prompt_input, enhance_checkbox, input_image, enhancement_seed],
208
  outputs=[embedding_file, enhanced_prompt_output, status_output]
209
  )
210
 
 
76
  prompt: str,
77
  enhance_prompt: bool = True,
78
  input_image = None,
79
+ seed: int = 42,
80
+ negative_prompt: str = ""
81
  ):
82
  """
83
  Encode a text prompt using Gemma text encoder.
 
87
  enhance_prompt: Whether to use AI to enhance the prompt
88
  input_image: Optional image for image-to-video enhancement
89
  seed: Random seed for prompt enhancement
90
+ negative_prompt: Optional negative prompt for CFG (two-stage pipeline)
91
 
92
  Returns:
93
  tuple: (file_path, enhanced_prompt_text, status_message)
 
122
  seed=seed
123
  )
124
 
125
+ # Encode the positive prompt using the pre-loaded text encoder
126
  video_context, audio_context = encode_text_simple(text_encoder, final_prompt)
127
 
128
+ # Encode negative prompt if provided
129
+ video_context_negative = None
130
+ audio_context_negative = None
131
+ if negative_prompt:
132
+ video_context_negative, audio_context_negative = encode_text_simple(text_encoder, negative_prompt)
133
+
134
  # Save embeddings to file
135
  output_dir = Path("embeddings")
136
  output_dir.mkdir(exist_ok=True)
137
  output_path = output_dir / f"embedding_{int(time.time())}.pt"
138
 
139
+ # Save embeddings (with negative contexts if provided)
140
+ embedding_data = {
141
  'video_context': video_context.cpu(),
142
  'audio_context': audio_context.cpu(),
143
  'prompt': final_prompt,
144
  'original_prompt': prompt if enhance_prompt else final_prompt,
145
+ }
146
+
147
+ # Add negative contexts if they were encoded
148
+ if video_context_negative is not None:
149
+ embedding_data['video_context_negative'] = video_context_negative.cpu()
150
+ embedding_data['audio_context_negative'] = audio_context_negative.cpu()
151
+ embedding_data['negative_prompt'] = negative_prompt
152
+
153
+ torch.save(embedding_data, output_path)
154
 
155
  # Get memory stats
156
  elapsed_time = time.time() - start_time
 
187
  value="An astronaut hatches from a fragile egg on the surface of the Moon"
188
  )
189
 
190
+ negative_prompt_input = gr.Textbox(
191
+ label="Negative Prompt (Optional)",
192
+ placeholder="Enter negative prompt for CFG (used by two-stage pipeline)...",
193
+ lines=2,
194
+ value=""
195
+ )
196
+
197
  enhance_checkbox = gr.Checkbox(
198
  label="Enhance Prompt",
199
  value=True,
 
227
 
228
  encode_btn.click(
229
  fn=encode_prompt,
230
+ inputs=[prompt_input, enhance_checkbox, input_image, enhancement_seed, negative_prompt_input],
231
  outputs=[embedding_file, enhanced_prompt_output, status_output]
232
  )
233