{ "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": [ "##~ LAUNCH CODE | BY: ANXETY ~##\n", "\n", "import os\n", "import re\n", "import time\n", "import json\n", "import requests\n", "from datetime import timedelta\n", "\n", "\n", "# ================= DETECT ENV =================\n", "def detect_environment():\n", " free_plan = (os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / (1024. ** 3) <= 20)\n", " environments = {\n", " 'COLAB_GPU': ('Google Colab', \"/root\" if free_plan else \"/content\"),\n", " 'KAGGLE_URL_BASE': ('Kaggle', \"/kaggle/working/content\"),\n", " 'SAGEMAKER_INTERNAL_IMAGE_URI': ('SageMaker Studio Lab', \"/home/studio-lab-user/content\")\n", " }\n", "\n", " for env_var, (environment, path) in environments.items():\n", " if env_var in os.environ:\n", " return environment, path, free_plan\n", "\n", "env, root_path, free_plan = detect_environment()\n", "webui_path = f\"{root_path}/sdw\"\n", "# ----------------------------------------------\n", "\n", "def load_settings():\n", " SETTINGS_FILE = f'{root_path}/settings.json'\n", " if os.path.exists(SETTINGS_FILE):\n", " with open(SETTINGS_FILE, 'r') as f:\n", " settings = json.load(f)\n", " return settings\n", "\n", "settings = load_settings()\n", "ngrok_token = settings['ngrok_token']\n", "commandline_arguments = settings['commandline_arguments']\n", "\n", "\n", "# ======================== TUNNEL ========================\n", "if env != \"SageMaker Studio Lab\":\n", " import cloudpickle as pickle\n", "\n", " def get_public_ip(version='ipv4'):\n", " try:\n", " url = f'https://api64.ipify.org?format=json&{version}=true'\n", " response = requests.get(url)\n", " data = response.json()\n", " public_ip = data['ip']\n", " return public_ip\n", " except Exception as e:\n", " print(f\"Error getting public {version} address:\", e)\n", "\n", " public_ipv4 = get_public_ip(version='ipv4')\n", "\n", " tunnel_class = pickle.load(open(f\"{root_path}/new_tunnel\", \"rb\"), encoding=\"utf-8\")\n", " tunnel_port= 1769\n", " tunnel = tunnel_class(tunnel_port)\n", " tunnel.add_tunnel(command=\"cl tunnel --url localhost:{port}\", name=\"cl\", pattern=re.compile(r\"[\\w-]+\\.trycloudflare\\.com\"))\n", " tunnel.add_tunnel(command=\"lt --port {port}\", name=\"lt\", pattern=re.compile(r\"[\\w-]+\\.loca\\.lt\"), note=\"Password : \" + \"\\033[32m\" + public_ipv4 + \"\\033[0m\" + \" rerun cell if 404 error.\")\n", "# ======================== TUNNEL ========================\n", "\n", "\n", "# automatic fixing path V2\n", "!sed -i 's#\"tagger_hf_cache_dir\": \".*models/interrogators\"#\"tagger_hf_cache_dir\": \"{root_path}/sdw/models/interrogators\"#' {webui_path}/config.json\n", "!sed -i 's#\"additional_networks_extra_lora_path\": \".*models/Lora/\"#\"additional_networks_extra_lora_path\": \"{root_path}/sdw/models/Lora/\"#' {webui_path}/config.json\n", "# ---\n", "!sed -i 's/\"sd_checkpoint_hash\": \".*\"/\"sd_checkpoint_hash\": \"\"/g; s/\"sd_model_checkpoint\": \".*\"/\"sd_model_checkpoint\": \"\"/g; s/\"sd_vae\": \".*\"/\"sd_vae\": \"None\"/g' {webui_path}/config.json\n", "\n", "\n", "if env != \"SageMaker Studio Lab\":\n", " with tunnel:\n", " %cd {webui_path}\n", " commandline_arguments += f\" --port=1769\"\n", "\n", " if ngrok_token:\n", " commandline_arguments += ' --ngrok ' + ngrok_token\n", " if env != \"Google Colab\":\n", " commandline_arguments += f\" --encrypt-pass=1769\"\n", "\n", " !COMMANDLINE_ARGS=\"{commandline_arguments}\" python launch.py\n", "\n", " start_colab = float(open(f'{webui_path}/static/colabTimer.txt', 'r').read())\n", " time_since_start = str(timedelta(seconds=time.time()-start_colab)).split('.')[0]\n", " print(f\"\\n⌚️ \\033[0mВы проводите эту сессию в течение - \\033[33m{time_since_start}\\033[0m\\n\\n\")\n", "\n", "else:\n", " if ngrok_token:\n", " %cd {webui_path}\n", "\n", " commandline_arguments += ' --ngrok ' + ngrok_token\n", "\n", " !COMMANDLINE_ARGS=\"{commandline_arguments}\" python launch.py\n", "\n", " start_colab = float(open(f'{webui_path}/static/colabTimer.txt', 'r').read())\n", " time_since_start = str(timedelta(seconds=time.time()-start_colab)).split('.')[0]\n", " print(f\"\\n⌚️ \\033[0mВы проводите эту сессию в течение - \\033[33m{time_since_start}\\033[0m\\n\\n\")\n", "\n", " else:\n", " print(\"Упс... Я думаю, вы забыли вставить токен `ngrok`..... вернитесь к виджетам и вставьте его, чтобы запустить webui... без него никак :/\\nВы можете получить токен здесь:\\n\\nhttps://dashboard.ngrok.com/get-started/your-authtoken\")" ] } ] }