Spaces:
Build error
Build error
Commit
·
d445f18
1
Parent(s):
16aa660
Initial attempt
Browse files- app.py +82 -10
- requirements.txt +6 -1
app.py
CHANGED
@@ -1,11 +1,83 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
from os.path import exists as path_exists
|
6 |
+
from git.repo.base import Repo
|
7 |
+
from einops import rearrange
|
8 |
+
import torchvision.transforms as transforms
|
9 |
+
from torchvision.utils import make_grid
|
10 |
+
|
11 |
+
if not (path_exists(f"rudalle-aspect-ratio")):
|
12 |
+
Repo.clone_from("https://github.com/shonenkov-AI/rudalle-aspect-ratio", "rudalle-aspect-ratio")
|
13 |
+
|
14 |
+
import sys
|
15 |
+
sys.path.append('./rudalle-aspect-ratio')
|
16 |
+
from rudalle_aspect_ratio import RuDalleAspectRatio, get_rudalle_model
|
17 |
+
from rudalle import get_vae, get_tokenizer
|
18 |
+
from rudalle.pipelines import show
|
19 |
+
#model_path_e = hf_hub_download(repo_id="multimodalart/compvis-latent-diffusion-text2img-large", filename="txt2img-f8-large.ckpt")
|
20 |
+
|
21 |
+
device = 'cuda'
|
22 |
+
dalle_surreal = get_rudalle_model('Surrealist_XL', fp16=True, device=device)
|
23 |
+
dalle_real = get_rudalle_model('Malevich',fp16=True,device=device)
|
24 |
+
dalle_emoji = get_rudalle_model('Emojich',fp16=True,device=device)
|
25 |
+
vae, tokenizer = get_vae().to(device), get_tokenizer()
|
26 |
+
def np_gallery(array, ncols=3):
|
27 |
+
nindex, height, width, intensity = array.shape
|
28 |
+
nrows = nindex//ncols
|
29 |
+
assert nindex == nrows*ncols
|
30 |
+
# want result.shape = (height*nrows, width*ncols, intensity)
|
31 |
+
result = (array.reshape(nrows, ncols, height, width, intensity)
|
32 |
+
.swapaxes(1,2)
|
33 |
+
.reshape(height*nrows, width*ncols, intensity))
|
34 |
+
return result
|
35 |
+
|
36 |
+
def image_to_np(image):
|
37 |
+
return np.asarray(image)
|
38 |
+
|
39 |
+
def run(prompt, aspect_ratio, model):
|
40 |
+
if(model=='Surrealism'):
|
41 |
+
dalle = dalle_surreal
|
42 |
+
elif(model=='Realism'):
|
43 |
+
dalle = dalle_real
|
44 |
+
elif(model=='Emoji'):
|
45 |
+
dalle = dalle_emoji
|
46 |
+
if(aspect_ratio == 'Square'):
|
47 |
+
aspect_ratio_value = 1
|
48 |
+
top_k = 512
|
49 |
+
elif(aspect_ratio == 'Horizontal'):
|
50 |
+
aspect_ratio_value = 32/9
|
51 |
+
top_k = 1024
|
52 |
+
elif(aspect_ratio == 'Vertical'):
|
53 |
+
aspect_ratio_value = 9/32
|
54 |
+
top_k = 512
|
55 |
+
rudalle_ar = RuDalleAspectRatio(
|
56 |
+
dalle=dalle, vae=vae, tokenizer=tokenizer,
|
57 |
+
aspect_ratio=aspect_ratio_value, bs=1, device=device
|
58 |
+
)
|
59 |
+
|
60 |
+
_, result_pil_images = rudalle_ar.generate_images(prompt, top_k, 0.975, 1)
|
61 |
+
#np_images = map(image_to_np,result_pil_images)
|
62 |
+
#np_grid = np_gallery(np.array(list(np_images)),2)
|
63 |
+
#result_grid = Image.fromarray(np_grid)
|
64 |
+
|
65 |
+
return(result_pil_images[0])
|
66 |
+
|
67 |
+
image = gr.outputs.Image(type="pil", label="Your result")
|
68 |
+
iface = gr.Interface(fn=run, inputs=[
|
69 |
+
gr.inputs.Textbox(label="Prompt (if not in Russian, it will be automatically translated to Russian)",default="chalk pastel drawing of a dog wearing a funny hat"),
|
70 |
+
#gr.inputs.Slider(label="Steps - more steps can increase quality but will take longer to generate",default=45,maximum=50,minimum=1,step=1),
|
71 |
+
gr.inputs.Radio(label="Aspect Ratio", choices=["Square", "Horizontal", "Vertical"],default="Horizontal"),
|
72 |
+
gr.inputs.Dropdown(label="Model", choices=["Surrealism","Realism", "Emojis"], default="Surrealism")
|
73 |
+
#gr.inputs.Radio(label="Height", choices=[32,64,128,256,512],default=256),
|
74 |
+
#gr.inputs.Slider(label="Images - How many images you wish to generate", default=2, step=1, minimum=1, maximum=4),
|
75 |
+
#gr.inputs.Slider(label="Diversity scale - How different from one another you wish the images to be",default=5.0, minimum=1.0, maximum=15.0),
|
76 |
+
#gr.inputs.Slider(label="ETA - between 0 and 1. Lower values can provide better quality, higher values can be more diverse",default=0.0,minimum=0.0, maximum=1.0,step=0.1),
|
77 |
+
],
|
78 |
+
outputs=image,
|
79 |
+
#css=css,
|
80 |
+
title="Generate images from text with ruDALLE",
|
81 |
+
description="<div>By typing a prompt and pressing submit you can generate images based on this prompt. <a href='https://github.com/CompVis/latent-diffusion' target='_blank'>ruDALLE</a> is an open source text-to-image model, this Arbitrary Aspect ration implementation was created by <a href='https://github.com/shonenkov-AI' target='_blank'>Alex Shonenkov</a><br>This UI to the model was assembled by <a style='color: rgb(245, 158, 11);font-weight:bold' href='https://twitter.com/multimodalart' target='_blank'>@multimodalart</a></div>",
|
82 |
+
article="<h4 style='font-size: 110%;margin-top:.5em'>Biases acknowledgment</h4><div>Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exarcbates societal biases. According to the <a href='https://arxiv.org/abs/2112.10752' target='_blank'>Latent Diffusion paper</a>:<i> \"Deep learning modules tend to reproduce or exacerbate biases that are already present in the data\"</i>. The models are meant to be used for research purposes, such as this one.</div><h4 style='font-size: 110%;margin-top:1em'>Who owns the images produced by this demo?</h4><div>Definetly not me! Probably you do. I say probably because the Copyright discussion about AI generated art is ongoing. So <a href='https://www.theverge.com/2022/2/21/22944335/us-copyright-office-reject-ai-generated-art-recent-entrance-to-paradise' target='_blank'>it may be the case that everything produced here falls automatically into the public domain</a>. But in any case it is either yours or is in the public domain.</div>")
|
83 |
+
iface.launch(enable_queue=True)
|
requirements.txt
CHANGED
@@ -1 +1,6 @@
|
|
1 |
-
torch
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
gitpython
|
4 |
+
rudalle==1.0.0
|
5 |
+
einops
|
6 |
+
numpy
|