Dagfinn1962 commited on
Commit
841ca51
1 Parent(s): 6436627

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -38
app.py CHANGED
@@ -1,45 +1,51 @@
1
  import gradio as gr
2
- import modin.pandas as pd
3
  import torch
4
  import numpy as np
 
5
  from PIL import Image
6
- from diffusers import DiffusionPipeline
7
- from huggingface_hub import login
8
- #import os
9
-
10
- #login(token=os.environ.get('HF_KEY'))
11
-
12
- device = "cuda" if torch.cuda.is_available() else "cpu"
13
- pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16) if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0")
14
- pipe = pipe.to(device)
15
 
16
- def resize(value,img):
17
- img = Image.open(img)
18
- img = img.resize((value,value))
19
- return img
20
 
21
- def infer(source_img, prompt, negative_prompt, guide, steps, seed, Strength):
22
- generator = torch.Generator(device).manual_seed(seed)
23
- source_image = resize(768, source_img)
24
- source_image.save('source.png')
25
- image = pipe(prompt, negative_prompt=negative_prompt, image=source_image, strength=Strength, guidance_scale=guide, num_inference_steps=steps).images[0]
26
- return image
27
 
28
- gr.Interface(theme='ParityError/Anime', fn=genie, inputs=[gr.Textbox(label='Input field right under here(Prompt)'),
29
- gr.Textbox(label='What You dont want (Negative Prompt)'),
30
- gr.Slider(512, 1024, 768, step=128, label='Height'),
31
- gr.Slider(512, 1024, 768, step=128, label='Width'),
32
- gr.Slider(1, maximum=15, value=10, step=.25),
33
- gr.Slider(25, maximum=100, value=50, step=25),
34
- gr.Slider(minimum=1, step=1, maximum=9999999999999999, randomize=True),
35
- # gr.Radio(["Yes", "No"], label='Upscale?'),
36
- #gr.Textbox(label='Upscaler Prompt: Optional'),
37
- #gr.Textbox(label='Upscaler Negative Prompt: Both Optional And Experimental'),
38
- #gr.Slider(minimum=0, maximum=15, value=0, step=1, label='Upscale Guidance Scale'),
39
- #gr.Slider(minimum=5, maximum=25, value=5, step=5, label='Upscaler Iterations')
40
-
41
- ],
42
- outputs=gr.Image(label='Generated Image'),
43
- title="Dream Art (SD) ",
44
- description="<br> <h4> <div style='width:100%'> Info:Dream Art (SD) <br> This App is our favorite now and shows how Stable diffusion works i a good way !</h4> </div>",
45
- ).launch(debug=True, max_threads=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
  import torch
3
  import numpy as np
4
+ import modin.pandas as pd
5
  from PIL import Image
6
+ from diffusers import DiffusionPipeline
 
 
 
 
 
 
 
 
7
 
 
 
 
 
8
 
9
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
 
 
 
 
 
10
 
11
+ if torch.cuda.is_available():
12
+ PYTORCH_CUDA_ALLOC_CONF={'max_split_size_mb': 6000}
13
+ torch.cuda.max_memory_allocated(device=device)
14
+ torch.cuda.empty_cache()
15
+
16
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
17
+ pipe.enable_xformers_memory_efficient_attention()
18
+ pipe = pipe.to(device)
19
+ torch.cuda.empty_cache()
20
+
21
+ else:
22
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", use_safetensors=True)
23
+ pipe = pipe.to(device)
24
+ refiner = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", use_safetensors=True)
25
+ refiner = refiner.to(device)
26
+
27
+ def genie (prompt, negative_prompt, height, width, scale, steps, seed, upscaling):
28
+ generator = torch.Generator(device=device).manual_seed(seed)
29
+ int_image = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=steps, height=height, width=width, guidance_scale=scale, num_images_per_prompt=1, generator=generator, output_type="latent").images
30
+ if upscaling == 'Yes':
31
+ image = refiner(prompt=prompt, image=int_image).images[0]
32
+ upscaled = upscaler(prompt=prompt, negative_prompt=negative_prompt, image=image, num_inference_steps=5, guidance_scale=0).images[0]
33
+ torch.cuda.empty_cache()
34
+ return (image, upscaled)
35
+ else:
36
+ image = refiner(prompt=prompt, negative_prompt=negative_prompt, image=int_image).images[0]
37
+ torch.cuda.empty_cache()
38
+ return (image, image)
39
+
40
+ gr.Interface(fn=genie, inputs=[gr.Textbox(label='What you want the AI to generate. 77 Token Limit. A Token is Any Word, Number, Symbol, or Punctuation. Everything Over 77 Will Be Truncated!'),
41
+ gr.Textbox(label='What you Do Not want the AI to generate. 77 Token Limit'),
42
+ gr.Slider(512, 1024, 768, step=128, label='Height'),
43
+ gr.Slider(512, 1024, 768, step=128, label='Width'),
44
+ gr.Slider(1, 15, 10, step=.25, label='Guidance Scale: How Closely the AI follows the Prompt'),
45
+ gr.Slider(25, maximum=100, value=50, step=25, label='Number of Iterations'),
46
+ gr.Slider(minimum=1, step=1, maximum=999999999999999999, randomize=True, label='Seed'),
47
+ gr.Radio(['Yes', 'No'], label='Upscale?')],
48
+ outputs=['image', 'image'],
49
+ title="Stable Diffusion XL 1.0 GPU",
50
+ description="SDXL 1.0 GPU. <br><br><b>WARNING: Capable of producing NSFW (Softcore) images.</b>",
51
+ article = "If You Enjoyed this Demo and would like to Donate, you can send to any of these Wallets. <br>BTC: bc1qzdm9j73mj8ucwwtsjx4x4ylyfvr6kp7svzjn84 <br>3LWRoKYx6bCLnUrKEdnPo3FCSPQUSFDjFP <br>DOGE: DK6LRc4gfefdCTRk9xPD239N31jh9GjKez <br>SHIB (BEP20): 0xbE8f2f3B71DFEB84E5F7E3aae1909d60658aB891 <br>PayPal: https://www.paypal.me/ManjushriBodhisattva <br>ETH: 0xbE8f2f3B71DFEB84E5F7E3aae1909d60658aB891 <br>Code Monkey: <a href=\"https://huggingface.co/Manjushri\">Manjushri</a>").launch(debug=True, max_threads=80)