apolinario commited on
Commit
b9be232
1 Parent(s): 28616cb

Add Google Translation

Browse files
Files changed (2) hide show
  1. app.py +22 -10
  2. requirements.txt +2 -1
app.py CHANGED
@@ -7,6 +7,7 @@ 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")
@@ -18,11 +19,13 @@ 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
@@ -37,6 +40,13 @@ 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'):
@@ -47,37 +57,39 @@ def run(prompt, aspect_ratio, model):
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", "Emoji"], 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)
 
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")
 
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
  def np_gallery(array, ncols=3):
30
  nindex, height, width, intensity = array.shape
31
  nrows = nindex//ncols
 
40
  return np.asarray(image)
41
 
42
  def run(prompt, aspect_ratio, model):
43
+ detected_language = translator.detect(prompt)
44
+ if "lang=ru" not in str(detected_language):
45
+ input_text = translator.translate(prompt, dest='ru').text
46
+ if input_text == prompt:
47
+ return(None,"Error on Translating the prompt. Try a different one or translate it to russian independently before pasting it here")
48
+ else:
49
+ prompt = input_text
50
  if(model=='Surrealism'):
51
  dalle = dalle_surreal
52
  elif(model=='Realism'):
 
57
  aspect_ratio_value = 1
58
  top_k = 512
59
  elif(aspect_ratio == 'Horizontal'):
60
+ aspect_ratio_value = 24/9
61
  top_k = 1024
62
  elif(aspect_ratio == 'Vertical'):
63
+ aspect_ratio_value = 9/24
64
  top_k = 512
65
+
66
  rudalle_ar = RuDalleAspectRatio(
67
  dalle=dalle, vae=vae, tokenizer=tokenizer,
68
+ aspect_ratio=aspect_ratio_value, bs=2, device=device
69
  )
70
 
71
+ _, result_pil_images = rudalle_ar.generate_images(prompt, top_k, 0.975, 2)
72
  #np_images = map(image_to_np,result_pil_images)
73
  #np_grid = np_gallery(np.array(list(np_images)),2)
74
  #result_grid = Image.fromarray(np_grid)
75
 
76
+ return(result_pil_images[0], None)
77
 
78
  image = gr.outputs.Image(type="pil", label="Your result")
79
+ css = ".output-image{height: 528px !important}"
80
  iface = gr.Interface(fn=run, inputs=[
81
+ gr.inputs.Textbox(label="Prompt (if not in Russian, it will be automatically translated to Russian)",default="an amusement park in mars"),
82
  #gr.inputs.Slider(label="Steps - more steps can increase quality but will take longer to generate",default=45,maximum=50,minimum=1,step=1),
83
+ gr.inputs.Radio(label="Aspect Ratio", choices=["Square", "Horizontal", "Vertical"],default="Square"),
84
  gr.inputs.Dropdown(label="Model", choices=["Surrealism","Realism", "Emoji"], default="Surrealism")
85
  #gr.inputs.Radio(label="Height", choices=[32,64,128,256,512],default=256),
86
  #gr.inputs.Slider(label="Images - How many images you wish to generate", default=2, step=1, minimum=1, maximum=4),
87
  #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),
88
  #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),
89
  ],
90
+ outputs=[image,gr.outputs.Textbox(label="Error")],
91
+ css=css,
92
  title="Generate images from text with ruDALLE",
93
+ 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(245, 158, 11);font-weight:bold' href='https://twitter.com/multimodalart' target='_blank'>@multimodalart</a></div>, keep up with the <a href='https://multimodal.art/news'>latest multimodal ai art news here</a> and consider <a href='https://www.patreon.com/multimodalart'>supporting us on Patreon</a>",
94
  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>")
95
  iface.launch(enable_queue=True)
requirements.txt CHANGED
@@ -3,4 +3,5 @@ torchvision
3
  gitpython
4
  rudalle==1.0.0
5
  einops
6
- numpy
 
 
3
  gitpython
4
  rudalle==1.0.0
5
  einops
6
+ numpy
7
+ googletrans