cheezay/ControlNet
0
1#!/usr/bin/env python2 3from __future__ import annotations4 5import os6import pathlib7import shlex8import subprocess9 10import gradio as gr11 12if os.getenv('SYSTEM') == 'spaces':13 with open('patch') as f:14 subprocess.run(shlex.split('patch -p1'), stdin=f, cwd='ControlNet')15 16base_url = 'https://huggingface.co/lllyasviel/ControlNet/resolve/main/annotator/ckpts/'17names = [18 'body_pose_model.pth',19 'dpt_hybrid-midas-501f0c75.pt',20 'hand_pose_model.pth',21 'mlsd_large_512_fp32.pth',22 'mlsd_tiny_512_fp32.pth',23 'network-bsds500.pth',24 'upernet_global_small.pth',25]26for name in names:27 command = f'wget https://huggingface.co/lllyasviel/ControlNet/resolve/main/annotator/ckpts/{name} -O {name}'28 out_path = pathlib.Path(f'ControlNet/annotator/ckpts/{name}')29 if out_path.exists():30 continue31 subprocess.run(shlex.split(command), cwd='ControlNet/annotator/ckpts/')32 33from gradio_canny2image import create_demo as create_demo_canny34from gradio_depth2image import create_demo as create_demo_depth35from gradio_fake_scribble2image import create_demo as create_demo_fake_scribble36from gradio_hed2image import create_demo as create_demo_hed37from gradio_hough2image import create_demo as create_demo_hough38from gradio_normal2image import create_demo as create_demo_normal39from gradio_pose2image import create_demo as create_demo_pose40from gradio_scribble2image import create_demo as create_demo_scribble41from gradio_scribble2image_interactive import \42 create_demo as create_demo_scribble_interactive43from gradio_seg2image import create_demo as create_demo_seg44from model import Model45 46MAX_IMAGES = 147DESCRIPTION = '''# ControlNet48 49This is an unofficial demo for [https://github.com/lllyasviel/ControlNet](https://github.com/lllyasviel/ControlNet).50 51If you are interested in trying out other base models, check out [this Space](https://huggingface.co/spaces/hysts/ControlNet-with-other-models) as well.52'''53if (SPACE_ID := os.getenv('SPACE_ID')) is not None:54 DESCRIPTION += f'''<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.<br/>55<a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true">56<img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>57<p/>58'''59 60model = Model()61 62with gr.Blocks(css='style.css') as demo:63 gr.Markdown(DESCRIPTION)64 with gr.Tabs():65 with gr.TabItem('Canny'):66 create_demo_canny(model.process_canny, max_images=MAX_IMAGES)67 with gr.TabItem('Hough'):68 create_demo_hough(model.process_hough, max_images=MAX_IMAGES)69 with gr.TabItem('HED'):70 create_demo_hed(model.process_hed, max_images=MAX_IMAGES)71 with gr.TabItem('Scribble'):72 create_demo_scribble(model.process_scribble, max_images=MAX_IMAGES)73 with gr.TabItem('Scribble Interactive'):74 create_demo_scribble_interactive(75 model.process_scribble_interactive, max_images=MAX_IMAGES)76 with gr.TabItem('Fake Scribble'):77 create_demo_fake_scribble(model.process_fake_scribble,78 max_images=MAX_IMAGES)79 with gr.TabItem('Pose'):80 create_demo_pose(model.process_pose, max_images=MAX_IMAGES)81 with gr.TabItem('Segmentation'):82 create_demo_seg(model.process_seg, max_images=MAX_IMAGES)83 with gr.TabItem('Depth'):84 create_demo_depth(model.process_depth, max_images=MAX_IMAGES)85 with gr.TabItem('Normal map'):86 create_demo_normal(model.process_normal, max_images=MAX_IMAGES)87 88demo.queue(api_open=False).launch()89 