fffiloni commited on
Commit
6770682
1 Parent(s): fdb7f66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -2
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import cv2
4
  import numpy as np
5
  from moviepy.editor import *
 
6
 
7
  token = os.environ.get('HF_TOKEN')
8
  pix2pix = gr.Blocks.load(name="spaces/fffiloni/instruct-pix2pix-clone", api_key=token)
@@ -11,6 +12,8 @@ def get_frames(video_in):
11
  frames = []
12
  #resize the video
13
  clip = VideoFileClip(video_in)
 
 
14
  if clip.fps > 30:
15
  print("vide rate is over 30, resetting to 30")
16
  clip_resized = clip.resize(height=512)
@@ -39,8 +42,10 @@ def get_frames(video_in):
39
  cap.release()
40
  cv2.destroyAllWindows()
41
  print("broke the video into frames")
 
42
  return frames, fps
43
 
 
44
  def create_video(frames, fps):
45
  print("building video result")
46
  clip = ImageSequenceClip(frames, fps=fps)
@@ -56,11 +61,14 @@ def infer(prompt,video_in, seed_in, trim_value):
56
  frames_list= break_vid[0]
57
  fps = break_vid[1]
58
  n_frame = int(trim_value*fps)
 
59
  if n_frame >= len(frames_list):
60
  print("video is shorter than the cut value")
61
  n_frame = len(frames_list)
 
62
  result_frames = []
63
  print("set stop frames to: " + str(n_frame))
 
64
  for i in frames_list[0:int(n_frame)]:
65
  pix2pix_img = pix2pix(prompt,5.5,1.5,i,15,"",512,512,seed_in,fn_index=0)
66
  images = [os.path.join(pix2pix_img[0], img) for img in os.listdir(pix2pix_img[0])]
@@ -69,7 +77,8 @@ def infer(prompt,video_in, seed_in, trim_value):
69
 
70
  final_vid = create_video(result_frames, fps)
71
  print("finished !")
72
- return final_vid
 
73
 
74
  title = """
75
  <div style="text-align: center; max-width: 700px; margin: 0 auto;">
@@ -116,10 +125,17 @@ with gr.Blocks(css='style.css') as demo:
116
 
117
  video_out = gr.Video(label="Pix2pix video result")
118
  submit_btn = gr.Button("Generate Pix2Pix video")
 
 
 
 
 
119
  gr.HTML(article)
 
120
  inputs = [prompt,video_inp,seed_inp, trim_in]
121
- outputs = [video_out]
122
 
123
  submit_btn.click(infer, inputs, outputs)
 
124
 
125
  demo.launch().queue(max_size=12)
 
3
  import cv2
4
  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)
 
12
  frames = []
13
  #resize the video
14
  clip = VideoFileClip(video_in)
15
+
16
+ #check fps
17
  if clip.fps > 30:
18
  print("vide rate is over 30, resetting to 30")
19
  clip_resized = clip.resize(height=512)
 
42
  cap.release()
43
  cv2.destroyAllWindows()
44
  print("broke the video into frames")
45
+
46
  return frames, fps
47
 
48
+
49
  def create_video(frames, fps):
50
  print("building video result")
51
  clip = ImageSequenceClip(frames, fps=fps)
 
61
  frames_list= break_vid[0]
62
  fps = break_vid[1]
63
  n_frame = int(trim_value*fps)
64
+
65
  if n_frame >= len(frames_list):
66
  print("video is shorter than the cut value")
67
  n_frame = len(frames_list)
68
+
69
  result_frames = []
70
  print("set stop frames to: " + str(n_frame))
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])]
 
77
 
78
  final_vid = create_video(result_frames, fps)
79
  print("finished !")
80
+
81
+ return final_vid, gr.Group.update(visible=True)
82
 
83
  title = """
84
  <div style="text-align: center; max-width: 700px; margin: 0 auto;">
 
125
 
126
  video_out = gr.Video(label="Pix2pix video result")
127
  submit_btn = gr.Button("Generate Pix2Pix video")
128
+
129
+ with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
130
+ community_icon = gr.HTML(community_icon_html)
131
+ loading_icon = gr.HTML(loading_icon_html)
132
+ share_button = gr.Button("Share to community", elem_id="share-btn")
133
  gr.HTML(article)
134
+
135
  inputs = [prompt,video_inp,seed_inp, trim_in]
136
+ outputs = [video_out, share_group]
137
 
138
  submit_btn.click(infer, inputs, outputs)
139
+ share_button.click(None, [], [], _js=share_js)
140
 
141
  demo.launch().queue(max_size=12)