Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import DiffusionPipeline
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
pipeline = DiffusionPipeline.from_pretrained("Qilex/neoGen2")
|
7 |
+
|
8 |
+
def generate_pets(num_to_generate):
|
9 |
+
images = pipeline(num_to_generate)["sample"]
|
10 |
+
return images
|
11 |
+
|
12 |
+
def concatenate_imgs(imgs):
|
13 |
+
length = len(imgs)
|
14 |
+
if length == 1:
|
15 |
+
return imgs[0]
|
16 |
+
top = Image.fromarray(np.concatenate([np.array(x) for x in imgs[:2]],axis=1))
|
17 |
+
if length == 2:
|
18 |
+
return top
|
19 |
+
if len(imgs)==3:
|
20 |
+
fake = np.zeros([128,128,3],dtype=np.uint8)
|
21 |
+
fake[:] = 255
|
22 |
+
bottom = Image.fromarray(np.concatenate([imgs[2], fake],axis=1))
|
23 |
+
elif len(imgs)==4:
|
24 |
+
bottom = Image.fromarray(np.concatenate([imgs[2], imgs[3]],axis=1))
|
25 |
+
return Image.fromarray(np.concatenate([top,bottom],axis=0))
|
26 |
+
|
27 |
+
def go(num):
|
28 |
+
imgs = generate_pets(num)
|
29 |
+
grid = concatenate_imgs(imgs)
|
30 |
+
print(type(grid))
|
31 |
+
return grid
|
32 |
+
|
33 |
+
title = "VirtualPet Dream"
|
34 |
+
description = """
|
35 |
+
This AI will 'dream' you up a virtual pet.
|
36 |
+
\nThis is a denoising diffusion model trained in 48 hours for a hackathon, so the images can be pretty wonky.
|
37 |
+
\nImages are 128x128px.
|
38 |
+
"""
|
39 |
+
article = '''Here's a gallery of some of the better pets:
|
40 |
+
<div style="display: flex; justify-content:space-evenly">
|
41 |
+
<img src="https://alexlyman.org/external_images/sample_10.png">
|
42 |
+
<img src="https://alexlyman.org/external_images/sample_5.png" >
|
43 |
+
<img src="https://alexlyman.org/external_images/sample_4.png" >
|
44 |
+
<img src="https://alexlyman.org/external_images/sample_8.png" >
|
45 |
+
</div>'''
|
46 |
+
gr.Interface(
|
47 |
+
fn=go,
|
48 |
+
inputs= gr.Slider(1, 4, value = 2, step = 1, label="Number of images to generate (more takes longer)"),
|
49 |
+
outputs=gr.Image(),
|
50 |
+
title=title,
|
51 |
+
description=description,
|
52 |
+
article = article,
|
53 |
+
).launch()
|