Python
add missing libs and example plugins
Chris Malone1e6cc81e48c4
diff --git a/plugin_system.py b/plugin_system.pynew file mode 100644index 0000000..7aa12f3--- /dev/null+++ b/plugin_system.py@@ -0,0 +1,129 @@+import os+import sys+import importlib.util+import inspect+from typing import Dict, Any, Optional, List, Union+from dataclasses import dataclass+import gradio as gr++@dataclass+class PluginTab:+ id: str+ label: str+ component_constructor: callable+ position: int = -1++class WAN2GPPlugin:+ def __init__(self):+ self.tabs: Dict[str, PluginTab] = {}+ self.name = self.__class__.__name__+ self.version = "1.0.0"+ self.component_requests: List[str] = []++ def setup_ui(self) -> None:+ pass++ def add_tab(self, tab_id: str, label: str, component_constructor: callable, position: int = -1):+ self.tabs[tab_id] = PluginTab(id=tab_id, label=label, component_constructor=component_constructor, position=position)++ def request_component(self, component_id: str) -> None:+ if component_id not in self.component_requests:+ self.component_requests.append(component_id)++ def post_ui_setup(self, components: Dict[str, gr.components.Component]) -> Dict[gr.components.Component, Union[gr.update, Any]]:+ return {}++class PluginManager:+ def __init__(self):+ self.plugins: Dict[str, WAN2GPPlugin] = {}+ self._load_builtin_plugins()++ def _load_builtin_plugins(self) -> None:+ pass++ def load_plugin(self, plugin_path: str) -> bool:+ try:+ module_name = os.path.splitext(os.path.basename(plugin_path))[0]++ spec = importlib.util.spec_from_file_location(f"wan2gp_plugins.{module_name}", plugin_path)+ if spec is None or spec.loader is None:+ print(f"Error: Could not load plugin {plugin_path}")+ return False++ module = importlib.util.module_from_spec(spec)+ sys.modules[module_name] = module+ spec.loader.exec_module(module)++ for name, obj in inspect.getmembers(module, inspect.isclass):+ if issubclass(obj, WAN2GPPlugin) and obj != WAN2GPPlugin:+ plugin = obj()+ plugin.setup_ui()+ self.plugins[module_name] = plugin+ print(f"Loaded plugin: {plugin.name}")+ return True++ print(f"No plugin class found in {plugin_path}")+ return False++ except Exception as e:+ print(f"Error loading plugin {plugin_path}: {str(e)}")+ import traceback+ traceback.print_exc()+ return False++ def load_plugins_from_directory(self, directory: str) -> None:+ if not os.path.isdir(directory):+ os.makedirs(directory, exist_ok=True)+ return++ for filename in os.listdir(directory):+ if filename.endswith('.py') and not filename.startswith('_'):+ self.load_plugin(os.path.join(directory, filename))++ def get_plugin(self, plugin_id: str) -> Optional[WAN2GPPlugin]:+ return self.plugins.get(plugin_id)++ def get_all_plugins(self) -> Dict[str, WAN2GPPlugin]:+ return self.plugins.copy()++ def setup_ui(self) -> Dict[str, Dict[str, Any]]:+ tabs = {}++ for plugin_id, plugin in self.plugins.items():+ try:+ for tab_id, tab in plugin.tabs.items():+ tabs[tab_id] = {+ 'label': tab.label,+ 'component_constructor': tab.component_constructor,+ 'position': tab.position+ }+ except Exception as e:+ print(f"Error in setup_ui for plugin {plugin_id}: {str(e)}")+ import traceback+ traceback.print_exc()++ return {'tabs': tabs}++ def run_post_ui_setup(self, all_components: Dict[str, gr.components.Component]) -> Dict[gr.components.Component, Union[gr.update, Any]]:+ all_updates: Dict[gr.components.Component, Union[gr.update, Any]] = {}+ for plugin_id, plugin in self.plugins.items():+ try:+ requested_components = {+ comp_id: all_components[comp_id]+ for comp_id in plugin.component_requests+ if comp_id in all_components+ }++ if not requested_components and plugin.component_requests:+ print(f"Warning: Plugin '{plugin.name}' requested components that were not found: "+ f"{[c for c in plugin.component_requests if c not in all_components]}")++ updates = plugin.post_ui_setup(requested_components)+ if updates:+ all_updates.update(updates)+ except Exception as e:+ print(f"Error in post_ui_setup for plugin {plugin_id}: {str(e)}")+ import traceback+ traceback.print_exc()++ return all_updates\ No newline at end of filediff --git a/plugins/example_plugin.py b/plugins/example_plugin.pynew file mode 100644index 0000000..9ab36ec--- /dev/null+++ b/plugins/example_plugin.py@@ -0,0 +1,31 @@+import gradio as gr+from plugin_system import WAN2GPPlugin++class ExamplePlugin(WAN2GPPlugin):++ def __init__(self):+ super().__init__()+ self.name = "Example Plugin"+ self.version = "1.0.1"++ def create_hello_world_tab(self):+ with gr.Blocks() as demo:+ gr.Markdown("## Hello from the Example Plugin!")+ with gr.Row():+ name = gr.Textbox(label="Enter your name")+ output = gr.Textbox(label="Output")+ greet_btn = gr.Button("Greet")++ greet_btn.click(+ fn=lambda n: f"Hello, {n}!",+ inputs=[name],+ outputs=output+ )+ return demo++ def setup_ui(self) -> None:+ self.add_tab(+ tab_id="example_plugin_tab",+ label="Example Plugin",+ component_constructor=self.create_hello_world_tab+ )\ No newline at end of filediff --git a/plugins/lora_setter_plugin.py b/plugins/lora_setter_plugin.pynew file mode 100644index 0000000..9ac8516--- /dev/null+++ b/plugins/lora_setter_plugin.py@@ -0,0 +1,22 @@+import gradio as gr+from plugin_system import WAN2GPPlugin++class LoraSetterPlugin(WAN2GPPlugin):+ def __init__(self):+ super().__init__()+ self.name = "Lora Multiplier Setter"+ self.version = "1.0.0"+ self.request_component("loras_multipliers")++ def post_ui_setup(self, components: dict) -> dict:+ loras_mult_textbox = components.get("loras_multipliers")++ if loras_mult_textbox:+ return {+ loras_mult_textbox: gr.Textbox(value="test")+ }++ return {}++ def setup_ui(self) -> None:+ pass\ No newline at end of file