2024-08-08 10:30:13 +00:00
|
|
|
import asyncio
|
2024-03-24 00:01:13 +00:00
|
|
|
import json
|
2024-03-31 19:17:29 +00:00
|
|
|
import logging
|
2024-08-27 22:10:27 +00:00
|
|
|
import random
|
|
|
|
import urllib.parse
|
|
|
|
import urllib.request
|
|
|
|
from typing import Optional
|
2024-03-31 19:17:29 +00:00
|
|
|
|
2024-08-27 22:10:27 +00:00
|
|
|
import websocket # NOTE: websocket-client (https://github.com/websocket-client/websocket-client)
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.env import SRC_LOG_LEVELS
|
2024-08-27 22:10:27 +00:00
|
|
|
from pydantic import BaseModel
|
2024-03-31 19:17:29 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["COMFYUI"])
|
2024-03-24 00:01:13 +00:00
|
|
|
|
2024-08-29 20:57:39 +00:00
|
|
|
default_headers = {"User-Agent": "Mozilla/5.0"}
|
|
|
|
|
2024-03-24 00:01:13 +00:00
|
|
|
|
|
|
|
def queue_prompt(prompt, client_id, base_url):
|
2024-03-31 19:17:29 +00:00
|
|
|
log.info("queue_prompt")
|
2024-03-24 00:01:13 +00:00
|
|
|
p = {"prompt": prompt, "client_id": client_id}
|
|
|
|
data = json.dumps(p).encode("utf-8")
|
2024-08-29 20:57:39 +00:00
|
|
|
log.debug(f"queue_prompt data: {data}")
|
|
|
|
try:
|
|
|
|
req = urllib.request.Request(
|
|
|
|
f"{base_url}/prompt", data=data, headers=default_headers
|
|
|
|
)
|
|
|
|
response = urllib.request.urlopen(req).read()
|
|
|
|
return json.loads(response)
|
|
|
|
except Exception as e:
|
|
|
|
log.exception(f"Error while queuing prompt: {e}")
|
|
|
|
raise e
|
2024-03-24 00:01:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_image(filename, subfolder, folder_type, base_url):
|
2024-03-31 19:17:29 +00:00
|
|
|
log.info("get_image")
|
2024-03-24 00:01:13 +00:00
|
|
|
data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
|
|
|
|
url_values = urllib.parse.urlencode(data)
|
2024-08-29 20:57:39 +00:00
|
|
|
req = urllib.request.Request(
|
|
|
|
f"{base_url}/view?{url_values}", headers=default_headers
|
|
|
|
)
|
|
|
|
with urllib.request.urlopen(req) as response:
|
2024-03-24 00:01:13 +00:00
|
|
|
return response.read()
|
|
|
|
|
|
|
|
|
|
|
|
def get_image_url(filename, subfolder, folder_type, base_url):
|
2024-03-31 19:17:29 +00:00
|
|
|
log.info("get_image")
|
2024-03-24 00:01:13 +00:00
|
|
|
data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
|
|
|
|
url_values = urllib.parse.urlencode(data)
|
|
|
|
return f"{base_url}/view?{url_values}"
|
|
|
|
|
|
|
|
|
|
|
|
def get_history(prompt_id, base_url):
|
2024-03-31 19:17:29 +00:00
|
|
|
log.info("get_history")
|
2024-08-29 20:57:39 +00:00
|
|
|
|
|
|
|
req = urllib.request.Request(
|
|
|
|
f"{base_url}/history/{prompt_id}", headers=default_headers
|
|
|
|
)
|
|
|
|
with urllib.request.urlopen(req) as response:
|
2024-03-24 00:01:13 +00:00
|
|
|
return json.loads(response.read())
|
|
|
|
|
|
|
|
|
|
|
|
def get_images(ws, prompt, client_id, base_url):
|
|
|
|
prompt_id = queue_prompt(prompt, client_id, base_url)["prompt_id"]
|
|
|
|
output_images = []
|
|
|
|
while True:
|
|
|
|
out = ws.recv()
|
|
|
|
if isinstance(out, str):
|
|
|
|
message = json.loads(out)
|
|
|
|
if message["type"] == "executing":
|
|
|
|
data = message["data"]
|
|
|
|
if data["node"] is None and data["prompt_id"] == prompt_id:
|
|
|
|
break # Execution is done
|
|
|
|
else:
|
|
|
|
continue # previews are binary data
|
|
|
|
|
|
|
|
history = get_history(prompt_id, base_url)[prompt_id]
|
|
|
|
for o in history["outputs"]:
|
|
|
|
for node_id in history["outputs"]:
|
|
|
|
node_output = history["outputs"][node_id]
|
|
|
|
if "images" in node_output:
|
|
|
|
for image in node_output["images"]:
|
|
|
|
url = get_image_url(
|
|
|
|
image["filename"], image["subfolder"], image["type"], base_url
|
|
|
|
)
|
|
|
|
output_images.append({"url": url})
|
|
|
|
return {"data": output_images}
|
|
|
|
|
|
|
|
|
2024-08-20 16:17:15 +00:00
|
|
|
class ComfyUINodeInput(BaseModel):
|
2024-08-21 12:16:33 +00:00
|
|
|
type: Optional[str] = None
|
2024-08-20 23:39:30 +00:00
|
|
|
node_ids: list[str] = []
|
2024-08-20 16:17:15 +00:00
|
|
|
key: Optional[str] = "text"
|
|
|
|
value: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
class ComfyUIWorkflow(BaseModel):
|
|
|
|
workflow: str
|
|
|
|
nodes: list[ComfyUINodeInput]
|
|
|
|
|
|
|
|
|
|
|
|
class ComfyUIGenerateImageForm(BaseModel):
|
|
|
|
workflow: ComfyUIWorkflow
|
|
|
|
|
2024-03-24 00:01:13 +00:00
|
|
|
prompt: str
|
2024-08-20 16:17:15 +00:00
|
|
|
negative_prompt: Optional[str] = None
|
2024-03-24 00:01:13 +00:00
|
|
|
width: int
|
|
|
|
height: int
|
|
|
|
n: int = 1
|
2024-08-20 16:17:15 +00:00
|
|
|
|
|
|
|
steps: Optional[int] = None
|
|
|
|
seed: Optional[int] = None
|
2024-03-24 00:01:13 +00:00
|
|
|
|
2024-06-16 22:34:15 +00:00
|
|
|
|
2024-08-08 10:30:13 +00:00
|
|
|
async def comfyui_generate_image(
|
2024-08-20 16:17:15 +00:00
|
|
|
model: str, payload: ComfyUIGenerateImageForm, client_id, base_url
|
2024-03-24 00:01:13 +00:00
|
|
|
):
|
2024-04-21 16:19:34 +00:00
|
|
|
ws_url = base_url.replace("http://", "ws://").replace("https://", "wss://")
|
2024-08-20 16:17:15 +00:00
|
|
|
workflow = json.loads(payload.workflow.workflow)
|
|
|
|
|
|
|
|
for node in payload.workflow.nodes:
|
2024-08-21 12:16:33 +00:00
|
|
|
if node.type:
|
|
|
|
if node.type == "model":
|
2024-08-20 23:39:30 +00:00
|
|
|
for node_id in node.node_ids:
|
|
|
|
workflow[node_id]["inputs"][node.key] = model
|
2024-08-21 12:16:33 +00:00
|
|
|
elif node.type == "prompt":
|
2024-08-20 23:39:30 +00:00
|
|
|
for node_id in node.node_ids:
|
2024-10-11 20:11:17 +00:00
|
|
|
workflow[node_id]["inputs"][
|
|
|
|
node.key if node.key else "text"
|
|
|
|
] = payload.prompt
|
2024-08-21 12:16:33 +00:00
|
|
|
elif node.type == "negative_prompt":
|
2024-08-20 23:39:30 +00:00
|
|
|
for node_id in node.node_ids:
|
2024-10-11 20:11:17 +00:00
|
|
|
workflow[node_id]["inputs"][
|
|
|
|
node.key if node.key else "text"
|
|
|
|
] = payload.negative_prompt
|
2024-08-21 12:16:33 +00:00
|
|
|
elif node.type == "width":
|
2024-08-20 23:39:30 +00:00
|
|
|
for node_id in node.node_ids:
|
2024-10-11 20:11:17 +00:00
|
|
|
workflow[node_id]["inputs"][
|
|
|
|
node.key if node.key else "width"
|
|
|
|
] = payload.width
|
2024-08-21 12:16:33 +00:00
|
|
|
elif node.type == "height":
|
2024-08-20 23:39:30 +00:00
|
|
|
for node_id in node.node_ids:
|
2024-10-11 20:11:17 +00:00
|
|
|
workflow[node_id]["inputs"][
|
|
|
|
node.key if node.key else "height"
|
|
|
|
] = payload.height
|
2024-08-21 12:16:33 +00:00
|
|
|
elif node.type == "n":
|
2024-08-20 23:39:30 +00:00
|
|
|
for node_id in node.node_ids:
|
2024-10-11 20:11:17 +00:00
|
|
|
workflow[node_id]["inputs"][
|
|
|
|
node.key if node.key else "batch_size"
|
|
|
|
] = payload.n
|
2024-08-21 12:16:33 +00:00
|
|
|
elif node.type == "steps":
|
2024-08-20 23:39:30 +00:00
|
|
|
for node_id in node.node_ids:
|
2024-10-11 20:11:17 +00:00
|
|
|
workflow[node_id]["inputs"][
|
|
|
|
node.key if node.key else "steps"
|
|
|
|
] = payload.steps
|
2024-08-21 12:16:33 +00:00
|
|
|
elif node.type == "seed":
|
2024-08-20 23:39:30 +00:00
|
|
|
seed = (
|
2024-08-20 16:17:15 +00:00
|
|
|
payload.seed
|
|
|
|
if payload.seed
|
|
|
|
else random.randint(0, 18446744073709551614)
|
|
|
|
)
|
2024-08-20 23:39:30 +00:00
|
|
|
for node_id in node.node_ids:
|
2024-08-21 21:11:12 +00:00
|
|
|
workflow[node_id]["inputs"][node.key] = seed
|
2024-08-20 16:17:15 +00:00
|
|
|
else:
|
2024-08-20 23:39:30 +00:00
|
|
|
for node_id in node.node_ids:
|
|
|
|
workflow[node_id]["inputs"][node.key] = node.value
|
2024-03-24 00:01:13 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
ws = websocket.WebSocket()
|
2024-04-21 16:19:34 +00:00
|
|
|
ws.connect(f"{ws_url}/ws?clientId={client_id}")
|
2024-03-31 19:17:29 +00:00
|
|
|
log.info("WebSocket connection established.")
|
2024-03-24 00:01:13 +00:00
|
|
|
except Exception as e:
|
2024-03-31 19:17:29 +00:00
|
|
|
log.exception(f"Failed to connect to WebSocket server: {e}")
|
2024-03-24 00:01:13 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
2024-08-21 21:22:12 +00:00
|
|
|
log.info("Sending workflow to WebSocket server.")
|
|
|
|
log.info(f"Workflow: {workflow}")
|
2024-08-20 16:17:15 +00:00
|
|
|
images = await asyncio.to_thread(get_images, ws, workflow, client_id, base_url)
|
2024-03-24 00:01:13 +00:00
|
|
|
except Exception as e:
|
2024-03-31 19:17:29 +00:00
|
|
|
log.exception(f"Error while receiving images: {e}")
|
2024-03-24 00:01:13 +00:00
|
|
|
images = None
|
|
|
|
|
|
|
|
ws.close()
|
|
|
|
|
|
|
|
return images
|