Skip to content

Commit

Permalink
Add ComfyUI support and refactor Create (#45, #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuettlerTNG committed Nov 21, 2024
1 parent 98c55b2 commit d4f0cc9
Show file tree
Hide file tree
Showing 23 changed files with 2,035 additions and 549 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ installer.nsh
*.exe
*.7z
*.whl
ComfyUI/
1 change: 1 addition & 0 deletions WebUI/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dist/
dist-ssr
*.local
release/
ComfyUI/

# Editor directories and files
.vscode/*
Expand Down
56 changes: 51 additions & 5 deletions WebUI/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ const settings: LocalSettings = {
currentTheme:"lnl"
};

const comfyuiState = {
currentVersion: null,
port: 0,
}

let webContentsFinishedLoad = false;
const startupMessageCache: {message: string, source: 'electron-backend' | 'ai-backend', level: 'error' | 'info'}[] = []
const startupMessageCache: {message: string, source: 'electron-backend' | 'ai-backend' | 'comfyui-backend', level: 'error' | 'info'}[] = []

const logger = {
info: (message: string, source: 'electron-backend' | 'ai-backend' = 'electron-backend') => {
info: (message: string, source: 'electron-backend' | 'ai-backend' | 'comfyui-backend' = 'electron-backend') => {
console.info(`[${source}]: ${message}`);
if (webContentsFinishedLoad) {
try {
Expand All @@ -90,7 +95,7 @@ const logger = {
startupMessageCache.push({ level: 'info', source, message })
}
},
error: (message: string, source: 'electron-backend' | 'ai-backend' = 'electron-backend') => {
error: (message: string, source: 'electron-backend' | 'ai-backend' | 'comfyui-backend' = 'electron-backend') => {
console.error(`[${source}]: ${message}`);
if (webContentsFinishedLoad) {
try {
Expand Down Expand Up @@ -120,6 +125,7 @@ async function loadSettings() {
});
}
settings.port = await getPort({ port: portNumbers(59000, 59999) });
comfyuiState.port = await getPort({ port: portNumbers(59000, 59999) });
settings.apiHost = `http://127.0.0.1:${settings.port}`;
}

Expand All @@ -131,8 +137,10 @@ async function createWindow() {
resizable: true,
frame: false,
// fullscreen: true,
width: 1440,
height: 951,
x: 2800,
y: 600,
width: 2440,
height: 1400,
webPreferences: {
preload: path.join(__dirname, "../preload/preload.js"),
contextIsolation: true
Expand Down Expand Up @@ -475,6 +483,25 @@ function initEventHandle() {
win?.webContents.openDevTools({ mode: "detach", activate: true });
});

ipcMain.handle("getComfyuiState", () => {
return comfyuiState;
});

ipcMain.handle("updateComfyui", () => {
return;
});

ipcMain.handle("reloadImageWorkflows", () => {
const files = fs.readdirSync(path.join(externalRes, "workflows"));
const workflows = files.map((file) => fs.readFileSync(path.join(externalRes, "workflows", file), { encoding: "utf-8" }));
return workflows;
});

ipcMain.handle("startComfyui", () => {
console.log('startComfyui')
return;
});

ipcMain.on("openImageWithSystem", (event, url: string) => {
// Assuming 'settings' and 'externalRes' are properly defined
let imagePath = url.replace(settings.apiHost + "/", ""); // Remove the API host part
Expand Down Expand Up @@ -537,6 +564,7 @@ function isProcessRunning(pid: number) {

function wakeupApiService() {
const wordkDir = path.resolve(app.isPackaged ? path.join(process.resourcesPath, "service") : path.join(__dirname, "../../../service"));
const comfyWordkDir = path.resolve(app.isPackaged ? path.join(process.resourcesPath, "ComfyUI") : path.join(__dirname, "../../../ComfyUI"));
const baseDir = app.isPackaged ? process.resourcesPath : path.join(__dirname, "../../../");
const pythonExe = path.resolve(path.join(baseDir, "env/python.exe"));
const additionalEnvVariables = {
Expand All @@ -546,6 +574,7 @@ function wakeupApiService() {
};

spawnAPI(pythonExe, wordkDir, additionalEnvVariables);
spawnComfy(pythonExe, comfyWordkDir, additionalEnvVariables);
}

function spawnAPI(pythonExe: string, wordkDir: string, additionalEnvVariables: Record<string, string>, tries = 0) {
Expand Down Expand Up @@ -595,6 +624,23 @@ function spawnAPI(pythonExe: string, wordkDir: string, additionalEnvVariables: R
})
}

function spawnComfy(pythonExe: string, wordkDir: string, additionalEnvVariables: Record<string, string>, tries = 0) {
logger.info(`#1 try to start ComfyUI API`)

const webProcess = spawn(pythonExe, ["main.py", "--port", comfyuiState.port.toString(), "--preview-method", "auto", "--bf16-unet", "--lowvram"], {
cwd: wordkDir,
windowsHide: true,
env: Object.assign(process.env, additionalEnvVariables)
});

webProcess.stdout.on('data', (message) => {
logger.info(`${message}`, 'comfyui-backend')
})
webProcess.stderr.on('data', (message) => {
logger.info(`${message}`, 'comfyui-backend')
})
}


function closeApiService() {
apiService.normalExit = true;
Expand Down
4 changes: 4 additions & 0 deletions WebUI/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ contextBridge.exposeInMainWorld("envVars", {
productVersion: pkg.version,
});
contextBridge.exposeInMainWorld("electronAPI", {
getComfyuiState: () => ipcRenderer.invoke("getComfyuiState"),
updateComfyui: () => ipcRenderer.invoke("updateComfyui"),
startComfyui: () => ipcRenderer.invoke("startComfyui"),
reloadImageWorkflows: () => ipcRenderer.invoke("reloadImageWorkflows"),
openDevTools: () => ipcRenderer.send("openDevTools"),
openUrl: (url: string) => ipcRenderer.send("openUrl", url),
getLocalSettings: () => ipcRenderer.invoke("getLocalSettings"),
Expand Down
219 changes: 219 additions & 0 deletions WebUI/external/workflows/flux.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{
"name": "Flux.1 schnell",
"backend": "comfyui",
"tags": [
"flux",
"comfyui",
"high-vram"
],
"requirements": [
"high-vram"
],
"inputs": [],
"outputs": [
{
"name": "output_image",
"type": "image"
}
],
"defaultSettings": {
"width": 1024,
"height": 1024,
"inferenceSteps": 4
},
"displayedSettings": [
"inferenceSteps",
"imageModel"
],
"modifiableSettings": [
"seed",
"batchSize",
"imagePreview",
"resolution"
],
"comfyUiApiWorkflow": {
"167": {
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"179",
0
]
},
"class_type": "SaveImage",
"_meta": {
"title": "Save Image"
}
},
"169": {
"inputs": {
"noise": [
"184",
0
],
"guider": [
"178",
0
],
"sampler": [
"185",
0
],
"sigmas": [
"181",
0
],
"latent_image": [
"180",
0
]
},
"class_type": "SamplerCustomAdvanced",
"_meta": {
"title": "SamplerCustomAdvanced"
}
},
"170": {
"inputs": {
"unet_name": "flux1-schnell-Q2_K.gguf"
},
"class_type": "UnetLoaderGGUF",
"_meta": {
"title": "Unet Loader (GGUF)"
}
},
"171": {
"inputs": {
"vae_name": "diffusion_pytorch_model.safetensors"
},
"class_type": "VAELoader",
"_meta": {
"title": "Load VAE"
}
},
"174": {
"inputs": {
"guidance": 1,
"conditioning": [
"177",
0
]
},
"class_type": "FluxGuidance",
"_meta": {
"title": "FluxGuidance"
}
},
"175": {
"inputs": {
"clip_name1": "t5xxl_fp8_e4m3fn.safetensors",
"clip_name2": "clip_l.safetensors",
"type": "flux"
},
"class_type": "DualCLIPLoader",
"_meta": {
"title": "DualCLIPLoader"
}
},
"177": {
"inputs": {
"text": "A cool llama wearing a pair of sunglasses, holding a blue and purple neon sign that says \"Lunar Lake\" in front, vibrant colors, blurry cyberpunk gaming background.",
"clip": [
"188",
0
]
},
"class_type": "CLIPTextEncode",
"_meta": {
"title": "prompt"
}
},
"178": {
"inputs": {
"model": [
"170",
0
],
"conditioning": [
"174",
0
]
},
"class_type": "BasicGuider",
"_meta": {
"title": "BasicGuider"
}
},
"179": {
"inputs": {
"samples": [
"169",
1
],
"vae": [
"171",
0
]
},
"class_type": "VAEDecode",
"_meta": {
"title": "VAE Decode"
}
},
"180": {
"inputs": {
"width": 768,
"height": 768,
"batch_size": 1
},
"class_type": "EmptyLatentImage",
"_meta": {
"title": "Empty Latent Image"
}
},
"181": {
"inputs": {
"scheduler": "normal",
"steps": 4,
"denoise": 1,
"model": [
"170",
0
]
},
"class_type": "BasicScheduler",
"_meta": {
"title": "BasicScheduler"
}
},
"184": {
"inputs": {
"noise_seed": 508274201813129
},
"class_type": "RandomNoise",
"_meta": {
"title": "RandomNoise"
}
},
"185": {
"inputs": {
"sampler_name": "euler"
},
"class_type": "KSamplerSelect",
"_meta": {
"title": "KSamplerSelect"
}
},
"188": {
"inputs": {
"clip_name1": "t5-v1_1-xxl-encoder-Q3_K_M.gguf",
"clip_name2": "clip_l.safetensors",
"type": "flux"
},
"class_type": "DualCLIPLoaderGGUF",
"_meta": {
"title": "DualCLIPLoader (GGUF)"
}
}
}
}
Loading

0 comments on commit d4f0cc9

Please sign in to comment.