rudalle / app.py
apolinario's picture
Styling fixes
de4241a
import gradio as gr
from PIL import Image
import torch
import numpy as np
from os.path import exists as path_exists
from git.repo.base import Repo
from einops import rearrange
import torchvision.transforms as transforms
from torchvision.utils import make_grid
from googletrans import Translator
if not (path_exists(f"rudalle-aspect-ratio")):
Repo.clone_from("https://github.com/shonenkov-AI/rudalle-aspect-ratio", "rudalle-aspect-ratio")
import sys
sys.path.append('./rudalle-aspect-ratio')
from rudalle_aspect_ratio import RuDalleAspectRatio, get_rudalle_model
from rudalle import get_vae, get_tokenizer
from rudalle.pipelines import show
#model_path_e = hf_hub_download(repo_id="multimodalart/compvis-latent-diffusion-text2img-large", filename="txt2img-f8-large.ckpt")
torch.cuda.empty_cache()
device = 'cuda'
dalle_surreal = get_rudalle_model('Surrealist_XL', fp16=True, device=device)
dalle_real = get_rudalle_model('Malevich',fp16=True,device=device)
dalle_emoji = get_rudalle_model('Emojich',fp16=True,device=device)
vae, tokenizer = get_vae().to(device), get_tokenizer()
translator = Translator()
def np_gallery(array, ncols=3):
nindex, height, width, intensity = array.shape
nrows = nindex//ncols
assert nindex == nrows*ncols
# want result.shape = (height*nrows, width*ncols, intensity)
result = (array.reshape(nrows, ncols, height, width, intensity)
.swapaxes(1,2)
.reshape(height*nrows, width*ncols, intensity))
return result
def image_to_np(image):
return np.asarray(image)
def run(prompt, aspect_ratio, model):
detected_language = translator.detect(prompt)
if "lang=ru" not in str(detected_language):
input_text = translator.translate(prompt, dest='ru').text
if input_text == prompt:
return(None,"Error on Translating the prompt. Try a different one or translate it to russian independently before pasting it here")
else:
prompt = input_text
if(model=='Surrealism'):
dalle = dalle_surreal
elif(model=='Realism'):
dalle = dalle_real
elif(model=='Emoji'):
dalle = dalle_emoji
if(aspect_ratio == 'Square'):
aspect_ratio_value = 1
top_k = 512
elif(aspect_ratio == 'Horizontal'):
aspect_ratio_value = 24/9
top_k = 1024
elif(aspect_ratio == 'Vertical'):
aspect_ratio_value = 9/24
top_k = 512
rudalle_ar = RuDalleAspectRatio(
dalle=dalle, vae=vae, tokenizer=tokenizer,
aspect_ratio=aspect_ratio_value, bs=1, device=device
)
_, result_pil_images = rudalle_ar.generate_images(prompt, top_k, 0.975, 1)
#np_images = map(image_to_np,result_pil_images)
#np_grid = np_gallery(np.array(list(np_images)),2)
#result_grid = Image.fromarray(np_grid)
return(result_pil_images[0], None)
css = ".output-image{height: 400px !important}"
image = gr.outputs.Image(type="pil", label="Your result")
iface = gr.Interface(fn=run, inputs=[
gr.inputs.Textbox(label="Prompt (if not in Russian, it will be automatically translated to Russian)",default="an amusement park in mars"),
#gr.inputs.Slider(label="Steps - more steps can increase quality but will take longer to generate",default=45,maximum=50,minimum=1,step=1),
gr.inputs.Radio(label="Aspect Ratio", choices=["Square", "Horizontal", "Vertical"],default="Square"),
gr.inputs.Dropdown(label="Model", choices=["Surrealism","Realism", "Emoji"], default="Surrealism")
#gr.inputs.Radio(label="Height", choices=[32,64,128,256,512],default=256),
#gr.inputs.Slider(label="Images - How many images you wish to generate", default=2, step=1, minimum=1, maximum=4),
#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),
#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),
],
outputs=[image,gr.outputs.Textbox(label="Error")],
css=css,
title="Generate images from text with ruDALLE",
description="<div>By typing a prompt and pressing submit you generate images based on it. <a href='https://github.com/ai-forever/ru-dalle' target='_blank'>ruDALLE</a> is an open source text-to-image model, this Arbitrary Aspect Ratio implementation was created by <a href='https://github.com/shonenkov-AI' target='_blank'>Alex Shonenkov</a><br>This Spaces UI to the model was assembled by <a style='color: rgb(99, 102, 241);font-weight:bold' href='https://twitter.com/multimodalart' target='_blank'>@multimodalart</a>, keep up with the <a style='color: rgb(99, 102, 241);' href='https://multimodal.art/news' target='_blank'>latest multimodal ai art news here</a> and consider <a style='color: rgb(99, 102, 241);' href='https://www.patreon.com/multimodalart' target='_blank'>supporting us on Patreon</a></div>",
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>")
iface.launch(enable_queue=True)