cosmicdream commited on
Commit
0dfbf6a
1 Parent(s): a259967

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -3
app.py CHANGED
@@ -1,4 +1,96 @@
1
- if not (path_exists(f"Image_Variations")):
2
- Repo.clone_from("https://github.com/kosmicdream/huggingfaceapp", "Image_Variations")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from googletrans import Translator
11
+
12
+ if not (path_exists(f"rudalle-aspect-ratio")):
13
+ Repo.clone_from("https://github.com/shonenkov-AI/rudalle-aspect-ratio", "rudalle-aspect-ratio")
14
+
15
+ import sys
16
+ sys.path.append('./rudalle-aspect-ratio')
17
+ from rudalle_aspect_ratio import RuDalleAspectRatio, get_rudalle_model
18
+ from rudalle import get_vae, get_tokenizer
19
+ from rudalle.pipelines import show
20
+ #model_path_e = hf_hub_download(repo_id="multimodalart/compvis-latent-diffusion-text2img-large", filename="txt2img-f8-large.ckpt")
21
+
22
+ torch.cuda.empty_cache()
23
+ device = 'cuda'
24
+ dalle_surreal = get_rudalle_model('Surrealist_XL', fp16=True, device=device)
25
+ dalle_real = get_rudalle_model('Malevich',fp16=True,device=device)
26
+ dalle_emoji = get_rudalle_model('Emojich',fp16=True,device=device)
27
+ vae, tokenizer = get_vae().to(device), get_tokenizer()
28
+ translator = Translator()
29
+
30
+ def np_gallery(array, ncols=3):
31
+ nindex, height, width, intensity = array.shape
32
+ nrows = nindex//ncols
33
+ assert nindex == nrows*ncols
34
+ # want result.shape = (height*nrows, width*ncols, intensity)
35
+ result = (array.reshape(nrows, ncols, height, width, intensity)
36
+ .swapaxes(1,2)
37
+ .reshape(height*nrows, width*ncols, intensity))
38
+ return result
39
+
40
+ def image_to_np(image):
41
+ return np.asarray(image)
42
+
43
+ def run(prompt, aspect_ratio, model):
44
+ detected_language = translator.detect(prompt)
45
+ if "lang=ru" not in str(detected_language):
46
+ input_text = translator.translate(prompt, dest='ru').text
47
+ if input_text == prompt:
48
+ return(None,"Error on Translating the prompt. Try a different one or translate it to russian independently before pasting it here")
49
+ else:
50
+ prompt = input_text
51
+ if(model=='Surrealism'):
52
+ dalle = dalle_surreal
53
+ elif(model=='Realism'):
54
+ dalle = dalle_real
55
+ elif(model=='Emoji'):
56
+ dalle = dalle_emoji
57
+ if(aspect_ratio == 'Square'):
58
+ aspect_ratio_value = 1
59
+ top_k = 512
60
+ elif(aspect_ratio == 'Horizontal'):
61
+ aspect_ratio_value = 24/9
62
+ top_k = 1024
63
+ elif(aspect_ratio == 'Vertical'):
64
+ aspect_ratio_value = 9/24
65
+ top_k = 512
66
 
67
+ rudalle_ar = RuDalleAspectRatio(
68
+ dalle=dalle, vae=vae, tokenizer=tokenizer,
69
+ aspect_ratio=aspect_ratio_value, bs=1, device=device
70
+ )
71
+
72
+ _, result_pil_images = rudalle_ar.generate_images(prompt, top_k, 0.975, 1)
73
+ #np_images = map(image_to_np,result_pil_images)
74
+ #np_grid = np_gallery(np.array(list(np_images)),2)
75
+ #result_grid = Image.fromarray(np_grid)
76
+
77
+ return(result_pil_images[0], None)
78
+
79
+ css = ".output-image{height: 400px !important}"
80
+ image = gr.outputs.Image(type="pil", label="Your result")
81
+ iface = gr.Interface(fn=run, inputs=[
82
+ gr.inputs.Textbox(label="Prompt (if not in Russian, it will be automatically translated to Russian)",default="an amusement park in mars"),
83
+ #gr.inputs.Slider(label="Steps - more steps can increase quality but will take longer to generate",default=45,maximum=50,minimum=1,step=1),
84
+ gr.inputs.Radio(label="Aspect Ratio", choices=["Square", "Horizontal", "Vertical"],default="Square"),
85
+ gr.inputs.Dropdown(label="Model", choices=["Surrealism","Realism", "Emoji"], default="Surrealism")
86
+ #gr.inputs.Radio(label="Height", choices=[32,64,128,256,512],default=256),
87
+ #gr.inputs.Slider(label="Images - How many images you wish to generate", default=2, step=1, minimum=1, maximum=4),
88
+ #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),
89
+ #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),
90
+ ],
91
+ outputs=[image,gr.outputs.Textbox(label="Error")],
92
+ css=css,
93
+ title="Generate images from text with ruDALLE",
94
+ 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>",
95
+ 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>")
96
+ iface.launch(enable_queue=True)