Skip to content

Commit ce4671b

Browse files
committed
update comfyui.py to use comfystream pipeline
1 parent d7ad557 commit ce4671b

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

runner/app/live/pipelines/comfyui.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import os
22
import json
33
import torch
4-
import asyncio
54
import numpy as np
65
from PIL import Image
76
from typing import Union
87
from pydantic import BaseModel, field_validator
98
import pathlib
109

1110
from .interface import Pipeline
12-
from comfystream.client import ComfyStreamClient
11+
from comfystream.pipeline import Pipeline as ComfyStreamPipeline
1312
from trickle import VideoFrame, VideoOutput
1413

1514
import logging
@@ -52,52 +51,43 @@ def validate_prompt(cls, v) -> dict:
5251
class ComfyUI(Pipeline):
5352
def __init__(self):
5453
comfy_ui_workspace = os.getenv(COMFY_UI_WORKSPACE_ENV)
55-
self.client = ComfyStreamClient(cwd=comfy_ui_workspace)
54+
self.pipeline = ComfyStreamPipeline(width=512, height=512, cwd=comfy_ui_workspace)
5655
self.params: ComfyUIParams
57-
self.video_incoming_frames: asyncio.Queue[VideoOutput] = asyncio.Queue()
5856

5957
async def initialize(self, **params):
6058
new_params = ComfyUIParams(**params)
6159
logging.info(f"Initializing ComfyUI Pipeline with prompt: {new_params.prompt}")
62-
# TODO: currently its a single prompt, but need to support multiple prompts
63-
await self.client.set_prompts([new_params.prompt])
60+
await self.pipeline.set_prompts([new_params.prompt])
6461
self.params = new_params
6562

6663
# Warm up the pipeline
67-
dummy_frame = VideoFrame(None, 0, 0)
68-
dummy_frame.side_data.input = torch.randn(1, 512, 512, 3)
69-
70-
for _ in range(WARMUP_RUNS):
71-
self.client.put_video_input(dummy_frame)
72-
_ = await self.client.get_video_output()
64+
await self.pipeline.warm_video()
7365
logging.info("Pipeline initialization and warmup complete")
7466

7567
async def put_video_frame(self, frame: VideoFrame, request_id: str):
68+
# Convert VideoFrame to format expected by comfystream
7669
image_np = np.array(frame.image.convert("RGB")).astype(np.float32) / 255.0
7770
frame.side_data.input = torch.tensor(image_np).unsqueeze(0)
7871
frame.side_data.skipped = True
79-
self.client.put_video_input(frame)
80-
await self.video_incoming_frames.put(VideoOutput(frame, request_id))
81-
82-
async def get_processed_video_frame(self):
83-
result_tensor = await self.client.get_video_output()
84-
out = await self.video_incoming_frames.get()
85-
while out.frame.side_data.skipped:
86-
out = await self.video_incoming_frames.get()
72+
frame.side_data.request_id = request_id
73+
await self.pipeline.put_video_frame(frame)
8774

75+
async def get_processed_video_frame(self) -> VideoOutput:
76+
processed_frame = await self.pipeline.get_processed_video_frame()
77+
# Convert back to VideoOutput format
78+
result_tensor = processed_frame.side_data.input
8879
result_tensor = result_tensor.squeeze(0)
8980
result_image_np = (result_tensor * 255).byte()
9081
result_image = Image.fromarray(result_image_np.cpu().numpy())
91-
return out.replace_image(result_image)
82+
return VideoOutput(processed_frame, processed_frame.side_data.request_id).replace_image(result_image)
9283

9384
async def update_params(self, **params):
9485
new_params = ComfyUIParams(**params)
9586
logging.info(f"Updating ComfyUI Pipeline Prompt: {new_params.prompt}")
96-
# TODO: currently its a single prompt, but need to support multiple prompts
97-
await self.client.update_prompts([new_params.prompt])
87+
await self.pipeline.update_prompts([new_params.prompt])
9888
self.params = new_params
9989

10090
async def stop(self):
10191
logging.info("Stopping ComfyUI pipeline")
102-
await self.client.stop()
92+
await self.pipeline.cleanup()
10393
logging.info("ComfyUI pipeline stopped")

runner/docker/Dockerfile.live-base-comfyui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ARG BASE_IMAGE=livepeer/comfyui-base@sha256:4435bad85c3a2fce2b491135bee49eedb8edbd8bdf5d124cb0a95a1d4ecb6856
1+
ARG BASE_IMAGE=livepeer/comfyui-base:feat-refactor-package-export
22
FROM ${BASE_IMAGE}
33

44
# -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)