File size: 2,782 Bytes
a71b31f fc06c9f a71b31f 9dbb8e1 b980f68 9dbb8e1 f15963e 9dbb8e1 f15963e 9dbb8e1 f15963e 9dbb8e1 f15963e 9dbb8e1 72d9282 9dbb8e1 a71b31f 9dbb8e1 b980f68 9dbb8e1 b980f68 9dbb8e1 b980f68 9dbb8e1 b980f68 9dbb8e1 b980f68 9dbb8e1 a71b31f f15963e 7203cfa 9dbb8e1 b980f68 9dbb8e1 b980f68 a71b31f b980f68 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import torch
from safetensors.torch import save_file, load_file
import gradio as gr
import os
def convert_embedding(uploaded_files):
if len(uploaded_files) > 5:
raise ValueError("You can upload a maximum of 5 files at a time.")
output_files = []
for uploaded_file in uploaded_files:
file_name, file_extension = os.path.splitext(os.path.basename(uploaded_file))
output_path = f"{file_name}_XL.safetensors"
if file_extension == '.pt':
sd15_embedding = torch.load(uploaded_file, map_location=torch.device('cpu'))
sd15_tensor = sd15_embedding.get('string_to_param', {}).get('*')
elif file_extension == '.safetensors':
loaded_tensors = load_file(uploaded_file)
sd15_tensor = loaded_tensors.get('emb_params')
else:
raise ValueError(f"Unsupported file format: {file_extension}")
if sd15_tensor is None:
raise ValueError(f"Invalid embedding structure in file: {uploaded_file}")
num_vectors = sd15_tensor.shape[0]
clip_g_shape = (num_vectors, 1280)
clip_l_shape = (num_vectors, 768)
clip_g = torch.zeros(clip_g_shape, dtype=torch.float16)
clip_l = torch.zeros(clip_l_shape, dtype=torch.float16)
clip_l[:sd15_tensor.shape[0], :sd15_tensor.shape[1]] = sd15_tensor.to(dtype=torch.float16)
save_file({"clip_g": clip_g, "clip_l": clip_l}, output_path)
output_files.append(output_path)
return output_files
custom_css = """
body {
background-color: #121212;
color: #ffffff;
font-family: Arial, sans-serif;
}
.gradio-container {
max-width: 800px;
margin: auto;
padding: 20px;
border-radius: 10px;
background: #1e1e1e;
box-shadow: 0 0 10px rgba(255, 102, 0, 0.5);
}
.gradio-container h1 {
text-align: center;
font-size: 24px;
color: #ff6600;
}
.gradio-container button {
background-color: #ff6600;
color: white;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
.gradio-container button:hover {
background-color: #cc5500;
}
"""
iface = gr.Interface(
fn=convert_embedding,
inputs=gr.Files(label="Upload SD-1.5 embeddings (Max 5)", type="filepath"),
outputs=gr.Files(label="Download converted SDXL safetensors embeddings"),
title="✨ SD-1.5 to SDXL Embedding Converter | Now supports multiple files ⚡",
description="Upload up to 5 SD-1.5 embedding files to convert them to SDXL. Stylish and efficient!",
theme="default",
css=custom_css,
live=True
)
iface.launch()
|