From a35ddd415ba1bdf9dec9679f179ef1ae8f125777 Mon Sep 17 00:00:00 2001 From: will chandler Date: Fri, 6 Sep 2024 17:10:14 -0400 Subject: [PATCH 1/2] async wait for cloudflare link read --- fast_stable_diffusion_AUTOMATIC1111.ipynb | 61 ++++++++++++++++------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/fast_stable_diffusion_AUTOMATIC1111.ipynb b/fast_stable_diffusion_AUTOMATIC1111.ipynb index 5c83a0b5..13c23526 100644 --- a/fast_stable_diffusion_AUTOMATIC1111.ipynb +++ b/fast_stable_diffusion_AUTOMATIC1111.ipynb @@ -578,6 +578,10 @@ "import fileinput\n", "from pyngrok import ngrok, conf\n", "import re\n", + "import aiofiles\n", + "import asyncio\n", + "import re\n", + "from typing import Optional\n", "\n", "\n", "Use_Cloudflare_Tunnel = False #@param {type:\"boolean\"}\n", @@ -602,7 +606,7 @@ " !wget -q -O sd_models.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-w$blsaphemy/master/modules/sd_models.py\n", " !wget -q -O /usr/local/lib/python3.10/dist-packages/gradio/blocks.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/blocks.py\n", " %cd /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/\n", - " \n", + "\n", " !sed -i 's@shared.opts.data\\[\"sd_model_checkpoint\"] = checkpoint_info.title@shared.opts.data\\[\"sd_model_checkpoint\"] = checkpoint_info.title;model.half()@' /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/sd_models.py\n", " #!sed -i 's@ui.create_ui().*@ui.create_ui();shared.demo.queue(concurrency_count=999999,status_update_rate=0.1)@' /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/webui.py\n", " !sed -i \"s@map_location='cpu'@map_location='cuda'@\" /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/extras.py\n", @@ -614,6 +618,27 @@ " !sed -i 's@print(\\\"No module.*@@' /content/gdrive/$mainpth/sd/stablediffusion/ldm/modules/diffusionmodules/model.py\n", " !sed -i 's@\\[\"sd_model_checkpoint\"\\]@\\[\"sd_model_checkpoint\", \"sd_vae\", \"CLIP_stop_at_last_layers\", \"inpainting_mask_weight\", \"initial_noise_multiplier\"\\]@g' /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/modules/shared.py\n", "\n", + "\n", + "async def get_cloudflare_url(filename: str, timeout: int = 60) -> Optional[str]:\n", + " pattern = r\"https?://(?:\\S+?\\.)?trycloudflare\\.com\\S*\"\n", + " start_time = asyncio.get_event_loop().time()\n", + "\n", + " async def read_file():\n", + " async with aiofiles.open(filename, mode='r') as file:\n", + " while True:\n", + " line = await file.readline()\n", + " if line:\n", + " yield line\n", + " else:\n", + " await asyncio.sleep(0.1)\n", + "\n", + " async for line in read_file():\n", + " if asyncio.get_event_loop().time() - start_time > timeout:\n", + " return None\n", + " match = re.search(pattern, line)\n", + " if match:\n", + " return match.group(0)\n", + "\n", "share=''\n", "if Ngrok_token!=\"\":\n", " ngrok.kill()\n", @@ -633,22 +658,24 @@ "elif Use_Cloudflare_Tunnel:\n", " with capture.capture_output() as cap:\n", " !pkill cloudflared\n", - " time.sleep(4)\n", " !nohup cloudflared tunnel --url http://localhost:7860 > /content/srv.txt 2>&1 &\n", - " time.sleep(4)\n", - " with open('/content/srv.txt', \"r\") as file: text = file.read()\n", - " srv= re.findall(r\"https?://(?:\\S+?\\.)?trycloudflare\\.com\\S*\", text)[0]\n", - "\n", - " for line in fileinput.input('/usr/local/lib/python3.10/dist-packages/gradio/blocks.py', inplace=True):\n", - " if line.strip().startswith('self.server_name ='):\n", - " line = f' self.server_name = \"{srv[8:]}\"\\n'\n", - " if line.strip().startswith('self.protocol = \"https\"'):\n", - " line = ' self.protocol = \"https\"\\n'\n", - " if line.strip().startswith('if self.local_url.startswith(\"https\") or self.is_colab'):\n", - " line = ''\n", - " if line.strip().startswith('else \"http\"'):\n", - " line = ''\n", - " sys.stdout.write(line)\n", + "\n", + " future = asyncio.ensure_future(get_cloudflare_url('/content/srv.txt'))\n", + " srv = await future\n", + "\n", + " if srv:\n", + " for line in fileinput.input('/usr/local/lib/python3.10/dist-packages/gradio/blocks.py', inplace=True):\n", + " if line.strip().startswith('self.server_name ='):\n", + " line = f' self.server_name = \"{srv[8:]}\"\\n'\n", + " if line.strip().startswith('self.protocol = \"https\"'):\n", + " line = ' self.protocol = \"https\"\\n'\n", + " if line.strip().startswith('if self.local_url.startswith(\"https\") or self.is_colab'):\n", + " line = ''\n", + " if line.strip().startswith('else \"http\"'):\n", + " line = ''\n", + " sys.stdout.write(line)\n", + " else:\n", + " print(\"Cloudflare URL not found within the timeout period.\")\n", "\n", " !rm /content/srv.txt\n", "\n", @@ -686,4 +713,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file From 521a5079a2cef817a4c7bd1493bdbe32a56d067d Mon Sep 17 00:00:00 2001 From: will chandler Date: Fri, 6 Sep 2024 18:04:09 -0400 Subject: [PATCH 2/2] default to original way if no async loop in runtime --- fast_stable_diffusion_AUTOMATIC1111.ipynb | 41 ++++++++++++++--------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/fast_stable_diffusion_AUTOMATIC1111.ipynb b/fast_stable_diffusion_AUTOMATIC1111.ipynb index 13c23526..4329bd68 100644 --- a/fast_stable_diffusion_AUTOMATIC1111.ipynb +++ b/fast_stable_diffusion_AUTOMATIC1111.ipynb @@ -658,24 +658,33 @@ "elif Use_Cloudflare_Tunnel:\n", " with capture.capture_output() as cap:\n", " !pkill cloudflared\n", - " !nohup cloudflared tunnel --url http://localhost:7860 > /content/srv.txt 2>&1 &\n", - "\n", - " future = asyncio.ensure_future(get_cloudflare_url('/content/srv.txt'))\n", - " srv = await future\n", + " try:\n", + " loop = asyncio.get_event_loop()\n", + " if loop.is_running():\n", + " !nohup cloudflared tunnel --url http://localhost:7860 > /content/srv.txt 2>&1 &\n", + " future = asyncio.ensure_future(get_cloudflare_url('/content/srv.txt'))\n", + " srv = await future\n", + " else:\n", + " raise RuntimeError()\n", + " except RuntimeError:\n", + " time.sleep(2)\n", + " !nohup cloudflared tunnel --url http://localhost:7860 > /content/srv.txt 2>&1 &\n", + " time.sleep(4)\n", + " with open('/content/srv.txt', \"r\") as file:\n", + " text = file.read()\n", + " srv = re.findall(r\"https?://(?:\\S+?\\.)?trycloudflare\\.com\\S*\", text)[0]\n", "\n", " if srv:\n", - " for line in fileinput.input('/usr/local/lib/python3.10/dist-packages/gradio/blocks.py', inplace=True):\n", - " if line.strip().startswith('self.server_name ='):\n", - " line = f' self.server_name = \"{srv[8:]}\"\\n'\n", - " if line.strip().startswith('self.protocol = \"https\"'):\n", - " line = ' self.protocol = \"https\"\\n'\n", - " if line.strip().startswith('if self.local_url.startswith(\"https\") or self.is_colab'):\n", - " line = ''\n", - " if line.strip().startswith('else \"http\"'):\n", - " line = ''\n", - " sys.stdout.write(line)\n", - " else:\n", - " print(\"Cloudflare URL not found within the timeout period.\")\n", + " for line in fileinput.input('/usr/local/lib/python3.10/dist-packages/gradio/blocks.py', inplace=True):\n", + " if line.strip().startswith('self.server_name ='):\n", + " line = f' self.server_name = \"{srv[8:]}\"\\n'\n", + " if line.strip().startswith('self.protocol = \"https\"'):\n", + " line = ' self.protocol = \"https\"\\n'\n", + " if line.strip().startswith('if self.local_url.startswith(\"https\") or self.is_colab'):\n", + " line = ''\n", + " if line.strip().startswith('else \"http\"'):\n", + " line = ''\n", + " sys.stdout.write(line)\n", "\n", " !rm /content/srv.txt\n", "\n",