Manjushri commited on
Commit
d767ca6
·
verified ·
1 Parent(s): 7d80404

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -96
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 sample(
26
- image: Image, prompt, negative_prompt,
27
- seed: Optional[int] = 42,
28
- randomize_seed: bool = True,
29
- num_inference_steps: int = 25,
30
- num_frames: int = 60,
31
- fps_id: int = 12,
32
- decode_noise_scale: float = 0.3,
33
- max_sequence_length: int = 512,
34
- decoding_t: int = 3, # Number of frames decoded at a time! This eats most VRAM. Reduce if necessary.
35
- device: str = "cuda",
36
- output_folder: str = "outputs",
37
- ):
38
- if image.mode == "RGBA":
39
- image = image.convert("RGB")
40
-
41
- if(randomize_seed):
42
- seed = random.randint(0, max_64_bit_int)
43
- torch.cuda.max_memory_allocated(device=device)
44
- torch.cuda.empty_cache()
45
- generator = torch.manual_seed(seed)
46
 
47
- os.makedirs(output_folder, exist_ok=True)
48
- base_count = len(glob(os.path.join(output_folder, "*.mp4")))
49
- video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
50
- video = pipe(image=image, prompt=prompt, negative_prompt=negative_prompt, width=768, height=512, num_frames=num_frames, num_inference_steps=num_inference_steps, guidance_scale=3.5, decode_noise_scale=0.3, max_sequence_length=512).frames[0]
51
- export_to_video(video, video_path, fps=fps_id)
52
- torch.manual_seed(seed)
53
- torch.cuda.empty_cache()
54
- return video_path, seed
55
-
56
- def resize_image(image, output_size=(768, 512)):
57
- # Calculate aspect ratios
58
- target_aspect = output_size[0] / output_size[1] # Aspect ratio of the desired size
59
- image_aspect = image.width / image.height # Aspect ratio of the original image
 
 
 
 
 
 
 
 
60
 
61
- # Resize then crop if the original image is larger
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
- video = gr.Video()
95
- with gr.Accordion("Advanced options", open=False):
96
- seed = gr.Slider(label="Seed", value=42, randomize=True, minimum=0, maximum=max_64_bit_int, step=1)
97
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
- prompt=gr.Textbox(label="Prompt")
99
- negative_prompt=gr.Textbox(label="Negative Prompt", value="Blur, Motion Blur, Artifacts, Motion Artifacts, Poor Quality, Low Resolution, Smudging, Streaks, Motion Streaks, Pixels, Pixelated, Ugly, Mutation, Mutated")
100
- num_inference_steps=gr.Slider(label="Number of Inference Steps", value=25, minimum=25, maximum=100, step=25)
101
- num_frames=gr.Slider(label = "Number of Frames", value=60, minimum=30, maximum=120)
102
- fps_id = gr.Slider(label="Frames per second", info="The length of your video in seconds will be frames/fps", value=12, minimum=5, maximum=30, step=1)
103
-
104
- image.upload(fn=resize_image, inputs=image, outputs=image, queue=False)
105
- generate_btn.click(fn=sample, inputs=[image, prompt, negative_prompt, seed, randomize_seed, num_inference_steps, num_frames, fps_id], outputs=[video, seed], api_name="video")
106
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
- if __name__ == "__main__":
109
- demo.queue(max_size=20, api_open=False)
110
- demo.launch(show_api=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)