fffiloni commited on
Commit
19e4624
1 Parent(s): 3c9650f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -5
app.py CHANGED
@@ -5,8 +5,62 @@ import numpy as np
5
  from moviepy.editor import *
6
  from share_btn import community_icon_html, loading_icon_html, share_js
7
 
8
- token = os.environ.get('HF_TOKEN')
9
- pix2pix = gr.Blocks.load(name="spaces/fffiloni/instruct-pix2pix-clone", api_key=token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def get_frames(video_in):
12
  frames = []
@@ -71,7 +125,7 @@ def infer(prompt,video_in, seed_in, trim_value):
71
 
72
  for i in frames_list[0:int(n_frame)]:
73
  pix2pix_img = pix2pix(prompt,5.5,1.5,i,15,"",512,512,seed_in,fn_index=0)
74
- images = [os.path.join(pix2pix_img[0], img) for img in os.listdir(pix2pix_img[0])]
75
  result_frames.append(images[0])
76
  print("frame " + i + ": done;")
77
 
@@ -132,10 +186,13 @@ with gr.Blocks(css='style.css') as demo:
132
  prompt = gr.Textbox(label="Prompt", placeholder="enter prompt", show_label=False, elem_id="prompt-in")
133
  video_inp = gr.Video(label="Video source", source="upload", type="filepath", include_audio=False, elem_id="input-vid")
134
  with gr.Row():
135
- seed_inp = gr.Slider(label="Seed", minimum=0, maximum=10000, step=1, value=123456)
136
  trim_in = gr.Slider(label="Cut video at (s)", minimun=1, maximum=3, step=1, value=1)
137
  with gr.Column():
138
-
 
 
 
139
  video_out = gr.Video(label="Pix2pix video result", elem_id="video-output")
140
  submit_btn = gr.Button("Generate Pix2Pix video")
141
 
 
5
  from moviepy.editor import *
6
  from share_btn import community_icon_html, loading_icon_html, share_js
7
 
8
+ from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
9
+ import torch
10
+ from PIL import Image
11
+ import time
12
+ import psutil
13
+ import random
14
+
15
+ #token = os.environ.get('HF_TOKEN')
16
+ #pix2pix = gr.Blocks.load(name="spaces/fffiloni/instruct-pix2pix-clone", api_key=token)
17
+
18
+ pipe = DiffusionPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None)
19
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
20
+ pipe.enable_xformers_memory_efficient_attention()
21
+ pipe.unet.to(memory_format=torch.channels_last)
22
+
23
+ device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
24
+
25
+ if torch.cuda.is_available():
26
+ pipe = pipe.to("cuda")
27
+
28
+ def pix2pix(
29
+ prompt,
30
+ text_guidance_scale,
31
+ image_guidance_scale,
32
+ image,
33
+ steps,
34
+ neg_prompt="",
35
+ width=512,
36
+ height=512,
37
+ seed=0,
38
+ ):
39
+ print(psutil.virtual_memory()) # print memory usage
40
+
41
+ if seed == 0:
42
+ seed = random.randint(0, 2147483647)
43
+
44
+ generator = torch.Generator("cuda").manual_seed(seed)
45
+
46
+ try:
47
+ ratio = min(height / image.height, width / image.width)
48
+ image = image.resize((int(image.width * ratio), int(image.height * ratio)), Image.LANCZOS)
49
+
50
+ result = pipe(
51
+ prompt,
52
+ negative_prompt=neg_prompt,
53
+ image=image,
54
+ num_inference_steps=int(steps),
55
+ image_guidance_scale=image_guidance_scale,
56
+ guidance_scale=text_guidance_scale,
57
+ generator=generator,
58
+ )
59
+
60
+ # return replace_nsfw_images(result)
61
+ return result.images, result.nsfw_content_detected, seed
62
+ except Exception as e:
63
+ return None, None, error_str(e)
64
 
65
  def get_frames(video_in):
66
  frames = []
 
125
 
126
  for i in frames_list[0:int(n_frame)]:
127
  pix2pix_img = pix2pix(prompt,5.5,1.5,i,15,"",512,512,seed_in,fn_index=0)
128
+ images = pix2pix_img[0]
129
  result_frames.append(images[0])
130
  print("frame " + i + ": done;")
131
 
 
186
  prompt = gr.Textbox(label="Prompt", placeholder="enter prompt", show_label=False, elem_id="prompt-in")
187
  video_inp = gr.Video(label="Video source", source="upload", type="filepath", include_audio=False, elem_id="input-vid")
188
  with gr.Row():
189
+ seed_inp = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, value=123456)
190
  trim_in = gr.Slider(label="Cut video at (s)", minimun=1, maximum=3, step=1, value=1)
191
  with gr.Column():
192
+ gr.HTML("""
193
+ <a style="display:inline-block" href="https://huggingface.co/spaces/fffiloni/Pix2Pix-Video?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></p>
194
+
195
+ """)
196
  video_out = gr.Video(label="Pix2pix video result", elem_id="video-output")
197
  submit_btn = gr.Button("Generate Pix2Pix video")
198