File size: 1,534 Bytes
5b6e956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Base plugin interface for all backend plugins.

All backends (ComfyUI, OmniGen2, Gemini, etc.) implement this interface.
"""

from abc import ABC, abstractmethod
from typing import Any, Dict, Optional, List
from PIL import Image
from pathlib import Path
import yaml


class BaseBackendPlugin(ABC):
    """Abstract base class for all backend plugins."""

    def __init__(self, config_path: Path):
        """Initialize plugin with configuration."""
        self.config = self.load_config(config_path)
        self.name = self.config.get('name', 'Unknown')
        self.version = self.config.get('version', '1.0.0')
        self.enabled = self.config.get('enabled', True)

    @abstractmethod
    def health_check(self) -> bool:
        """Check if backend is available and healthy."""
        pass

    @abstractmethod
    def generate_image(
        self,
        prompt: str,
        input_images: Optional[List[Image.Image]] = None,
        **kwargs
    ) -> Image.Image:
        """Generate image using this backend."""
        pass

    @abstractmethod
    def get_capabilities(self) -> Dict[str, Any]:
        """Report backend capabilities."""
        pass

    def load_config(self, config_path: Path) -> Dict[str, Any]:
        """Load plugin configuration from YAML."""
        if not config_path.exists():
            return {}
        with open(config_path) as f:
            return yaml.safe_load(f) or {}

    def __repr__(self):
        return f"{self.__class__.__name__}(name={self.name}, version={self.version})"