|
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 |
|
|
|
|
|
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 |
|
|
|
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) |
|
|
|
|
|
|
|
|
|
return(result_pil_images[0], None) |
|
|
|
image = gr.outputs.Image(type="pil", label="Your result") |
|
css = ".output-image{height: 260px !important}" |
|
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.Radio(label="Aspect Ratio", choices=["Square", "Horizontal", "Vertical"],default="Square"), |
|
gr.inputs.Dropdown(label="Model", choices=["Surrealism","Realism", "Emoji"], default="Surrealism") |
|
|
|
|
|
|
|
|
|
], |
|
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 can generate images based on this prompt. <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'>latest multimodal ai art news here</a> and consider <a style='color: rgb(99, 102, 241);' href='https://www.patreon.com/multimodalart'>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) |