File size: 4,888 Bytes
49bc630
 
 
 
 
 
 
 
 
 
 
 
574e257
49bc630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
574e257
49bc630
 
5325fa9
49bc630
 
 
 
 
5325fa9
49bc630
 
 
 
 
 
 
 
 
 
 
 
cab936f
49bc630
 
 
 
 
 
 
4174244
49bc630
 
 
 
 
 
f20ae9a
49bc630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4174244
 
f20ae9a
 
4174244
 
 
 
49bc630
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import sys

import gradio as gr
import numpy as np
import random
import shutil

if not os.path.exists("sd-ggml"):
    os.system("git clone https://huggingface.co/svjack/sd-ggml")
else:
    shutil.rmtree("sd-ggml")
    os.system("git clone https://huggingface.co/svjack/sd-ggml")
assert os.path.exists("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[1],
             label="GGML Models"
            )
            prompt = gr.Textbox(label="Prompt", value = "Anthropomorphic cat dressed as a fire fighter")
            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/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')