Spaces:
Running
on
A10G
Running
on
A10G
Update app.py
Browse files
app.py
CHANGED
@@ -5,18 +5,16 @@ import numpy as np
|
|
5 |
from moviepy.editor import *
|
6 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
7 |
|
8 |
-
from diffusers import StableDiffusionInstructPix2PixPipeline
|
9 |
import torch
|
10 |
from PIL import Image
|
11 |
import time
|
12 |
import psutil
|
|
|
13 |
import random
|
14 |
|
15 |
|
16 |
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None)
|
17 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
18 |
-
pipe.enable_xformers_memory_efficient_attention()
|
19 |
-
pipe.unet.to(memory_format=torch.channels_last)
|
20 |
|
21 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
22 |
|
@@ -24,37 +22,37 @@ if torch.cuda.is_available():
|
|
24 |
pipe = pipe.to("cuda")
|
25 |
|
26 |
|
27 |
-
def pix2pix(
|
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 |
|
@@ -120,7 +118,8 @@ def infer(prompt,video_in, seed_in, trim_value):
|
|
120 |
print("set stop frames to: " + str(n_frame))
|
121 |
|
122 |
for i in frames_list[0:int(n_frame)]:
|
123 |
-
|
|
|
124 |
print(pix2pix_img)
|
125 |
image = Image.open(pix2pix_img)
|
126 |
rgb_im = image.convert("RGB")
|
|
|
5 |
from moviepy.editor import *
|
6 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
7 |
|
8 |
+
from diffusers import StableDiffusionInstructPix2PixPipeline
|
9 |
import torch
|
10 |
from PIL import Image
|
11 |
import time
|
12 |
import psutil
|
13 |
+
import math
|
14 |
import random
|
15 |
|
16 |
|
17 |
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None)
|
|
|
|
|
|
|
18 |
|
19 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
20 |
|
|
|
22 |
pipe = pipe.to("cuda")
|
23 |
|
24 |
|
25 |
+
def pix2pix(
|
26 |
+
input_image: Image.Image,
|
27 |
+
instruction: str,
|
28 |
+
steps: int,
|
29 |
+
randomize_seed: bool,
|
30 |
+
seed: int,
|
31 |
+
randomize_cfg: bool,
|
32 |
+
text_cfg_scale: float,
|
33 |
+
image_cfg_scale: float,
|
34 |
+
):
|
35 |
+
seed = random.randint(0, 100000) if randomize_seed else seed
|
36 |
+
text_cfg_scale = round(random.uniform(6.0, 9.0), ndigits=2) if randomize_cfg else text_cfg_scale
|
37 |
+
image_cfg_scale = round(random.uniform(1.2, 1.8), ndigits=2) if randomize_cfg else image_cfg_scale
|
38 |
+
|
39 |
+
width, height = input_image.size
|
40 |
+
factor = 512 / max(width, height)
|
41 |
+
factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height)
|
42 |
+
width = int((width * factor) // 64) * 64
|
43 |
+
height = int((height * factor) // 64) * 64
|
44 |
+
input_image = ImageOps.fit(input_image, (width, height), method=Image.Resampling.LANCZOS)
|
45 |
+
|
46 |
+
if instruction == "":
|
47 |
+
return [input_image, seed]
|
48 |
+
|
49 |
+
generator = torch.manual_seed(seed)
|
50 |
+
edited_image = pipe(
|
51 |
+
instruction, image=input_image,
|
52 |
+
guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale,
|
53 |
+
num_inference_steps=steps, generator=generator,
|
54 |
+
).images[0]
|
55 |
+
return edited_image
|
56 |
|
57 |
|
58 |
|
|
|
118 |
print("set stop frames to: " + str(n_frame))
|
119 |
|
120 |
for i in frames_list[0:int(n_frame)]:
|
121 |
+
pil_i = Image.open(i)
|
122 |
+
pix2pix_img = pix2pix(pil_i, prompt, 50, False, seed_in, False, 7.5, 1.5)
|
123 |
print(pix2pix_img)
|
124 |
image = Image.open(pix2pix_img)
|
125 |
rgb_im = image.convert("RGB")
|