ismot multimodalart HF staff commited on
Commit
2d7afe4
0 Parent(s):

Duplicate from multimodalart/rudalle

Browse files

Co-authored-by: Multimodal AI art <multimodalart@users.noreply.huggingface.co>

Files changed (4) hide show
  1. .gitattributes +28 -0
  2. README.md +14 -0
  3. app.py +96 -0
  4. requirements.txt +8 -0
.gitattributes ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.model filter=lfs diff=lfs merge=lfs -text
12
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
13
+ *.onnx filter=lfs diff=lfs merge=lfs -text
14
+ *.ot filter=lfs diff=lfs merge=lfs -text
15
+ *.parquet filter=lfs diff=lfs merge=lfs -text
16
+ *.pb filter=lfs diff=lfs merge=lfs -text
17
+ *.pt filter=lfs diff=lfs merge=lfs -text
18
+ *.pth filter=lfs diff=lfs merge=lfs -text
19
+ *.rar filter=lfs diff=lfs merge=lfs -text
20
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
22
+ *.tflite filter=lfs diff=lfs merge=lfs -text
23
+ *.tgz filter=lfs diff=lfs merge=lfs -text
24
+ *.wasm filter=lfs diff=lfs merge=lfs -text
25
+ *.xz filter=lfs diff=lfs merge=lfs -text
26
+ *.zip filter=lfs diff=lfs merge=lfs -text
27
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
28
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Rudalle
3
+ emoji: 📚
4
+ colorFrom: green
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 2.9.4
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ duplicated_from: multimodalart/rudalle
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu113
2
+ torch
3
+ torchvision
4
+ gitpython
5
+ rudalle==1.1.1
6
+ einops
7
+ numpy
8
+ googletrans==3.1.0a0