Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
from lambda_diffusers import StableDiffusionImageEmbedPipeline
|
6 |
+
|
7 |
+
def main(
|
8 |
+
input_im,
|
9 |
+
scale=3.0,
|
10 |
+
n_samples=4,
|
11 |
+
steps=25,
|
12 |
+
seed=0,
|
13 |
+
):
|
14 |
+
|
15 |
+
generator = torch.Generator(device=device).manual_seed(int(seed))
|
16 |
+
|
17 |
+
images_list = pipe(
|
18 |
+
n_samples*[input_im],
|
19 |
+
guidance_scale=scale,
|
20 |
+
num_inference_steps=steps,
|
21 |
+
generator=generator,
|
22 |
+
)
|
23 |
+
|
24 |
+
images = []
|
25 |
+
for i, image in enumerate(images_list["sample"]):
|
26 |
+
if(images_list["nsfw_content_detected"][i]):
|
27 |
+
safe_image = Image.open(r"unsafe.png")
|
28 |
+
images.append(safe_image)
|
29 |
+
else:
|
30 |
+
images.append(image)
|
31 |
+
return images
|
32 |
+
|
33 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
34 |
+
pipe = StableDiffusionImageEmbedPipeline.from_pretrained(
|
35 |
+
"lambdalabs/sd-image-variations-diffusers",
|
36 |
+
revision="273115e88df42350019ef4d628265b8c29ef4af5",
|
37 |
+
)
|
38 |
+
pipe = pipe.to(device)
|
39 |
+
|
40 |
+
inputs = [
|
41 |
+
gr.Image(),
|
42 |
+
gr.Slider(0, 25, value=3, step=1, label="Guidance scale"),
|
43 |
+
gr.Slider(1, 4, value=1, step=1, label="Number images"),
|
44 |
+
gr.Slider(5, 50, value=25, step=5, label="Steps"),
|
45 |
+
gr.Number(0, labal="Seed", precision=0)
|
46 |
+
]
|
47 |
+
output = gr.Gallery(label="Generated variations")
|
48 |
+
output.style(grid=2)
|
49 |
+
|
50 |
+
demo = gr.Interface(
|
51 |
+
fn=main,
|
52 |
+
title="Stable Diffusion Image Variations",
|
53 |
+
description=description,
|
54 |
+
article=article,
|
55 |
+
inputs=inputs,
|
56 |
+
outputs=output,
|
57 |
+
examples=examples,
|
58 |
+
)
|
59 |
+
demo.launch()
|