Upload folder using huggingface_hub
Browse files- README.md +256 -79
- app.py +255 -78
- space.py +256 -78
- src/README.md +256 -79
- src/demo/app.py +255 -78
- src/demo/space.py +256 -78
README.md
CHANGED
|
@@ -38,7 +38,6 @@ The **PropertySheet** component for Gradio allows you to automatically generate
|
|
| 38 |
|
| 39 |
## Installation
|
| 40 |
|
| 41 |
-
|
| 42 |
```bash
|
| 43 |
pip install gradio_propertysheet
|
| 44 |
```
|
|
@@ -54,42 +53,97 @@ from typing import Literal
|
|
| 54 |
from gradio_propertysheet import PropertySheet
|
| 55 |
from gradio_htmlinjector import HTMLInjector
|
| 56 |
|
|
|
|
| 57 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 58 |
@dataclass
|
| 59 |
class ModelSettings:
|
| 60 |
-
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
|
|
|
| 63 |
@dataclass
|
| 64 |
class SamplingSettings:
|
| 65 |
-
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
@dataclass
|
| 69 |
class RenderConfig:
|
| 70 |
-
seed: int = field(
|
|
|
|
|
|
|
|
|
|
| 71 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 72 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
|
|
|
| 73 |
@dataclass
|
| 74 |
class Lighting:
|
| 75 |
-
sun_intensity: float = field(
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
@dataclass
|
| 78 |
class EnvironmentConfig:
|
| 79 |
-
background: Literal["Sky", "Color", "Image"] = field(
|
|
|
|
|
|
|
| 80 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 81 |
@dataclass
|
| 82 |
class EulerSettings:
|
| 83 |
-
s_churn: float = field(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
@dataclass
|
| 85 |
class DPM_Settings:
|
| 86 |
-
karras_style: bool = field(
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 89 |
initial_render_config = RenderConfig()
|
| 90 |
initial_env_config = EnvironmentConfig()
|
| 91 |
-
sampler_settings_map_py = {
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 95 |
def inject_assets():
|
|
@@ -99,160 +153,283 @@ def inject_assets():
|
|
| 99 |
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 100 |
css_code = ""
|
| 101 |
js_code = ""
|
| 102 |
-
|
| 103 |
try:
|
| 104 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 105 |
css_code += f.read() + "\n"
|
| 106 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 107 |
js_code += f.read() + "\n"
|
| 108 |
except FileNotFoundError as e:
|
| 109 |
print(f"Warning: Could not read asset file: {e}")
|
| 110 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 111 |
|
|
|
|
| 112 |
# --- 4. Gradio App Build ---
|
| 113 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 114 |
html_injector = HTMLInjector()
|
| 115 |
gr.Markdown("# PropertySheet Component Demos")
|
| 116 |
-
|
| 117 |
with gr.Row():
|
| 118 |
# --- Flyout popup ---
|
| 119 |
-
with gr.Column(
|
|
|
|
|
|
|
| 120 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 121 |
-
flyout_sheet = PropertySheet(
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
with gr.Tabs():
|
| 124 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 125 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 126 |
render_state = gr.State(value=initial_render_config)
|
| 127 |
env_state = gr.State(value=initial_env_config)
|
| 128 |
sidebar_visible = gr.State(False)
|
| 129 |
with gr.Row():
|
| 130 |
-
with gr.Column(scale=3):
|
| 131 |
generate = gr.Button("Show Settings", variant="primary")
|
| 132 |
with gr.Row():
|
| 133 |
output_render_json = gr.JSON(label="Live Render State")
|
| 134 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
-
render_sheet = PropertySheet(
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 139 |
new_visibility = not is_visible
|
| 140 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 141 |
-
return (
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
return updated_config, asdict(updated_config), updated_config
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
return updated_config, asdict(updated_config), current_state
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
with gr.TabItem("Flyout Popup Demo"):
|
| 155 |
-
gr.Markdown(
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
# --- State Management ---
|
| 158 |
flyout_visible = gr.State(False)
|
| 159 |
active_anchor_id = gr.State(None)
|
| 160 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 161 |
|
| 162 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 163 |
-
with gr.Row(
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
# --- Event Logic ---
|
| 174 |
-
def handle_flyout_toggle(
|
|
|
|
|
|
|
| 175 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 176 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 177 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 178 |
else:
|
| 179 |
-
js_data = json.dumps(
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
def update_ear_visibility(selection, settings_map):
|
| 183 |
has_settings = settings_map.get(selection) is not None
|
| 184 |
return gr.update(visible=has_settings)
|
| 185 |
|
| 186 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 187 |
-
if updated_settings is None or active_id is None:
|
| 188 |
return
|
| 189 |
if active_id == sampler_dd.elem_id:
|
| 190 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 191 |
elif active_id == model_dd.elem_id:
|
| 192 |
model_settings_map_py[model_val] = updated_settings
|
| 193 |
-
|
| 194 |
def close_the_flyout():
|
| 195 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 196 |
return False, None, gr.update(value=js_data)
|
| 197 |
-
|
| 198 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 199 |
-
|
| 200 |
sampler_dd.change(
|
| 201 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 202 |
inputs=[sampler_dd],
|
| 203 |
-
outputs=[sampler_ear_btn]
|
| 204 |
-
).then(
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
sampler_ear_btn.click(
|
| 208 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 209 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 210 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 212 |
|
| 213 |
model_dd.change(
|
| 214 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 215 |
inputs=[model_dd],
|
| 216 |
-
outputs=[model_ear_btn]
|
| 217 |
-
).then(
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
model_ear_btn.click(
|
| 221 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 222 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 223 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 225 |
-
|
| 226 |
flyout_sheet.change(
|
| 227 |
fn=on_flyout_change,
|
| 228 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 229 |
-
outputs=None
|
| 230 |
)
|
| 231 |
|
| 232 |
close_btn.click(
|
| 233 |
fn=close_the_flyout,
|
| 234 |
inputs=None,
|
| 235 |
-
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 236 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 237 |
|
| 238 |
def initial_flyout_setup(sampler_val, model_val):
|
| 239 |
return {
|
| 240 |
-
sampler_ear_btn: update_ear_visibility(
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
}
|
| 243 |
-
|
| 244 |
# --- App Load ---
|
| 245 |
-
demo.load(
|
| 246 |
-
fn=
|
| 247 |
-
|
| 248 |
-
|
| 249 |
).then(
|
| 250 |
-
fn=None,
|
| 251 |
-
|
|
|
|
|
|
|
| 252 |
)
|
| 253 |
-
|
| 254 |
if __name__ == "__main__":
|
| 255 |
demo.launch()
|
|
|
|
| 256 |
```
|
| 257 |
|
| 258 |
## `PropertySheet`
|
|
|
|
| 38 |
|
| 39 |
## Installation
|
| 40 |
|
|
|
|
| 41 |
```bash
|
| 42 |
pip install gradio_propertysheet
|
| 43 |
```
|
|
|
|
| 53 |
from gradio_propertysheet import PropertySheet
|
| 54 |
from gradio_htmlinjector import HTMLInjector
|
| 55 |
|
| 56 |
+
|
| 57 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 58 |
@dataclass
|
| 59 |
class ModelSettings:
|
| 60 |
+
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 61 |
+
default="SDXL", metadata={"component": "dropdown", "label": "Base Model"}
|
| 62 |
+
)
|
| 63 |
+
custom_model_path: str = field(
|
| 64 |
+
default="/path/to/default.safetensors",
|
| 65 |
+
metadata={
|
| 66 |
+
"label": "Custom Model Path",
|
| 67 |
+
"interactive_if": {"field": "model_type", "value": "Custom"},
|
| 68 |
+
},
|
| 69 |
+
)
|
| 70 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 71 |
+
|
| 72 |
+
|
| 73 |
@dataclass
|
| 74 |
class SamplingSettings:
|
| 75 |
+
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 76 |
+
default="DPM++ 2M Karras",
|
| 77 |
+
metadata={"component": "dropdown", "label": "Sampler"},
|
| 78 |
+
)
|
| 79 |
+
steps: int = field(
|
| 80 |
+
default=25,
|
| 81 |
+
metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1},
|
| 82 |
+
)
|
| 83 |
+
cfg_scale: float = field(
|
| 84 |
+
default=7.0,
|
| 85 |
+
metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5},
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
@dataclass
|
| 90 |
class RenderConfig:
|
| 91 |
+
seed: int = field(
|
| 92 |
+
default=-1,
|
| 93 |
+
metadata={"component": "number_integer", "label": "Seed (-1 for random)"},
|
| 94 |
+
)
|
| 95 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 96 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
@dataclass
|
| 100 |
class Lighting:
|
| 101 |
+
sun_intensity: float = field(
|
| 102 |
+
default=1.0,
|
| 103 |
+
metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1},
|
| 104 |
+
)
|
| 105 |
+
color: str = field(
|
| 106 |
+
default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"}
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
@dataclass
|
| 111 |
class EnvironmentConfig:
|
| 112 |
+
background: Literal["Sky", "Color", "Image"] = field(
|
| 113 |
+
default="Sky", metadata={"component": "dropdown"}
|
| 114 |
+
)
|
| 115 |
lighting: Lighting = field(default_factory=Lighting)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
@dataclass
|
| 119 |
class EulerSettings:
|
| 120 |
+
s_churn: float = field(
|
| 121 |
+
default=0.0,
|
| 122 |
+
metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01},
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
@dataclass
|
| 127 |
class DPM_Settings:
|
| 128 |
+
karras_style: bool = field(
|
| 129 |
+
default=True, metadata={"label": "Use Karras Sigma Schedule"}
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
|
| 133 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 134 |
initial_render_config = RenderConfig()
|
| 135 |
initial_env_config = EnvironmentConfig()
|
| 136 |
+
sampler_settings_map_py = {
|
| 137 |
+
"Euler": EulerSettings(),
|
| 138 |
+
"DPM++ 2M Karras": DPM_Settings(),
|
| 139 |
+
"UniPC": None,
|
| 140 |
+
}
|
| 141 |
+
model_settings_map_py = {
|
| 142 |
+
"SDXL 1.0": DPM_Settings(),
|
| 143 |
+
"Stable Diffusion 1.5": EulerSettings(),
|
| 144 |
+
"Pony": None,
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
|
| 148 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 149 |
def inject_assets():
|
|
|
|
| 153 |
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 154 |
css_code = ""
|
| 155 |
js_code = ""
|
| 156 |
+
|
| 157 |
try:
|
| 158 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 159 |
css_code += f.read() + "\n"
|
| 160 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 161 |
js_code += f.read() + "\n"
|
| 162 |
except FileNotFoundError as e:
|
| 163 |
print(f"Warning: Could not read asset file: {e}")
|
| 164 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 165 |
|
| 166 |
+
|
| 167 |
# --- 4. Gradio App Build ---
|
| 168 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 169 |
html_injector = HTMLInjector()
|
| 170 |
gr.Markdown("# PropertySheet Component Demos")
|
| 171 |
+
|
| 172 |
with gr.Row():
|
| 173 |
# --- Flyout popup ---
|
| 174 |
+
with gr.Column(
|
| 175 |
+
elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]
|
| 176 |
+
) as flyout_panel_source:
|
| 177 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 178 |
+
flyout_sheet = PropertySheet(
|
| 179 |
+
visible=True,
|
| 180 |
+
container=False,
|
| 181 |
+
label="Settings",
|
| 182 |
+
show_group_name_only_one=False,
|
| 183 |
+
disable_accordion=True,
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
with gr.Tabs():
|
| 187 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 188 |
+
gr.Markdown(
|
| 189 |
+
"An example of using the `PropertySheet` component as a traditional sidebar for settings."
|
| 190 |
+
)
|
| 191 |
render_state = gr.State(value=initial_render_config)
|
| 192 |
env_state = gr.State(value=initial_env_config)
|
| 193 |
sidebar_visible = gr.State(False)
|
| 194 |
with gr.Row():
|
| 195 |
+
with gr.Column(scale=3):
|
| 196 |
generate = gr.Button("Show Settings", variant="primary")
|
| 197 |
with gr.Row():
|
| 198 |
output_render_json = gr.JSON(label="Live Render State")
|
| 199 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 200 |
with gr.Column(scale=1):
|
| 201 |
+
render_sheet = PropertySheet(
|
| 202 |
+
value=initial_render_config,
|
| 203 |
+
label="Render Settings",
|
| 204 |
+
width=400,
|
| 205 |
+
height=550,
|
| 206 |
+
visible=False,
|
| 207 |
+
root_label="Generator",
|
| 208 |
+
)
|
| 209 |
+
environment_sheet = PropertySheet(
|
| 210 |
+
value=initial_env_config,
|
| 211 |
+
label="Environment Settings",
|
| 212 |
+
width=400,
|
| 213 |
+
open=False,
|
| 214 |
+
visible=False,
|
| 215 |
+
root_label="General",
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 219 |
new_visibility = not is_visible
|
| 220 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 221 |
+
return (
|
| 222 |
+
new_visibility,
|
| 223 |
+
gr.update(visible=new_visibility, value=render_cfg),
|
| 224 |
+
gr.update(visible=new_visibility, value=env_cfg),
|
| 225 |
+
gr.update(value=button_text),
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
def handle_render_change(
|
| 229 |
+
updated_config: RenderConfig, current_state: RenderConfig
|
| 230 |
+
):
|
| 231 |
+
if updated_config is None:
|
| 232 |
+
return current_state, asdict(current_state), current_state
|
| 233 |
+
if updated_config.model.model_type != "Custom":
|
| 234 |
+
updated_config.model.custom_model_path = (
|
| 235 |
+
"/path/to/default.safetensors"
|
| 236 |
+
)
|
| 237 |
return updated_config, asdict(updated_config), updated_config
|
| 238 |
+
|
| 239 |
+
def handle_env_change(
|
| 240 |
+
updated_config: EnvironmentConfig, current_state: EnvironmentConfig
|
| 241 |
+
):
|
| 242 |
+
if updated_config is None:
|
| 243 |
+
return current_state, asdict(current_state), current_state
|
| 244 |
return updated_config, asdict(updated_config), current_state
|
| 245 |
+
|
| 246 |
+
generate.click(
|
| 247 |
+
fn=change_visibility,
|
| 248 |
+
inputs=[sidebar_visible, render_state, env_state],
|
| 249 |
+
outputs=[sidebar_visible, render_sheet, environment_sheet, generate],
|
| 250 |
+
)
|
| 251 |
+
render_sheet.change(
|
| 252 |
+
fn=handle_render_change,
|
| 253 |
+
inputs=[render_sheet, render_state],
|
| 254 |
+
outputs=[render_sheet, output_render_json, render_state],
|
| 255 |
+
)
|
| 256 |
+
environment_sheet.change(
|
| 257 |
+
fn=handle_env_change,
|
| 258 |
+
inputs=[environment_sheet, env_state],
|
| 259 |
+
outputs=[environment_sheet, output_env_json, env_state],
|
| 260 |
+
)
|
| 261 |
+
demo.load(
|
| 262 |
+
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 263 |
+
inputs=[render_state, env_state],
|
| 264 |
+
outputs=[output_render_json, output_env_json],
|
| 265 |
+
)
|
| 266 |
|
| 267 |
with gr.TabItem("Flyout Popup Demo"):
|
| 268 |
+
gr.Markdown(
|
| 269 |
+
"An example of attaching a `PropertySheet` as a flyout panel to other components."
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
# --- State Management ---
|
| 273 |
flyout_visible = gr.State(False)
|
| 274 |
active_anchor_id = gr.State(None)
|
| 275 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 276 |
|
| 277 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 278 |
+
with gr.Row(
|
| 279 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 280 |
+
):
|
| 281 |
+
sampler_dd = gr.Dropdown(
|
| 282 |
+
choices=list(sampler_settings_map_py.keys()),
|
| 283 |
+
label="Sampler",
|
| 284 |
+
value="Euler",
|
| 285 |
+
elem_id="sampler_dd",
|
| 286 |
+
scale=10,
|
| 287 |
+
)
|
| 288 |
+
sampler_ear_btn = gr.Button(
|
| 289 |
+
"⚙️",
|
| 290 |
+
elem_id="sampler_ear_btn",
|
| 291 |
+
scale=1,
|
| 292 |
+
elem_classes=["integrated-ear-btn"],
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
with gr.Row(
|
| 296 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 297 |
+
):
|
| 298 |
+
model_dd = gr.Dropdown(
|
| 299 |
+
choices=list(model_settings_map_py.keys()),
|
| 300 |
+
label="Model",
|
| 301 |
+
value="SDXL 1.0",
|
| 302 |
+
elem_id="model_dd",
|
| 303 |
+
scale=10,
|
| 304 |
+
)
|
| 305 |
+
model_ear_btn = gr.Button(
|
| 306 |
+
"⚙️",
|
| 307 |
+
elem_id="model_ear_btn",
|
| 308 |
+
scale=1,
|
| 309 |
+
elem_classes=["integrated-ear-btn"],
|
| 310 |
+
)
|
| 311 |
|
| 312 |
# --- Event Logic ---
|
| 313 |
+
def handle_flyout_toggle(
|
| 314 |
+
is_vis, current_anchor, clicked_dropdown_id, settings_obj
|
| 315 |
+
):
|
| 316 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 317 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 318 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 319 |
else:
|
| 320 |
+
js_data = json.dumps(
|
| 321 |
+
{"isVisible": True, "anchorId": clicked_dropdown_id}
|
| 322 |
+
)
|
| 323 |
+
return (
|
| 324 |
+
True,
|
| 325 |
+
clicked_dropdown_id,
|
| 326 |
+
gr.update(value=settings_obj),
|
| 327 |
+
gr.update(value=js_data),
|
| 328 |
+
)
|
| 329 |
|
| 330 |
def update_ear_visibility(selection, settings_map):
|
| 331 |
has_settings = settings_map.get(selection) is not None
|
| 332 |
return gr.update(visible=has_settings)
|
| 333 |
|
| 334 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 335 |
+
if updated_settings is None or active_id is None:
|
| 336 |
return
|
| 337 |
if active_id == sampler_dd.elem_id:
|
| 338 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 339 |
elif active_id == model_dd.elem_id:
|
| 340 |
model_settings_map_py[model_val] = updated_settings
|
| 341 |
+
|
| 342 |
def close_the_flyout():
|
| 343 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 344 |
return False, None, gr.update(value=js_data)
|
| 345 |
+
|
| 346 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 347 |
+
|
| 348 |
sampler_dd.change(
|
| 349 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 350 |
inputs=[sampler_dd],
|
| 351 |
+
outputs=[sampler_ear_btn],
|
| 352 |
+
).then(
|
| 353 |
+
fn=close_the_flyout,
|
| 354 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 355 |
+
).then(
|
| 356 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
sampler_ear_btn.click(
|
| 360 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 361 |
+
is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)
|
| 362 |
+
),
|
| 363 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 364 |
+
outputs=[
|
| 365 |
+
flyout_visible,
|
| 366 |
+
active_anchor_id,
|
| 367 |
+
flyout_sheet,
|
| 368 |
+
js_data_bridge,
|
| 369 |
+
],
|
| 370 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 371 |
|
| 372 |
model_dd.change(
|
| 373 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 374 |
inputs=[model_dd],
|
| 375 |
+
outputs=[model_ear_btn],
|
| 376 |
+
).then(
|
| 377 |
+
fn=close_the_flyout,
|
| 378 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 379 |
+
).then(
|
| 380 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
model_ear_btn.click(
|
| 384 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 385 |
+
is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)
|
| 386 |
+
),
|
| 387 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 388 |
+
outputs=[
|
| 389 |
+
flyout_visible,
|
| 390 |
+
active_anchor_id,
|
| 391 |
+
flyout_sheet,
|
| 392 |
+
js_data_bridge,
|
| 393 |
+
],
|
| 394 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 395 |
+
|
| 396 |
flyout_sheet.change(
|
| 397 |
fn=on_flyout_change,
|
| 398 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 399 |
+
outputs=None,
|
| 400 |
)
|
| 401 |
|
| 402 |
close_btn.click(
|
| 403 |
fn=close_the_flyout,
|
| 404 |
inputs=None,
|
| 405 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 406 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 407 |
|
| 408 |
def initial_flyout_setup(sampler_val, model_val):
|
| 409 |
return {
|
| 410 |
+
sampler_ear_btn: update_ear_visibility(
|
| 411 |
+
sampler_val, sampler_settings_map_py
|
| 412 |
+
),
|
| 413 |
+
model_ear_btn: update_ear_visibility(
|
| 414 |
+
model_val, model_settings_map_py
|
| 415 |
+
),
|
| 416 |
}
|
| 417 |
+
|
| 418 |
# --- App Load ---
|
| 419 |
+
demo.load(fn=inject_assets, inputs=None, outputs=[html_injector]).then(
|
| 420 |
+
fn=initial_flyout_setup,
|
| 421 |
+
inputs=[sampler_dd, model_dd],
|
| 422 |
+
outputs=[sampler_ear_btn, model_ear_btn],
|
| 423 |
).then(
|
| 424 |
+
fn=None,
|
| 425 |
+
inputs=None,
|
| 426 |
+
outputs=None,
|
| 427 |
+
js="() => { setTimeout(reparent_flyout, 200); }",
|
| 428 |
)
|
| 429 |
+
|
| 430 |
if __name__ == "__main__":
|
| 431 |
demo.launch()
|
| 432 |
+
|
| 433 |
```
|
| 434 |
|
| 435 |
## `PropertySheet`
|
app.py
CHANGED
|
@@ -6,42 +6,97 @@ from typing import Literal
|
|
| 6 |
from gradio_propertysheet import PropertySheet
|
| 7 |
from gradio_htmlinjector import HTMLInjector
|
| 8 |
|
|
|
|
| 9 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 10 |
@dataclass
|
| 11 |
class ModelSettings:
|
| 12 |
-
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
|
|
|
| 15 |
@dataclass
|
| 16 |
class SamplingSettings:
|
| 17 |
-
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
@dataclass
|
| 21 |
class RenderConfig:
|
| 22 |
-
seed: int = field(
|
|
|
|
|
|
|
|
|
|
| 23 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 24 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
|
|
|
| 25 |
@dataclass
|
| 26 |
class Lighting:
|
| 27 |
-
sun_intensity: float = field(
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
@dataclass
|
| 30 |
class EnvironmentConfig:
|
| 31 |
-
background: Literal["Sky", "Color", "Image"] = field(
|
|
|
|
|
|
|
| 32 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 33 |
@dataclass
|
| 34 |
class EulerSettings:
|
| 35 |
-
s_churn: float = field(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
@dataclass
|
| 37 |
class DPM_Settings:
|
| 38 |
-
karras_style: bool = field(
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 41 |
initial_render_config = RenderConfig()
|
| 42 |
initial_env_config = EnvironmentConfig()
|
| 43 |
-
sampler_settings_map_py = {
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 47 |
def inject_assets():
|
|
@@ -51,157 +106,279 @@ def inject_assets():
|
|
| 51 |
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 52 |
css_code = ""
|
| 53 |
js_code = ""
|
| 54 |
-
|
| 55 |
try:
|
| 56 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 57 |
css_code += f.read() + "\n"
|
| 58 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 59 |
js_code += f.read() + "\n"
|
| 60 |
except FileNotFoundError as e:
|
| 61 |
print(f"Warning: Could not read asset file: {e}")
|
| 62 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 63 |
|
|
|
|
| 64 |
# --- 4. Gradio App Build ---
|
| 65 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 66 |
html_injector = HTMLInjector()
|
| 67 |
gr.Markdown("# PropertySheet Component Demos")
|
| 68 |
-
|
| 69 |
with gr.Row():
|
| 70 |
# --- Flyout popup ---
|
| 71 |
-
with gr.Column(
|
|
|
|
|
|
|
| 72 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 73 |
-
flyout_sheet = PropertySheet(
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
with gr.Tabs():
|
| 76 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 77 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 78 |
render_state = gr.State(value=initial_render_config)
|
| 79 |
env_state = gr.State(value=initial_env_config)
|
| 80 |
sidebar_visible = gr.State(False)
|
| 81 |
with gr.Row():
|
| 82 |
-
with gr.Column(scale=3):
|
| 83 |
generate = gr.Button("Show Settings", variant="primary")
|
| 84 |
with gr.Row():
|
| 85 |
output_render_json = gr.JSON(label="Live Render State")
|
| 86 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
-
render_sheet = PropertySheet(
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 91 |
new_visibility = not is_visible
|
| 92 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 93 |
-
return (
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return updated_config, asdict(updated_config), updated_config
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
return updated_config, asdict(updated_config), current_state
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
with gr.TabItem("Flyout Popup Demo"):
|
| 107 |
-
gr.Markdown(
|
| 108 |
-
|
|
|
|
|
|
|
| 109 |
# --- State Management ---
|
| 110 |
flyout_visible = gr.State(False)
|
| 111 |
active_anchor_id = gr.State(None)
|
| 112 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 113 |
|
| 114 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 115 |
-
with gr.Row(
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# --- Event Logic ---
|
| 126 |
-
def handle_flyout_toggle(
|
|
|
|
|
|
|
| 127 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 128 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 129 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 130 |
else:
|
| 131 |
-
js_data = json.dumps(
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
def update_ear_visibility(selection, settings_map):
|
| 135 |
has_settings = settings_map.get(selection) is not None
|
| 136 |
return gr.update(visible=has_settings)
|
| 137 |
|
| 138 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 139 |
-
if updated_settings is None or active_id is None:
|
| 140 |
return
|
| 141 |
if active_id == sampler_dd.elem_id:
|
| 142 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 143 |
elif active_id == model_dd.elem_id:
|
| 144 |
model_settings_map_py[model_val] = updated_settings
|
| 145 |
-
|
| 146 |
def close_the_flyout():
|
| 147 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 148 |
return False, None, gr.update(value=js_data)
|
| 149 |
-
|
| 150 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 151 |
-
|
| 152 |
sampler_dd.change(
|
| 153 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 154 |
inputs=[sampler_dd],
|
| 155 |
-
outputs=[sampler_ear_btn]
|
| 156 |
-
).then(
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
sampler_ear_btn.click(
|
| 160 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 161 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 162 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 164 |
|
| 165 |
model_dd.change(
|
| 166 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 167 |
inputs=[model_dd],
|
| 168 |
-
outputs=[model_ear_btn]
|
| 169 |
-
).then(
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
model_ear_btn.click(
|
| 173 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 174 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 175 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 177 |
-
|
| 178 |
flyout_sheet.change(
|
| 179 |
fn=on_flyout_change,
|
| 180 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 181 |
-
outputs=None
|
| 182 |
)
|
| 183 |
|
| 184 |
close_btn.click(
|
| 185 |
fn=close_the_flyout,
|
| 186 |
inputs=None,
|
| 187 |
-
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 188 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 189 |
|
| 190 |
def initial_flyout_setup(sampler_val, model_val):
|
| 191 |
return {
|
| 192 |
-
sampler_ear_btn: update_ear_visibility(
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
}
|
| 195 |
-
|
| 196 |
# --- App Load ---
|
| 197 |
-
demo.load(
|
| 198 |
-
fn=
|
|
|
|
|
|
|
| 199 |
).then(
|
| 200 |
-
fn=
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
js="() => { setTimeout(reparent_flyout, 200); }"
|
| 204 |
)
|
| 205 |
-
|
| 206 |
if __name__ == "__main__":
|
| 207 |
-
demo.launch()
|
|
|
|
| 6 |
from gradio_propertysheet import PropertySheet
|
| 7 |
from gradio_htmlinjector import HTMLInjector
|
| 8 |
|
| 9 |
+
|
| 10 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 11 |
@dataclass
|
| 12 |
class ModelSettings:
|
| 13 |
+
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 14 |
+
default="SDXL", metadata={"component": "dropdown", "label": "Base Model"}
|
| 15 |
+
)
|
| 16 |
+
custom_model_path: str = field(
|
| 17 |
+
default="/path/to/default.safetensors",
|
| 18 |
+
metadata={
|
| 19 |
+
"label": "Custom Model Path",
|
| 20 |
+
"interactive_if": {"field": "model_type", "value": "Custom"},
|
| 21 |
+
},
|
| 22 |
+
)
|
| 23 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 24 |
+
|
| 25 |
+
|
| 26 |
@dataclass
|
| 27 |
class SamplingSettings:
|
| 28 |
+
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 29 |
+
default="DPM++ 2M Karras",
|
| 30 |
+
metadata={"component": "dropdown", "label": "Sampler"},
|
| 31 |
+
)
|
| 32 |
+
steps: int = field(
|
| 33 |
+
default=25,
|
| 34 |
+
metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1},
|
| 35 |
+
)
|
| 36 |
+
cfg_scale: float = field(
|
| 37 |
+
default=7.0,
|
| 38 |
+
metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5},
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
@dataclass
|
| 43 |
class RenderConfig:
|
| 44 |
+
seed: int = field(
|
| 45 |
+
default=-1,
|
| 46 |
+
metadata={"component": "number_integer", "label": "Seed (-1 for random)"},
|
| 47 |
+
)
|
| 48 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 49 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
@dataclass
|
| 53 |
class Lighting:
|
| 54 |
+
sun_intensity: float = field(
|
| 55 |
+
default=1.0,
|
| 56 |
+
metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1},
|
| 57 |
+
)
|
| 58 |
+
color: str = field(
|
| 59 |
+
default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"}
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
@dataclass
|
| 64 |
class EnvironmentConfig:
|
| 65 |
+
background: Literal["Sky", "Color", "Image"] = field(
|
| 66 |
+
default="Sky", metadata={"component": "dropdown"}
|
| 67 |
+
)
|
| 68 |
lighting: Lighting = field(default_factory=Lighting)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
@dataclass
|
| 72 |
class EulerSettings:
|
| 73 |
+
s_churn: float = field(
|
| 74 |
+
default=0.0,
|
| 75 |
+
metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01},
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
@dataclass
|
| 80 |
class DPM_Settings:
|
| 81 |
+
karras_style: bool = field(
|
| 82 |
+
default=True, metadata={"label": "Use Karras Sigma Schedule"}
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
|
| 86 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 87 |
initial_render_config = RenderConfig()
|
| 88 |
initial_env_config = EnvironmentConfig()
|
| 89 |
+
sampler_settings_map_py = {
|
| 90 |
+
"Euler": EulerSettings(),
|
| 91 |
+
"DPM++ 2M Karras": DPM_Settings(),
|
| 92 |
+
"UniPC": None,
|
| 93 |
+
}
|
| 94 |
+
model_settings_map_py = {
|
| 95 |
+
"SDXL 1.0": DPM_Settings(),
|
| 96 |
+
"Stable Diffusion 1.5": EulerSettings(),
|
| 97 |
+
"Pony": None,
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
|
| 101 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 102 |
def inject_assets():
|
|
|
|
| 106 |
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 107 |
css_code = ""
|
| 108 |
js_code = ""
|
| 109 |
+
|
| 110 |
try:
|
| 111 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 112 |
css_code += f.read() + "\n"
|
| 113 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 114 |
js_code += f.read() + "\n"
|
| 115 |
except FileNotFoundError as e:
|
| 116 |
print(f"Warning: Could not read asset file: {e}")
|
| 117 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 118 |
|
| 119 |
+
|
| 120 |
# --- 4. Gradio App Build ---
|
| 121 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 122 |
html_injector = HTMLInjector()
|
| 123 |
gr.Markdown("# PropertySheet Component Demos")
|
| 124 |
+
|
| 125 |
with gr.Row():
|
| 126 |
# --- Flyout popup ---
|
| 127 |
+
with gr.Column(
|
| 128 |
+
elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]
|
| 129 |
+
) as flyout_panel_source:
|
| 130 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 131 |
+
flyout_sheet = PropertySheet(
|
| 132 |
+
visible=True,
|
| 133 |
+
container=False,
|
| 134 |
+
label="Settings",
|
| 135 |
+
show_group_name_only_one=False,
|
| 136 |
+
disable_accordion=True,
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
with gr.Tabs():
|
| 140 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 141 |
+
gr.Markdown(
|
| 142 |
+
"An example of using the `PropertySheet` component as a traditional sidebar for settings."
|
| 143 |
+
)
|
| 144 |
render_state = gr.State(value=initial_render_config)
|
| 145 |
env_state = gr.State(value=initial_env_config)
|
| 146 |
sidebar_visible = gr.State(False)
|
| 147 |
with gr.Row():
|
| 148 |
+
with gr.Column(scale=3):
|
| 149 |
generate = gr.Button("Show Settings", variant="primary")
|
| 150 |
with gr.Row():
|
| 151 |
output_render_json = gr.JSON(label="Live Render State")
|
| 152 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 153 |
with gr.Column(scale=1):
|
| 154 |
+
render_sheet = PropertySheet(
|
| 155 |
+
value=initial_render_config,
|
| 156 |
+
label="Render Settings",
|
| 157 |
+
width=400,
|
| 158 |
+
height=550,
|
| 159 |
+
visible=False,
|
| 160 |
+
root_label="Generator",
|
| 161 |
+
)
|
| 162 |
+
environment_sheet = PropertySheet(
|
| 163 |
+
value=initial_env_config,
|
| 164 |
+
label="Environment Settings",
|
| 165 |
+
width=400,
|
| 166 |
+
open=False,
|
| 167 |
+
visible=False,
|
| 168 |
+
root_label="General",
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 172 |
new_visibility = not is_visible
|
| 173 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 174 |
+
return (
|
| 175 |
+
new_visibility,
|
| 176 |
+
gr.update(visible=new_visibility, value=render_cfg),
|
| 177 |
+
gr.update(visible=new_visibility, value=env_cfg),
|
| 178 |
+
gr.update(value=button_text),
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
def handle_render_change(
|
| 182 |
+
updated_config: RenderConfig, current_state: RenderConfig
|
| 183 |
+
):
|
| 184 |
+
if updated_config is None:
|
| 185 |
+
return current_state, asdict(current_state), current_state
|
| 186 |
+
if updated_config.model.model_type != "Custom":
|
| 187 |
+
updated_config.model.custom_model_path = (
|
| 188 |
+
"/path/to/default.safetensors"
|
| 189 |
+
)
|
| 190 |
return updated_config, asdict(updated_config), updated_config
|
| 191 |
+
|
| 192 |
+
def handle_env_change(
|
| 193 |
+
updated_config: EnvironmentConfig, current_state: EnvironmentConfig
|
| 194 |
+
):
|
| 195 |
+
if updated_config is None:
|
| 196 |
+
return current_state, asdict(current_state), current_state
|
| 197 |
return updated_config, asdict(updated_config), current_state
|
| 198 |
+
|
| 199 |
+
generate.click(
|
| 200 |
+
fn=change_visibility,
|
| 201 |
+
inputs=[sidebar_visible, render_state, env_state],
|
| 202 |
+
outputs=[sidebar_visible, render_sheet, environment_sheet, generate],
|
| 203 |
+
)
|
| 204 |
+
render_sheet.change(
|
| 205 |
+
fn=handle_render_change,
|
| 206 |
+
inputs=[render_sheet, render_state],
|
| 207 |
+
outputs=[render_sheet, output_render_json, render_state],
|
| 208 |
+
)
|
| 209 |
+
environment_sheet.change(
|
| 210 |
+
fn=handle_env_change,
|
| 211 |
+
inputs=[environment_sheet, env_state],
|
| 212 |
+
outputs=[environment_sheet, output_env_json, env_state],
|
| 213 |
+
)
|
| 214 |
+
demo.load(
|
| 215 |
+
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 216 |
+
inputs=[render_state, env_state],
|
| 217 |
+
outputs=[output_render_json, output_env_json],
|
| 218 |
+
)
|
| 219 |
|
| 220 |
with gr.TabItem("Flyout Popup Demo"):
|
| 221 |
+
gr.Markdown(
|
| 222 |
+
"An example of attaching a `PropertySheet` as a flyout panel to other components."
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
# --- State Management ---
|
| 226 |
flyout_visible = gr.State(False)
|
| 227 |
active_anchor_id = gr.State(None)
|
| 228 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 229 |
|
| 230 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 231 |
+
with gr.Row(
|
| 232 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 233 |
+
):
|
| 234 |
+
sampler_dd = gr.Dropdown(
|
| 235 |
+
choices=list(sampler_settings_map_py.keys()),
|
| 236 |
+
label="Sampler",
|
| 237 |
+
value="Euler",
|
| 238 |
+
elem_id="sampler_dd",
|
| 239 |
+
scale=10,
|
| 240 |
+
)
|
| 241 |
+
sampler_ear_btn = gr.Button(
|
| 242 |
+
"⚙️",
|
| 243 |
+
elem_id="sampler_ear_btn",
|
| 244 |
+
scale=1,
|
| 245 |
+
elem_classes=["integrated-ear-btn"],
|
| 246 |
+
)
|
| 247 |
|
| 248 |
+
with gr.Row(
|
| 249 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 250 |
+
):
|
| 251 |
+
model_dd = gr.Dropdown(
|
| 252 |
+
choices=list(model_settings_map_py.keys()),
|
| 253 |
+
label="Model",
|
| 254 |
+
value="SDXL 1.0",
|
| 255 |
+
elem_id="model_dd",
|
| 256 |
+
scale=10,
|
| 257 |
+
)
|
| 258 |
+
model_ear_btn = gr.Button(
|
| 259 |
+
"⚙️",
|
| 260 |
+
elem_id="model_ear_btn",
|
| 261 |
+
scale=1,
|
| 262 |
+
elem_classes=["integrated-ear-btn"],
|
| 263 |
+
)
|
| 264 |
|
| 265 |
# --- Event Logic ---
|
| 266 |
+
def handle_flyout_toggle(
|
| 267 |
+
is_vis, current_anchor, clicked_dropdown_id, settings_obj
|
| 268 |
+
):
|
| 269 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 270 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 271 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 272 |
else:
|
| 273 |
+
js_data = json.dumps(
|
| 274 |
+
{"isVisible": True, "anchorId": clicked_dropdown_id}
|
| 275 |
+
)
|
| 276 |
+
return (
|
| 277 |
+
True,
|
| 278 |
+
clicked_dropdown_id,
|
| 279 |
+
gr.update(value=settings_obj),
|
| 280 |
+
gr.update(value=js_data),
|
| 281 |
+
)
|
| 282 |
|
| 283 |
def update_ear_visibility(selection, settings_map):
|
| 284 |
has_settings = settings_map.get(selection) is not None
|
| 285 |
return gr.update(visible=has_settings)
|
| 286 |
|
| 287 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 288 |
+
if updated_settings is None or active_id is None:
|
| 289 |
return
|
| 290 |
if active_id == sampler_dd.elem_id:
|
| 291 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 292 |
elif active_id == model_dd.elem_id:
|
| 293 |
model_settings_map_py[model_val] = updated_settings
|
| 294 |
+
|
| 295 |
def close_the_flyout():
|
| 296 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 297 |
return False, None, gr.update(value=js_data)
|
| 298 |
+
|
| 299 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 300 |
+
|
| 301 |
sampler_dd.change(
|
| 302 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 303 |
inputs=[sampler_dd],
|
| 304 |
+
outputs=[sampler_ear_btn],
|
| 305 |
+
).then(
|
| 306 |
+
fn=close_the_flyout,
|
| 307 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 308 |
+
).then(
|
| 309 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
sampler_ear_btn.click(
|
| 313 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 314 |
+
is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)
|
| 315 |
+
),
|
| 316 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 317 |
+
outputs=[
|
| 318 |
+
flyout_visible,
|
| 319 |
+
active_anchor_id,
|
| 320 |
+
flyout_sheet,
|
| 321 |
+
js_data_bridge,
|
| 322 |
+
],
|
| 323 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 324 |
|
| 325 |
model_dd.change(
|
| 326 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 327 |
inputs=[model_dd],
|
| 328 |
+
outputs=[model_ear_btn],
|
| 329 |
+
).then(
|
| 330 |
+
fn=close_the_flyout,
|
| 331 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 332 |
+
).then(
|
| 333 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
model_ear_btn.click(
|
| 337 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 338 |
+
is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)
|
| 339 |
+
),
|
| 340 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 341 |
+
outputs=[
|
| 342 |
+
flyout_visible,
|
| 343 |
+
active_anchor_id,
|
| 344 |
+
flyout_sheet,
|
| 345 |
+
js_data_bridge,
|
| 346 |
+
],
|
| 347 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 348 |
+
|
| 349 |
flyout_sheet.change(
|
| 350 |
fn=on_flyout_change,
|
| 351 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 352 |
+
outputs=None,
|
| 353 |
)
|
| 354 |
|
| 355 |
close_btn.click(
|
| 356 |
fn=close_the_flyout,
|
| 357 |
inputs=None,
|
| 358 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 359 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 360 |
|
| 361 |
def initial_flyout_setup(sampler_val, model_val):
|
| 362 |
return {
|
| 363 |
+
sampler_ear_btn: update_ear_visibility(
|
| 364 |
+
sampler_val, sampler_settings_map_py
|
| 365 |
+
),
|
| 366 |
+
model_ear_btn: update_ear_visibility(
|
| 367 |
+
model_val, model_settings_map_py
|
| 368 |
+
),
|
| 369 |
}
|
| 370 |
+
|
| 371 |
# --- App Load ---
|
| 372 |
+
demo.load(fn=inject_assets, inputs=None, outputs=[html_injector]).then(
|
| 373 |
+
fn=initial_flyout_setup,
|
| 374 |
+
inputs=[sampler_dd, model_dd],
|
| 375 |
+
outputs=[sampler_ear_btn, model_ear_btn],
|
| 376 |
).then(
|
| 377 |
+
fn=None,
|
| 378 |
+
inputs=None,
|
| 379 |
+
outputs=None,
|
| 380 |
+
js="() => { setTimeout(reparent_flyout, 200); }",
|
| 381 |
)
|
| 382 |
+
|
| 383 |
if __name__ == "__main__":
|
| 384 |
+
demo.launch()
|
space.py
CHANGED
|
@@ -46,42 +46,97 @@ from typing import Literal
|
|
| 46 |
from gradio_propertysheet import PropertySheet
|
| 47 |
from gradio_htmlinjector import HTMLInjector
|
| 48 |
|
|
|
|
| 49 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 50 |
@dataclass
|
| 51 |
class ModelSettings:
|
| 52 |
-
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
|
|
|
| 55 |
@dataclass
|
| 56 |
class SamplingSettings:
|
| 57 |
-
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
@dataclass
|
| 61 |
class RenderConfig:
|
| 62 |
-
seed: int = field(
|
|
|
|
|
|
|
|
|
|
| 63 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 64 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
|
|
|
| 65 |
@dataclass
|
| 66 |
class Lighting:
|
| 67 |
-
sun_intensity: float = field(
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
@dataclass
|
| 70 |
class EnvironmentConfig:
|
| 71 |
-
background: Literal["Sky", "Color", "Image"] = field(
|
|
|
|
|
|
|
| 72 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 73 |
@dataclass
|
| 74 |
class EulerSettings:
|
| 75 |
-
s_churn: float = field(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
@dataclass
|
| 77 |
class DPM_Settings:
|
| 78 |
-
karras_style: bool = field(
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 81 |
initial_render_config = RenderConfig()
|
| 82 |
initial_env_config = EnvironmentConfig()
|
| 83 |
-
sampler_settings_map_py = {
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 87 |
def inject_assets():
|
|
@@ -91,160 +146,283 @@ def inject_assets():
|
|
| 91 |
popup_html = \"\"\"<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>\"\"\"
|
| 92 |
css_code = ""
|
| 93 |
js_code = ""
|
| 94 |
-
|
| 95 |
try:
|
| 96 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 97 |
css_code += f.read() + "\n"
|
| 98 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 99 |
js_code += f.read() + "\n"
|
| 100 |
except FileNotFoundError as e:
|
| 101 |
print(f"Warning: Could not read asset file: {e}")
|
| 102 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 103 |
|
|
|
|
| 104 |
# --- 4. Gradio App Build ---
|
| 105 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 106 |
html_injector = HTMLInjector()
|
| 107 |
gr.Markdown("# PropertySheet Component Demos")
|
| 108 |
-
|
| 109 |
with gr.Row():
|
| 110 |
# --- Flyout popup ---
|
| 111 |
-
with gr.Column(
|
|
|
|
|
|
|
| 112 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 113 |
-
flyout_sheet = PropertySheet(
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
with gr.Tabs():
|
| 116 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 117 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 118 |
render_state = gr.State(value=initial_render_config)
|
| 119 |
env_state = gr.State(value=initial_env_config)
|
| 120 |
sidebar_visible = gr.State(False)
|
| 121 |
with gr.Row():
|
| 122 |
-
with gr.Column(scale=3):
|
| 123 |
generate = gr.Button("Show Settings", variant="primary")
|
| 124 |
with gr.Row():
|
| 125 |
output_render_json = gr.JSON(label="Live Render State")
|
| 126 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 127 |
with gr.Column(scale=1):
|
| 128 |
-
render_sheet = PropertySheet(
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 131 |
new_visibility = not is_visible
|
| 132 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 133 |
-
return (
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
return updated_config, asdict(updated_config), updated_config
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
return updated_config, asdict(updated_config), current_state
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
with gr.TabItem("Flyout Popup Demo"):
|
| 147 |
-
gr.Markdown(
|
| 148 |
-
|
|
|
|
|
|
|
| 149 |
# --- State Management ---
|
| 150 |
flyout_visible = gr.State(False)
|
| 151 |
active_anchor_id = gr.State(None)
|
| 152 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 153 |
|
| 154 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 155 |
-
with gr.Row(
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# --- Event Logic ---
|
| 166 |
-
def handle_flyout_toggle(
|
|
|
|
|
|
|
| 167 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 168 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 169 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 170 |
else:
|
| 171 |
-
js_data = json.dumps(
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
def update_ear_visibility(selection, settings_map):
|
| 175 |
has_settings = settings_map.get(selection) is not None
|
| 176 |
return gr.update(visible=has_settings)
|
| 177 |
|
| 178 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 179 |
-
if updated_settings is None or active_id is None:
|
| 180 |
return
|
| 181 |
if active_id == sampler_dd.elem_id:
|
| 182 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 183 |
elif active_id == model_dd.elem_id:
|
| 184 |
model_settings_map_py[model_val] = updated_settings
|
| 185 |
-
|
| 186 |
def close_the_flyout():
|
| 187 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 188 |
return False, None, gr.update(value=js_data)
|
| 189 |
-
|
| 190 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 191 |
-
|
| 192 |
sampler_dd.change(
|
| 193 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 194 |
inputs=[sampler_dd],
|
| 195 |
-
outputs=[sampler_ear_btn]
|
| 196 |
-
).then(
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
sampler_ear_btn.click(
|
| 200 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 201 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 202 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 204 |
|
| 205 |
model_dd.change(
|
| 206 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 207 |
inputs=[model_dd],
|
| 208 |
-
outputs=[model_ear_btn]
|
| 209 |
-
).then(
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
model_ear_btn.click(
|
| 213 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 214 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 215 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 217 |
-
|
| 218 |
flyout_sheet.change(
|
| 219 |
fn=on_flyout_change,
|
| 220 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 221 |
-
outputs=None
|
| 222 |
)
|
| 223 |
|
| 224 |
close_btn.click(
|
| 225 |
fn=close_the_flyout,
|
| 226 |
inputs=None,
|
| 227 |
-
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 228 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 229 |
|
| 230 |
def initial_flyout_setup(sampler_val, model_val):
|
| 231 |
return {
|
| 232 |
-
sampler_ear_btn: update_ear_visibility(
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
}
|
| 235 |
-
|
| 236 |
# --- App Load ---
|
| 237 |
-
demo.load(
|
| 238 |
-
fn=
|
| 239 |
-
|
| 240 |
-
|
| 241 |
).then(
|
| 242 |
-
fn=None,
|
| 243 |
-
|
|
|
|
|
|
|
| 244 |
)
|
| 245 |
-
|
| 246 |
if __name__ == "__main__":
|
| 247 |
demo.launch()
|
|
|
|
| 248 |
```
|
| 249 |
""", elem_classes=["md-custom"], header_links=True)
|
| 250 |
|
|
|
|
| 46 |
from gradio_propertysheet import PropertySheet
|
| 47 |
from gradio_htmlinjector import HTMLInjector
|
| 48 |
|
| 49 |
+
|
| 50 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 51 |
@dataclass
|
| 52 |
class ModelSettings:
|
| 53 |
+
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 54 |
+
default="SDXL", metadata={"component": "dropdown", "label": "Base Model"}
|
| 55 |
+
)
|
| 56 |
+
custom_model_path: str = field(
|
| 57 |
+
default="/path/to/default.safetensors",
|
| 58 |
+
metadata={
|
| 59 |
+
"label": "Custom Model Path",
|
| 60 |
+
"interactive_if": {"field": "model_type", "value": "Custom"},
|
| 61 |
+
},
|
| 62 |
+
)
|
| 63 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 64 |
+
|
| 65 |
+
|
| 66 |
@dataclass
|
| 67 |
class SamplingSettings:
|
| 68 |
+
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 69 |
+
default="DPM++ 2M Karras",
|
| 70 |
+
metadata={"component": "dropdown", "label": "Sampler"},
|
| 71 |
+
)
|
| 72 |
+
steps: int = field(
|
| 73 |
+
default=25,
|
| 74 |
+
metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1},
|
| 75 |
+
)
|
| 76 |
+
cfg_scale: float = field(
|
| 77 |
+
default=7.0,
|
| 78 |
+
metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5},
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
@dataclass
|
| 83 |
class RenderConfig:
|
| 84 |
+
seed: int = field(
|
| 85 |
+
default=-1,
|
| 86 |
+
metadata={"component": "number_integer", "label": "Seed (-1 for random)"},
|
| 87 |
+
)
|
| 88 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 89 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
@dataclass
|
| 93 |
class Lighting:
|
| 94 |
+
sun_intensity: float = field(
|
| 95 |
+
default=1.0,
|
| 96 |
+
metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1},
|
| 97 |
+
)
|
| 98 |
+
color: str = field(
|
| 99 |
+
default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"}
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
@dataclass
|
| 104 |
class EnvironmentConfig:
|
| 105 |
+
background: Literal["Sky", "Color", "Image"] = field(
|
| 106 |
+
default="Sky", metadata={"component": "dropdown"}
|
| 107 |
+
)
|
| 108 |
lighting: Lighting = field(default_factory=Lighting)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
@dataclass
|
| 112 |
class EulerSettings:
|
| 113 |
+
s_churn: float = field(
|
| 114 |
+
default=0.0,
|
| 115 |
+
metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01},
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
@dataclass
|
| 120 |
class DPM_Settings:
|
| 121 |
+
karras_style: bool = field(
|
| 122 |
+
default=True, metadata={"label": "Use Karras Sigma Schedule"}
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
|
| 126 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 127 |
initial_render_config = RenderConfig()
|
| 128 |
initial_env_config = EnvironmentConfig()
|
| 129 |
+
sampler_settings_map_py = {
|
| 130 |
+
"Euler": EulerSettings(),
|
| 131 |
+
"DPM++ 2M Karras": DPM_Settings(),
|
| 132 |
+
"UniPC": None,
|
| 133 |
+
}
|
| 134 |
+
model_settings_map_py = {
|
| 135 |
+
"SDXL 1.0": DPM_Settings(),
|
| 136 |
+
"Stable Diffusion 1.5": EulerSettings(),
|
| 137 |
+
"Pony": None,
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
|
| 141 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 142 |
def inject_assets():
|
|
|
|
| 146 |
popup_html = \"\"\"<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>\"\"\"
|
| 147 |
css_code = ""
|
| 148 |
js_code = ""
|
| 149 |
+
|
| 150 |
try:
|
| 151 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 152 |
css_code += f.read() + "\n"
|
| 153 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 154 |
js_code += f.read() + "\n"
|
| 155 |
except FileNotFoundError as e:
|
| 156 |
print(f"Warning: Could not read asset file: {e}")
|
| 157 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 158 |
|
| 159 |
+
|
| 160 |
# --- 4. Gradio App Build ---
|
| 161 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 162 |
html_injector = HTMLInjector()
|
| 163 |
gr.Markdown("# PropertySheet Component Demos")
|
| 164 |
+
|
| 165 |
with gr.Row():
|
| 166 |
# --- Flyout popup ---
|
| 167 |
+
with gr.Column(
|
| 168 |
+
elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]
|
| 169 |
+
) as flyout_panel_source:
|
| 170 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 171 |
+
flyout_sheet = PropertySheet(
|
| 172 |
+
visible=True,
|
| 173 |
+
container=False,
|
| 174 |
+
label="Settings",
|
| 175 |
+
show_group_name_only_one=False,
|
| 176 |
+
disable_accordion=True,
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
with gr.Tabs():
|
| 180 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 181 |
+
gr.Markdown(
|
| 182 |
+
"An example of using the `PropertySheet` component as a traditional sidebar for settings."
|
| 183 |
+
)
|
| 184 |
render_state = gr.State(value=initial_render_config)
|
| 185 |
env_state = gr.State(value=initial_env_config)
|
| 186 |
sidebar_visible = gr.State(False)
|
| 187 |
with gr.Row():
|
| 188 |
+
with gr.Column(scale=3):
|
| 189 |
generate = gr.Button("Show Settings", variant="primary")
|
| 190 |
with gr.Row():
|
| 191 |
output_render_json = gr.JSON(label="Live Render State")
|
| 192 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 193 |
with gr.Column(scale=1):
|
| 194 |
+
render_sheet = PropertySheet(
|
| 195 |
+
value=initial_render_config,
|
| 196 |
+
label="Render Settings",
|
| 197 |
+
width=400,
|
| 198 |
+
height=550,
|
| 199 |
+
visible=False,
|
| 200 |
+
root_label="Generator",
|
| 201 |
+
)
|
| 202 |
+
environment_sheet = PropertySheet(
|
| 203 |
+
value=initial_env_config,
|
| 204 |
+
label="Environment Settings",
|
| 205 |
+
width=400,
|
| 206 |
+
open=False,
|
| 207 |
+
visible=False,
|
| 208 |
+
root_label="General",
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 212 |
new_visibility = not is_visible
|
| 213 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 214 |
+
return (
|
| 215 |
+
new_visibility,
|
| 216 |
+
gr.update(visible=new_visibility, value=render_cfg),
|
| 217 |
+
gr.update(visible=new_visibility, value=env_cfg),
|
| 218 |
+
gr.update(value=button_text),
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
def handle_render_change(
|
| 222 |
+
updated_config: RenderConfig, current_state: RenderConfig
|
| 223 |
+
):
|
| 224 |
+
if updated_config is None:
|
| 225 |
+
return current_state, asdict(current_state), current_state
|
| 226 |
+
if updated_config.model.model_type != "Custom":
|
| 227 |
+
updated_config.model.custom_model_path = (
|
| 228 |
+
"/path/to/default.safetensors"
|
| 229 |
+
)
|
| 230 |
return updated_config, asdict(updated_config), updated_config
|
| 231 |
+
|
| 232 |
+
def handle_env_change(
|
| 233 |
+
updated_config: EnvironmentConfig, current_state: EnvironmentConfig
|
| 234 |
+
):
|
| 235 |
+
if updated_config is None:
|
| 236 |
+
return current_state, asdict(current_state), current_state
|
| 237 |
return updated_config, asdict(updated_config), current_state
|
| 238 |
+
|
| 239 |
+
generate.click(
|
| 240 |
+
fn=change_visibility,
|
| 241 |
+
inputs=[sidebar_visible, render_state, env_state],
|
| 242 |
+
outputs=[sidebar_visible, render_sheet, environment_sheet, generate],
|
| 243 |
+
)
|
| 244 |
+
render_sheet.change(
|
| 245 |
+
fn=handle_render_change,
|
| 246 |
+
inputs=[render_sheet, render_state],
|
| 247 |
+
outputs=[render_sheet, output_render_json, render_state],
|
| 248 |
+
)
|
| 249 |
+
environment_sheet.change(
|
| 250 |
+
fn=handle_env_change,
|
| 251 |
+
inputs=[environment_sheet, env_state],
|
| 252 |
+
outputs=[environment_sheet, output_env_json, env_state],
|
| 253 |
+
)
|
| 254 |
+
demo.load(
|
| 255 |
+
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 256 |
+
inputs=[render_state, env_state],
|
| 257 |
+
outputs=[output_render_json, output_env_json],
|
| 258 |
+
)
|
| 259 |
|
| 260 |
with gr.TabItem("Flyout Popup Demo"):
|
| 261 |
+
gr.Markdown(
|
| 262 |
+
"An example of attaching a `PropertySheet` as a flyout panel to other components."
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
# --- State Management ---
|
| 266 |
flyout_visible = gr.State(False)
|
| 267 |
active_anchor_id = gr.State(None)
|
| 268 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 269 |
|
| 270 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 271 |
+
with gr.Row(
|
| 272 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 273 |
+
):
|
| 274 |
+
sampler_dd = gr.Dropdown(
|
| 275 |
+
choices=list(sampler_settings_map_py.keys()),
|
| 276 |
+
label="Sampler",
|
| 277 |
+
value="Euler",
|
| 278 |
+
elem_id="sampler_dd",
|
| 279 |
+
scale=10,
|
| 280 |
+
)
|
| 281 |
+
sampler_ear_btn = gr.Button(
|
| 282 |
+
"⚙️",
|
| 283 |
+
elem_id="sampler_ear_btn",
|
| 284 |
+
scale=1,
|
| 285 |
+
elem_classes=["integrated-ear-btn"],
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
with gr.Row(
|
| 289 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 290 |
+
):
|
| 291 |
+
model_dd = gr.Dropdown(
|
| 292 |
+
choices=list(model_settings_map_py.keys()),
|
| 293 |
+
label="Model",
|
| 294 |
+
value="SDXL 1.0",
|
| 295 |
+
elem_id="model_dd",
|
| 296 |
+
scale=10,
|
| 297 |
+
)
|
| 298 |
+
model_ear_btn = gr.Button(
|
| 299 |
+
"⚙️",
|
| 300 |
+
elem_id="model_ear_btn",
|
| 301 |
+
scale=1,
|
| 302 |
+
elem_classes=["integrated-ear-btn"],
|
| 303 |
+
)
|
| 304 |
|
| 305 |
# --- Event Logic ---
|
| 306 |
+
def handle_flyout_toggle(
|
| 307 |
+
is_vis, current_anchor, clicked_dropdown_id, settings_obj
|
| 308 |
+
):
|
| 309 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 310 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 311 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 312 |
else:
|
| 313 |
+
js_data = json.dumps(
|
| 314 |
+
{"isVisible": True, "anchorId": clicked_dropdown_id}
|
| 315 |
+
)
|
| 316 |
+
return (
|
| 317 |
+
True,
|
| 318 |
+
clicked_dropdown_id,
|
| 319 |
+
gr.update(value=settings_obj),
|
| 320 |
+
gr.update(value=js_data),
|
| 321 |
+
)
|
| 322 |
|
| 323 |
def update_ear_visibility(selection, settings_map):
|
| 324 |
has_settings = settings_map.get(selection) is not None
|
| 325 |
return gr.update(visible=has_settings)
|
| 326 |
|
| 327 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 328 |
+
if updated_settings is None or active_id is None:
|
| 329 |
return
|
| 330 |
if active_id == sampler_dd.elem_id:
|
| 331 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 332 |
elif active_id == model_dd.elem_id:
|
| 333 |
model_settings_map_py[model_val] = updated_settings
|
| 334 |
+
|
| 335 |
def close_the_flyout():
|
| 336 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 337 |
return False, None, gr.update(value=js_data)
|
| 338 |
+
|
| 339 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 340 |
+
|
| 341 |
sampler_dd.change(
|
| 342 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 343 |
inputs=[sampler_dd],
|
| 344 |
+
outputs=[sampler_ear_btn],
|
| 345 |
+
).then(
|
| 346 |
+
fn=close_the_flyout,
|
| 347 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 348 |
+
).then(
|
| 349 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
sampler_ear_btn.click(
|
| 353 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 354 |
+
is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)
|
| 355 |
+
),
|
| 356 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 357 |
+
outputs=[
|
| 358 |
+
flyout_visible,
|
| 359 |
+
active_anchor_id,
|
| 360 |
+
flyout_sheet,
|
| 361 |
+
js_data_bridge,
|
| 362 |
+
],
|
| 363 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 364 |
|
| 365 |
model_dd.change(
|
| 366 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 367 |
inputs=[model_dd],
|
| 368 |
+
outputs=[model_ear_btn],
|
| 369 |
+
).then(
|
| 370 |
+
fn=close_the_flyout,
|
| 371 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 372 |
+
).then(
|
| 373 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
model_ear_btn.click(
|
| 377 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 378 |
+
is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)
|
| 379 |
+
),
|
| 380 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 381 |
+
outputs=[
|
| 382 |
+
flyout_visible,
|
| 383 |
+
active_anchor_id,
|
| 384 |
+
flyout_sheet,
|
| 385 |
+
js_data_bridge,
|
| 386 |
+
],
|
| 387 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 388 |
+
|
| 389 |
flyout_sheet.change(
|
| 390 |
fn=on_flyout_change,
|
| 391 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 392 |
+
outputs=None,
|
| 393 |
)
|
| 394 |
|
| 395 |
close_btn.click(
|
| 396 |
fn=close_the_flyout,
|
| 397 |
inputs=None,
|
| 398 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 399 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 400 |
|
| 401 |
def initial_flyout_setup(sampler_val, model_val):
|
| 402 |
return {
|
| 403 |
+
sampler_ear_btn: update_ear_visibility(
|
| 404 |
+
sampler_val, sampler_settings_map_py
|
| 405 |
+
),
|
| 406 |
+
model_ear_btn: update_ear_visibility(
|
| 407 |
+
model_val, model_settings_map_py
|
| 408 |
+
),
|
| 409 |
}
|
| 410 |
+
|
| 411 |
# --- App Load ---
|
| 412 |
+
demo.load(fn=inject_assets, inputs=None, outputs=[html_injector]).then(
|
| 413 |
+
fn=initial_flyout_setup,
|
| 414 |
+
inputs=[sampler_dd, model_dd],
|
| 415 |
+
outputs=[sampler_ear_btn, model_ear_btn],
|
| 416 |
).then(
|
| 417 |
+
fn=None,
|
| 418 |
+
inputs=None,
|
| 419 |
+
outputs=None,
|
| 420 |
+
js="() => { setTimeout(reparent_flyout, 200); }",
|
| 421 |
)
|
| 422 |
+
|
| 423 |
if __name__ == "__main__":
|
| 424 |
demo.launch()
|
| 425 |
+
|
| 426 |
```
|
| 427 |
""", elem_classes=["md-custom"], header_links=True)
|
| 428 |
|
src/README.md
CHANGED
|
@@ -38,7 +38,6 @@ The **PropertySheet** component for Gradio allows you to automatically generate
|
|
| 38 |
|
| 39 |
## Installation
|
| 40 |
|
| 41 |
-
|
| 42 |
```bash
|
| 43 |
pip install gradio_propertysheet
|
| 44 |
```
|
|
@@ -54,42 +53,97 @@ from typing import Literal
|
|
| 54 |
from gradio_propertysheet import PropertySheet
|
| 55 |
from gradio_htmlinjector import HTMLInjector
|
| 56 |
|
|
|
|
| 57 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 58 |
@dataclass
|
| 59 |
class ModelSettings:
|
| 60 |
-
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
|
|
|
| 63 |
@dataclass
|
| 64 |
class SamplingSettings:
|
| 65 |
-
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
@dataclass
|
| 69 |
class RenderConfig:
|
| 70 |
-
seed: int = field(
|
|
|
|
|
|
|
|
|
|
| 71 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 72 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
|
|
|
| 73 |
@dataclass
|
| 74 |
class Lighting:
|
| 75 |
-
sun_intensity: float = field(
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
@dataclass
|
| 78 |
class EnvironmentConfig:
|
| 79 |
-
background: Literal["Sky", "Color", "Image"] = field(
|
|
|
|
|
|
|
| 80 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 81 |
@dataclass
|
| 82 |
class EulerSettings:
|
| 83 |
-
s_churn: float = field(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
@dataclass
|
| 85 |
class DPM_Settings:
|
| 86 |
-
karras_style: bool = field(
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 89 |
initial_render_config = RenderConfig()
|
| 90 |
initial_env_config = EnvironmentConfig()
|
| 91 |
-
sampler_settings_map_py = {
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 95 |
def inject_assets():
|
|
@@ -99,160 +153,283 @@ def inject_assets():
|
|
| 99 |
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 100 |
css_code = ""
|
| 101 |
js_code = ""
|
| 102 |
-
|
| 103 |
try:
|
| 104 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 105 |
css_code += f.read() + "\n"
|
| 106 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 107 |
js_code += f.read() + "\n"
|
| 108 |
except FileNotFoundError as e:
|
| 109 |
print(f"Warning: Could not read asset file: {e}")
|
| 110 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 111 |
|
|
|
|
| 112 |
# --- 4. Gradio App Build ---
|
| 113 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 114 |
html_injector = HTMLInjector()
|
| 115 |
gr.Markdown("# PropertySheet Component Demos")
|
| 116 |
-
|
| 117 |
with gr.Row():
|
| 118 |
# --- Flyout popup ---
|
| 119 |
-
with gr.Column(
|
|
|
|
|
|
|
| 120 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 121 |
-
flyout_sheet = PropertySheet(
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
with gr.Tabs():
|
| 124 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 125 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 126 |
render_state = gr.State(value=initial_render_config)
|
| 127 |
env_state = gr.State(value=initial_env_config)
|
| 128 |
sidebar_visible = gr.State(False)
|
| 129 |
with gr.Row():
|
| 130 |
-
with gr.Column(scale=3):
|
| 131 |
generate = gr.Button("Show Settings", variant="primary")
|
| 132 |
with gr.Row():
|
| 133 |
output_render_json = gr.JSON(label="Live Render State")
|
| 134 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
-
render_sheet = PropertySheet(
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 139 |
new_visibility = not is_visible
|
| 140 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 141 |
-
return (
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
return updated_config, asdict(updated_config), updated_config
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
return updated_config, asdict(updated_config), current_state
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
with gr.TabItem("Flyout Popup Demo"):
|
| 155 |
-
gr.Markdown(
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
# --- State Management ---
|
| 158 |
flyout_visible = gr.State(False)
|
| 159 |
active_anchor_id = gr.State(None)
|
| 160 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 161 |
|
| 162 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 163 |
-
with gr.Row(
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
# --- Event Logic ---
|
| 174 |
-
def handle_flyout_toggle(
|
|
|
|
|
|
|
| 175 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 176 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 177 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 178 |
else:
|
| 179 |
-
js_data = json.dumps(
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
def update_ear_visibility(selection, settings_map):
|
| 183 |
has_settings = settings_map.get(selection) is not None
|
| 184 |
return gr.update(visible=has_settings)
|
| 185 |
|
| 186 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 187 |
-
if updated_settings is None or active_id is None:
|
| 188 |
return
|
| 189 |
if active_id == sampler_dd.elem_id:
|
| 190 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 191 |
elif active_id == model_dd.elem_id:
|
| 192 |
model_settings_map_py[model_val] = updated_settings
|
| 193 |
-
|
| 194 |
def close_the_flyout():
|
| 195 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 196 |
return False, None, gr.update(value=js_data)
|
| 197 |
-
|
| 198 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 199 |
-
|
| 200 |
sampler_dd.change(
|
| 201 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 202 |
inputs=[sampler_dd],
|
| 203 |
-
outputs=[sampler_ear_btn]
|
| 204 |
-
).then(
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
sampler_ear_btn.click(
|
| 208 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 209 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 210 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 212 |
|
| 213 |
model_dd.change(
|
| 214 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 215 |
inputs=[model_dd],
|
| 216 |
-
outputs=[model_ear_btn]
|
| 217 |
-
).then(
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
model_ear_btn.click(
|
| 221 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 222 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 223 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 225 |
-
|
| 226 |
flyout_sheet.change(
|
| 227 |
fn=on_flyout_change,
|
| 228 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 229 |
-
outputs=None
|
| 230 |
)
|
| 231 |
|
| 232 |
close_btn.click(
|
| 233 |
fn=close_the_flyout,
|
| 234 |
inputs=None,
|
| 235 |
-
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 236 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 237 |
|
| 238 |
def initial_flyout_setup(sampler_val, model_val):
|
| 239 |
return {
|
| 240 |
-
sampler_ear_btn: update_ear_visibility(
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
}
|
| 243 |
-
|
| 244 |
# --- App Load ---
|
| 245 |
-
demo.load(
|
| 246 |
-
fn=
|
| 247 |
-
|
| 248 |
-
|
| 249 |
).then(
|
| 250 |
-
fn=None,
|
| 251 |
-
|
|
|
|
|
|
|
| 252 |
)
|
| 253 |
-
|
| 254 |
if __name__ == "__main__":
|
| 255 |
demo.launch()
|
|
|
|
| 256 |
```
|
| 257 |
|
| 258 |
## `PropertySheet`
|
|
|
|
| 38 |
|
| 39 |
## Installation
|
| 40 |
|
|
|
|
| 41 |
```bash
|
| 42 |
pip install gradio_propertysheet
|
| 43 |
```
|
|
|
|
| 53 |
from gradio_propertysheet import PropertySheet
|
| 54 |
from gradio_htmlinjector import HTMLInjector
|
| 55 |
|
| 56 |
+
|
| 57 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 58 |
@dataclass
|
| 59 |
class ModelSettings:
|
| 60 |
+
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 61 |
+
default="SDXL", metadata={"component": "dropdown", "label": "Base Model"}
|
| 62 |
+
)
|
| 63 |
+
custom_model_path: str = field(
|
| 64 |
+
default="/path/to/default.safetensors",
|
| 65 |
+
metadata={
|
| 66 |
+
"label": "Custom Model Path",
|
| 67 |
+
"interactive_if": {"field": "model_type", "value": "Custom"},
|
| 68 |
+
},
|
| 69 |
+
)
|
| 70 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 71 |
+
|
| 72 |
+
|
| 73 |
@dataclass
|
| 74 |
class SamplingSettings:
|
| 75 |
+
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 76 |
+
default="DPM++ 2M Karras",
|
| 77 |
+
metadata={"component": "dropdown", "label": "Sampler"},
|
| 78 |
+
)
|
| 79 |
+
steps: int = field(
|
| 80 |
+
default=25,
|
| 81 |
+
metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1},
|
| 82 |
+
)
|
| 83 |
+
cfg_scale: float = field(
|
| 84 |
+
default=7.0,
|
| 85 |
+
metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5},
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
@dataclass
|
| 90 |
class RenderConfig:
|
| 91 |
+
seed: int = field(
|
| 92 |
+
default=-1,
|
| 93 |
+
metadata={"component": "number_integer", "label": "Seed (-1 for random)"},
|
| 94 |
+
)
|
| 95 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 96 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
@dataclass
|
| 100 |
class Lighting:
|
| 101 |
+
sun_intensity: float = field(
|
| 102 |
+
default=1.0,
|
| 103 |
+
metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1},
|
| 104 |
+
)
|
| 105 |
+
color: str = field(
|
| 106 |
+
default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"}
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
@dataclass
|
| 111 |
class EnvironmentConfig:
|
| 112 |
+
background: Literal["Sky", "Color", "Image"] = field(
|
| 113 |
+
default="Sky", metadata={"component": "dropdown"}
|
| 114 |
+
)
|
| 115 |
lighting: Lighting = field(default_factory=Lighting)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
@dataclass
|
| 119 |
class EulerSettings:
|
| 120 |
+
s_churn: float = field(
|
| 121 |
+
default=0.0,
|
| 122 |
+
metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01},
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
@dataclass
|
| 127 |
class DPM_Settings:
|
| 128 |
+
karras_style: bool = field(
|
| 129 |
+
default=True, metadata={"label": "Use Karras Sigma Schedule"}
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
|
| 133 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 134 |
initial_render_config = RenderConfig()
|
| 135 |
initial_env_config = EnvironmentConfig()
|
| 136 |
+
sampler_settings_map_py = {
|
| 137 |
+
"Euler": EulerSettings(),
|
| 138 |
+
"DPM++ 2M Karras": DPM_Settings(),
|
| 139 |
+
"UniPC": None,
|
| 140 |
+
}
|
| 141 |
+
model_settings_map_py = {
|
| 142 |
+
"SDXL 1.0": DPM_Settings(),
|
| 143 |
+
"Stable Diffusion 1.5": EulerSettings(),
|
| 144 |
+
"Pony": None,
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
|
| 148 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 149 |
def inject_assets():
|
|
|
|
| 153 |
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 154 |
css_code = ""
|
| 155 |
js_code = ""
|
| 156 |
+
|
| 157 |
try:
|
| 158 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 159 |
css_code += f.read() + "\n"
|
| 160 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 161 |
js_code += f.read() + "\n"
|
| 162 |
except FileNotFoundError as e:
|
| 163 |
print(f"Warning: Could not read asset file: {e}")
|
| 164 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 165 |
|
| 166 |
+
|
| 167 |
# --- 4. Gradio App Build ---
|
| 168 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 169 |
html_injector = HTMLInjector()
|
| 170 |
gr.Markdown("# PropertySheet Component Demos")
|
| 171 |
+
|
| 172 |
with gr.Row():
|
| 173 |
# --- Flyout popup ---
|
| 174 |
+
with gr.Column(
|
| 175 |
+
elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]
|
| 176 |
+
) as flyout_panel_source:
|
| 177 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 178 |
+
flyout_sheet = PropertySheet(
|
| 179 |
+
visible=True,
|
| 180 |
+
container=False,
|
| 181 |
+
label="Settings",
|
| 182 |
+
show_group_name_only_one=False,
|
| 183 |
+
disable_accordion=True,
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
with gr.Tabs():
|
| 187 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 188 |
+
gr.Markdown(
|
| 189 |
+
"An example of using the `PropertySheet` component as a traditional sidebar for settings."
|
| 190 |
+
)
|
| 191 |
render_state = gr.State(value=initial_render_config)
|
| 192 |
env_state = gr.State(value=initial_env_config)
|
| 193 |
sidebar_visible = gr.State(False)
|
| 194 |
with gr.Row():
|
| 195 |
+
with gr.Column(scale=3):
|
| 196 |
generate = gr.Button("Show Settings", variant="primary")
|
| 197 |
with gr.Row():
|
| 198 |
output_render_json = gr.JSON(label="Live Render State")
|
| 199 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 200 |
with gr.Column(scale=1):
|
| 201 |
+
render_sheet = PropertySheet(
|
| 202 |
+
value=initial_render_config,
|
| 203 |
+
label="Render Settings",
|
| 204 |
+
width=400,
|
| 205 |
+
height=550,
|
| 206 |
+
visible=False,
|
| 207 |
+
root_label="Generator",
|
| 208 |
+
)
|
| 209 |
+
environment_sheet = PropertySheet(
|
| 210 |
+
value=initial_env_config,
|
| 211 |
+
label="Environment Settings",
|
| 212 |
+
width=400,
|
| 213 |
+
open=False,
|
| 214 |
+
visible=False,
|
| 215 |
+
root_label="General",
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 219 |
new_visibility = not is_visible
|
| 220 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 221 |
+
return (
|
| 222 |
+
new_visibility,
|
| 223 |
+
gr.update(visible=new_visibility, value=render_cfg),
|
| 224 |
+
gr.update(visible=new_visibility, value=env_cfg),
|
| 225 |
+
gr.update(value=button_text),
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
def handle_render_change(
|
| 229 |
+
updated_config: RenderConfig, current_state: RenderConfig
|
| 230 |
+
):
|
| 231 |
+
if updated_config is None:
|
| 232 |
+
return current_state, asdict(current_state), current_state
|
| 233 |
+
if updated_config.model.model_type != "Custom":
|
| 234 |
+
updated_config.model.custom_model_path = (
|
| 235 |
+
"/path/to/default.safetensors"
|
| 236 |
+
)
|
| 237 |
return updated_config, asdict(updated_config), updated_config
|
| 238 |
+
|
| 239 |
+
def handle_env_change(
|
| 240 |
+
updated_config: EnvironmentConfig, current_state: EnvironmentConfig
|
| 241 |
+
):
|
| 242 |
+
if updated_config is None:
|
| 243 |
+
return current_state, asdict(current_state), current_state
|
| 244 |
return updated_config, asdict(updated_config), current_state
|
| 245 |
+
|
| 246 |
+
generate.click(
|
| 247 |
+
fn=change_visibility,
|
| 248 |
+
inputs=[sidebar_visible, render_state, env_state],
|
| 249 |
+
outputs=[sidebar_visible, render_sheet, environment_sheet, generate],
|
| 250 |
+
)
|
| 251 |
+
render_sheet.change(
|
| 252 |
+
fn=handle_render_change,
|
| 253 |
+
inputs=[render_sheet, render_state],
|
| 254 |
+
outputs=[render_sheet, output_render_json, render_state],
|
| 255 |
+
)
|
| 256 |
+
environment_sheet.change(
|
| 257 |
+
fn=handle_env_change,
|
| 258 |
+
inputs=[environment_sheet, env_state],
|
| 259 |
+
outputs=[environment_sheet, output_env_json, env_state],
|
| 260 |
+
)
|
| 261 |
+
demo.load(
|
| 262 |
+
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 263 |
+
inputs=[render_state, env_state],
|
| 264 |
+
outputs=[output_render_json, output_env_json],
|
| 265 |
+
)
|
| 266 |
|
| 267 |
with gr.TabItem("Flyout Popup Demo"):
|
| 268 |
+
gr.Markdown(
|
| 269 |
+
"An example of attaching a `PropertySheet` as a flyout panel to other components."
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
# --- State Management ---
|
| 273 |
flyout_visible = gr.State(False)
|
| 274 |
active_anchor_id = gr.State(None)
|
| 275 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 276 |
|
| 277 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 278 |
+
with gr.Row(
|
| 279 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 280 |
+
):
|
| 281 |
+
sampler_dd = gr.Dropdown(
|
| 282 |
+
choices=list(sampler_settings_map_py.keys()),
|
| 283 |
+
label="Sampler",
|
| 284 |
+
value="Euler",
|
| 285 |
+
elem_id="sampler_dd",
|
| 286 |
+
scale=10,
|
| 287 |
+
)
|
| 288 |
+
sampler_ear_btn = gr.Button(
|
| 289 |
+
"⚙️",
|
| 290 |
+
elem_id="sampler_ear_btn",
|
| 291 |
+
scale=1,
|
| 292 |
+
elem_classes=["integrated-ear-btn"],
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
with gr.Row(
|
| 296 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 297 |
+
):
|
| 298 |
+
model_dd = gr.Dropdown(
|
| 299 |
+
choices=list(model_settings_map_py.keys()),
|
| 300 |
+
label="Model",
|
| 301 |
+
value="SDXL 1.0",
|
| 302 |
+
elem_id="model_dd",
|
| 303 |
+
scale=10,
|
| 304 |
+
)
|
| 305 |
+
model_ear_btn = gr.Button(
|
| 306 |
+
"⚙️",
|
| 307 |
+
elem_id="model_ear_btn",
|
| 308 |
+
scale=1,
|
| 309 |
+
elem_classes=["integrated-ear-btn"],
|
| 310 |
+
)
|
| 311 |
|
| 312 |
# --- Event Logic ---
|
| 313 |
+
def handle_flyout_toggle(
|
| 314 |
+
is_vis, current_anchor, clicked_dropdown_id, settings_obj
|
| 315 |
+
):
|
| 316 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 317 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 318 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 319 |
else:
|
| 320 |
+
js_data = json.dumps(
|
| 321 |
+
{"isVisible": True, "anchorId": clicked_dropdown_id}
|
| 322 |
+
)
|
| 323 |
+
return (
|
| 324 |
+
True,
|
| 325 |
+
clicked_dropdown_id,
|
| 326 |
+
gr.update(value=settings_obj),
|
| 327 |
+
gr.update(value=js_data),
|
| 328 |
+
)
|
| 329 |
|
| 330 |
def update_ear_visibility(selection, settings_map):
|
| 331 |
has_settings = settings_map.get(selection) is not None
|
| 332 |
return gr.update(visible=has_settings)
|
| 333 |
|
| 334 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 335 |
+
if updated_settings is None or active_id is None:
|
| 336 |
return
|
| 337 |
if active_id == sampler_dd.elem_id:
|
| 338 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 339 |
elif active_id == model_dd.elem_id:
|
| 340 |
model_settings_map_py[model_val] = updated_settings
|
| 341 |
+
|
| 342 |
def close_the_flyout():
|
| 343 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 344 |
return False, None, gr.update(value=js_data)
|
| 345 |
+
|
| 346 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 347 |
+
|
| 348 |
sampler_dd.change(
|
| 349 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 350 |
inputs=[sampler_dd],
|
| 351 |
+
outputs=[sampler_ear_btn],
|
| 352 |
+
).then(
|
| 353 |
+
fn=close_the_flyout,
|
| 354 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 355 |
+
).then(
|
| 356 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
sampler_ear_btn.click(
|
| 360 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 361 |
+
is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)
|
| 362 |
+
),
|
| 363 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 364 |
+
outputs=[
|
| 365 |
+
flyout_visible,
|
| 366 |
+
active_anchor_id,
|
| 367 |
+
flyout_sheet,
|
| 368 |
+
js_data_bridge,
|
| 369 |
+
],
|
| 370 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 371 |
|
| 372 |
model_dd.change(
|
| 373 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 374 |
inputs=[model_dd],
|
| 375 |
+
outputs=[model_ear_btn],
|
| 376 |
+
).then(
|
| 377 |
+
fn=close_the_flyout,
|
| 378 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 379 |
+
).then(
|
| 380 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
model_ear_btn.click(
|
| 384 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 385 |
+
is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)
|
| 386 |
+
),
|
| 387 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 388 |
+
outputs=[
|
| 389 |
+
flyout_visible,
|
| 390 |
+
active_anchor_id,
|
| 391 |
+
flyout_sheet,
|
| 392 |
+
js_data_bridge,
|
| 393 |
+
],
|
| 394 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 395 |
+
|
| 396 |
flyout_sheet.change(
|
| 397 |
fn=on_flyout_change,
|
| 398 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 399 |
+
outputs=None,
|
| 400 |
)
|
| 401 |
|
| 402 |
close_btn.click(
|
| 403 |
fn=close_the_flyout,
|
| 404 |
inputs=None,
|
| 405 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 406 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 407 |
|
| 408 |
def initial_flyout_setup(sampler_val, model_val):
|
| 409 |
return {
|
| 410 |
+
sampler_ear_btn: update_ear_visibility(
|
| 411 |
+
sampler_val, sampler_settings_map_py
|
| 412 |
+
),
|
| 413 |
+
model_ear_btn: update_ear_visibility(
|
| 414 |
+
model_val, model_settings_map_py
|
| 415 |
+
),
|
| 416 |
}
|
| 417 |
+
|
| 418 |
# --- App Load ---
|
| 419 |
+
demo.load(fn=inject_assets, inputs=None, outputs=[html_injector]).then(
|
| 420 |
+
fn=initial_flyout_setup,
|
| 421 |
+
inputs=[sampler_dd, model_dd],
|
| 422 |
+
outputs=[sampler_ear_btn, model_ear_btn],
|
| 423 |
).then(
|
| 424 |
+
fn=None,
|
| 425 |
+
inputs=None,
|
| 426 |
+
outputs=None,
|
| 427 |
+
js="() => { setTimeout(reparent_flyout, 200); }",
|
| 428 |
)
|
| 429 |
+
|
| 430 |
if __name__ == "__main__":
|
| 431 |
demo.launch()
|
| 432 |
+
|
| 433 |
```
|
| 434 |
|
| 435 |
## `PropertySheet`
|
src/demo/app.py
CHANGED
|
@@ -6,42 +6,97 @@ from typing import Literal
|
|
| 6 |
from gradio_propertysheet import PropertySheet
|
| 7 |
from gradio_htmlinjector import HTMLInjector
|
| 8 |
|
|
|
|
| 9 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 10 |
@dataclass
|
| 11 |
class ModelSettings:
|
| 12 |
-
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
|
|
|
| 15 |
@dataclass
|
| 16 |
class SamplingSettings:
|
| 17 |
-
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
@dataclass
|
| 21 |
class RenderConfig:
|
| 22 |
-
seed: int = field(
|
|
|
|
|
|
|
|
|
|
| 23 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 24 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
|
|
|
| 25 |
@dataclass
|
| 26 |
class Lighting:
|
| 27 |
-
sun_intensity: float = field(
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
@dataclass
|
| 30 |
class EnvironmentConfig:
|
| 31 |
-
background: Literal["Sky", "Color", "Image"] = field(
|
|
|
|
|
|
|
| 32 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 33 |
@dataclass
|
| 34 |
class EulerSettings:
|
| 35 |
-
s_churn: float = field(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
@dataclass
|
| 37 |
class DPM_Settings:
|
| 38 |
-
karras_style: bool = field(
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 41 |
initial_render_config = RenderConfig()
|
| 42 |
initial_env_config = EnvironmentConfig()
|
| 43 |
-
sampler_settings_map_py = {
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 47 |
def inject_assets():
|
|
@@ -51,157 +106,279 @@ def inject_assets():
|
|
| 51 |
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 52 |
css_code = ""
|
| 53 |
js_code = ""
|
| 54 |
-
|
| 55 |
try:
|
| 56 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 57 |
css_code += f.read() + "\n"
|
| 58 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 59 |
js_code += f.read() + "\n"
|
| 60 |
except FileNotFoundError as e:
|
| 61 |
print(f"Warning: Could not read asset file: {e}")
|
| 62 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 63 |
|
|
|
|
| 64 |
# --- 4. Gradio App Build ---
|
| 65 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 66 |
html_injector = HTMLInjector()
|
| 67 |
gr.Markdown("# PropertySheet Component Demos")
|
| 68 |
-
|
| 69 |
with gr.Row():
|
| 70 |
# --- Flyout popup ---
|
| 71 |
-
with gr.Column(
|
|
|
|
|
|
|
| 72 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 73 |
-
flyout_sheet = PropertySheet(
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
with gr.Tabs():
|
| 76 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 77 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 78 |
render_state = gr.State(value=initial_render_config)
|
| 79 |
env_state = gr.State(value=initial_env_config)
|
| 80 |
sidebar_visible = gr.State(False)
|
| 81 |
with gr.Row():
|
| 82 |
-
with gr.Column(scale=3):
|
| 83 |
generate = gr.Button("Show Settings", variant="primary")
|
| 84 |
with gr.Row():
|
| 85 |
output_render_json = gr.JSON(label="Live Render State")
|
| 86 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
-
render_sheet = PropertySheet(
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 91 |
new_visibility = not is_visible
|
| 92 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 93 |
-
return (
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return updated_config, asdict(updated_config), updated_config
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
return updated_config, asdict(updated_config), current_state
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
with gr.TabItem("Flyout Popup Demo"):
|
| 107 |
-
gr.Markdown(
|
| 108 |
-
|
|
|
|
|
|
|
| 109 |
# --- State Management ---
|
| 110 |
flyout_visible = gr.State(False)
|
| 111 |
active_anchor_id = gr.State(None)
|
| 112 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 113 |
|
| 114 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 115 |
-
with gr.Row(
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# --- Event Logic ---
|
| 126 |
-
def handle_flyout_toggle(
|
|
|
|
|
|
|
| 127 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 128 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 129 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 130 |
else:
|
| 131 |
-
js_data = json.dumps(
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
def update_ear_visibility(selection, settings_map):
|
| 135 |
has_settings = settings_map.get(selection) is not None
|
| 136 |
return gr.update(visible=has_settings)
|
| 137 |
|
| 138 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 139 |
-
if updated_settings is None or active_id is None:
|
| 140 |
return
|
| 141 |
if active_id == sampler_dd.elem_id:
|
| 142 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 143 |
elif active_id == model_dd.elem_id:
|
| 144 |
model_settings_map_py[model_val] = updated_settings
|
| 145 |
-
|
| 146 |
def close_the_flyout():
|
| 147 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 148 |
return False, None, gr.update(value=js_data)
|
| 149 |
-
|
| 150 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 151 |
-
|
| 152 |
sampler_dd.change(
|
| 153 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 154 |
inputs=[sampler_dd],
|
| 155 |
-
outputs=[sampler_ear_btn]
|
| 156 |
-
).then(
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
sampler_ear_btn.click(
|
| 160 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 161 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 162 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 164 |
|
| 165 |
model_dd.change(
|
| 166 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 167 |
inputs=[model_dd],
|
| 168 |
-
outputs=[model_ear_btn]
|
| 169 |
-
).then(
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
model_ear_btn.click(
|
| 173 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 174 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 175 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 177 |
-
|
| 178 |
flyout_sheet.change(
|
| 179 |
fn=on_flyout_change,
|
| 180 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 181 |
-
outputs=None
|
| 182 |
)
|
| 183 |
|
| 184 |
close_btn.click(
|
| 185 |
fn=close_the_flyout,
|
| 186 |
inputs=None,
|
| 187 |
-
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 188 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 189 |
|
| 190 |
def initial_flyout_setup(sampler_val, model_val):
|
| 191 |
return {
|
| 192 |
-
sampler_ear_btn: update_ear_visibility(
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
}
|
| 195 |
-
|
| 196 |
# --- App Load ---
|
| 197 |
-
demo.load(
|
| 198 |
-
fn=
|
|
|
|
|
|
|
| 199 |
).then(
|
| 200 |
-
fn=
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
js="() => { setTimeout(reparent_flyout, 200); }"
|
| 204 |
)
|
| 205 |
-
|
| 206 |
if __name__ == "__main__":
|
| 207 |
-
demo.launch()
|
|
|
|
| 6 |
from gradio_propertysheet import PropertySheet
|
| 7 |
from gradio_htmlinjector import HTMLInjector
|
| 8 |
|
| 9 |
+
|
| 10 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 11 |
@dataclass
|
| 12 |
class ModelSettings:
|
| 13 |
+
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 14 |
+
default="SDXL", metadata={"component": "dropdown", "label": "Base Model"}
|
| 15 |
+
)
|
| 16 |
+
custom_model_path: str = field(
|
| 17 |
+
default="/path/to/default.safetensors",
|
| 18 |
+
metadata={
|
| 19 |
+
"label": "Custom Model Path",
|
| 20 |
+
"interactive_if": {"field": "model_type", "value": "Custom"},
|
| 21 |
+
},
|
| 22 |
+
)
|
| 23 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 24 |
+
|
| 25 |
+
|
| 26 |
@dataclass
|
| 27 |
class SamplingSettings:
|
| 28 |
+
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 29 |
+
default="DPM++ 2M Karras",
|
| 30 |
+
metadata={"component": "dropdown", "label": "Sampler"},
|
| 31 |
+
)
|
| 32 |
+
steps: int = field(
|
| 33 |
+
default=25,
|
| 34 |
+
metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1},
|
| 35 |
+
)
|
| 36 |
+
cfg_scale: float = field(
|
| 37 |
+
default=7.0,
|
| 38 |
+
metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5},
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
@dataclass
|
| 43 |
class RenderConfig:
|
| 44 |
+
seed: int = field(
|
| 45 |
+
default=-1,
|
| 46 |
+
metadata={"component": "number_integer", "label": "Seed (-1 for random)"},
|
| 47 |
+
)
|
| 48 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 49 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
@dataclass
|
| 53 |
class Lighting:
|
| 54 |
+
sun_intensity: float = field(
|
| 55 |
+
default=1.0,
|
| 56 |
+
metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1},
|
| 57 |
+
)
|
| 58 |
+
color: str = field(
|
| 59 |
+
default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"}
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
@dataclass
|
| 64 |
class EnvironmentConfig:
|
| 65 |
+
background: Literal["Sky", "Color", "Image"] = field(
|
| 66 |
+
default="Sky", metadata={"component": "dropdown"}
|
| 67 |
+
)
|
| 68 |
lighting: Lighting = field(default_factory=Lighting)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
@dataclass
|
| 72 |
class EulerSettings:
|
| 73 |
+
s_churn: float = field(
|
| 74 |
+
default=0.0,
|
| 75 |
+
metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01},
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
@dataclass
|
| 80 |
class DPM_Settings:
|
| 81 |
+
karras_style: bool = field(
|
| 82 |
+
default=True, metadata={"label": "Use Karras Sigma Schedule"}
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
|
| 86 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 87 |
initial_render_config = RenderConfig()
|
| 88 |
initial_env_config = EnvironmentConfig()
|
| 89 |
+
sampler_settings_map_py = {
|
| 90 |
+
"Euler": EulerSettings(),
|
| 91 |
+
"DPM++ 2M Karras": DPM_Settings(),
|
| 92 |
+
"UniPC": None,
|
| 93 |
+
}
|
| 94 |
+
model_settings_map_py = {
|
| 95 |
+
"SDXL 1.0": DPM_Settings(),
|
| 96 |
+
"Stable Diffusion 1.5": EulerSettings(),
|
| 97 |
+
"Pony": None,
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
|
| 101 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 102 |
def inject_assets():
|
|
|
|
| 106 |
popup_html = """<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>"""
|
| 107 |
css_code = ""
|
| 108 |
js_code = ""
|
| 109 |
+
|
| 110 |
try:
|
| 111 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 112 |
css_code += f.read() + "\n"
|
| 113 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 114 |
js_code += f.read() + "\n"
|
| 115 |
except FileNotFoundError as e:
|
| 116 |
print(f"Warning: Could not read asset file: {e}")
|
| 117 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 118 |
|
| 119 |
+
|
| 120 |
# --- 4. Gradio App Build ---
|
| 121 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 122 |
html_injector = HTMLInjector()
|
| 123 |
gr.Markdown("# PropertySheet Component Demos")
|
| 124 |
+
|
| 125 |
with gr.Row():
|
| 126 |
# --- Flyout popup ---
|
| 127 |
+
with gr.Column(
|
| 128 |
+
elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]
|
| 129 |
+
) as flyout_panel_source:
|
| 130 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 131 |
+
flyout_sheet = PropertySheet(
|
| 132 |
+
visible=True,
|
| 133 |
+
container=False,
|
| 134 |
+
label="Settings",
|
| 135 |
+
show_group_name_only_one=False,
|
| 136 |
+
disable_accordion=True,
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
with gr.Tabs():
|
| 140 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 141 |
+
gr.Markdown(
|
| 142 |
+
"An example of using the `PropertySheet` component as a traditional sidebar for settings."
|
| 143 |
+
)
|
| 144 |
render_state = gr.State(value=initial_render_config)
|
| 145 |
env_state = gr.State(value=initial_env_config)
|
| 146 |
sidebar_visible = gr.State(False)
|
| 147 |
with gr.Row():
|
| 148 |
+
with gr.Column(scale=3):
|
| 149 |
generate = gr.Button("Show Settings", variant="primary")
|
| 150 |
with gr.Row():
|
| 151 |
output_render_json = gr.JSON(label="Live Render State")
|
| 152 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 153 |
with gr.Column(scale=1):
|
| 154 |
+
render_sheet = PropertySheet(
|
| 155 |
+
value=initial_render_config,
|
| 156 |
+
label="Render Settings",
|
| 157 |
+
width=400,
|
| 158 |
+
height=550,
|
| 159 |
+
visible=False,
|
| 160 |
+
root_label="Generator",
|
| 161 |
+
)
|
| 162 |
+
environment_sheet = PropertySheet(
|
| 163 |
+
value=initial_env_config,
|
| 164 |
+
label="Environment Settings",
|
| 165 |
+
width=400,
|
| 166 |
+
open=False,
|
| 167 |
+
visible=False,
|
| 168 |
+
root_label="General",
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 172 |
new_visibility = not is_visible
|
| 173 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 174 |
+
return (
|
| 175 |
+
new_visibility,
|
| 176 |
+
gr.update(visible=new_visibility, value=render_cfg),
|
| 177 |
+
gr.update(visible=new_visibility, value=env_cfg),
|
| 178 |
+
gr.update(value=button_text),
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
def handle_render_change(
|
| 182 |
+
updated_config: RenderConfig, current_state: RenderConfig
|
| 183 |
+
):
|
| 184 |
+
if updated_config is None:
|
| 185 |
+
return current_state, asdict(current_state), current_state
|
| 186 |
+
if updated_config.model.model_type != "Custom":
|
| 187 |
+
updated_config.model.custom_model_path = (
|
| 188 |
+
"/path/to/default.safetensors"
|
| 189 |
+
)
|
| 190 |
return updated_config, asdict(updated_config), updated_config
|
| 191 |
+
|
| 192 |
+
def handle_env_change(
|
| 193 |
+
updated_config: EnvironmentConfig, current_state: EnvironmentConfig
|
| 194 |
+
):
|
| 195 |
+
if updated_config is None:
|
| 196 |
+
return current_state, asdict(current_state), current_state
|
| 197 |
return updated_config, asdict(updated_config), current_state
|
| 198 |
+
|
| 199 |
+
generate.click(
|
| 200 |
+
fn=change_visibility,
|
| 201 |
+
inputs=[sidebar_visible, render_state, env_state],
|
| 202 |
+
outputs=[sidebar_visible, render_sheet, environment_sheet, generate],
|
| 203 |
+
)
|
| 204 |
+
render_sheet.change(
|
| 205 |
+
fn=handle_render_change,
|
| 206 |
+
inputs=[render_sheet, render_state],
|
| 207 |
+
outputs=[render_sheet, output_render_json, render_state],
|
| 208 |
+
)
|
| 209 |
+
environment_sheet.change(
|
| 210 |
+
fn=handle_env_change,
|
| 211 |
+
inputs=[environment_sheet, env_state],
|
| 212 |
+
outputs=[environment_sheet, output_env_json, env_state],
|
| 213 |
+
)
|
| 214 |
+
demo.load(
|
| 215 |
+
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 216 |
+
inputs=[render_state, env_state],
|
| 217 |
+
outputs=[output_render_json, output_env_json],
|
| 218 |
+
)
|
| 219 |
|
| 220 |
with gr.TabItem("Flyout Popup Demo"):
|
| 221 |
+
gr.Markdown(
|
| 222 |
+
"An example of attaching a `PropertySheet` as a flyout panel to other components."
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
# --- State Management ---
|
| 226 |
flyout_visible = gr.State(False)
|
| 227 |
active_anchor_id = gr.State(None)
|
| 228 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 229 |
|
| 230 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 231 |
+
with gr.Row(
|
| 232 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 233 |
+
):
|
| 234 |
+
sampler_dd = gr.Dropdown(
|
| 235 |
+
choices=list(sampler_settings_map_py.keys()),
|
| 236 |
+
label="Sampler",
|
| 237 |
+
value="Euler",
|
| 238 |
+
elem_id="sampler_dd",
|
| 239 |
+
scale=10,
|
| 240 |
+
)
|
| 241 |
+
sampler_ear_btn = gr.Button(
|
| 242 |
+
"⚙️",
|
| 243 |
+
elem_id="sampler_ear_btn",
|
| 244 |
+
scale=1,
|
| 245 |
+
elem_classes=["integrated-ear-btn"],
|
| 246 |
+
)
|
| 247 |
|
| 248 |
+
with gr.Row(
|
| 249 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 250 |
+
):
|
| 251 |
+
model_dd = gr.Dropdown(
|
| 252 |
+
choices=list(model_settings_map_py.keys()),
|
| 253 |
+
label="Model",
|
| 254 |
+
value="SDXL 1.0",
|
| 255 |
+
elem_id="model_dd",
|
| 256 |
+
scale=10,
|
| 257 |
+
)
|
| 258 |
+
model_ear_btn = gr.Button(
|
| 259 |
+
"⚙️",
|
| 260 |
+
elem_id="model_ear_btn",
|
| 261 |
+
scale=1,
|
| 262 |
+
elem_classes=["integrated-ear-btn"],
|
| 263 |
+
)
|
| 264 |
|
| 265 |
# --- Event Logic ---
|
| 266 |
+
def handle_flyout_toggle(
|
| 267 |
+
is_vis, current_anchor, clicked_dropdown_id, settings_obj
|
| 268 |
+
):
|
| 269 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 270 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 271 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 272 |
else:
|
| 273 |
+
js_data = json.dumps(
|
| 274 |
+
{"isVisible": True, "anchorId": clicked_dropdown_id}
|
| 275 |
+
)
|
| 276 |
+
return (
|
| 277 |
+
True,
|
| 278 |
+
clicked_dropdown_id,
|
| 279 |
+
gr.update(value=settings_obj),
|
| 280 |
+
gr.update(value=js_data),
|
| 281 |
+
)
|
| 282 |
|
| 283 |
def update_ear_visibility(selection, settings_map):
|
| 284 |
has_settings = settings_map.get(selection) is not None
|
| 285 |
return gr.update(visible=has_settings)
|
| 286 |
|
| 287 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 288 |
+
if updated_settings is None or active_id is None:
|
| 289 |
return
|
| 290 |
if active_id == sampler_dd.elem_id:
|
| 291 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 292 |
elif active_id == model_dd.elem_id:
|
| 293 |
model_settings_map_py[model_val] = updated_settings
|
| 294 |
+
|
| 295 |
def close_the_flyout():
|
| 296 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 297 |
return False, None, gr.update(value=js_data)
|
| 298 |
+
|
| 299 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 300 |
+
|
| 301 |
sampler_dd.change(
|
| 302 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 303 |
inputs=[sampler_dd],
|
| 304 |
+
outputs=[sampler_ear_btn],
|
| 305 |
+
).then(
|
| 306 |
+
fn=close_the_flyout,
|
| 307 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 308 |
+
).then(
|
| 309 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
sampler_ear_btn.click(
|
| 313 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 314 |
+
is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)
|
| 315 |
+
),
|
| 316 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 317 |
+
outputs=[
|
| 318 |
+
flyout_visible,
|
| 319 |
+
active_anchor_id,
|
| 320 |
+
flyout_sheet,
|
| 321 |
+
js_data_bridge,
|
| 322 |
+
],
|
| 323 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 324 |
|
| 325 |
model_dd.change(
|
| 326 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 327 |
inputs=[model_dd],
|
| 328 |
+
outputs=[model_ear_btn],
|
| 329 |
+
).then(
|
| 330 |
+
fn=close_the_flyout,
|
| 331 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 332 |
+
).then(
|
| 333 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
model_ear_btn.click(
|
| 337 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 338 |
+
is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)
|
| 339 |
+
),
|
| 340 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 341 |
+
outputs=[
|
| 342 |
+
flyout_visible,
|
| 343 |
+
active_anchor_id,
|
| 344 |
+
flyout_sheet,
|
| 345 |
+
js_data_bridge,
|
| 346 |
+
],
|
| 347 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 348 |
+
|
| 349 |
flyout_sheet.change(
|
| 350 |
fn=on_flyout_change,
|
| 351 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 352 |
+
outputs=None,
|
| 353 |
)
|
| 354 |
|
| 355 |
close_btn.click(
|
| 356 |
fn=close_the_flyout,
|
| 357 |
inputs=None,
|
| 358 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 359 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 360 |
|
| 361 |
def initial_flyout_setup(sampler_val, model_val):
|
| 362 |
return {
|
| 363 |
+
sampler_ear_btn: update_ear_visibility(
|
| 364 |
+
sampler_val, sampler_settings_map_py
|
| 365 |
+
),
|
| 366 |
+
model_ear_btn: update_ear_visibility(
|
| 367 |
+
model_val, model_settings_map_py
|
| 368 |
+
),
|
| 369 |
}
|
| 370 |
+
|
| 371 |
# --- App Load ---
|
| 372 |
+
demo.load(fn=inject_assets, inputs=None, outputs=[html_injector]).then(
|
| 373 |
+
fn=initial_flyout_setup,
|
| 374 |
+
inputs=[sampler_dd, model_dd],
|
| 375 |
+
outputs=[sampler_ear_btn, model_ear_btn],
|
| 376 |
).then(
|
| 377 |
+
fn=None,
|
| 378 |
+
inputs=None,
|
| 379 |
+
outputs=None,
|
| 380 |
+
js="() => { setTimeout(reparent_flyout, 200); }",
|
| 381 |
)
|
| 382 |
+
|
| 383 |
if __name__ == "__main__":
|
| 384 |
+
demo.launch()
|
src/demo/space.py
CHANGED
|
@@ -46,42 +46,97 @@ from typing import Literal
|
|
| 46 |
from gradio_propertysheet import PropertySheet
|
| 47 |
from gradio_htmlinjector import HTMLInjector
|
| 48 |
|
|
|
|
| 49 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 50 |
@dataclass
|
| 51 |
class ModelSettings:
|
| 52 |
-
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
|
|
|
|
|
|
| 55 |
@dataclass
|
| 56 |
class SamplingSettings:
|
| 57 |
-
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
@dataclass
|
| 61 |
class RenderConfig:
|
| 62 |
-
seed: int = field(
|
|
|
|
|
|
|
|
|
|
| 63 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 64 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
|
|
|
|
|
|
| 65 |
@dataclass
|
| 66 |
class Lighting:
|
| 67 |
-
sun_intensity: float = field(
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
@dataclass
|
| 70 |
class EnvironmentConfig:
|
| 71 |
-
background: Literal["Sky", "Color", "Image"] = field(
|
|
|
|
|
|
|
| 72 |
lighting: Lighting = field(default_factory=Lighting)
|
|
|
|
|
|
|
| 73 |
@dataclass
|
| 74 |
class EulerSettings:
|
| 75 |
-
s_churn: float = field(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
@dataclass
|
| 77 |
class DPM_Settings:
|
| 78 |
-
karras_style: bool = field(
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 81 |
initial_render_config = RenderConfig()
|
| 82 |
initial_env_config = EnvironmentConfig()
|
| 83 |
-
sampler_settings_map_py = {
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 87 |
def inject_assets():
|
|
@@ -91,160 +146,283 @@ def inject_assets():
|
|
| 91 |
popup_html = \"\"\"<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>\"\"\"
|
| 92 |
css_code = ""
|
| 93 |
js_code = ""
|
| 94 |
-
|
| 95 |
try:
|
| 96 |
-
with open("custom.css", "r", encoding="utf-8") as f:
|
| 97 |
css_code += f.read() + "\n"
|
| 98 |
-
with open("custom.js", "r", encoding="utf-8") as f:
|
| 99 |
js_code += f.read() + "\n"
|
| 100 |
except FileNotFoundError as e:
|
| 101 |
print(f"Warning: Could not read asset file: {e}")
|
| 102 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 103 |
|
|
|
|
| 104 |
# --- 4. Gradio App Build ---
|
| 105 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 106 |
html_injector = HTMLInjector()
|
| 107 |
gr.Markdown("# PropertySheet Component Demos")
|
| 108 |
-
|
| 109 |
with gr.Row():
|
| 110 |
# --- Flyout popup ---
|
| 111 |
-
with gr.Column(
|
|
|
|
|
|
|
| 112 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 113 |
-
flyout_sheet = PropertySheet(
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
with gr.Tabs():
|
| 116 |
-
with gr.TabItem("Original Sidebar Demo"):
|
| 117 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 118 |
render_state = gr.State(value=initial_render_config)
|
| 119 |
env_state = gr.State(value=initial_env_config)
|
| 120 |
sidebar_visible = gr.State(False)
|
| 121 |
with gr.Row():
|
| 122 |
-
with gr.Column(scale=3):
|
| 123 |
generate = gr.Button("Show Settings", variant="primary")
|
| 124 |
with gr.Row():
|
| 125 |
output_render_json = gr.JSON(label="Live Render State")
|
| 126 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 127 |
with gr.Column(scale=1):
|
| 128 |
-
render_sheet = PropertySheet(
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 131 |
new_visibility = not is_visible
|
| 132 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 133 |
-
return (
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
return updated_config, asdict(updated_config), updated_config
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
return updated_config, asdict(updated_config), current_state
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
with gr.TabItem("Flyout Popup Demo"):
|
| 147 |
-
gr.Markdown(
|
| 148 |
-
|
|
|
|
|
|
|
| 149 |
# --- State Management ---
|
| 150 |
flyout_visible = gr.State(False)
|
| 151 |
active_anchor_id = gr.State(None)
|
| 152 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 153 |
|
| 154 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 155 |
-
with gr.Row(
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# --- Event Logic ---
|
| 166 |
-
def handle_flyout_toggle(
|
|
|
|
|
|
|
| 167 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 168 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 169 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 170 |
else:
|
| 171 |
-
js_data = json.dumps(
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
def update_ear_visibility(selection, settings_map):
|
| 175 |
has_settings = settings_map.get(selection) is not None
|
| 176 |
return gr.update(visible=has_settings)
|
| 177 |
|
| 178 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 179 |
-
if updated_settings is None or active_id is None:
|
| 180 |
return
|
| 181 |
if active_id == sampler_dd.elem_id:
|
| 182 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 183 |
elif active_id == model_dd.elem_id:
|
| 184 |
model_settings_map_py[model_val] = updated_settings
|
| 185 |
-
|
| 186 |
def close_the_flyout():
|
| 187 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 188 |
return False, None, gr.update(value=js_data)
|
| 189 |
-
|
| 190 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 191 |
-
|
| 192 |
sampler_dd.change(
|
| 193 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 194 |
inputs=[sampler_dd],
|
| 195 |
-
outputs=[sampler_ear_btn]
|
| 196 |
-
).then(
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
sampler_ear_btn.click(
|
| 200 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 201 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 202 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 204 |
|
| 205 |
model_dd.change(
|
| 206 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 207 |
inputs=[model_dd],
|
| 208 |
-
outputs=[model_ear_btn]
|
| 209 |
-
).then(
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
model_ear_btn.click(
|
| 213 |
-
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
|
|
|
|
|
|
| 214 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 215 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 217 |
-
|
| 218 |
flyout_sheet.change(
|
| 219 |
fn=on_flyout_change,
|
| 220 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 221 |
-
outputs=None
|
| 222 |
)
|
| 223 |
|
| 224 |
close_btn.click(
|
| 225 |
fn=close_the_flyout,
|
| 226 |
inputs=None,
|
| 227 |
-
outputs=[flyout_visible, active_anchor_id, js_data_bridge]
|
| 228 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 229 |
|
| 230 |
def initial_flyout_setup(sampler_val, model_val):
|
| 231 |
return {
|
| 232 |
-
sampler_ear_btn: update_ear_visibility(
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
}
|
| 235 |
-
|
| 236 |
# --- App Load ---
|
| 237 |
-
demo.load(
|
| 238 |
-
fn=
|
| 239 |
-
|
| 240 |
-
|
| 241 |
).then(
|
| 242 |
-
fn=None,
|
| 243 |
-
|
|
|
|
|
|
|
| 244 |
)
|
| 245 |
-
|
| 246 |
if __name__ == "__main__":
|
| 247 |
demo.launch()
|
|
|
|
| 248 |
```
|
| 249 |
""", elem_classes=["md-custom"], header_links=True)
|
| 250 |
|
|
|
|
| 46 |
from gradio_propertysheet import PropertySheet
|
| 47 |
from gradio_htmlinjector import HTMLInjector
|
| 48 |
|
| 49 |
+
|
| 50 |
# --- 1. Dataclass Definitions (unchanged) ---
|
| 51 |
@dataclass
|
| 52 |
class ModelSettings:
|
| 53 |
+
model_type: Literal["SD 1.5", "SDXL", "Pony", "Custom"] = field(
|
| 54 |
+
default="SDXL", metadata={"component": "dropdown", "label": "Base Model"}
|
| 55 |
+
)
|
| 56 |
+
custom_model_path: str = field(
|
| 57 |
+
default="/path/to/default.safetensors",
|
| 58 |
+
metadata={
|
| 59 |
+
"label": "Custom Model Path",
|
| 60 |
+
"interactive_if": {"field": "model_type", "value": "Custom"},
|
| 61 |
+
},
|
| 62 |
+
)
|
| 63 |
vae_path: str = field(default="", metadata={"label": "VAE Path (optional)"})
|
| 64 |
+
|
| 65 |
+
|
| 66 |
@dataclass
|
| 67 |
class SamplingSettings:
|
| 68 |
+
sampler_name: Literal["Euler", "Euler a", "DPM++ 2M Karras", "UniPC"] = field(
|
| 69 |
+
default="DPM++ 2M Karras",
|
| 70 |
+
metadata={"component": "dropdown", "label": "Sampler"},
|
| 71 |
+
)
|
| 72 |
+
steps: int = field(
|
| 73 |
+
default=25,
|
| 74 |
+
metadata={"component": "slider", "minimum": 1, "maximum": 150, "step": 1},
|
| 75 |
+
)
|
| 76 |
+
cfg_scale: float = field(
|
| 77 |
+
default=7.0,
|
| 78 |
+
metadata={"component": "slider", "minimum": 1.0, "maximum": 30.0, "step": 0.5},
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
@dataclass
|
| 83 |
class RenderConfig:
|
| 84 |
+
seed: int = field(
|
| 85 |
+
default=-1,
|
| 86 |
+
metadata={"component": "number_integer", "label": "Seed (-1 for random)"},
|
| 87 |
+
)
|
| 88 |
model: ModelSettings = field(default_factory=ModelSettings)
|
| 89 |
sampling: SamplingSettings = field(default_factory=SamplingSettings)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
@dataclass
|
| 93 |
class Lighting:
|
| 94 |
+
sun_intensity: float = field(
|
| 95 |
+
default=1.0,
|
| 96 |
+
metadata={"component": "slider", "minimum": 0, "maximum": 5, "step": 0.1},
|
| 97 |
+
)
|
| 98 |
+
color: str = field(
|
| 99 |
+
default="#FFDDBB", metadata={"component": "colorpicker", "label": "Sun Color"}
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
@dataclass
|
| 104 |
class EnvironmentConfig:
|
| 105 |
+
background: Literal["Sky", "Color", "Image"] = field(
|
| 106 |
+
default="Sky", metadata={"component": "dropdown"}
|
| 107 |
+
)
|
| 108 |
lighting: Lighting = field(default_factory=Lighting)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
@dataclass
|
| 112 |
class EulerSettings:
|
| 113 |
+
s_churn: float = field(
|
| 114 |
+
default=0.0,
|
| 115 |
+
metadata={"component": "slider", "minimum": 0.0, "maximum": 1.0, "step": 0.01},
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
@dataclass
|
| 120 |
class DPM_Settings:
|
| 121 |
+
karras_style: bool = field(
|
| 122 |
+
default=True, metadata={"label": "Use Karras Sigma Schedule"}
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
|
| 126 |
# --- 2. Data Mappings and Initial Instances (unchanged) ---
|
| 127 |
initial_render_config = RenderConfig()
|
| 128 |
initial_env_config = EnvironmentConfig()
|
| 129 |
+
sampler_settings_map_py = {
|
| 130 |
+
"Euler": EulerSettings(),
|
| 131 |
+
"DPM++ 2M Karras": DPM_Settings(),
|
| 132 |
+
"UniPC": None,
|
| 133 |
+
}
|
| 134 |
+
model_settings_map_py = {
|
| 135 |
+
"SDXL 1.0": DPM_Settings(),
|
| 136 |
+
"Stable Diffusion 1.5": EulerSettings(),
|
| 137 |
+
"Pony": None,
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
|
| 141 |
# --- 3. CSS & JS Injection function (unchanged) ---
|
| 142 |
def inject_assets():
|
|
|
|
| 146 |
popup_html = \"\"\"<div id="injected_flyout_container" class="flyout-sheet" style="display: none;"></div>\"\"\"
|
| 147 |
css_code = ""
|
| 148 |
js_code = ""
|
| 149 |
+
|
| 150 |
try:
|
| 151 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
| 152 |
css_code += f.read() + "\n"
|
| 153 |
+
with open("custom.js", "r", encoding="utf-8") as f:
|
| 154 |
js_code += f.read() + "\n"
|
| 155 |
except FileNotFoundError as e:
|
| 156 |
print(f"Warning: Could not read asset file: {e}")
|
| 157 |
return {"js": js_code, "css": css_code, "body_html": popup_html}
|
| 158 |
|
| 159 |
+
|
| 160 |
# --- 4. Gradio App Build ---
|
| 161 |
with gr.Blocks(title="PropertySheet Demos") as demo:
|
| 162 |
html_injector = HTMLInjector()
|
| 163 |
gr.Markdown("# PropertySheet Component Demos")
|
| 164 |
+
|
| 165 |
with gr.Row():
|
| 166 |
# --- Flyout popup ---
|
| 167 |
+
with gr.Column(
|
| 168 |
+
elem_id="flyout_panel_source", elem_classes=["flyout-source-hidden"]
|
| 169 |
+
) as flyout_panel_source:
|
| 170 |
close_btn = gr.Button("×", elem_classes=["flyout-close-btn"])
|
| 171 |
+
flyout_sheet = PropertySheet(
|
| 172 |
+
visible=True,
|
| 173 |
+
container=False,
|
| 174 |
+
label="Settings",
|
| 175 |
+
show_group_name_only_one=False,
|
| 176 |
+
disable_accordion=True,
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
with gr.Tabs():
|
| 180 |
+
with gr.TabItem("Original Sidebar Demo"):
|
| 181 |
+
gr.Markdown(
|
| 182 |
+
"An example of using the `PropertySheet` component as a traditional sidebar for settings."
|
| 183 |
+
)
|
| 184 |
render_state = gr.State(value=initial_render_config)
|
| 185 |
env_state = gr.State(value=initial_env_config)
|
| 186 |
sidebar_visible = gr.State(False)
|
| 187 |
with gr.Row():
|
| 188 |
+
with gr.Column(scale=3):
|
| 189 |
generate = gr.Button("Show Settings", variant="primary")
|
| 190 |
with gr.Row():
|
| 191 |
output_render_json = gr.JSON(label="Live Render State")
|
| 192 |
output_env_json = gr.JSON(label="Live Environment State")
|
| 193 |
with gr.Column(scale=1):
|
| 194 |
+
render_sheet = PropertySheet(
|
| 195 |
+
value=initial_render_config,
|
| 196 |
+
label="Render Settings",
|
| 197 |
+
width=400,
|
| 198 |
+
height=550,
|
| 199 |
+
visible=False,
|
| 200 |
+
root_label="Generator",
|
| 201 |
+
)
|
| 202 |
+
environment_sheet = PropertySheet(
|
| 203 |
+
value=initial_env_config,
|
| 204 |
+
label="Environment Settings",
|
| 205 |
+
width=400,
|
| 206 |
+
open=False,
|
| 207 |
+
visible=False,
|
| 208 |
+
root_label="General",
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
def change_visibility(is_visible, render_cfg, env_cfg):
|
| 212 |
new_visibility = not is_visible
|
| 213 |
button_text = "Hide Settings" if new_visibility else "Show Settings"
|
| 214 |
+
return (
|
| 215 |
+
new_visibility,
|
| 216 |
+
gr.update(visible=new_visibility, value=render_cfg),
|
| 217 |
+
gr.update(visible=new_visibility, value=env_cfg),
|
| 218 |
+
gr.update(value=button_text),
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
def handle_render_change(
|
| 222 |
+
updated_config: RenderConfig, current_state: RenderConfig
|
| 223 |
+
):
|
| 224 |
+
if updated_config is None:
|
| 225 |
+
return current_state, asdict(current_state), current_state
|
| 226 |
+
if updated_config.model.model_type != "Custom":
|
| 227 |
+
updated_config.model.custom_model_path = (
|
| 228 |
+
"/path/to/default.safetensors"
|
| 229 |
+
)
|
| 230 |
return updated_config, asdict(updated_config), updated_config
|
| 231 |
+
|
| 232 |
+
def handle_env_change(
|
| 233 |
+
updated_config: EnvironmentConfig, current_state: EnvironmentConfig
|
| 234 |
+
):
|
| 235 |
+
if updated_config is None:
|
| 236 |
+
return current_state, asdict(current_state), current_state
|
| 237 |
return updated_config, asdict(updated_config), current_state
|
| 238 |
+
|
| 239 |
+
generate.click(
|
| 240 |
+
fn=change_visibility,
|
| 241 |
+
inputs=[sidebar_visible, render_state, env_state],
|
| 242 |
+
outputs=[sidebar_visible, render_sheet, environment_sheet, generate],
|
| 243 |
+
)
|
| 244 |
+
render_sheet.change(
|
| 245 |
+
fn=handle_render_change,
|
| 246 |
+
inputs=[render_sheet, render_state],
|
| 247 |
+
outputs=[render_sheet, output_render_json, render_state],
|
| 248 |
+
)
|
| 249 |
+
environment_sheet.change(
|
| 250 |
+
fn=handle_env_change,
|
| 251 |
+
inputs=[environment_sheet, env_state],
|
| 252 |
+
outputs=[environment_sheet, output_env_json, env_state],
|
| 253 |
+
)
|
| 254 |
+
demo.load(
|
| 255 |
+
fn=lambda r_cfg, e_cfg: (asdict(r_cfg), asdict(e_cfg)),
|
| 256 |
+
inputs=[render_state, env_state],
|
| 257 |
+
outputs=[output_render_json, output_env_json],
|
| 258 |
+
)
|
| 259 |
|
| 260 |
with gr.TabItem("Flyout Popup Demo"):
|
| 261 |
+
gr.Markdown(
|
| 262 |
+
"An example of attaching a `PropertySheet` as a flyout panel to other components."
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
# --- State Management ---
|
| 266 |
flyout_visible = gr.State(False)
|
| 267 |
active_anchor_id = gr.State(None)
|
| 268 |
js_data_bridge = gr.Textbox(visible=False, elem_id="js_data_bridge")
|
| 269 |
|
| 270 |
with gr.Column(elem_classes=["flyout-context-area"]):
|
| 271 |
+
with gr.Row(
|
| 272 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 273 |
+
):
|
| 274 |
+
sampler_dd = gr.Dropdown(
|
| 275 |
+
choices=list(sampler_settings_map_py.keys()),
|
| 276 |
+
label="Sampler",
|
| 277 |
+
value="Euler",
|
| 278 |
+
elem_id="sampler_dd",
|
| 279 |
+
scale=10,
|
| 280 |
+
)
|
| 281 |
+
sampler_ear_btn = gr.Button(
|
| 282 |
+
"⚙️",
|
| 283 |
+
elem_id="sampler_ear_btn",
|
| 284 |
+
scale=1,
|
| 285 |
+
elem_classes=["integrated-ear-btn"],
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
with gr.Row(
|
| 289 |
+
elem_classes=["fake-input-container", "no-border-dropdown"]
|
| 290 |
+
):
|
| 291 |
+
model_dd = gr.Dropdown(
|
| 292 |
+
choices=list(model_settings_map_py.keys()),
|
| 293 |
+
label="Model",
|
| 294 |
+
value="SDXL 1.0",
|
| 295 |
+
elem_id="model_dd",
|
| 296 |
+
scale=10,
|
| 297 |
+
)
|
| 298 |
+
model_ear_btn = gr.Button(
|
| 299 |
+
"⚙️",
|
| 300 |
+
elem_id="model_ear_btn",
|
| 301 |
+
scale=1,
|
| 302 |
+
elem_classes=["integrated-ear-btn"],
|
| 303 |
+
)
|
| 304 |
|
| 305 |
# --- Event Logic ---
|
| 306 |
+
def handle_flyout_toggle(
|
| 307 |
+
is_vis, current_anchor, clicked_dropdown_id, settings_obj
|
| 308 |
+
):
|
| 309 |
if is_vis and current_anchor == clicked_dropdown_id:
|
| 310 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 311 |
return False, None, gr.update(), gr.update(value=js_data)
|
| 312 |
else:
|
| 313 |
+
js_data = json.dumps(
|
| 314 |
+
{"isVisible": True, "anchorId": clicked_dropdown_id}
|
| 315 |
+
)
|
| 316 |
+
return (
|
| 317 |
+
True,
|
| 318 |
+
clicked_dropdown_id,
|
| 319 |
+
gr.update(value=settings_obj),
|
| 320 |
+
gr.update(value=js_data),
|
| 321 |
+
)
|
| 322 |
|
| 323 |
def update_ear_visibility(selection, settings_map):
|
| 324 |
has_settings = settings_map.get(selection) is not None
|
| 325 |
return gr.update(visible=has_settings)
|
| 326 |
|
| 327 |
def on_flyout_change(updated_settings, active_id, sampler_val, model_val):
|
| 328 |
+
if updated_settings is None or active_id is None:
|
| 329 |
return
|
| 330 |
if active_id == sampler_dd.elem_id:
|
| 331 |
sampler_settings_map_py[sampler_val] = updated_settings
|
| 332 |
elif active_id == model_dd.elem_id:
|
| 333 |
model_settings_map_py[model_val] = updated_settings
|
| 334 |
+
|
| 335 |
def close_the_flyout():
|
| 336 |
js_data = json.dumps({"isVisible": False, "anchorId": None})
|
| 337 |
return False, None, gr.update(value=js_data)
|
| 338 |
+
|
| 339 |
js_update_flyout = "(jsonData) => { update_flyout_from_state(jsonData); }"
|
| 340 |
+
|
| 341 |
sampler_dd.change(
|
| 342 |
fn=lambda sel: update_ear_visibility(sel, sampler_settings_map_py),
|
| 343 |
inputs=[sampler_dd],
|
| 344 |
+
outputs=[sampler_ear_btn],
|
| 345 |
+
).then(
|
| 346 |
+
fn=close_the_flyout,
|
| 347 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 348 |
+
).then(
|
| 349 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
sampler_ear_btn.click(
|
| 353 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 354 |
+
is_vis, anchor, sampler_dd.elem_id, sampler_settings_map_py.get(sel)
|
| 355 |
+
),
|
| 356 |
inputs=[flyout_visible, active_anchor_id, sampler_dd],
|
| 357 |
+
outputs=[
|
| 358 |
+
flyout_visible,
|
| 359 |
+
active_anchor_id,
|
| 360 |
+
flyout_sheet,
|
| 361 |
+
js_data_bridge,
|
| 362 |
+
],
|
| 363 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 364 |
|
| 365 |
model_dd.change(
|
| 366 |
fn=lambda sel: update_ear_visibility(sel, model_settings_map_py),
|
| 367 |
inputs=[model_dd],
|
| 368 |
+
outputs=[model_ear_btn],
|
| 369 |
+
).then(
|
| 370 |
+
fn=close_the_flyout,
|
| 371 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 372 |
+
).then(
|
| 373 |
+
fn=None, inputs=[js_data_bridge], js=js_update_flyout
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
model_ear_btn.click(
|
| 377 |
+
fn=lambda is_vis, anchor, sel: handle_flyout_toggle(
|
| 378 |
+
is_vis, anchor, model_dd.elem_id, model_settings_map_py.get(sel)
|
| 379 |
+
),
|
| 380 |
inputs=[flyout_visible, active_anchor_id, model_dd],
|
| 381 |
+
outputs=[
|
| 382 |
+
flyout_visible,
|
| 383 |
+
active_anchor_id,
|
| 384 |
+
flyout_sheet,
|
| 385 |
+
js_data_bridge,
|
| 386 |
+
],
|
| 387 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 388 |
+
|
| 389 |
flyout_sheet.change(
|
| 390 |
fn=on_flyout_change,
|
| 391 |
inputs=[flyout_sheet, active_anchor_id, sampler_dd, model_dd],
|
| 392 |
+
outputs=None,
|
| 393 |
)
|
| 394 |
|
| 395 |
close_btn.click(
|
| 396 |
fn=close_the_flyout,
|
| 397 |
inputs=None,
|
| 398 |
+
outputs=[flyout_visible, active_anchor_id, js_data_bridge],
|
| 399 |
).then(fn=None, inputs=[js_data_bridge], js=js_update_flyout)
|
| 400 |
|
| 401 |
def initial_flyout_setup(sampler_val, model_val):
|
| 402 |
return {
|
| 403 |
+
sampler_ear_btn: update_ear_visibility(
|
| 404 |
+
sampler_val, sampler_settings_map_py
|
| 405 |
+
),
|
| 406 |
+
model_ear_btn: update_ear_visibility(
|
| 407 |
+
model_val, model_settings_map_py
|
| 408 |
+
),
|
| 409 |
}
|
| 410 |
+
|
| 411 |
# --- App Load ---
|
| 412 |
+
demo.load(fn=inject_assets, inputs=None, outputs=[html_injector]).then(
|
| 413 |
+
fn=initial_flyout_setup,
|
| 414 |
+
inputs=[sampler_dd, model_dd],
|
| 415 |
+
outputs=[sampler_ear_btn, model_ear_btn],
|
| 416 |
).then(
|
| 417 |
+
fn=None,
|
| 418 |
+
inputs=None,
|
| 419 |
+
outputs=None,
|
| 420 |
+
js="() => { setTimeout(reparent_flyout, 200); }",
|
| 421 |
)
|
| 422 |
+
|
| 423 |
if __name__ == "__main__":
|
| 424 |
demo.launch()
|
| 425 |
+
|
| 426 |
```
|
| 427 |
""", elem_classes=["md-custom"], header_links=True)
|
| 428 |
|