Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -1,110 +1,121 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
3 |
import modin.pandas as pd
|
4 |
-
import torch
|
5 |
-
import os
|
6 |
-
from glob import glob
|
7 |
-
from pathlib import Path
|
8 |
-
from typing import Optional
|
9 |
-
from diffusers import LTXImageToVideoPipeline
|
10 |
-
from diffusers.utils import load_image, export_to_video
|
11 |
from PIL import Image
|
12 |
-
|
13 |
-
import uuid
|
14 |
-
import random
|
15 |
from huggingface_hub import hf_hub_download
|
16 |
|
17 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
18 |
torch.cuda.max_memory_allocated(device=device)
|
19 |
torch.cuda.empty_cache()
|
20 |
-
pipe = LTXImageToVideoPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16)
|
21 |
-
pipe.to(device)
|
22 |
-
torch.cuda.empty_cache()
|
23 |
-
max_64_bit_int = 2**63 - 1
|
24 |
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
)
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
if image_aspect > target_aspect:
|
63 |
-
# Resize the image to match the target height, maintaining aspect ratio
|
64 |
-
new_height = output_size[1]
|
65 |
-
new_width = int(new_height * image_aspect)
|
66 |
-
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
67 |
-
# Calculate coordinates for cropping
|
68 |
-
left = (new_width - output_size[0]) / 2
|
69 |
-
top = 0
|
70 |
-
right = (new_width + output_size[0]) / 2
|
71 |
-
bottom = output_size[1]
|
72 |
-
else:
|
73 |
-
# Resize the image to match the target width, maintaining aspect ratio
|
74 |
-
new_width = output_size[0]
|
75 |
-
new_height = int(new_width / image_aspect)
|
76 |
-
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
77 |
-
# Calculate coordinates for cropping
|
78 |
-
left = 0
|
79 |
-
top = (new_height - output_size[1]) / 2
|
80 |
-
right = output_size[0]
|
81 |
-
bottom = (new_height + output_size[1]) / 2
|
82 |
-
|
83 |
-
torch.cuda.empty_cache()
|
84 |
-
cropped_image = resized_image.crop((left, top, right, bottom))
|
85 |
-
return cropped_image
|
86 |
-
|
87 |
-
with gr.Blocks() as demo:
|
88 |
-
|
89 |
-
with gr.Row():
|
90 |
-
with gr.Column():
|
91 |
-
image = gr.Image(label="Upload your image", type="pil")
|
92 |
-
generate_btn = gr.Button("Generate")
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 #, StableDiffusion3Pipeline
|
|
|
|
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
|
9 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
10 |
torch.cuda.max_memory_allocated(device=device)
|
11 |
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
def genie (Model, Prompt, negative_prompt, height, width, scale, steps, seed, refine, high_noise_frac, upscale):
|
14 |
+
generator = np.random.seed(0) if seed == 0 else torch.manual_seed(seed)
|
15 |
+
|
16 |
+
if Model == "PhotoReal":
|
17 |
+
pipe = DiffusionPipeline.from_pretrained("circulus/canvers-real-v3.9.1", torch_dtype=torch.float16, safety_checker=None) if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("circulus/canvers-real-v3.8.1")
|
18 |
+
pipe.enable_xformers_memory_efficient_attention()
|
19 |
+
pipe = pipe.to(device)
|
20 |
+
torch.cuda.empty_cache()
|
21 |
+
if refine == "Yes":
|
22 |
+
refiner = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", use_safetensors=True, torch_dtype=torch.float16, variant="fp16") if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0")
|
23 |
+
refiner.enable_xformers_memory_efficient_attention()
|
24 |
+
refiner = refiner.to(device)
|
25 |
+
torch.cuda.empty_cache()
|
26 |
+
int_image = pipe(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale).images
|
27 |
+
image = refiner(Prompt, negative_prompt=negative_prompt, image=int_image, denoising_start=high_noise_frac).images[0]
|
28 |
+
torch.cuda.empty_cache()
|
29 |
+
return image
|
30 |
+
else:
|
31 |
+
image = pipe(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale).images[0]
|
32 |
+
torch.cuda.empty_cache()
|
33 |
+
return image
|
34 |
|
35 |
+
if Model == "Animagine XL 3.0":
|
36 |
+
animagine = DiffusionPipeline.from_pretrained("cagliostrolab/animagine-xl-3.0", torch_dtype=torch.float16, safety_checker=None) if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("cagliostrolab/animagine-xl-3.0")
|
37 |
+
animagine.enable_xformers_memory_efficient_attention()
|
38 |
+
animagine = animagine.to(device)
|
39 |
+
torch.cuda.empty_cache()
|
40 |
+
if refine == "Yes":
|
41 |
+
torch.cuda.empty_cache()
|
42 |
+
torch.cuda.max_memory_allocated(device=device)
|
43 |
+
int_image = animagine(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale, output_type="latent").images
|
44 |
+
torch.cuda.empty_cache()
|
45 |
+
animagine = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", use_safetensors=True, torch_dtype=torch.float16, variant="fp16") if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0")
|
46 |
+
animagine.enable_xformers_memory_efficient_attention()
|
47 |
+
animagine = animagine.to(device)
|
48 |
+
torch.cuda.empty_cache()
|
49 |
+
image = animagine(Prompt, negative_prompt=negative_prompt, image=int_image, denoising_start=high_noise_frac).images[0]
|
50 |
+
torch.cuda.empty_cache()
|
51 |
+
return image
|
52 |
+
else:
|
53 |
+
image = animagine(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale).images[0]
|
54 |
+
torch.cuda.empty_cache()
|
55 |
+
return image
|
56 |
|
57 |
+
if Model == "SDXL 1.0":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
torch.cuda.empty_cache()
|
60 |
+
torch.cuda.max_memory_allocated(device=device)
|
61 |
+
sdxl = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
62 |
+
sdxl.enable_xformers_memory_efficient_attention()
|
63 |
+
sdxl = sdxl.to(device)
|
64 |
+
torch.cuda.empty_cache()
|
65 |
+
|
66 |
+
if refine == "Yes":
|
67 |
+
torch.cuda.max_memory_allocated(device=device)
|
68 |
+
torch.cuda.empty_cache()
|
69 |
+
image = sdxl(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale, output_type="latent").images
|
70 |
+
torch.cuda.empty_cache()
|
71 |
+
sdxl = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", use_safetensors=True, torch_dtype=torch.float16, variant="fp16") if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0")
|
72 |
+
sdxl.enable_xformers_memory_efficient_attention()
|
73 |
+
sdxl = sdxl.to(device)
|
74 |
+
torch.cuda.empty_cache()
|
75 |
+
refined = sdxl(Prompt, negative_prompt=negative_prompt, image=image, denoising_start=high_noise_frac).images[0]
|
76 |
+
torch.cuda.empty_cache()
|
77 |
+
return refined
|
78 |
+
else:
|
79 |
+
image = sdxl(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale).images[0]
|
80 |
+
torch.cuda.empty_cache()
|
81 |
+
return image
|
82 |
+
|
83 |
+
if Model == 'FusionXL':
|
84 |
+
torch.cuda.empty_cache()
|
85 |
+
torch.cuda.max_memory_allocated(device=device)
|
86 |
+
pipe = DiffusionPipeline.from_pretrained("circulus/canvers-fusionXL-v1", torch_dtype=torch.float16, safety_checker=None) if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("circulus/canvers-real-v3.8.1")
|
87 |
+
pipe.enable_xformers_memory_efficient_attention()
|
88 |
+
pipe = pipe.to(device)
|
89 |
+
torch.cuda.empty_cache()
|
90 |
+
if refine == "Yes":
|
91 |
+
torch.cuda.empty_cache()
|
92 |
+
torch.cuda.max_memory_allocated(device=device)
|
93 |
+
int_image = pipe(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale, output_type="latent").images
|
94 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", use_safetensors=True, torch_dtype=torch.float16, variant="fp16") if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0")
|
95 |
+
pipe.enable_xformers_memory_efficient_attention()
|
96 |
+
pipe = pipe.to(device)
|
97 |
+
torch.cuda.empty_cache()
|
98 |
+
image = pipe(Prompt, negative_prompt=negative_prompt, image=int_image, denoising_start=high_noise_frac).images[0]
|
99 |
+
torch.cuda.empty_cache()
|
100 |
+
return image
|
101 |
+
else:
|
102 |
+
image = pipe(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale).images[0]
|
103 |
+
torch.cuda.empty_cache()
|
104 |
+
return image
|
105 |
|
106 |
+
return image
|
107 |
+
|
108 |
+
gr.Interface(fn=genie, inputs=[gr.Radio(['PhotoReal', 'Animagine XL 3.0', 'SDXL 1.0', 'FusionXL',], value='PhotoReal', label='Choose Model'),
|
109 |
+
gr.Textbox(label='What you want the AI to generate. 77 Token Limit.'),
|
110 |
+
gr.Textbox(label='What you Do Not want the AI to generate. 77 Token Limit'),
|
111 |
+
gr.Slider(512, 1024, 768, step=128, label='Height'),
|
112 |
+
gr.Slider(512, 1024, 768, step=128, label='Width'),
|
113 |
+
gr.Slider(1, maximum=15, value=5, step=.25, label='Guidance Scale'),
|
114 |
+
gr.Slider(5, maximum=100, value=50, step=5, label='Number of Iterations'),
|
115 |
+
gr.Slider(minimum=0, step=1, maximum=9999999999999999, randomize=True, label='Seed: 0 is Random'),
|
116 |
+
gr.Radio(["Yes", "No"], label='SDXL 1.0 Refiner: Use if the Image has too much Noise', value='No'),
|
117 |
+
gr.Slider(minimum=.9, maximum=.99, value=.95, step=.01, label='Refiner Denoise Start %')],
|
118 |
+
outputs=gr.Image(label='Generated Image'),
|
119 |
+
title="Manju Dream Booth V2.1 with SDXL 1.0 Refiner - GPU",
|
120 |
+
description="<br><br><b/>Warning: This Demo is capable of producing NSFW content.",
|
121 |
+
article = "If You Enjoyed this Demo and would like to Donate, you can send any amount to any of these Wallets. <br><br>SHIB (BEP20): 0xbE8f2f3B71DFEB84E5F7E3aae1909d60658aB891 <br>PayPal: https://www.paypal.me/ManjushriBodhisattva <br>ETH: 0xbE8f2f3B71DFEB84E5F7E3aae1909d60658aB891 <br>DOGE: D9QdVPtcU1EFH8jDC8jhU9uBcSTqUiA8h6<br><br>Code Monkey: <a href=\"https://huggingface.co/Manjushri\">Manjushri</a>").launch(debug=True, max_threads=80)
|