Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| import gradio as gr | |
| import numpy as np | |
| import random | |
| import shutil | |
| ''' | |
| if not os.path.exists("sd-ggml-cpp-dp"): | |
| os.system("git clone https://huggingface.co/svjack/sd-ggml-cpp-dp") | |
| else: | |
| shutil.rmtree("sd-ggml-cpp-dp") | |
| os.system("git clone https://huggingface.co/svjack/sd-ggml-cpp-dp") | |
| assert os.path.exists("sd-ggml-cpp-dp") | |
| ''' | |
| os.system("pip install huggingface_hub") | |
| #### https://huggingface.co/svjack/sd-ggml-cpp-dp/resolve/main/models/Cyberpunk_Anime_Diffusion-ggml-model_q4_0.bin | |
| def make_and_download_clean_dir(repo_name = "svjack/sd-ggml", | |
| rp_tgt_tail_dict = { | |
| "models": "wget https://huggingface.co/{}/resolve/main/{}/{}" | |
| } | |
| ): | |
| import shutil | |
| import os | |
| from tqdm import tqdm | |
| from huggingface_hub import HfFileSystem | |
| fs = HfFileSystem() | |
| req_dir = repo_name.split("/")[-1] | |
| if os.path.exists(req_dir): | |
| shutil.rmtree(req_dir) | |
| os.mkdir(req_dir) | |
| os.chdir(req_dir) | |
| fd_list = fs.ls(repo_name, detail = False) | |
| fd_clean_list = list(filter(lambda x: not x.split("/")[-1].startswith("."), fd_list)) | |
| for path in tqdm(fd_clean_list): | |
| src = path | |
| tgt = src.split("/")[-1] | |
| print("downloading {} to {}".format(src, tgt)) | |
| if tgt not in rp_tgt_tail_dict: | |
| fs.download( | |
| src, tgt, recursive = True | |
| ) | |
| else: | |
| tgt_cmd_format = rp_tgt_tail_dict[tgt] | |
| os.mkdir(tgt) | |
| os.chdir(tgt) | |
| sub_fd_list = fs.ls(src, detail = False) | |
| for sub_file in tqdm(sub_fd_list): | |
| tgt_cmd = tgt_cmd_format.format( | |
| repo_name, tgt, sub_file.split("/")[-1] | |
| ) | |
| print("run {}".format(tgt_cmd)) | |
| os.system(tgt_cmd) | |
| os.chdir("..") | |
| os.chdir("..") | |
| make_and_download_clean_dir("svjack/sd-ggml") | |
| os.chdir("sd-ggml") | |
| assert os.path.exists("stable-diffusion.cpp") | |
| os.system("cmake stable-diffusion.cpp") | |
| os.system("cmake --build . --config Release") | |
| assert os.path.exists("bin") | |
| ''' | |
| ./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 7 | |
| ./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 7 | |
| ''' | |
| def process(model_path ,prompt, num_samples, image_resolution, sample_steps, seed,): | |
| from PIL import Image | |
| from uuid import uuid1 | |
| output_path = "output_image_dir" | |
| if not os.path.exists(output_path): | |
| os.mkdir(output_path) | |
| else: | |
| shutil.rmtree(output_path) | |
| os.mkdir(output_path) | |
| assert os.path.exists(output_path) | |
| run_format = './bin/sd -m {} --sampling-method "dpm++2mv2" -o "{}/{}.png" -p "{}" --steps {} -H {} -W {} -s {}' | |
| images = [] | |
| for i in range(num_samples): | |
| uid = str(uuid1()) | |
| run_cmd = run_format.format(model_path, output_path, | |
| uid, prompt, sample_steps, image_resolution, | |
| image_resolution, seed + i) | |
| print("run cmd: {}".format(run_cmd)) | |
| os.system(run_cmd) | |
| assert os.path.exists(os.path.join(output_path, "{}.png".format(uid))) | |
| image = Image.open(os.path.join(output_path, "{}.png".format(uid))) | |
| images.append(np.asarray(image)) | |
| results = images | |
| return results | |
| #return [255 - detected_map] + results | |
| block = gr.Blocks().queue() | |
| with block: | |
| with gr.Row(): | |
| gr.Markdown("## StableDiffusion on CPU in CPP ") | |
| #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") | |
| with gr.Row(): | |
| with gr.Column(): | |
| #input_image = gr.Image(source='upload', type="numpy", value = "hate_dog.png") | |
| model_list = list(map(lambda x: os.path.join("models", x), os.listdir("models"))) | |
| assert model_list | |
| model_path = gr.Dropdown( | |
| model_list, value = model_list[0], | |
| label="GGML Models" | |
| ) | |
| prompt = gr.Textbox(label="Prompt", value = "A lovely cat drinking a cup of tea") | |
| run_button = gr.Button(label="Run") | |
| with gr.Accordion("Advanced options", open=False): | |
| num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1) | |
| image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=256) | |
| #low_threshold = gr.Slider(label="Canny low threshold", minimum=1, maximum=255, value=100, step=1) | |
| #high_threshold = gr.Slider(label="Canny high threshold", minimum=1, maximum=255, value=200, step=1) | |
| sample_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=8, step=1) | |
| #scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1) | |
| seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True) | |
| #eta = gr.Number(label="eta", value=0.0) | |
| #a_prompt = gr.Textbox(label="Added Prompt", value='') | |
| #n_prompt = gr.Textbox(label="Negative Prompt", | |
| # value='低质量,模糊,混乱') | |
| with gr.Column(): | |
| result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto') | |
| #ips = [None, prompt, None, None, num_samples, image_resolution, sample_steps, None, seed, None, None, None] | |
| ips = [model_path ,prompt, num_samples, image_resolution, sample_steps, seed] | |
| run_button.click(fn=process, inputs=ips, outputs=[result_gallery], show_progress = True) | |
| gr.Examples( | |
| [ | |
| ["models/deliberate-ggml-model-q4_0.bin", "A glass of cola, 8k", 1, 256, 8, 320], | |
| ["models/anime-ggml-model-q4_0.bin", "A lovely cat drinking a cup of tea", 1, 512, 8, 10], | |
| ["models/deliberate-ggml-model-q4_0.bin", "Anthropomorphic cat dressed as a fire fighter", 1, 512, 8, 20], | |
| ], | |
| inputs = [model_path ,prompt, num_samples, image_resolution, sample_steps, seed], | |
| label = "Examples" | |
| ) | |
| block.launch(server_name='0.0.0.0') | |