Files
ucloud-comfyui-plugin/py/nodes.py
T
jasonandjason.mei 681f6ebc5f support text generation
* add gitignore

* add video api

* fix: export to video

* fix

* fix

* fix

* fix

* fix

* add i2v

* fix

* fix

* add openai

* fix: add base url

* change default models

* fix: add base url

* add v1

* fix models

* fix models

* fix models

* fix models

* fix models

* fix bug

* fix

* try to fix

* use IO.STRING test

* image fix

* add chat content

* add files

* fix optional

* fix file read

* fix file read

* change name

* add get models

* add unused apikey

* change model sort

* fix IMAGE_LIST

* fix: change to image

* add model cache

* remove json_object

* change max_tokens to 65536

* change max_tokens to 6000

---------

Co-authored-by: jason.mei <jason.mei@ucloud.cn>
2025-09-15 15:31:26 +08:00

136 lines
3.8 KiB
Python

import os
import configparser
from comfy.comfy_types.node_typing import IO
try:
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
config_path = os.path.join(parent_dir, 'config.ini')
config = configparser.ConfigParser()
if not os.path.exists(config_path):
config['API'] = {'MODELVERSE_API_KEY': ''}
with open(config_path, 'w') as config_file:
config.write(config_file)
config.read(config_path)
except Exception as e:
print(f"Error reading or creating config file: {e}")
config = None
class ModelverseAPIClient:
"""
Ucloud Modelverse API Client Node
This node creates a client for connecting to the Ucloud Modelverse API.
"""
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"api_key": ("STRING", {"multiline": False, "default": ""})
},
}
RETURN_TYPES = ("MODELVERSE_API_CLIENT",)
RETURN_NAMES = ("client",)
FUNCTION = "create_client"
CATEGORY = "UCLOUD_MODELVERSE"
def create_client(self, api_key):
"""
Create a UCloud Modelverse API client
Args:
api_key: UCloud Modelverse API key
Returns:
ModelVerseAPI: UCloud Modelverse API client
"""
modelverse_api_key = ""
if api_key == "":
try:
modelverse_api_key = config['API']['MODELVERSE_API_KEY']
if modelverse_api_key == '':
raise ValueError('API_KEY is empty')
except KeyError:
raise ValueError('Unable to find API_KEY in config.ini')
else:
modelverse_api_key = api_key
return ({
"api_key": modelverse_api_key
},)
class ModelverseImagePacker:
"""
Ucloud Modelverse Image Packer
This node packs multiple images into a list for multi-image editing.
Args:
images1: The first image/image_list to be packed together with.
images2: The second image/image_list to be packed together with. et cetera.
Returns:
image_list: the pack of all input images.
"""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"images1": (IO.IMAGE, {"tooltip": "The first image/list to be packed together. Add more if you need."})
},
"optional": {
"images2": (IO.IMAGE, {"default": None, "tooltip": "The second image/list to be packed together."}),
"images3": (IO.IMAGE, {"default": None, "tooltip": "The third image/list to be packed together."}),
"images4": (IO.IMAGE, {"default": None, "tooltip": "The fourth image/list to be packed together."}),
"images5": (IO.IMAGE, {"default": None, "tooltip": "The fifth image/list to be packed together."})
}
}
RETURN_TYPES = ("IMAGE_LIST",)
RETURN_NAMES = ("image_list",)
FUNCTION = "pack_images"
CATEGORY = "UCLOUD_MODELVERSE"
def pack_images(self,
images1,
images2=None,
images3=None,
images4=None,
images5=None
):
# 把单个IMAGE转成单元素list,便于之后统一extend
def to_list(x): return x if isinstance(x, list) else [x]
result = []
for i in (images1, images2, images3, images4, images5):
if i is not None:
result.extend(to_list(i))
return (result,)
NODE_CLASS_MAPPINGS = {
'UCloud ModelVerse Client': ModelverseAPIClient,
'ModelVerse Image Packer': ModelverseImagePacker
}
NODE_DISPLAY_NAME_MAPPINGS = {
'UCloud ModelVerse Client': 'Modelverse Client',
'ModelVerse Image Packer': 'Modelverse Image Packer'
}