svjack/stable-diffusion.cpp
3
1import os2import sys3 4import gradio as gr5import numpy as np6import random7import shutil8 9'''10if not os.path.exists("sd-ggml-cpp-dp"):11 os.system("git clone https://huggingface.co/svjack/sd-ggml-cpp-dp")12else:13 shutil.rmtree("sd-ggml-cpp-dp")14 os.system("git clone https://huggingface.co/svjack/sd-ggml-cpp-dp")15assert os.path.exists("sd-ggml-cpp-dp")16'''17 18os.system("pip install huggingface_hub")19#### https://huggingface.co/svjack/sd-ggml-cpp-dp/resolve/main/models/Cyberpunk_Anime_Diffusion-ggml-model_q4_0.bin20def make_and_download_clean_dir(repo_name = "svjack/sd-ggml", 21 rp_tgt_tail_dict = {22 "models": "wget https://huggingface.co/{}/resolve/main/{}/{}"23 }24 ):25 import shutil26 import os27 from tqdm import tqdm28 from huggingface_hub import HfFileSystem29 fs = HfFileSystem()30 req_dir = repo_name.split("/")[-1]31 if os.path.exists(req_dir):32 shutil.rmtree(req_dir)33 os.mkdir(req_dir)34 os.chdir(req_dir)35 fd_list = fs.ls(repo_name, detail = False)36 fd_clean_list = list(filter(lambda x: not x.split("/")[-1].startswith("."), fd_list))37 for path in tqdm(fd_clean_list):38 src = path39 tgt = src.split("/")[-1]40 print("downloading {} to {}".format(src, tgt))41 if tgt not in rp_tgt_tail_dict:42 fs.download(43 src, tgt, recursive = True44 )45 else:46 tgt_cmd_format = rp_tgt_tail_dict[tgt]47 os.mkdir(tgt)48 os.chdir(tgt)49 sub_fd_list = fs.ls(src, detail = False)50 for sub_file in tqdm(sub_fd_list):51 tgt_cmd = tgt_cmd_format.format(52 repo_name, tgt, sub_file.split("/")[-1]53 )54 print("run {}".format(tgt_cmd))55 os.system(tgt_cmd)56 os.chdir("..")57 os.chdir("..")58make_and_download_clean_dir("svjack/sd-ggml")59os.chdir("sd-ggml")60 61assert os.path.exists("stable-diffusion.cpp")62os.system("cmake stable-diffusion.cpp")63os.system("cmake --build . --config Release")64assert os.path.exists("bin")65 66'''67./bin/sd -m ../../../Downloads1/deliberate-ggml-model-q4_0.bin --sampling-method "euler_a" -o "fire-fighter-euler_a-7.png" -p "Anthropomorphic cat dressed as a fire fighter" --steps 768./bin/sd -m ../../../Downloads/anime-ggml-model-q4_0.bin --sampling-method "dpm++2mv2" -o "couple-dpm++2mv2-7-anime.png" -p "In this scene, there's a couple (represented by 👨 and 👩) who share an intense passion or attraction towards each other (symbolized by 🔥). The setting takes place in cold weather conditions represented by snowflakes ❄️" --steps 769'''70 71def process(model_path ,prompt, num_samples, image_resolution, sample_steps, seed,):72 from PIL import Image73 from uuid import uuid174 output_path = "output_image_dir"75 if not os.path.exists(output_path):76 os.mkdir(output_path)77 else:78 shutil.rmtree(output_path)79 os.mkdir(output_path)80 assert os.path.exists(output_path)81 82 run_format = './bin/sd -m {} --sampling-method "dpm++2mv2" -o "{}/{}.png" -p "{}" --steps {} -H {} -W {} -s {}'83 images = []84 for i in range(num_samples):85 uid = str(uuid1())86 run_cmd = run_format.format(model_path, output_path,87 uid, prompt, sample_steps, image_resolution,88 image_resolution, seed + i)89 print("run cmd: {}".format(run_cmd))90 os.system(run_cmd)91 assert os.path.exists(os.path.join(output_path, "{}.png".format(uid)))92 image = Image.open(os.path.join(output_path, "{}.png".format(uid)))93 images.append(np.asarray(image))94 results = images95 return results96 #return [255 - detected_map] + results97 98block = gr.Blocks().queue()99with block:100 with gr.Row():101 gr.Markdown("## StableDiffusion on CPU in CPP ")102 #gr.Markdown("This _example_ was **drive** from <br/><b><h4>[https://github.com/svjack/ControlLoRA-Chinese](https://github.com/svjack/ControlLoRA-Chinese)</h4></b>\n")103 with gr.Row():104 with gr.Column():105 #input_image = gr.Image(source='upload', type="numpy", value = "hate_dog.png")106 model_list = list(map(lambda x: os.path.join("models", x), os.listdir("models")))107 assert model_list108 model_path = gr.Dropdown(109 model_list, value = model_list[0],110 label="GGML Models"111 )112 prompt = gr.Textbox(label="Prompt", value = "A lovely cat drinking a cup of tea")113 run_button = gr.Button(label="Run")114 with gr.Accordion("Advanced options", open=False):115 num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)116 image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=256)117 #low_threshold = gr.Slider(label="Canny low threshold", minimum=1, maximum=255, value=100, step=1)118 #high_threshold = gr.Slider(label="Canny high threshold", minimum=1, maximum=255, value=200, step=1)119 sample_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=8, step=1)120 #scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)121 seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True)122 #eta = gr.Number(label="eta", value=0.0)123 #a_prompt = gr.Textbox(label="Added Prompt", value='')124 #n_prompt = gr.Textbox(label="Negative Prompt",125 # value='低质量,模糊,混乱')126 with gr.Column():127 result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')128 #ips = [None, prompt, None, None, num_samples, image_resolution, sample_steps, None, seed, None, None, None]129 ips = [model_path ,prompt, num_samples, image_resolution, sample_steps, seed]130 run_button.click(fn=process, inputs=ips, outputs=[result_gallery], show_progress = True)131 132 gr.Examples(133 [134 ["models/deliberate-ggml-model-q4_0.bin", "A glass of cola, 8k", 1, 256, 8, 320],135 ["models/anime-ggml-model-q4_0.bin", "A lovely cat drinking a cup of tea", 1, 512, 8, 10],136 ["models/deliberate-ggml-model-q4_0.bin", "Anthropomorphic cat dressed as a fire fighter", 1, 512, 8, 20],137 ],138 inputs = [model_path ,prompt, num_samples, image_resolution, sample_steps, seed],139 label = "Examples"140 )141 142block.launch(server_name='0.0.0.0')143 