Spaces:
Starting
on
T4
Starting
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -3,52 +3,46 @@ import torch
|
|
3 |
import numpy as np
|
4 |
import modin.pandas as pd
|
5 |
from PIL import Image
|
6 |
-
from diffusers import
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
-
from diffusers import BitsAndBytesConfig, SD3Transformer2DModel
|
9 |
|
10 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
11 |
torch.cuda.max_memory_allocated(device=device)
|
12 |
-
torch.cuda.empty_cache()
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
nf4_config = BitsAndBytesConfig(
|
17 |
-
load_in_4bit=True,
|
18 |
-
bnb_4bit_quant_type="nf4",
|
19 |
-
bnb_4bit_compute_dtype=torch.bfloat16
|
20 |
-
)
|
21 |
-
model_nf4 = SD3Transformer2DModel.from_pretrained(
|
22 |
-
model_id,
|
23 |
-
subfolder="transformer",
|
24 |
-
quantization_config=nf4_config,
|
25 |
-
torch_dtype=torch.bfloat16
|
26 |
-
)
|
27 |
-
|
28 |
-
t5_nf4 = T5EncoderModel.from_pretrained("diffusers/t5-nf4", torch_dtype=torch.bfloat16)
|
29 |
-
|
30 |
-
pipeline = StableDiffusion3Pipeline.from_pretrained(
|
31 |
-
model_id,
|
32 |
-
transformer=model_nf4,
|
33 |
-
text_encoder_3=t5_nf4,
|
34 |
-
torch_dtype=torch.bfloat16
|
35 |
-
)
|
36 |
-
pipeline.enable_model_cpu_offload()
|
37 |
-
|
38 |
-
def genie (Prompt, height, width, seed):
|
39 |
generator = np.random.seed(0) if seed == 0 else torch.manual_seed(seed)
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
43 |
return image
|
44 |
|
45 |
-
gr.Interface(fn=genie, inputs=[
|
46 |
gr.Textbox(label='What you want the AI to generate. 77 Token Limit.'),
|
47 |
-
|
48 |
gr.Slider(512, 1024, 768, step=128, label='Height'),
|
49 |
gr.Slider(512, 1024, 768, step=128, label='Width'),
|
50 |
-
|
51 |
-
|
52 |
gr.Slider(minimum=0, step=1, maximum=9999999999999999, randomize=True, label='Seed: 0 is Random'),
|
53 |
],
|
54 |
outputs=gr.Image(label='Generated Image'),
|
|
|
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):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.9.1")
|
18 |
+
pipe.enable_xformers_memory_efficient_attention()
|
19 |
+
pipe = pipe.to(device)
|
20 |
+
torch.cuda.empty_cache()
|
21 |
+
|
22 |
+
image = pipe(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale).images[0]
|
23 |
+
torch.cuda.empty_cache()
|
24 |
+
return image
|
25 |
+
|
26 |
+
if Model == "Animagine XL 4":
|
27 |
+
animagine = DiffusionPipeline.from_pretrained("cagliostrolab/animagine-xl-4.0", torch_dtype=torch.float16, safety_checker=None) if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("cagliostrolab/animagine-xl-4.0")
|
28 |
+
animagine.enable_xformers_memory_efficient_attention()
|
29 |
+
animagine = animagine.to(device)
|
30 |
+
torch.cuda.empty_cache()
|
31 |
+
|
32 |
+
image = animagine(Prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=scale).images[0]
|
33 |
+
torch.cuda.empty_cache()
|
34 |
+
return image
|
35 |
|
36 |
+
|
37 |
return image
|
38 |
|
39 |
+
gr.Interface(fn=genie, inputs=[gr.Radio(['PhotoReal', 'Animagine XL 4',], value='PhotoReal', label='Choose Model'),
|
40 |
gr.Textbox(label='What you want the AI to generate. 77 Token Limit.'),
|
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(3, maximum=12, value=5, step=.25, label='Guidance Scale', info="5-7 for PhotoReal and 7-10 for Animagine"),
|
45 |
+
gr.Slider(25, maximum=50, value=25, step=25, label='Number of Iterations'),
|
46 |
gr.Slider(minimum=0, step=1, maximum=9999999999999999, randomize=True, label='Seed: 0 is Random'),
|
47 |
],
|
48 |
outputs=gr.Image(label='Generated Image'),
|