Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def convert_embedding(sd15_embedding):
|
| 7 |
+
# Temporary file paths
|
| 8 |
+
input_path = "temp_input.pt"
|
| 9 |
+
output_path = "temp_output.safetensors"
|
| 10 |
+
|
| 11 |
+
# Save uploaded file to disk to be processed
|
| 12 |
+
with open(input_path, "wb") as f:
|
| 13 |
+
f.write(sd15_embedding.read())
|
| 14 |
+
|
| 15 |
+
# Your existing conversion logic
|
| 16 |
+
sd15_embedding = torch.load(input_path, map_location=torch.device('cpu'))
|
| 17 |
+
sd15_tensor = sd15_embedding['string_to_param']['*']
|
| 18 |
+
num_vectors = sd15_tensor.shape[0]
|
| 19 |
+
clip_g_shape = (num_vectors, 1280)
|
| 20 |
+
clip_l_shape = (num_vectors, 768)
|
| 21 |
+
clip_g = torch.zeros(clip_g_shape, dtype=torch.float16)
|
| 22 |
+
clip_l = torch.zeros(clip_l_shape, dtype=torch.float16)
|
| 23 |
+
clip_l[:sd15_tensor.shape[0], :sd15_tensor.shape[1]] = sd15_tensor.to(dtype=torch.float16)
|
| 24 |
+
save_file({"clip_g": clip_g, "clip_l": clip_l}, output_path)
|
| 25 |
+
|
| 26 |
+
# Remove the temporary input file
|
| 27 |
+
os.remove(input_path)
|
| 28 |
+
|
| 29 |
+
# Return the path to the converted file for download
|
| 30 |
+
return output_path
|
| 31 |
+
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=convert_embedding,
|
| 34 |
+
inputs=gr.inputs.File(label="Upload SD1.5 Embedding"),
|
| 35 |
+
outputs=gr.outputs.File(label="Download Converted SDXL Embedding"),
|
| 36 |
+
title="SD1.5 to SDXL Embedding Converter",
|
| 37 |
+
description="Upload an SD1.5 embedding file to convert it to SDXL format."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
iface.launch()
|