{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "JKTCrY9LU7Oq" }, "outputs": [], "source": [ "##~ WIDGET CODE | BY: ANXETY ~##\n", "\n", "import os\n", "import json\n", "import time\n", "import ipywidgets as widgets\n", "from ipywidgets import widgets, Layout, Label, Button, VBox, HBox\n", "from IPython.display import display, HTML, Javascript, clear_output\n", "\n", "\n", "# Setup Env\n", "env = os.getenv('ENV_NAME')\n", "root_path = os.getenv('ROOT_PATH')\n", "webui_path = os.getenv('WEBUI_PATH')\n", "free_plan = os.getenv('FREE_PLAN')\n", "\n", "\n", "# ==================== CSS JS ====================\n", "##~ custom background images V1.5 ~##\n", "import argparse\n", "parser = argparse.ArgumentParser(description='This script processes an background image.')\n", "parser.add_argument('-i', '--image', type=str, help='URL of the image to process', metavar='')\n", "parser.add_argument('-o', '--opacity', type=float, help='Opacity level for the image, between 0 and 1', metavar='', default=0.3)\n", "parser.add_argument('-b', '--blur', type=str, help='Blur level for the image', metavar='', default=0)\n", "parser.add_argument('-y', type=int, help='Y coordinate for the image in px', metavar='', default=0)\n", "parser.add_argument('-x', type=int, help='X coordinate for the image in px', metavar='', default=0)\n", "parser.add_argument('-s', '--scale', type=int, help='Scale image in %%', metavar='', default=100)\n", "parser.add_argument('-m', '--mode', action='store_true', help='Removes repetitive image tiles')\n", "parser.add_argument('-t', '--transparent', action='store_true', help='Makes input/selection fields 35%% more transparent')\n", "parser.add_argument('-bf', '--blur-fields', type=str, help='Background blur level for input/selection fields', metavar='', default=2)\n", "\n", "args = parser.parse_args()\n", "\n", "url_img = args.image\n", "opacity_img = args.opacity\n", "blur_img = args.blur\n", "y_img = args.y\n", "x_img = args.x\n", "scale_img = args.scale\n", "blur_fields = args.blur_fields\n", "\n", "## ---\n", "\"\"\" WTF KAGGLE - WHAT THE FUCK IS THE DIFFERENCE OF 35 PIXELS!?!?!? \"\"\"\n", "fix_heigh_img = \"-810px\" if env == \"Kaggle\" else \"-775px\"\n", "\n", "\"\"\" transperent fields \"\"\"\n", "t_bg_alpha = \"1\" if not args.transparent else \"0.65\"\n", "\n", "\"\"\" mode img - repeats \"\"\"\n", "mode_img = \"repeat\" if not args.mode else \"no-repeat\"\n", "\n", "container_background = f'''\n", "\"))\n", "\n", "# Main JS\n", "JS = '''\n", "\n", "\n", "'''\n", "display(HTML(JS))\n", "\n", "\n", "# ==================== WIDGETS V2 ====================\n", "HR = widgets.HTML('
')\n", "\n", "class WidgetFactory:\n", " def __init__(self, style=None, layout=None):\n", " self.style = style if style else {'description_width': 'initial'}\n", " self.layout = layout if layout else widgets.Layout(max_width='1080px', width='100%')\n", "\n", " def create_html(self, content, class_name=None):\n", " html_widget = widgets.HTML(content)\n", " if class_name:\n", " html_widget.add_class(class_name)\n", " return html_widget\n", "\n", " def create_header(self, name):\n", " return widgets.HTML(f'
{name}
')\n", "\n", " def create_dropdown(self, options, value, description):\n", " return widgets.Dropdown(options=options, value=value, description=description, style=self.style, layout=self.layout)\n", "\n", " def create_text(self, description, placeholder='', value=''):\n", " return widgets.Text(description=description, placeholder=placeholder, value=value, style=self.style, layout=self.layout)\n", "\n", " def create_checkbox(self, value, description):\n", " return widgets.Checkbox(value=value, description=description, style=self.style, layout=self.layout)\n", "\n", " def create_button(self, description, class_name=None):\n", " button = widgets.Button(description=description)\n", " if class_name:\n", " button.add_class(class_name)\n", " return button\n", "\n", " def create_hbox(self, children):\n", " return widgets.HBox(children)\n", "\n", " def create_vbox(self, children, class_names=None):\n", " vbox = widgets.VBox(children)\n", " if class_names:\n", " for class_name in class_names:\n", " vbox.add_class(class_name)\n", " return vbox\n", "\n", " def display(self, widget):\n", " display(widget)\n", "\n", "# Instantiate the factory\n", "factory = WidgetFactory()\n", "\n", "# --- MODEL ---\n", "model_header = factory.create_header('Model Selection')\n", "model_options = ['none',\n", " '1.Anime (by XpucT) + INP',\n", " '2.BluMix [Anime] [V7] + INP',\n", " '3.Cetus-Mix [Anime] [V4] + INP',\n", " '4.Counterfeit [Anime] [V3] + INP',\n", " '5.CuteColor [Anime] [V3]',\n", " '6.Dark-Sushi-Mix [Anime]',\n", " '7.Deliberate [Realism] [V6] + INP',\n", " '8.Meina-Mix [Anime] [V11] + INP',\n", " '9.Mix-Pro [Anime] [V4] + INP']\n", "model_widget = factory.create_dropdown(model_options, '4.Counterfeit [Anime] [V3] + INP', 'Model:')\n", "model_num_widget = factory.create_text('Model Number:', 'Enter the model numbers to be downloaded using comma/space.')\n", "inpainting_model_widget = factory.create_checkbox(False, 'Inpainting Models')\n", "\n", "# Display Model\n", "all_model_box = factory.create_vbox([model_header, model_widget, model_num_widget, inpainting_model_widget], class_names=[\"container\", \"image_1\"])\n", "factory.display(all_model_box)\n", "\n", "# --- VAE ---\n", "vae_header = factory.create_header('VAE Selection')\n", "vae_options = ['none',\n", " '1.Anime.vae',\n", " '2.Anything.vae',\n", " '3.Blessed2.vae',\n", " '4.ClearVae.vae',\n", " '5.WD.vae']\n", "vae_widget = factory.create_dropdown(vae_options, '3.Blessed2.vae', 'Vae:')\n", "vae_num_widget = factory.create_text('Vae Number:', 'Enter the vae numbers to be downloaded using comma/space.')\n", "\n", "# Display Vae\n", "all_vae_box = factory.create_vbox([vae_header, vae_widget, vae_num_widget], class_names=[\"container\", \"image_2\"])\n", "factory.display(all_vae_box)\n", "\n", "# --- ADDITIONAL ---\n", "additional_header = factory.create_header('Additional')\n", "latest_webui_widget = factory.create_checkbox(True, 'Update WebUI')\n", "latest_exstensions_widget = factory.create_checkbox(True, 'Update Extensions')\n", "change_webui_widget = factory.create_dropdown(['A1111', 'Forge'], 'A1111', 'Change WebUI:')\n", "detailed_download_widget = factory.create_dropdown(['off', 'on'], 'off', 'Detailed Download:')\n", "choose_changes_widget = factory.create_hbox([latest_webui_widget, latest_exstensions_widget, change_webui_widget, detailed_download_widget])\n", "\n", "controlnet_options = ['none', 'ALL', '1.canny',\n", " '2.openpose', '3.depth',\n", " '4.normal_map', '5.mlsd',\n", " '6.lineart', '7.soft_edge',\n", " '8.scribble', '9.segmentation',\n", " '10.shuffle', '11.tile',\n", " '12.inpaint', '13.instruct_p2p']\n", "controlnet_widget = factory.create_dropdown(controlnet_options, 'none', 'ControlNet:')\n", "controlnet_num_widget = factory.create_text('ControlNet Number:', 'Enter the ControlNet model numbers to be downloaded using comma/space.')\n", "commit_hash_widget = factory.create_text('Commit Hash:')\n", "huggingface_token_widget = factory.create_text('HuggingFace Token:')\n", "\n", "ngrok_token_widget = factory.create_text('Ngrok Token:')\n", "ngrock_button = factory.create_html('Получить Ngrok Токен', class_name=\"button_ngrok\")\n", "ngrok_widget = factory.create_hbox([ngrok_token_widget, ngrock_button])\n", "\n", "zrok_token_widget = factory.create_text('Zrok Token:')\n", "zrok_button = factory.create_html('Зарегать Zrok Токен', class_name=\"button_ngrok\")\n", "zrok_widget = factory.create_hbox([zrok_token_widget, zrok_button])\n", "\n", "commandline_arguments_options = \"--listen --enable-insecure-extension-access --theme dark --no-half-vae --disable-console-progressbars --xformers\"\n", "commandline_arguments_widget = factory.create_text('Arguments:', value=commandline_arguments_options)\n", "\n", "# Display Additional\n", "additional_widget_list = [additional_header,\n", " choose_changes_widget,\n", " HR,\n", " controlnet_widget,\n", " controlnet_num_widget,\n", " commit_hash_widget,\n", " huggingface_token_widget,\n", " ngrok_widget,\n", " zrok_widget,\n", " HR,\n", " commandline_arguments_widget]\n", "\n", "if free_plan and env == \"Google Colab\": # remove ngrok from colab\n", " additional_widget_list.remove(ngrok_widget)\n", "\n", "all_additional_box = factory.create_vbox(additional_widget_list, class_names=[\"container\", \"image_3\"])\n", "factory.display(all_additional_box)\n", "\n", "# --- CUSTOM DOWNLOAD ---\n", "custom_download_header_popup = factory.create_html('''\n", "\n", "\n", "
Custom Download
\n", "\n", "
INFO
\n", "
\n", " Separate multiple URLs with a comma/space. For a custom name file/extension, specify it with []\n", " after the URL without spaces.\n", " For files, be sure to specify - Filename Extension.\n", "
\n", " Example for File:\n", " https://civitai.com/api/download/models/229782[Detailer.safetensors]\n", "
\n", " Example for Extension:\n", " https://github.com/hako-mikan/sd-webui-regional-prompter[Regional-Prompter]\n", "
\n", "
\n", "''')\n", "\n", "Model_url_widget = factory.create_text('Model:')\n", "Vae_url_widget = factory.create_text('Vae:')\n", "LoRA_url_widget = factory.create_text('LoRa:')\n", "Embedding_url_widget = factory.create_text('Embedding:')\n", "Extensions_url_widget = factory.create_text('Extensions:')\n", "custom_file_urls_widget = factory.create_text('File (txt):')\n", "\n", "# Display CustomDl\n", "all_custom_box = factory.create_vbox([\n", " custom_download_header_popup, Model_url_widget, Vae_url_widget, LoRA_url_widget, Embedding_url_widget, Extensions_url_widget, custom_file_urls_widget\n", "], class_names=[\"container\", \"image_4\", \"container_custom_downlad\"])\n", "factory.display(all_custom_box)\n", "\n", "# --- Save Button ---\n", "save_button = factory.create_button('Save', class_name=\"button_save\")\n", "factory.display(save_button)\n", "\n", "\n", "# ==================== OTHER FUNC ====================\n", "# Setup UI\n", "def setup_webui(change_webui_widget):\n", " if 'SDW_UI' in os.environ:\n", " UI = os.environ['SDW_UI']\n", " else:\n", " os.environ['SDW_UI'] = change_webui_widget\n", " os.environ['SDW_OLD_UI'] = change_webui_widget\n", "\n", " UI = os.getenv('SDW_UI')\n", "\n", " if UI != change_webui_widget:\n", " os.environ['SDW_UI'] = change_webui_widget\n", "\n", "\n", "# ============ Load / Save - Settings V2 ============\n", "SETTINGS_FILE = f'{root_path}/settings.json'\n", "\n", "SETTINGS_KEYS = [\n", " 'model', 'model_num', 'inpainting_model',\n", " 'vae', 'vae_num', 'latest_webui', 'latest_exstensions',\n", " 'change_webui', 'detailed_download', 'controlnet',\n", " 'controlnet_num', 'commit_hash', 'huggingface_token',\n", " 'ngrok_token', 'zrok_token', 'commandline_arguments',\n", " 'Model_url', 'Vae_url', 'LoRA_url', 'Embedding_url',\n", " 'Extensions_url', 'custom_file_urls'\n", "]\n", "\n", "def save_settings():\n", " settings = {key: globals()[f\"{key}_widget\"].value for key in SETTINGS_KEYS}\n", " with open(SETTINGS_FILE, 'w') as f:\n", " json.dump(settings, f, indent=2)\n", "\n", "def load_settings():\n", " if os.path.exists(SETTINGS_FILE):\n", " with open(SETTINGS_FILE, 'r') as f:\n", " settings = json.load(f)\n", " for key in SETTINGS_KEYS:\n", " globals()[f\"{key}_widget\"].value = settings.get(key, \"\")\n", "\n", "def hide_widgets():\n", " widgets_list = [all_model_box, all_vae_box, all_additional_box, all_custom_box, save_button]\n", " for widget in widgets_list:\n", " widget.add_class(\"hide\")\n", " time.sleep(0.5)\n", " widgets.Widget.close_all()\n", "\n", "def save_data(button):\n", " save_settings()\n", " setup_webui(change_webui_widget.value)\n", " hide_widgets()\n", "\n", "load_settings()\n", "save_button.on_click(save_data)" ] } ] }