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

72 lines
2.6 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": "generation"
},
# Add more workflows here as you create them
# "image_generation": {
# "file": "example_image_gen",
# "name": "Image Generation",
# "description": "Advanced image generation with controlnets",
# "model_types": ["checkpoint", "lora", "controlnet"],
# "category": "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()
]