Spaces:
Runtime error
Runtime error
Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ import psutil
|
|
| 12 |
import math
|
| 13 |
import random
|
| 14 |
|
| 15 |
-
|
| 16 |
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None)
|
| 17 |
|
| 18 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
|
@@ -20,7 +20,6 @@ device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
|
| 20 |
if torch.cuda.is_available():
|
| 21 |
pipe = pipe.to("cuda")
|
| 22 |
|
| 23 |
-
|
| 24 |
def pix2pix(
|
| 25 |
input_image: Image.Image,
|
| 26 |
instruction: str,
|
|
@@ -29,7 +28,7 @@ def pix2pix(
|
|
| 29 |
text_cfg_scale: float,
|
| 30 |
image_cfg_scale: float,
|
| 31 |
):
|
| 32 |
-
|
| 33 |
width, height = input_image.size
|
| 34 |
factor = 512 / max(width, height)
|
| 35 |
factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height)
|
|
@@ -49,13 +48,11 @@ def pix2pix(
|
|
| 49 |
print(f"EDITED: {edited_image}")
|
| 50 |
return edited_image
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
def get_frames(video_in):
|
| 55 |
frames = []
|
| 56 |
#resize the video
|
| 57 |
clip = VideoFileClip(video_in)
|
| 58 |
-
|
| 59 |
#check fps
|
| 60 |
if clip.fps > 30:
|
| 61 |
print("vide rate is over 30, resetting to 30")
|
|
@@ -65,12 +62,12 @@ def get_frames(video_in):
|
|
| 65 |
print("video rate is OK")
|
| 66 |
clip_resized = clip.resize(height=512)
|
| 67 |
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
| 68 |
-
|
| 69 |
print("video resized to 512 height")
|
| 70 |
-
|
| 71 |
# Opens the Video file with CV2
|
| 72 |
cap= cv2.VideoCapture("video_resized.mp4")
|
| 73 |
-
|
| 74 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 75 |
print("video fps: " + str(fps))
|
| 76 |
i=0
|
|
@@ -81,45 +78,43 @@ def get_frames(video_in):
|
|
| 81 |
cv2.imwrite('kang'+str(i)+'.jpg',frame)
|
| 82 |
frames.append('kang'+str(i)+'.jpg')
|
| 83 |
i+=1
|
| 84 |
-
|
| 85 |
cap.release()
|
| 86 |
cv2.destroyAllWindows()
|
| 87 |
print("broke the video into frames")
|
| 88 |
-
|
| 89 |
-
return frames, fps
|
| 90 |
|
|
|
|
| 91 |
|
| 92 |
def create_video(frames, fps):
|
| 93 |
print("building video result")
|
| 94 |
clip = ImageSequenceClip(frames, fps=fps)
|
| 95 |
clip.write_videofile("movie.mp4", fps=fps)
|
| 96 |
-
|
| 97 |
-
return 'movie.mp4'
|
| 98 |
|
|
|
|
| 99 |
|
| 100 |
def infer(prompt,video_in, seed_in, trim_value):
|
| 101 |
print(prompt)
|
| 102 |
break_vid = get_frames(video_in)
|
| 103 |
-
|
| 104 |
frames_list= break_vid[0]
|
| 105 |
fps = break_vid[1]
|
| 106 |
n_frame = int(trim_value*fps)
|
| 107 |
-
|
| 108 |
if n_frame >= len(frames_list):
|
| 109 |
print("video is shorter than the cut value")
|
| 110 |
n_frame = len(frames_list)
|
| 111 |
-
|
| 112 |
result_frames = []
|
| 113 |
print("set stop frames to: " + str(n_frame))
|
| 114 |
-
|
| 115 |
for i in frames_list[0:int(n_frame)]:
|
| 116 |
pil_i = Image.open(i).convert("RGB")
|
| 117 |
-
|
| 118 |
pix2pix_img = pix2pix(pil_i, prompt, 50, seed_in, 7.5, 1.5)
|
| 119 |
#print(pix2pix_img)
|
| 120 |
#image = Image.open(pix2pix_img)
|
| 121 |
#rgb_im = image.convert("RGB")
|
| 122 |
-
|
| 123 |
# exporting the image
|
| 124 |
pix2pix_img.save(f"result_img-{i}.jpg")
|
| 125 |
result_frames.append(f"result_img-{i}.jpg")
|
|
@@ -127,7 +122,7 @@ def infer(prompt,video_in, seed_in, trim_value):
|
|
| 127 |
|
| 128 |
final_vid = create_video(result_frames, fps)
|
| 129 |
print("finished !")
|
| 130 |
-
|
| 131 |
return final_vid
|
| 132 |
|
| 133 |
title = """
|
|
@@ -145,13 +140,13 @@ title = """
|
|
| 145 |
</h1>
|
| 146 |
</div>
|
| 147 |
<p style="margin-bottom: 10px; font-size: 94%">
|
| 148 |
-
Apply Instruct Pix2Pix Diffusion to a video
|
| 149 |
</p>
|
| 150 |
</div>
|
| 151 |
"""
|
| 152 |
|
| 153 |
article = """
|
| 154 |
-
|
| 155 |
<div class="footer">
|
| 156 |
<p>
|
| 157 |
Examples by <a href="https://twitter.com/CitizenPlain" target="_blank">Nathan Shipley</a> •
|
|
@@ -161,20 +156,21 @@ article = """
|
|
| 161 |
<div id="may-like-container" style="display: flex;justify-content: center;flex-direction: column;align-items: center;margin-bottom: 30px;">
|
| 162 |
<p>You may also like: </p>
|
| 163 |
<div id="may-like-content" style="display:flex;flex-wrap: wrap;align-items:center;height:20px;">
|
| 164 |
-
|
| 165 |
-
<svg height="20" width="162" style="margin-left:4px;margin-bottom: 6px;">
|
| 166 |
<a href="https://huggingface.co/spaces/timbrooks/instruct-pix2pix" target="_blank">
|
| 167 |
<image href="https://img.shields.io/badge/🤗 Spaces-Instruct_Pix2Pix-blue" src="https://img.shields.io/badge/🤗 Spaces-Instruct_Pix2Pix-blue.png" height="20"/>
|
| 168 |
</a>
|
| 169 |
</svg>
|
| 170 |
-
|
| 171 |
</div>
|
| 172 |
-
|
| 173 |
</div>
|
| 174 |
-
|
| 175 |
"""
|
| 176 |
|
| 177 |
-
|
|
|
|
| 178 |
with gr.Column(elem_id="col-container"):
|
| 179 |
gr.HTML(title)
|
| 180 |
with gr.Row():
|
|
@@ -187,29 +183,36 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 187 |
with gr.Column():
|
| 188 |
video_out = gr.Video(label="Pix2pix video result", elem_id="video-output")
|
| 189 |
gr.HTML("""
|
| 190 |
-
<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>
|
| 191 |
-
work with longer videos / skip the queue:
|
| 192 |
""", elem_id="duplicate-container")
|
| 193 |
submit_btn = gr.Button("Generate Pix2Pix video")
|
| 194 |
-
|
| 195 |
inputs = [prompt,video_inp,seed_inp, trim_in]
|
| 196 |
outputs = [video_out]
|
| 197 |
-
|
| 198 |
ex = gr.Examples(
|
| 199 |
[
|
| 200 |
["Make it a marble sculpture", "./examples/pexels-jill-burrow-7665249_512x512.mp4", 422112651, 4],
|
| 201 |
["Make it molten lava", "./examples/Ocean_Pexels_ 8953474_512x512.mp4", 43571876, 4]
|
| 202 |
],
|
| 203 |
inputs=inputs,
|
| 204 |
-
# outputs=outputs,
|
| 205 |
-
# fn=infer,
|
| 206 |
-
# cache_examples=True,
|
| 207 |
)
|
| 208 |
-
|
| 209 |
gr.HTML(article)
|
| 210 |
-
|
| 211 |
-
submit_btn.click(infer, inputs, outputs, show_api=False)
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
import math
|
| 13 |
import random
|
| 14 |
|
| 15 |
+
# Initialize the pipeline
|
| 16 |
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None)
|
| 17 |
|
| 18 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
|
|
|
| 20 |
if torch.cuda.is_available():
|
| 21 |
pipe = pipe.to("cuda")
|
| 22 |
|
|
|
|
| 23 |
def pix2pix(
|
| 24 |
input_image: Image.Image,
|
| 25 |
instruction: str,
|
|
|
|
| 28 |
text_cfg_scale: float,
|
| 29 |
image_cfg_scale: float,
|
| 30 |
):
|
| 31 |
+
|
| 32 |
width, height = input_image.size
|
| 33 |
factor = 512 / max(width, height)
|
| 34 |
factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height)
|
|
|
|
| 48 |
print(f"EDITED: {edited_image}")
|
| 49 |
return edited_image
|
| 50 |
|
|
|
|
|
|
|
| 51 |
def get_frames(video_in):
|
| 52 |
frames = []
|
| 53 |
#resize the video
|
| 54 |
clip = VideoFileClip(video_in)
|
| 55 |
+
|
| 56 |
#check fps
|
| 57 |
if clip.fps > 30:
|
| 58 |
print("vide rate is over 30, resetting to 30")
|
|
|
|
| 62 |
print("video rate is OK")
|
| 63 |
clip_resized = clip.resize(height=512)
|
| 64 |
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
| 65 |
+
|
| 66 |
print("video resized to 512 height")
|
| 67 |
+
|
| 68 |
# Opens the Video file with CV2
|
| 69 |
cap= cv2.VideoCapture("video_resized.mp4")
|
| 70 |
+
|
| 71 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 72 |
print("video fps: " + str(fps))
|
| 73 |
i=0
|
|
|
|
| 78 |
cv2.imwrite('kang'+str(i)+'.jpg',frame)
|
| 79 |
frames.append('kang'+str(i)+'.jpg')
|
| 80 |
i+=1
|
| 81 |
+
|
| 82 |
cap.release()
|
| 83 |
cv2.destroyAllWindows()
|
| 84 |
print("broke the video into frames")
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
return frames, fps
|
| 87 |
|
| 88 |
def create_video(frames, fps):
|
| 89 |
print("building video result")
|
| 90 |
clip = ImageSequenceClip(frames, fps=fps)
|
| 91 |
clip.write_videofile("movie.mp4", fps=fps)
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
return 'movie.mp4'
|
| 94 |
|
| 95 |
def infer(prompt,video_in, seed_in, trim_value):
|
| 96 |
print(prompt)
|
| 97 |
break_vid = get_frames(video_in)
|
| 98 |
+
|
| 99 |
frames_list= break_vid[0]
|
| 100 |
fps = break_vid[1]
|
| 101 |
n_frame = int(trim_value*fps)
|
| 102 |
+
|
| 103 |
if n_frame >= len(frames_list):
|
| 104 |
print("video is shorter than the cut value")
|
| 105 |
n_frame = len(frames_list)
|
| 106 |
+
|
| 107 |
result_frames = []
|
| 108 |
print("set stop frames to: " + str(n_frame))
|
| 109 |
+
|
| 110 |
for i in frames_list[0:int(n_frame)]:
|
| 111 |
pil_i = Image.open(i).convert("RGB")
|
| 112 |
+
|
| 113 |
pix2pix_img = pix2pix(pil_i, prompt, 50, seed_in, 7.5, 1.5)
|
| 114 |
#print(pix2pix_img)
|
| 115 |
#image = Image.open(pix2pix_img)
|
| 116 |
#rgb_im = image.convert("RGB")
|
| 117 |
+
|
| 118 |
# exporting the image
|
| 119 |
pix2pix_img.save(f"result_img-{i}.jpg")
|
| 120 |
result_frames.append(f"result_img-{i}.jpg")
|
|
|
|
| 122 |
|
| 123 |
final_vid = create_video(result_frames, fps)
|
| 124 |
print("finished !")
|
| 125 |
+
|
| 126 |
return final_vid
|
| 127 |
|
| 128 |
title = """
|
|
|
|
| 140 |
</h1>
|
| 141 |
</div>
|
| 142 |
<p style="margin-bottom: 10px; font-size: 94%">
|
| 143 |
+
Apply Instruct Pix2Pix Diffusion to a video
|
| 144 |
</p>
|
| 145 |
</div>
|
| 146 |
"""
|
| 147 |
|
| 148 |
article = """
|
| 149 |
+
|
| 150 |
<div class="footer">
|
| 151 |
<p>
|
| 152 |
Examples by <a href="https://twitter.com/CitizenPlain" target="_blank">Nathan Shipley</a> •
|
|
|
|
| 156 |
<div id="may-like-container" style="display: flex;justify-content: center;flex-direction: column;align-items: center;margin-bottom: 30px;">
|
| 157 |
<p>You may also like: </p>
|
| 158 |
<div id="may-like-content" style="display:flex;flex-wrap: wrap;align-items:center;height:20px;">
|
| 159 |
+
|
| 160 |
+
<svg height="20" width="162" style="margin-left:4px;margin-bottom: 6px;">
|
| 161 |
<a href="https://huggingface.co/spaces/timbrooks/instruct-pix2pix" target="_blank">
|
| 162 |
<image href="https://img.shields.io/badge/🤗 Spaces-Instruct_Pix2Pix-blue" src="https://img.shields.io/badge/🤗 Spaces-Instruct_Pix2Pix-blue.png" height="20"/>
|
| 163 |
</a>
|
| 164 |
</svg>
|
| 165 |
+
|
| 166 |
</div>
|
| 167 |
+
|
| 168 |
</div>
|
| 169 |
+
|
| 170 |
"""
|
| 171 |
|
| 172 |
+
# Create the Gradio 6 interface
|
| 173 |
+
with gr.Blocks() as demo:
|
| 174 |
with gr.Column(elem_id="col-container"):
|
| 175 |
gr.HTML(title)
|
| 176 |
with gr.Row():
|
|
|
|
| 183 |
with gr.Column():
|
| 184 |
video_out = gr.Video(label="Pix2pix video result", elem_id="video-output")
|
| 185 |
gr.HTML("""
|
| 186 |
+
<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>
|
| 187 |
+
work with longer videos / skip the queue:
|
| 188 |
""", elem_id="duplicate-container")
|
| 189 |
submit_btn = gr.Button("Generate Pix2Pix video")
|
| 190 |
+
|
| 191 |
inputs = [prompt,video_inp,seed_inp, trim_in]
|
| 192 |
outputs = [video_out]
|
| 193 |
+
|
| 194 |
ex = gr.Examples(
|
| 195 |
[
|
| 196 |
["Make it a marble sculpture", "./examples/pexels-jill-burrow-7665249_512x512.mp4", 422112651, 4],
|
| 197 |
["Make it molten lava", "./examples/Ocean_Pexels_ 8953474_512x512.mp4", 43571876, 4]
|
| 198 |
],
|
| 199 |
inputs=inputs,
|
|
|
|
|
|
|
|
|
|
| 200 |
)
|
| 201 |
+
|
| 202 |
gr.HTML(article)
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
submit_btn.click(infer, inputs, outputs, api_visibility="private")
|
| 205 |
+
|
| 206 |
+
# Launch with Gradio 6 syntax - all parameters in launch()
|
| 207 |
+
demo.queue(max_size=12).launch(
|
| 208 |
+
theme=gr.themes.Soft(
|
| 209 |
+
primary_hue="pink",
|
| 210 |
+
secondary_hue="purple",
|
| 211 |
+
font=gr.themes.GoogleFont("IBM Plex Sans")
|
| 212 |
+
),
|
| 213 |
+
css_paths=["style.css"],
|
| 214 |
+
footer_links=[
|
| 215 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 216 |
+
],
|
| 217 |
+
show_api=False
|
| 218 |
+
)
|