- run_batch(): sequential|concurrent|auto - chain(): 파이프라인 체이닝 - schema(): 스키마 자동 추출 - ResultCache: LRU+TTL 캐시 - preview_callback: 실시간 프리뷰 - upload_file/audio/video() - BridgeInputSEED: random/increment/fixed - MCPServer: MCP Tool 자동 생성
72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
"""ComfyBridgeSync — asyncio가 없는 환경을 위한 동기 래퍼."""
|
|
|
|
import asyncio
|
|
from collections.abc import Callable
|
|
from pathlib import Path
|
|
from typing import Any, Dict, Optional, Union
|
|
|
|
from .bridge import ComfyBridge
|
|
from .result import BridgeResult
|
|
|
|
|
|
class ComfyBridgeSync:
|
|
"""
|
|
ComfyBridge의 동기 래퍼.
|
|
|
|
사용 예시::
|
|
|
|
bridge = ComfyBridgeSync(host="localhost", port=8188)
|
|
result = bridge.run("workflow_api.json", inputs={"prompt": "a cat"})
|
|
image = result.as_pil("output_image")
|
|
image.show()
|
|
"""
|
|
|
|
def __init__(self, host: str = "localhost", port: int = 8188, timeout: float = 300.0):
|
|
self._bridge = ComfyBridge(host=host, port=port, timeout=timeout)
|
|
|
|
def run(
|
|
self,
|
|
workflow: Union[str, dict, Path],
|
|
inputs: Optional[Dict[str, Any]] = None,
|
|
progress_callback: Optional[Callable[[float], None]] = None,
|
|
preview_callback: Optional[Callable[[bytes, int, int], None]] = None,
|
|
) -> BridgeResult:
|
|
return asyncio.run(self._bridge.run(workflow, inputs, progress_callback, preview_callback))
|
|
|
|
def run_batch(
|
|
self,
|
|
workflow: Union[str, dict, Path],
|
|
inputs: list[Dict[str, Any]],
|
|
mode: str = "auto",
|
|
max_concurrent: int = 4,
|
|
) -> list[BridgeResult]:
|
|
return asyncio.run(self._bridge.run_batch(workflow, inputs, mode, max_concurrent))
|
|
|
|
def chain(
|
|
self,
|
|
workflows: list[tuple[Union[str, dict, Path], Dict[str, Any]]],
|
|
progress_callback: Optional[Callable[[float], None]] = None,
|
|
) -> BridgeResult:
|
|
return asyncio.run(self._bridge.chain(workflows, progress_callback))
|
|
|
|
def upload_image(self, image: Union[bytes, str, Path], filename: str = "input.png") -> str:
|
|
return asyncio.run(self._bridge.upload_image(image, filename))
|
|
|
|
def upload_audio(self, audio: Union[bytes, str, Path], filename: str = "input.wav") -> str:
|
|
return asyncio.run(self._bridge.upload_audio(audio, filename))
|
|
|
|
def upload_video(self, video: Union[bytes, str, Path], filename: str = "input.mp4") -> str:
|
|
return asyncio.run(self._bridge.upload_video(video, filename))
|
|
|
|
def upload_file(self, file_path: Union[str, Path], upload_type: str = "input") -> str:
|
|
return asyncio.run(self._bridge.upload_file(file_path, upload_type))
|
|
|
|
def get_schema(self, workflow: Union[str, dict, Path]) -> Dict[str, Any]:
|
|
return self._bridge.get_schema(workflow)
|
|
|
|
def new_cache(self, max_size: int = 100, ttl: float = 3600.0):
|
|
return self._bridge.new_cache(max_size, ttl)
|
|
|
|
def get_queue_status(self) -> Dict:
|
|
return asyncio.run(self._bridge.get_queue_status())
|