Files
kadirnar e21711a049 Refactor computer vision and NLP pipelines to simplify model selection and enhance functionality
- Updated model selection in image-to-image, mask generation, object detection, video classification, zero-shot image classification, zero-shot object detection, document question answering, feature extraction, image-to-text, image-text-to-text, visual question answering, conversational, fill mask, question answering, sentence similarity, table question answering, zero-shot classification, text classification, text generation, and token classification pipelines to use a default string instead of a model list.
- Removed unused keypoint detection, summarization, text2text generation, and translation pipelines.
- Added utility nodes for type conversion: IntToString, StringToInt, FloatToString, and StringToFloat.
- Updated all pipeline calls to include trust_remote_code=True for enhanced security.
- Improved requirements by adding timm and opencv-python for better functionality.
2026-02-08 18:26:10 +00:00

37 lines
1.1 KiB
Python

import numpy as np
import torch
from PIL import Image
from transformers import pipeline as hf_pipeline
import json
_pipeline_cache = {}
def get_pipeline(task, model_name, **kwargs):
"""Cached pipeline loader - avoids reloading models on every call."""
key = (task, model_name, frozenset(kwargs.items()))
if key not in _pipeline_cache:
_pipeline_cache[key] = hf_pipeline(task, model=model_name, trust_remote_code=True, **kwargs)
return _pipeline_cache[key]
def comfyui_to_pil(image_tensor):
"""ComfyUI IMAGE tensor (B,H,W,C) [0,1] -> PIL Image (first frame)."""
img = 255.0 * image_tensor[0].cpu().numpy()
return Image.fromarray(np.clip(img, 0, 255).astype(np.uint8))
def pil_to_comfyui(pil_image):
"""PIL Image -> ComfyUI IMAGE tensor (1,H,W,C) [0,1]."""
arr = np.array(pil_image).astype(np.float32) / 255.0
if len(arr.shape) == 2:
arr = np.stack([arr, arr, arr], axis=-1)
elif arr.shape[-1] == 4:
arr = arr[:, :, :3]
return torch.from_numpy(arr)[None,]
def format_output(data):
"""Convert pipeline output to formatted JSON string."""
return json.dumps(data, indent=2, default=str)