File size: 14,931 Bytes
618f472 |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# Wan2GP Plugin System
This system allows you to extend and customize the Wan2GP user interface and functionality without modifying the core application code. This document will guide you through the process of creating and installing your own plugins.
## Table of Contents
1. [Plugin Structure](#plugin-structure)
2. [Getting Started: Creating a Plugin](#getting-started-creating-a-plugin)
3. [Plugin Distribution and Installation](#plugin-distribution-and-installation)
4. [Plugin API Reference](#plugin-api-reference)
* [The `WAN2GPPlugin` Class](#the-wan2gpplugin-class)
* [Core Methods](#core-methods)
5. [Examples](#examples)
* [Example 1: Creating a New Tab](#example-1-creating-a-new-tab)
* [Example 2: Injecting UI Elements](#example-2-injecting-ui-elements)
* [Example 3: Advanced UI Injection and Interaction](#example-3-advanced-ui-injection-and-interaction)
* [Example 4: Accessing Global Functions and Variables](#example-4-accessing-global-functions-and-variables)
* [Example 5: Using Helper Modules (Relative Imports)](#example-5-using-helper-modules-relative-imports)
6. [Finding Component IDs](#finding-component-ids)
## Plugin Structure
Plugins are standard Python packages (folders) located within the main `plugins/` directory. This structure allows for multiple files, dependencies, and proper packaging.
A valid plugin folder must contain at a minimum:
* `__init__.py`: An empty file that tells Python to treat the directory as a package.
* `plugin.py`: The main file containing your class that inherits from `WAN2GPPlugin`.
A complete plugin folder typically looks like this:
```
plugins/
βββ my-awesome-plugin/
βββ __init__.py # (Required, can be empty) Makes this a Python package.
βββ plugin.py # (Required) Main plugin logic and class definition.
βββ requirements.txt # (Optional) Lists pip dependencies for your plugin.
βββ ... # Other helper .py files, assets, etc.
```
## Getting Started: Creating a Plugin
1. **Create a Plugin Folder**: Inside the main `plugins/` directory, create a new folder for your plugin (e.g., `my-awesome-plugin`).
2. **Create Core Files**:
* Inside `my-awesome-plugin/`, create an empty file named `__init__.py`.
* Create another file named `plugin.py`. This will be the entry point for your plugin.
3. **Define a Plugin Class**: In `plugin.py`, create a class that inherits from `WAN2GPPlugin` and set its metadata attributes.
```python
from shared.utils.plugins import WAN2GPPlugin
class MyPlugin(WAN2GPPlugin):
def __init__(self):
super().__init__()
self.name = "My Awesome Plugin"
self.version = "1.0.0"
self.description = "This plugin adds awesome new features."
```
4. **Add Dependencies (Optional)**: If your plugin requires external Python libraries (e.g., `numpy`), list them in a `requirements.txt` file inside your plugin folder. These will be installed automatically when a user installs your plugin via the UI.
5. **Enable and Test**:
* Start Wan2GP.
* Go to the **Plugins** tab.
* You should see your new plugin (`my-awesome-plugin`) in the list.
* Check the box to enable it and click "Save Settings".
* **Restart the Wan2GP application.** Your plugin will now be active.
## Plugin Distribution and Installation
#### Packaging for Distribution
To share your plugin, simply upload your entire plugin folder (e.g., `my-awesome-plugin/`) to a public GitHub repository.
#### Installing from the UI
Users can install your plugin directly from the Wan2GP interface:
1. Go to the **Plugins** tab.
2. Under "Install New Plugin," paste the full URL of your plugin's GitHub repository.
3. Click "Download and Install Plugin."
4. The system will clone the repository and install any dependencies from `requirements.txt`.
5. The new plugin will appear in the "Available Plugins" list. The user must then enable it and restart the application to activate it.
The plugin manager also supports updating plugins (if installed from git) and uninstalling them.
## Plugin API Reference
### The `WAN2GPPlugin` Class
Every plugin must define its main class in `plugin.py` inheriting from `WAN2GPPlugin`.
```python
# in plugins/my-awesome-plugin/plugin.py
from shared.utils.plugins import WAN2GPPlugin
import gradio as gr
class MyAwesomePlugin(WAN2GPPlugin):
def __init__(self):
super().__init__()
# Metadata for the Plugin Manager UI
self.name = "My Awesome Plugin"
self.version = "1.0.0"
self.description = "A short description of what my plugin does."
def setup_ui(self):
# UI setup calls go here
pass
def post_ui_setup(self, components: dict):
# Event wiring and UI injection calls go here
pass
```
### Core Methods
These are the methods you can override or call to build your plugin.
#### `setup_ui(self)`
This method is called when your plugin is first loaded. It's the place to declare new tabs or request access to components and globals before the main UI is built.
* **`self.add_tab(tab_id, label, component_constructor, position)`**: Adds a new top-level tab to the UI.
* **`self.request_component(component_id)`**: Requests access to an existing Gradio component by its `elem_id`. The component will be available as an attribute (e.g., `self.loras_multipliers`) in `post_ui_setup`.
* **`self.request_global(global_name)`**: Requests access to a global variable or function from the main `wgp.py` application. The global will be available as an attribute (e.g., `self.server_config`).
#### `post_ui_setup(self, components)`
This method runs after the entire main UI has been built. Use it to wire up events for your custom UI and to inject new components into the existing layout.
* `components` (dict): A dictionary of the components you requested via `request_component`.
* **`self.insert_after(target_component_id, new_component_constructor)`**: A powerful method to dynamically inject new UI elements into the page.
#### `on_tab_select(self, state)` and `on_tab_deselect(self, state)`
If you used `add_tab`, these methods will be called automatically when your tab is selected or deselected, respectively. This is useful for loading data or managing resources.
#### `set_global(self, variable_name, new_value)`
Allows your plugin to safely modify a global variable in the main `wgp.py` application.
#### `register_data_hook(self, hook_name, callback)`
Allows you to intercept and modify data at key points. For example, the `before_metadata_save` hook lets you add custom data to the metadata before it's saved to a file.
## Examples
### Example 1: Creating a New Tab
**File Structure:**
```
plugins/
βββ greeter_plugin/
βββ __init__.py
βββ plugin.py
```
**Code:**
```python
# in plugins/greeter_plugin/plugin.py
import gradio as gr
from shared.utils.plugins import WAN2GPPlugin
class GreeterPlugin(WAN2GPPlugin):
def __init__(self):
super().__init__()
self.name = "Greeter Plugin"
self.version = "1.0.0"
self.description = "Adds a simple 'Greeter' tab."
def setup_ui(self):
self.add_tab(
tab_id="greeter_tab",
label="Greeter",
component_constructor=self.create_greeter_ui,
position=2 # Place it as the 3rd tab (0-indexed)
)
def create_greeter_ui(self):
with gr.Blocks() as demo:
gr.Markdown("## A Simple Greeter")
with gr.Row():
name_input = gr.Textbox(label="Enter your name")
output_text = gr.Textbox(label="Output")
greet_btn = gr.Button("Greet")
greet_btn.click(
fn=lambda name: f"Hello, {name}!",
inputs=[name_input],
outputs=output_text
)
return demo
```
### Example 2: Injecting UI Elements
This example adds a simple HTML element right after the "Loras Multipliers" textbox.
**File Structure:**
```
plugins/
βββ injector_plugin/
βββ __init__.py
βββ plugin.py
```
**Code:**
```python
# in plugins/injector_plugin/plugin.py
import gradio as gr
from shared.utils.plugins import WAN2GPPlugin
class InjectorPlugin(WAN2GPPlugin):
def __init__(self):
super().__init__()
self.name = "UI Injector"
self.version = "1.0.0"
self.description = "Injects a message into the main UI."
def post_ui_setup(self, components: dict):
def create_inserted_component():
return gr.HTML(value="<div style='padding: 10px; color: gray; text-align: center;'>--- Injected by a plugin! ---</div>")
self.insert_after(
target_component_id="loras_multipliers",
new_component_constructor=create_inserted_component
)
```
### Example 3: Advanced UI Injection and Interaction
This plugin injects a button that interacts with other components on the page.
**File Structure:**
```
plugins/
βββ advanced_ui_plugin/
βββ __init__.py
βββ plugin.py
```
**Code:**
```python
# in plugins/advanced_ui_plugin/plugin.py
import gradio as gr
from shared.utils.plugins import WAN2GPPlugin
class AdvancedUIPlugin(WAN2GPPlugin):
def __init__(self):
super().__init__()
self.name = "LoRA Helper"
self.description = "Adds a button to copy selected LoRAs."
def setup_ui(self):
# Request access to the components we want to read from and write to.
self.request_component("loras_multipliers")
self.request_component("loras_choices")
def post_ui_setup(self, components: dict):
# This function will create our new UI and wire its events.
def create_and_wire_advanced_ui():
with gr.Accordion("LoRA Helper Panel (Plugin)", open=False):
copy_btn = gr.Button("Copy selected LoRA names to Multipliers")
# Define the function for the button's click event.
def copy_lora_names(selected_loras):
return " ".join(selected_loras)
# Wire the event. We can access the components as attributes of `self`.
copy_btn.click(
fn=copy_lora_names,
inputs=[self.loras_choices],
outputs=[self.loras_multipliers]
)
return panel # Return the top-level component to be inserted.
# Tell the manager to insert our UI after the 'loras_multipliers' textbox.
self.insert_after(
target_component_id="loras_multipliers",
new_component_constructor=create_and_wire_advanced_ui
)
```
### Example 4: Accessing Global Functions and Variables
**File Structure:**
```
plugins/
βββ global_access_plugin/
βββ __init__.py
βββ plugin.py
```
**Code:**
```python
# in plugins/global_access_plugin/plugin.py
import gradio as gr
from shared.utils.plugins import WAN2GPPlugin
class GlobalAccessPlugin(WAN2GPPlugin):
def __init__(self):
super().__init__()
self.name = "Global Access Plugin"
self.description = "Demonstrates reading and writing global state."
def setup_ui(self):
# Request read access to globals
self.request_global("server_config")
self.request_global("get_video_info")
# Add a tab to host our UI
self.add_tab("global_access_tab", "Global Access", self.create_ui)
def create_ui(self):
with gr.Blocks() as demo:
gr.Markdown("### Read Globals")
video_input = gr.Video(label="Upload a video to analyze")
info_output = gr.JSON(label="Video Info")
def analyze_video(video_path):
if not video_path: return "Upload a video."
# Access globals as attributes of `self`
save_path = self.server_config.get("save_path", "outputs")
fps, w, h, frames = self.get_video_info(video_path)
return {"save_path": save_path, "fps": fps, "dimensions": f"{w}x{h}"}
analyze_btn = gr.Button("Analyze Video")
analyze_btn.click(fn=analyze_video, inputs=[video_input], outputs=[info_output])
gr.Markdown("--- \n ### Write Globals")
theme_changer = gr.Dropdown(choices=["default", "gradio"], label="Change UI Theme (Requires Restart)")
save_theme_btn = gr.Button("Save Theme Change")
def save_theme(theme_choice):
# Use the safe `set_global` method
self.set_global("UI_theme", theme_choice)
gr.Info(f"Theme set to '{theme_choice}'. Restart required.")
save_theme_btn.click(fn=save_theme, inputs=[theme_changer])
return demo
```
### Example 5: Using Helper Modules (Relative Imports)
This example shows how to organize your code into multiple files within your plugin package.
**File Structure:**
```
plugins/
βββ helper_plugin/
βββ __init__.py
βββ plugin.py
βββ helpers.py
```
**Code:**
```python
# in plugins/helper_plugin/helpers.py
def format_greeting(name: str) -> str:
"""A helper function in a separate file."""
if not name:
return "Hello, mystery person!"
return f"A very special hello to {name.upper()}!"
# in plugins/helper_plugin/plugin.py
import gradio as gr
from shared.utils.plugins import WAN2GPPlugin
from .helpers import format_greeting # <-- Relative import works!
class HelperPlugin(WAN2GPPlugin):
def __init__(self):
super().__init__()
self.name = "Helper Module Example"
self.description = "Shows how to use relative imports."
def setup_ui(self):
self.add_tab("helper_tab", "Helper Example", self.create_ui)
def create_ui(self):
with gr.Blocks() as demo:
name_input = gr.Textbox(label="Name")
output = gr.Textbox(label="Formatted Greeting")
btn = gr.Button("Greet with Helper")
btn.click(fn=format_greeting, inputs=[name_input], outputs=[output])
return demo
```
## Finding Component IDs
To interact with an existing component using `request_component` or `insert_after`, you need its `elem_id`. You can find these IDs by:
1. **Inspecting the Source Code**: Look through `wgp.py` for Gradio components defined with an `elem_id`.
2. **Browser Developer Tools**: Run Wan2GP, open your browser's developer tools (F12), and use the "Inspect Element" tool to find the `id` of the HTML element you want to target.
Some common `elem_id`s include:
* `loras_multipliers`
* `loras_choices`
* `main_tabs`
* `gallery`
* `family_list`, `model_base_types_list`, `model_list` |