Spaces:
Running
Running
Duskfallcrew
commited on
Commit
•
85e9591
1
Parent(s):
b07024e
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
model_id = 'Duskfallcrew/neon-isometric'
|
7 |
+
prefix = 'dskise'
|
8 |
+
|
9 |
+
scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
|
10 |
+
|
11 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
12 |
+
model_id,
|
13 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
14 |
+
scheduler=scheduler)
|
15 |
+
|
16 |
+
pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
|
17 |
+
model_id,
|
18 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
19 |
+
scheduler=scheduler)
|
20 |
+
|
21 |
+
if torch.cuda.is_available():
|
22 |
+
pipe = pipe.to("cuda")
|
23 |
+
pipe_i2i = pipe_i2i.to("cuda")
|
24 |
+
|
25 |
+
def error_str(error, title="Error"):
|
26 |
+
return f"""#### {title}
|
27 |
+
{error}""" if error else ""
|
28 |
+
|
29 |
+
def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", auto_prefix=False):
|
30 |
+
|
31 |
+
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
32 |
+
prompt = f"{prefix} {prompt}" if auto_prefix else prompt
|
33 |
+
|
34 |
+
try:
|
35 |
+
if img is not None:
|
36 |
+
return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None
|
37 |
+
else:
|
38 |
+
return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
|
39 |
+
except Exception as e:
|
40 |
+
return None, error_str(e)
|
41 |
+
|
42 |
+
def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
|
43 |
+
|
44 |
+
result = pipe(
|
45 |
+
prompt,
|
46 |
+
negative_prompt = neg_prompt,
|
47 |
+
num_inference_steps = int(steps),
|
48 |
+
guidance_scale = guidance,
|
49 |
+
width = width,
|
50 |
+
height = height,
|
51 |
+
generator = generator)
|
52 |
+
|
53 |
+
return result.images[0]
|
54 |
+
|
55 |
+
def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
|
56 |
+
|
57 |
+
ratio = min(height / img.height, width / img.width)
|
58 |
+
img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
|
59 |
+
result = pipe_i2i(
|
60 |
+
prompt,
|
61 |
+
negative_prompt = neg_prompt,
|
62 |
+
init_image = img,
|
63 |
+
num_inference_steps = int(steps),
|
64 |
+
strength = strength,
|
65 |
+
guidance_scale = guidance,
|
66 |
+
width = width,
|
67 |
+
height = height,
|
68 |
+
generator = generator)
|
69 |
+
|
70 |
+
return result.images[0]
|
71 |
+
|
72 |
+
css = """.main-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.main-div div h1{font-weight:900;margin-bottom:7px}.main-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
|
73 |
+
"""
|
74 |
+
with gr.Blocks(css=css) as demo:
|
75 |
+
gr.HTML(
|
76 |
+
f"""
|
77 |
+
<div class="main-div">
|
78 |
+
<div>
|
79 |
+
<h1>Neon Isometric</h1>
|
80 |
+
</div>
|
81 |
+
<p>
|
82 |
+
Demo for <a href="https://huggingface.co/Duskfallcrew/neon-isometric">Neon Isometric</a> Stable Diffusion model. <br> All samples and info are here:
|
83 |
+
<a href="hhttps://civitai.com/user/duskfallcrew"> On Civit </a> <br>
|
84 |
+
If you want to donate towards costs and don't want to subscribe:
|
85 |
+
<a href="hhttps://ko-fi.com/DUSKFALLcrew> Coffee Is nice! </a> <br>
|
86 |
+
{"Add the following tokens to your prompts for the model to work properly: <b>prefix</b>" if prefix else ""}
|
87 |
+
</p>
|
88 |
+
Running on {"<b>GPU 🔥</b>" if torch.cuda.is_available() else f"<b>CPU 🥶</b>. For faster inference it is recommended to <b>upgrade to GPU in <a href='https://huggingface.co/spaces/Duskfallcrew/neon-isometric/settings'>Settings</a></b>"} after duplicating the space<br><br>
|
89 |
+
<a style="display:inline-block" href="https://huggingface.co/spaces/Duskfallcrew/neon-isometric?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
|
90 |
+
</div>
|
91 |
+
"""
|
92 |
+
)
|
93 |
+
with gr.Row():
|
94 |
+
|
95 |
+
with gr.Column(scale=55):
|
96 |
+
with gr.Group():
|
97 |
+
with gr.Row():
|
98 |
+
prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder=f"{prefix} [your prompt]").style(container=False)
|
99 |
+
generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
|
100 |
+
|
101 |
+
image_out = gr.Image(height=512)
|
102 |
+
error_output = gr.Markdown()
|
103 |
+
|
104 |
+
with gr.Column(scale=45):
|
105 |
+
with gr.Tab("Options"):
|
106 |
+
with gr.Group():
|
107 |
+
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
|
108 |
+
auto_prefix = gr.Checkbox(label="Prefix styling tokens automatically (dskise)", value=prefix, visible=prefix)
|
109 |
+
|
110 |
+
with gr.Row():
|
111 |
+
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
112 |
+
steps = gr.Slider(label="Steps", value=25, minimum=2, maximum=75, step=1)
|
113 |
+
|
114 |
+
with gr.Row():
|
115 |
+
width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
|
116 |
+
height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
|
117 |
+
|
118 |
+
seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
|
119 |
+
|
120 |
+
with gr.Tab("Image to image"):
|
121 |
+
with gr.Group():
|
122 |
+
image = gr.Image(label="Image", height=256, tool="editor", type="pil")
|
123 |
+
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
|
124 |
+
|
125 |
+
auto_prefix.change(lambda x: gr.update(placeholder=f"{prefix} [your prompt]" if x else "[Your prompt]"), inputs=auto_prefix, outputs=prompt, queue=False)
|
126 |
+
|
127 |
+
inputs = [prompt, guidance, steps, width, height, seed, image, strength, neg_prompt, auto_prefix]
|
128 |
+
outputs = [image_out, error_output]
|
129 |
+
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
130 |
+
generate.click(inference, inputs=inputs, outputs=outputs)
|
131 |
+
|
132 |
+
gr.HTML("""
|
133 |
+
<div style="border-top: 1px solid #303030;">
|
134 |
+
<br>
|
135 |
+
<p>This space was created using <a href="https://huggingface.co/spaces/anzorq/sd-space-creator">SD Space Creator</a>.</p>
|
136 |
+
</div>
|
137 |
+
""")
|
138 |
+
|
139 |
+
demo.queue(concurrency_count=1)
|
140 |
+
demo.launch()
|