Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
+
import random
|
| 8 |
+
import shutil
|
| 9 |
+
|
| 10 |
+
if not os.path.exists("sd-ggml"):
|
| 11 |
+
os.system("git clone https://huggingface.co/svjack/sd-ggml")
|
| 12 |
+
else:
|
| 13 |
+
shutil.rmtree("sd-ggml")
|
| 14 |
+
assert os.path.exists("sd-ggml")
|
| 15 |
+
os.chdir("sd-ggml")
|
| 16 |
+
assert os.path.exists("stable-diffusion.cpp")
|
| 17 |
+
os.system("cmake stable-diffusion.cpp")
|
| 18 |
+
os.system("cmake --build . --config Release")
|
| 19 |
+
assert os.path.exists("bin")
|
| 20 |
+
|
| 21 |
+
'''
|
| 22 |
+
./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
|
| 23 |
+
./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
|
| 24 |
+
'''
|
| 25 |
+
|
| 26 |
+
def process(model_path ,prompt, num_samples, image_resolution, sample_steps, seed,):
|
| 27 |
+
from PIL import Image
|
| 28 |
+
from uuid import uuid1
|
| 29 |
+
output_path = "output_image_dir"
|
| 30 |
+
if not os.path.exists(output_path):
|
| 31 |
+
os.mkdir(output_path)
|
| 32 |
+
else:
|
| 33 |
+
shutil.rmtree(output_path)
|
| 34 |
+
assert os.path.exists(output_path)
|
| 35 |
+
|
| 36 |
+
run_format = './bin/sd -m {} --sampling-method "dpm++2mv2" -o "{}/{}.png" -p "{}" --steps {} -H {} -W {}'
|
| 37 |
+
images = []
|
| 38 |
+
for i in range(num_samples):
|
| 39 |
+
uid = str(uuid1())
|
| 40 |
+
run_cmd = run_format.format(model_path, output_path,
|
| 41 |
+
uid, prompt, sample_steps, image_resolution,
|
| 42 |
+
image_resolution)
|
| 43 |
+
print("run cmd: {}".format(run_cmd))
|
| 44 |
+
os.system(run_cmd)
|
| 45 |
+
assert os.path.exists(os.path.join(output_path, "{}.png".format(uid)))
|
| 46 |
+
image = Image.open(os.path.join(output_path, "{}.png".format(uid)))
|
| 47 |
+
images.append(np.asarray(image))
|
| 48 |
+
results = images
|
| 49 |
+
return results
|
| 50 |
+
#return [255 - detected_map] + results
|
| 51 |
+
|
| 52 |
+
block = gr.Blocks().queue()
|
| 53 |
+
with block:
|
| 54 |
+
with gr.Row():
|
| 55 |
+
gr.Markdown("## Rapid Diffusion model from warp-ai/wuerstchen")
|
| 56 |
+
#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")
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
#input_image = gr.Image(source='upload', type="numpy", value = "hate_dog.png")
|
| 60 |
+
model_list = list(map(lambda x: os.path.join("models", x), os.listdir("models")))
|
| 61 |
+
assert model_list
|
| 62 |
+
model_path = gr.Dropdown(
|
| 63 |
+
model_list, value = model_list[0],
|
| 64 |
+
label="GGML Models"
|
| 65 |
+
)
|
| 66 |
+
prompt = gr.Textbox(label="Prompt", value = "Anthropomorphic cat dressed as a fire fighter")
|
| 67 |
+
run_button = gr.Button(label="Run")
|
| 68 |
+
with gr.Accordion("Advanced options", open=False):
|
| 69 |
+
num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
|
| 70 |
+
image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=256)
|
| 71 |
+
#low_threshold = gr.Slider(label="Canny low threshold", minimum=1, maximum=255, value=100, step=1)
|
| 72 |
+
#high_threshold = gr.Slider(label="Canny high threshold", minimum=1, maximum=255, value=200, step=1)
|
| 73 |
+
sample_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=8, step=1)
|
| 74 |
+
#scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)
|
| 75 |
+
seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True)
|
| 76 |
+
#eta = gr.Number(label="eta", value=0.0)
|
| 77 |
+
#a_prompt = gr.Textbox(label="Added Prompt", value='')
|
| 78 |
+
#n_prompt = gr.Textbox(label="Negative Prompt",
|
| 79 |
+
# value='低质量,模糊,混乱')
|
| 80 |
+
with gr.Column():
|
| 81 |
+
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
|
| 82 |
+
#ips = [None, prompt, None, None, num_samples, image_resolution, sample_steps, None, seed, None, None, None]
|
| 83 |
+
ips = [model_path ,prompt, num_samples, image_resolution, sample_steps, seed]
|
| 84 |
+
run_button.click(fn=process, inputs=ips, outputs=[result_gallery], show_progress = True)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
block.launch(server_name='0.0.0.0')
|