Archived
45 lines
1.6 KiB
Python
45 lines
1.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 file 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 file names
|
|
# The modules will be reloaded on each execution for hot reloading
|
|
WORKFLOW_FILES: Dict[str, str] = {
|
|
"test_workflow": "test",
|
|
# Add more workflows here as you create them
|
|
# "image_generation": "example_image_gen",
|
|
# "upscale": "upscale_workflow",
|
|
}
|
|
|
|
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
|
|
module = load_workflow_module(WORKFLOW_FILES[name])
|
|
return module.run_workflow
|
|
|
|
def list_workflows() -> list:
|
|
"""Get list of available workflow names."""
|
|
return list(WORKFLOW_FILES.keys())
|