This repository has been archived on 2026-10-21. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
imagelab_v3_extension/workflows.py
T

93 lines
3.3 KiB
Python

"""
Workflow registry for external API.
Import workflow functions from the workflows/ folder and map them here.
Supports hot reloading - workflows are reloaded from disk on each execution.
"""
import os
import importlib.util
from typing import Callable, Dict
def load_workflow_module(workflow_name: str):
"""Dynamically load/reload a workflow module from the workflows folder."""
workflow_path = os.path.join(os.path.dirname(__file__), "workflows", f"{workflow_name}.py")
if not os.path.exists(workflow_path):
raise FileNotFoundError(f"Workflow script not found: {workflow_path}")
spec = importlib.util.spec_from_file_location(f"workflows.{workflow_name}", workflow_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
# Workflow registry - map workflow names to their config (file + metadata)
# The modules will be reloaded on each execution for hot reloading
WORKFLOW_FILES: Dict[str, dict] = {
"test_workflow": {
"script": "test",
"name": "Test Workflow",
"description": "Simple test workflow for image generation",
"model_types": ["SDXL", "Pony", "Illustrious"],
"category": "Image Generation"
},
# Add more workflows here as you create them
"basic_sdxl": {
"script": "basic_sdxl",
"name": "Basic SDXL Text to Image",
"description": "Workflow with SDXL presets for quick iteration.",
"model_types": ["SDXL"],
"category": "Image Generation"
},
"basic_sd15": {
"script": "basic_sd15",
"name": "Basic SD15 Text to Image",
"description": "Workflow with SD15 presets for quick iteration.",
"model_types": ["SD15"],
"category": "Image Generation"
},
"basic_pony": {
"script": "basic_pony",
"name": "Basic Pony Text to Image",
"description": "Workflow with Pony presets for quick iteration.",
"model_types": ["Pony"],
"category": "Image Generation"
},
"basic_illustrious": {
"script": "basic_illustrious",
"name": "Basic Illustrious Text to Image",
"description": "Workflow with Illustrious presets for quick iteration.",
"model_types": ["Illustrious"],
"category": "Image Generation"
},
}
def get_workflow(name: str) -> Callable:
"""
Get a workflow function by name.
Reloads the module from disk each time for hot reloading.
"""
if name not in WORKFLOW_FILES:
raise ValueError(f"Workflow '{name}' not found. Available: {list(WORKFLOW_FILES.keys())}")
# Reload the module from disk each time
workflow_config = WORKFLOW_FILES[name]
module = load_workflow_module(workflow_config["script"])
return module.run_workflow
def get_workflow_metadata(name: str) -> dict:
"""Get metadata for a specific workflow."""
if name not in WORKFLOW_FILES:
raise ValueError(f"Workflow '{name}' not found. Available: {list(WORKFLOW_FILES.keys())}")
config = WORKFLOW_FILES[name].copy()
config.pop("script") # Don't expose internal file path
return config
def list_workflows() -> list:
"""Get list of available workflow names with their metadata."""
return [
{
"id": name,
**get_workflow_metadata(name)
}
for name in WORKFLOW_FILES.keys()
]