File size: 8,926 Bytes
86d4cbe |
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";
import { $el } from "../../scripts/ui.js";
import { framerComfyDialog } from "./input-dialog.js";
function getLoaderNodeKeys(workflow) {
const loaderNodeTypes = [
"CheckpointLoader",
"CheckpointLoaderSimple",
"DiffusersLoader",
"unCLIPCheckpointLoader",
"LoraLoader",
"VAELoader",
"ControlNetLoader",
"DiffControlNetLoader",
"UNETLoader",
"CLIPLoader",
"DualCLIPLoader",
"CLIPVisionLoader",
"StyleModelLoader",
"GLIGENLoader",
"UpscaleModelLoader",
"HypernetworkLoader"
];
return Object.entries(workflow).filter(([key, node]) => loaderNodeTypes.includes(node.class_type))
}
function getFilenameFromHuggingFaceLink(url) {
const urlParts = url.split('/');
const repoIndex = urlParts.indexOf('huggingface.co') + 1;
const repoIdParts = urlParts.slice(repoIndex, urlParts.indexOf('resolve'));
const repo_id = repoIdParts.join('/');
const file_name = urlParts[urlParts.length - 1];
return { repo_id, file_name };
}
app.registerExtension({
name: "Comfy.SaveAsScript",
init() {
$el("style", {
parent: document.head,
});
},
async setup() {
async function savePythonScript() {
// Use hardcoded API key for now
var framer_comfy_api_key = "04bc0ebd-f8e5-48b5-92f6-72bb829ff76c"
const loaderNodeSchemas = {
"CheckpointLoader": {
"ckpt_name": "Enter checkpoint url on Huggingface:",
},
"CheckpointLoaderSimple": {
"ckpt_name": "Enter checkpoint url on Huggingface:",
},
"DiffusersLoader": {
"model_path": "Enter model url on Huggingface:",
},
"unCLIPCheckpointLoader": {
"ckpt_name": "Enter checkpoint url on Huggingface:",
},
"LoraLoader": {
"lora_name": "Enter lora url on Huggingface:",
},
"VAELoader": {
"vae_name": "Enter vae url on Huggingface:",
},
"ControlNetLoader": {
"control_net_name": "Enter controlnet url on Huggingface:",
},
"DiffControlNetLoader": {
"control_net_name": "Enter controlnet url on Huggingface:",
},
"UNETLoader": {
"unet_name": "Enter unet url on Huggingface:",
},
"CLIPLoader": {
"clip_name": "Enter clip url on Huggingface:",
},
"DualCLIPLoader": {
"clip_name1": "Enter clip url on Huggingface:",
"clip_name2": "Enter clip url on Huggingface:",
},
"CLIPVisionLoader": {
"clip_name": "Enter clip url on Huggingface:",
},
"StyleModelLoader": {
"style_model_name": "Enter model url on Huggingface:",
},
"GLIGENLoader": {
"gligen_name": "Enter model url on Huggingface:",
},
"UpscaleModelLoader": {
"model_name": "Enter model url on Huggingface:",
},
"HypernetworkLoader": {
"hypernetwork_name": "Enter hypernetwork url on Huggingface:",
}
}
const classtype_to_path = {
"CheckpointLoader": "models/checkpoints",
"CheckpointLoaderSimple": "models/checkpoints",
"DiffusersLoader": "models/diffusers",
"unCLIPCheckpointLoader": "models/checkpoints",
"LoraLoader": "models/loras",
"VAELoader": "models/vae",
"ControlNetLoader": "models/controlnet",
"DiffControlNetLoader": "models/controlnet",
"UNETLoader": "models/unet",
"CLIPLoader": "models/clip",
"DualCLIPLoader": "models/clip",
"CLIPVisionLoader": "models/clip_vision",
"StyleModelLoader": "models/style_models",
"GLIGENLoader": "models/gligen",
"UpscaleModelLoader": "models/upscale_models",
"HypernetworkLoader": "models/hypernetworks"
}
let workflow_models = []
let workflow_name = "ComfyUI-FramerComfy"
let huggingface_access_token = ""
try {
const p = await app.graphToPrompt();
const loader_models = getLoaderNodeKeys(p.output);
// Create input configuration for dialog
const modelInputs = [];
// Collect all required inputs for loader models
for (const [nodeId, nodeData] of loader_models) {
const nodeInputs = loaderNodeSchemas[nodeData.class_type];
for (const [inputName, promptMessage] of Object.entries(nodeInputs)) {
modelInputs.push({
nodeId,
nodeTitle: nodeData._meta.title,
inputName,
promptMessage
});
}
}
// Show the dialog even if we don't have models to get input for
// because we need the workflow name and access token
const dialogResponse = await framerComfyDialog.showModelUrlsDialog(modelInputs);
// Process the dialog results
if (dialogResponse && dialogResponse.workflowInfo) {
// Get workflow information
workflow_name = dialogResponse.workflowInfo.workflow_name;
huggingface_access_token = dialogResponse.workflowInfo.huggingface_access_token;
// Process model results
for (const result of dialogResponse.modelResults) {
const { nodeId, inputName, value } = result;
const nodeData = p.output[nodeId];
// Process the HuggingFace URL
const { repo_id, file_name } = getFilenameFromHuggingFaceLink(value);
const model_local_path = classtype_to_path[nodeData.class_type];
// Update the node inputs and track the model
p.output[nodeId].inputs[inputName] = file_name;
workflow_models.push({ repo_id, file_name, model_local_path });
}
} else {
// User canceled the dialog
return;
}
// Create JSON payload
const json = JSON.stringify({
name: workflow_name,
workflow_models: workflow_models,
framer_comfy_api_key: framer_comfy_api_key,
hf_access_token: huggingface_access_token,
workflow: JSON.stringify(p.output, null, 2)
}, null, 2);
// Send to API
var response = await api.fetchApi(`/saveasscript`, { method: "POST", body: json });
if (response.status == 200) {
const blob = new Blob([await response.text()], { type: "text/python;charset=utf-8" });
const url = URL.createObjectURL(blob);
let filename = workflow_name;
if (!filename.endsWith(".py")) {
filename += ".py";
}
const a = $el("a", {
href: url,
download: filename,
style: { display: "none" },
parent: document.body,
});
a.click();
setTimeout(function () {
a.remove();
window.URL.revokeObjectURL(url);
}, 0);
}
} catch (error) {
console.error("Error in savePythonScript:", error);
alert("Failed to save script: " + error.message);
}
}
const menu = document.querySelector(".comfy-menu");
const separator = document.createElement("hr");
separator.style.margin = "20px 0";
separator.style.width = "100%";
menu.append(separator);
const saveButton = document.createElement("button");
saveButton.textContent = "Save as Script";
saveButton.onclick = () => savePythonScript();
menu.append(saveButton);
// Also load to new style menu
const dropdownMenu = document.querySelectorAll(".p-menubar-submenu ")[0];
// Get submenu items
const listItems = dropdownMenu.querySelectorAll("li");
let newSetsize = listItems.length;
const separatorMenu = document.createElement("li");
separatorMenu.setAttribute("id", "pv_id_8_0_" + (newSetsize - 1).toString());
separatorMenu.setAttribute("class", "p-menubar-separator");
separatorMenu.setAttribute("role", "separator");
separatorMenu.setAttribute("data-pc-section", "separator");
dropdownMenu.append(separatorMenu);
// Adjust list items within to increase setsize
listItems.forEach((item) => {
// First check if it's a separator
if (item.getAttribute("data-pc-section") !== "separator") {
item.setAttribute("aria-setsize", newSetsize);
}
});
console.log(newSetsize);
// Here's the format of list items
const saveButtonText = document.createElement("li");
saveButtonText.setAttribute("id", "pv_id_8_0_" + newSetsize.toString());
saveButtonText.setAttribute("class", "p-menubar-item relative");
saveButtonText.setAttribute("role", "menuitem");
saveButtonText.setAttribute("aria-label", "Deploy to FramerComfy");
saveButtonText.setAttribute("aria-level", "2");
saveButtonText.setAttribute("aria-setsize", newSetsize.toString());
saveButtonText.setAttribute("aria-posinset", newSetsize.toString());
saveButtonText.setAttribute("data-pc-section", "item");
saveButtonText.setAttribute("data-p-active", "false");
saveButtonText.setAttribute("data-p-focused", "false");
saveButtonText.innerHTML = `
<div class="p-menubar-item-content" data-pc-section="itemcontent">
<a class="p-menubar-item-link" tabindex="-1" aria-hidden="true" data-pc-section="itemlink" target="_blank">
<span class="p-menubar-item-icon pi pi-book"></span>
<span class="p-menubar-item-label">Deploy to FramerComfy</span>
</a>
</div>
`
saveButtonText.onclick = () => savePythonScript();
dropdownMenu.append(saveButtonText);
console.log("SaveAsScript loaded");
}
});
|