Spaces:
Running on Zero
Running on Zero
Port encode to new LTX-2 GemmaTextEncoder API
#2
by multimodalart HF Staff - opened
app.py
CHANGED
|
@@ -61,17 +61,19 @@ model_ledger = ModelLedger(
|
|
| 61 |
gemma_root_path=gemma_local_path,
|
| 62 |
)
|
| 63 |
|
| 64 |
-
# Load text encoder once and keep
|
| 65 |
text_encoder = model_ledger.text_encoder().to("cuda")
|
|
|
|
| 66 |
|
| 67 |
print("=" * 80)
|
| 68 |
print("Text encoder loaded and ready!")
|
| 69 |
print("=" * 80)
|
| 70 |
|
| 71 |
-
def encode_text_simple(
|
| 72 |
-
"""
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
@spaces.GPU()
|
| 77 |
def encode_prompt(
|
|
@@ -125,13 +127,13 @@ def encode_prompt(
|
|
| 125 |
)
|
| 126 |
|
| 127 |
# Encode the positive prompt using the pre-loaded text encoder
|
| 128 |
-
video_context, audio_context = encode_text_simple(
|
| 129 |
|
| 130 |
# Encode negative prompt if provided
|
| 131 |
video_context_negative = None
|
| 132 |
audio_context_negative = None
|
| 133 |
if negative_prompt:
|
| 134 |
-
video_context_negative, audio_context_negative = encode_text_simple(
|
| 135 |
|
| 136 |
# Save embeddings to file
|
| 137 |
output_dir = Path("embeddings")
|
|
@@ -141,7 +143,7 @@ def encode_prompt(
|
|
| 141 |
# Save embeddings (with negative contexts if provided)
|
| 142 |
embedding_data = {
|
| 143 |
'video_context': video_context.cpu(),
|
| 144 |
-
'audio_context': audio_context.cpu(),
|
| 145 |
'prompt': final_prompt,
|
| 146 |
'original_prompt': prompt if enhance_prompt else final_prompt,
|
| 147 |
}
|
|
@@ -149,7 +151,9 @@ def encode_prompt(
|
|
| 149 |
# Add negative contexts if they were encoded
|
| 150 |
if video_context_negative is not None:
|
| 151 |
embedding_data['video_context_negative'] = video_context_negative.cpu()
|
| 152 |
-
embedding_data['audio_context_negative'] =
|
|
|
|
|
|
|
| 153 |
embedding_data['negative_prompt'] = negative_prompt
|
| 154 |
|
| 155 |
torch.save(embedding_data, output_path)
|
|
|
|
| 61 |
gemma_root_path=gemma_local_path,
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# Load text encoder and embeddings processor once and keep them in memory
|
| 65 |
text_encoder = model_ledger.text_encoder().to("cuda")
|
| 66 |
+
embeddings_processor = model_ledger.gemma_embeddings_processor().to("cuda")
|
| 67 |
|
| 68 |
print("=" * 80)
|
| 69 |
print("Text encoder loaded and ready!")
|
| 70 |
print("=" * 80)
|
| 71 |
|
| 72 |
+
def encode_text_simple(prompt: str):
|
| 73 |
+
"""Encode a prompt: Gemma hidden states -> embeddings processor."""
|
| 74 |
+
hidden_states, attention_mask = text_encoder.encode(prompt)
|
| 75 |
+
out = embeddings_processor.process_hidden_states(hidden_states, attention_mask)
|
| 76 |
+
return out.video_encoding, out.audio_encoding
|
| 77 |
|
| 78 |
@spaces.GPU()
|
| 79 |
def encode_prompt(
|
|
|
|
| 127 |
)
|
| 128 |
|
| 129 |
# Encode the positive prompt using the pre-loaded text encoder
|
| 130 |
+
video_context, audio_context = encode_text_simple(final_prompt)
|
| 131 |
|
| 132 |
# Encode negative prompt if provided
|
| 133 |
video_context_negative = None
|
| 134 |
audio_context_negative = None
|
| 135 |
if negative_prompt:
|
| 136 |
+
video_context_negative, audio_context_negative = encode_text_simple(negative_prompt)
|
| 137 |
|
| 138 |
# Save embeddings to file
|
| 139 |
output_dir = Path("embeddings")
|
|
|
|
| 143 |
# Save embeddings (with negative contexts if provided)
|
| 144 |
embedding_data = {
|
| 145 |
'video_context': video_context.cpu(),
|
| 146 |
+
'audio_context': audio_context.cpu() if audio_context is not None else None,
|
| 147 |
'prompt': final_prompt,
|
| 148 |
'original_prompt': prompt if enhance_prompt else final_prompt,
|
| 149 |
}
|
|
|
|
| 151 |
# Add negative contexts if they were encoded
|
| 152 |
if video_context_negative is not None:
|
| 153 |
embedding_data['video_context_negative'] = video_context_negative.cpu()
|
| 154 |
+
embedding_data['audio_context_negative'] = (
|
| 155 |
+
audio_context_negative.cpu() if audio_context_negative is not None else None
|
| 156 |
+
)
|
| 157 |
embedding_data['negative_prompt'] = negative_prompt
|
| 158 |
|
| 159 |
torch.save(embedding_data, output_path)
|