Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
2242fb6
1
Parent(s):
37f4150
fix tensor mismatch
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import torch
|
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
from transformers import T5EncoderModel
|
| 6 |
import tempfile
|
|
|
|
| 7 |
|
| 8 |
# Global variable to store the text pipeline
|
| 9 |
text_pipe = None
|
|
@@ -15,7 +16,6 @@ def load_model():
|
|
| 15 |
print("Loading T5 text encoder...")
|
| 16 |
|
| 17 |
# Get token from environment
|
| 18 |
-
import os
|
| 19 |
token = os.getenv("HF_TOKEN")
|
| 20 |
|
| 21 |
text_encoder = T5EncoderModel.from_pretrained(
|
|
@@ -24,13 +24,14 @@ def load_model():
|
|
| 24 |
load_in_8bit=True,
|
| 25 |
variant="8bit",
|
| 26 |
device_map="auto",
|
| 27 |
-
token=token
|
| 28 |
)
|
| 29 |
text_pipe = DiffusionPipeline.from_pretrained(
|
| 30 |
"DeepFloyd/IF-I-L-v1.0",
|
| 31 |
text_encoder=text_encoder,
|
| 32 |
unet=None,
|
| 33 |
-
token=token
|
|
|
|
| 34 |
)
|
| 35 |
print("Model loaded successfully!")
|
| 36 |
return text_pipe
|
|
@@ -48,6 +49,12 @@ def generate_embeddings(prompts_text):
|
|
| 48 |
# Load model if not already loaded
|
| 49 |
pipe = load_model()
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Parse prompts (one per line)
|
| 52 |
prompts = [p.strip() for p in prompts_text.strip().split('\n') if p.strip()]
|
| 53 |
|
|
@@ -68,8 +75,11 @@ def generate_embeddings(prompts_text):
|
|
| 68 |
# Extract positive prompt embeddings
|
| 69 |
prompt_embeds, negative_prompt_embeds = zip(*prompt_embeds_list)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
# Create dictionary
|
| 72 |
-
prompt_embeds_dict = dict(zip(prompts,
|
| 73 |
|
| 74 |
# Save to temporary file
|
| 75 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.pth')
|
|
@@ -83,7 +93,9 @@ def generate_embeddings(prompts_text):
|
|
| 83 |
return temp_file.name, status_msg
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Create Gradio interface
|
| 89 |
with gr.Blocks(title="T5 Text Encoder - Embeddings Generator") as demo:
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
from transformers import T5EncoderModel
|
| 6 |
import tempfile
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
# Global variable to store the text pipeline
|
| 10 |
text_pipe = None
|
|
|
|
| 16 |
print("Loading T5 text encoder...")
|
| 17 |
|
| 18 |
# Get token from environment
|
|
|
|
| 19 |
token = os.getenv("HF_TOKEN")
|
| 20 |
|
| 21 |
text_encoder = T5EncoderModel.from_pretrained(
|
|
|
|
| 24 |
load_in_8bit=True,
|
| 25 |
variant="8bit",
|
| 26 |
device_map="auto",
|
| 27 |
+
token=token
|
| 28 |
)
|
| 29 |
text_pipe = DiffusionPipeline.from_pretrained(
|
| 30 |
"DeepFloyd/IF-I-L-v1.0",
|
| 31 |
text_encoder=text_encoder,
|
| 32 |
unet=None,
|
| 33 |
+
token=token,
|
| 34 |
+
device_map="auto" # Add this
|
| 35 |
)
|
| 36 |
print("Model loaded successfully!")
|
| 37 |
return text_pipe
|
|
|
|
| 49 |
# Load model if not already loaded
|
| 50 |
pipe = load_model()
|
| 51 |
|
| 52 |
+
# Move pipeline to CUDA if available
|
| 53 |
+
if torch.cuda.is_available():
|
| 54 |
+
device = torch.device("cuda")
|
| 55 |
+
if hasattr(pipe, 'text_encoder') and pipe.text_encoder is not None:
|
| 56 |
+
pipe.text_encoder = pipe.text_encoder.to(device)
|
| 57 |
+
|
| 58 |
# Parse prompts (one per line)
|
| 59 |
prompts = [p.strip() for p in prompts_text.strip().split('\n') if p.strip()]
|
| 60 |
|
|
|
|
| 75 |
# Extract positive prompt embeddings
|
| 76 |
prompt_embeds, negative_prompt_embeds = zip(*prompt_embeds_list)
|
| 77 |
|
| 78 |
+
# Move embeddings to CPU before saving
|
| 79 |
+
prompt_embeds_cpu = [emb.cpu() if isinstance(emb, torch.Tensor) else emb for emb in prompt_embeds]
|
| 80 |
+
|
| 81 |
# Create dictionary
|
| 82 |
+
prompt_embeds_dict = dict(zip(prompts, prompt_embeds_cpu))
|
| 83 |
|
| 84 |
# Save to temporary file
|
| 85 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.pth')
|
|
|
|
| 93 |
return temp_file.name, status_msg
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
+
import traceback
|
| 97 |
+
error_details = traceback.format_exc()
|
| 98 |
+
return None, f"❌ Error: {str(e)}\n\nDetails:\n{error_details}"
|
| 99 |
|
| 100 |
# Create Gradio interface
|
| 101 |
with gr.Blocks(title="T5 Text Encoder - Embeddings Generator") as demo:
|