fffiloni commited on
Commit
3c5c831
1 Parent(s): 3a96db7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -36
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, EulerAncestralDiscreteScheduler
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(prompt,
28
- image,
29
- steps,
30
- image_guidance_scale,
31
- seed):
32
- print(psutil.virtual_memory()) # print memory usage
33
-
34
- if seed == 0:
35
- seed = random.randint(0, 2147483647)
36
-
37
- generator = torch.Generator("cuda").manual_seed(seed)
38
-
39
- #try:
40
- image = Image.open(image)
41
- ratio = min(512 / image.height, 512 / image.width)
42
- image = image.resize((int(image.width * ratio), int(image.height * ratio)), Image.LANCZOS)
43
-
44
- result = pipe(
45
- prompt,
46
- image=image,
47
- num_inference_steps=int(steps),
48
- image_guidance_scale=image_guidance_scale,
49
- #guidance_scale=text_guidance_scale,
50
- generator=generator,
51
- #negative_prompt=neg_prompt,
52
- )
53
-
54
- # return replace_nsfw_images(result)
55
- return result[0]
56
- #except Exception as e:
57
- # return None
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
- pix2pix_img = pix2pix(prompt,i,5.5,15,seed_in)
 
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")