Files
Runware-ComfyUI-Runware/modules/upscaler.py
T

291 lines
13 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from .utils import runwareUtils as rwUtils
class upscaler:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"Image": ("IMAGE", {
"tooltip": "Specifies the input image to be upscaled."
}),
"useUpscaleFactor": ("BOOLEAN", {
"tooltip": "Include upscaleFactor in the upscale request.",
"default": True,
"label_on": "Enabled",
"label_off": "Disabled",
}),
"upscaleFactor": ("INT", {
"tooltip": "Each level will increase the size of the image by the corresponding factor. Only used when 'Use Upscale Factor' is enabled.",
"default": 2,
"min": 1,
"max": 8,
"step": 1,
}),
"useTargetMegapixels": ("BOOLEAN", {
"tooltip": "Enable targetMegapixels (root-level) on the upscale request.",
"default": False,
"label_on": "Enabled",
"label_off": "Disabled",
}),
"targetMegapixels": ("INT", {
"tooltip": "Target resolution in megapixels (1–8). Only used when 'Use Target Megapixels' is enabled.",
"default": 4,
"min": 1,
"max": 8,
"step": 1,
}),
},
"optional": {
"model": ("STRING", {
"tooltip": "Model AIR ID (runware:500@1, runware:501@1, runware:502@1, runware:503@1, runware:504@1, bria:52@1)",
"default": "",
}),
# Common parameters toggles
"useSteps": ("BOOLEAN", {
"tooltip": "Enable quality steps parameter",
"default": False,
}),
"steps": ("INT", {
"tooltip": "Quality steps (4-60 depending on model)",
"default": 20,
"min": 1,
"max": 100,
}),
"useSeed": ("BOOLEAN", {
"tooltip": "Enable seed for reproducibility",
"default": False,
}),
"seed": ("INT", {
"tooltip": "Reproducibility toggle",
"default": 0,
"min": 0,
}),
"useCFGScale": ("BOOLEAN", {
"tooltip": "Enable CFG Scale parameter",
"default": False,
}),
"CFGScale": ("FLOAT", {
"tooltip": "Guidance CFG (3-20 depending on model)",
"default": 7.0,
"min": 1.0,
"max": 30.0,
"step": 0.1,
}),
"usePrompts": ("BOOLEAN", {
"tooltip": "Enable positive and negative prompts",
"default": False,
}),
"positivePrompt": ("STRING", {
"tooltip": "Positive prompt for upscaling",
"multiline": True,
"default": "",
}),
"negativePrompt": ("STRING", {
"tooltip": "Negative prompt for upscaling",
"multiline": True,
"default": "",
}),
# Clarity upscaler specific toggles
"useClarityParams": ("BOOLEAN", {
"tooltip": "Enable Clarity upscaler specific parameters",
"default": False,
}),
"controlNetWeight": ("FLOAT", {
"tooltip": "Style preservation/Resemblance (0-1) - Clarity upscaler specific",
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01,
}),
"strength": ("FLOAT", {
"tooltip": "Creativity (0-1) - Clarity upscaler specific",
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01,
}),
"scheduler": ("STRING", {
"tooltip": "Controls noise addition/removal - Clarity upscaler specific",
"default": "DPM++ 2M Karras",
}),
# CCSR and Latent upscaler specific toggles
"useCCSRParams": ("BOOLEAN", {
"tooltip": "Enable CCSR and Latent upscaler specific parameters",
"default": False,
}),
"colorFix": ("BOOLEAN", {
"tooltip": "Color correction (ADAIN/NOFIX) - CCSR and Latent upscaler specific",
"default": False,
}),
"tileDiffusion": ("BOOLEAN", {
"tooltip": "Tile diffusion for large images - CCSR and Latent upscaler specific",
"default": False,
}),
# Latent upscaler specific toggle
"useLatentParams": ("BOOLEAN", {
"tooltip": "Enable Latent upscaler specific parameters",
"default": False,
}),
"clipSkip": ("INT", {
"tooltip": "Skip CLIP layers during guidance (0-2) - Latent upscaler specific",
"default": 0,
"min": 0,
"max": 2,
}),
"safetyInputs": ("RUNWARESAFETYINPUTS", {
"tooltip": "Connect Runware Safety Inputs node to include safety settings in the upscale request.",
}),
"Accelerator": ("RUNWAREACCELERATOR", {
"tooltip": "Connect a Runware Accelerator Options Node to configure caching and acceleration settings.",
}),
"providerSettings": ("RUNWAREPROVIDERSETTINGS", {
"tooltip": "Connect a Runware Provider Settings node to configure provider-specific parameters.",
}),
"imageUpscalerSettings": ("RUNWAREIMAGEUPSCALERSETTINGS", {
"tooltip": "Connect Runware Image Upscaler Settings for settings.enhanceDetails and settings.realism.",
}),
},
}
DESCRIPTION = "Enhance the resolution and quality of your images using Runware's advanced upscaling API. Transform low-resolution images into sharp, high-definition visuals."
FUNCTION = "upscale"
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("IMAGE",)
CATEGORY = "Runware"
def upscale(self, **kwargs):
image = kwargs.get("Image")
useUpscaleFactor = kwargs.get("useUpscaleFactor", True)
# Get toggle parameters
useSteps = kwargs.get("useSteps", False)
useSeed = kwargs.get("useSeed", False)
useCFGScale = kwargs.get("useCFGScale", False)
usePrompts = kwargs.get("usePrompts", False)
useClarityParams = kwargs.get("useClarityParams", False)
useCCSRParams = kwargs.get("useCCSRParams", False)
useLatentParams = kwargs.get("useLatentParams", False)
# Get optional parameters
model = kwargs.get("model", "")
steps = kwargs.get("steps", 20)
seed = kwargs.get("seed", 0)
CFGScale = kwargs.get("CFGScale", 7.0)
positivePrompt = kwargs.get("positivePrompt", "")
negativePrompt = kwargs.get("negativePrompt", "")
controlNetWeight = kwargs.get("controlNetWeight", 0.5)
strength = kwargs.get("strength", 0.5)
scheduler = kwargs.get("scheduler", "DPM++ 2M Karras")
colorFix = kwargs.get("colorFix", False)
tileDiffusion = kwargs.get("tileDiffusion", False)
clipSkip = kwargs.get("clipSkip", 0)
safetyInputs = kwargs.get("safetyInputs", None)
runwareAccelerator = kwargs.get("Accelerator", None)
providerSettings = kwargs.get("providerSettings", None)
imageUpscalerSettingsBundle = kwargs.get("imageUpscalerSettings", None)
useTargetMegapixels = kwargs.get("useTargetMegapixels", False)
# Build the base configuration
genConfig = {
"taskType": "imageUpscale",
"taskUUID": rwUtils.genRandUUID(),
"inputImage": rwUtils.convertTensor2IMG(image),
"outputFormat": rwUtils.OUTPUT_FORMAT,
"outputQuality": rwUtils.OUTPUT_QUALITY,
"outputType": "URL",
}
if useUpscaleFactor:
genConfig["upscaleFactor"] = int(kwargs.get("upscaleFactor", 2))
# Add model if specified
if model:
genConfig["model"] = model
if useTargetMegapixels:
genConfig["targetMegapixels"] = int(kwargs.get("targetMegapixels", 4))
# Add common parameters at top level - only add if their toggle is enabled
if useSteps:
genConfig["steps"] = steps
if useSeed:
genConfig["seed"] = seed
if useCFGScale:
genConfig["CFGScale"] = CFGScale
if usePrompts:
if positivePrompt:
genConfig["positivePrompt"] = positivePrompt
if negativePrompt:
genConfig["negativePrompt"] = negativePrompt
# Build settings object for advanced parameters
settings = {}
# Clarity upscaler specific parameters
if useClarityParams:
settings["controlNetWeight"] = controlNetWeight
settings["strength"] = strength
settings["scheduler"] = scheduler
# CCSR and Latent upscaler specific parameters
if useCCSRParams:
settings["colorFix"] = colorFix
settings["tileDiffusion"] = tileDiffusion
# Latent upscaler specific parameters
if useLatentParams:
settings["clipSkip"] = clipSkip
if (
imageUpscalerSettingsBundle is not None
and isinstance(imageUpscalerSettingsBundle, dict)
and "settings" in imageUpscalerSettingsBundle
and isinstance(imageUpscalerSettingsBundle["settings"], dict)
):
settings.update(imageUpscalerSettingsBundle["settings"])
# Add settings to config if any settings were specified
if settings:
genConfig["settings"] = settings
# Add safety inputs if provided
if safetyInputs is not None and isinstance(safetyInputs, dict) and len(safetyInputs) > 0:
genConfig["safety"] = safetyInputs
# Add accelerator options if provided
if runwareAccelerator is not None and isinstance(runwareAccelerator, dict) and len(runwareAccelerator) > 0:
genConfig["acceleratorOptions"] = runwareAccelerator
# Handle providerSettings - extract provider name from model and merge with custom settings
if providerSettings is not None:
# Extract provider name from model (e.g., "runware:500@1" -> "runware")
provider_name = model.split(":")[0] if ":" in model and model else "runware"
# If providerSettings is a dictionary, create the correct API format
if isinstance(providerSettings, dict):
# Create the providerSettings object with provider name as key
final_provider_settings = {
provider_name: providerSettings
}
genConfig["providerSettings"] = final_provider_settings
print(f"[Debugging] Provider settings: {final_provider_settings}")
else:
# If it's just a string, use it directly
genConfig["providerSettings"] = providerSettings
# Debug: Print the request being sent
print(f"[DEBUG] Sending Image Upscale Request:", flush=True)
print(f"[DEBUG] Request Payload: {rwUtils.safe_json_dumps([genConfig], indent=2)}", flush=True)
try:
genResult = rwUtils.inferenecRequest([genConfig])
# Debug: Print the response received
print(f"[DEBUG] Received Image Upscale Response:", flush=True)
print(f"[DEBUG] Response: {rwUtils.safe_json_dumps(genResult, indent=2)}", flush=True)
except Exception as e:
print(f"[DEBUG] Error in Image Upscale Request: {str(e)}", flush=True)
raise
images = rwUtils.extractImageURLs(genResult)
return (images,)