Fabrice-TIERCELIN
commited on
Commit
•
9244d0e
1
Parent(s):
bc9fe2f
Delete app.py
Browse files
app.py
DELETED
@@ -1,162 +0,0 @@
|
|
1 |
-
import spaces
|
2 |
-
import gradio as gr
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image
|
5 |
-
import cv2
|
6 |
-
from moviepy.editor import VideoFileClip
|
7 |
-
from share_btn import community_icon_html, loading_icon_html, share_js
|
8 |
-
import torch
|
9 |
-
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
|
10 |
-
from diffusers.utils import export_to_video
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
def convert_mp4_to_frames(video_path, duration=3):
|
15 |
-
# Read the video file
|
16 |
-
video = cv2.VideoCapture(video_path)
|
17 |
-
|
18 |
-
# Get the frames per second (fps) of the video
|
19 |
-
fps = video.get(cv2.CAP_PROP_FPS)
|
20 |
-
|
21 |
-
# Calculate the number of frames to extract
|
22 |
-
num_frames = int(fps * duration)
|
23 |
-
|
24 |
-
frames = []
|
25 |
-
frame_count = 0
|
26 |
-
|
27 |
-
# Iterate through each frame
|
28 |
-
while True:
|
29 |
-
# Read a frame
|
30 |
-
ret, frame = video.read()
|
31 |
-
|
32 |
-
# If the frame was not successfully read or we have reached the desired duration, break the loop
|
33 |
-
if not ret or frame_count == num_frames:
|
34 |
-
break
|
35 |
-
|
36 |
-
# Convert BGR to RGB
|
37 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
38 |
-
|
39 |
-
# Append the frame to the list of frames
|
40 |
-
frames.append(frame)
|
41 |
-
|
42 |
-
frame_count += 1
|
43 |
-
|
44 |
-
# Release the video object
|
45 |
-
video.release()
|
46 |
-
|
47 |
-
# Convert the list of frames to a numpy array
|
48 |
-
frames = np.array(frames)
|
49 |
-
|
50 |
-
return frames
|
51 |
-
|
52 |
-
@spaces.GPU(duration=120)
|
53 |
-
def infer(prompt, video_in, denoise_strength):
|
54 |
-
|
55 |
-
negative_prompt = "text, watermark, copyright, blurry, nsfw"
|
56 |
-
|
57 |
-
video = convert_mp4_to_frames(video_in, duration=3)
|
58 |
-
video_resized = [Image.fromarray(frame).resize((1024, 576)) for frame in video]
|
59 |
-
|
60 |
-
pipe_xl = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_XL", torch_dtype=torch.float32, revision="refs/pr/17")
|
61 |
-
pipe_xl.vae.enable_slicing()
|
62 |
-
pipe_xl.scheduler = DPMSolverMultistepScheduler.from_config(pipe_xl.scheduler.config)
|
63 |
-
pipe_xl.enable_model_cpu_offload()
|
64 |
-
pipe_xl.to("cpu")
|
65 |
-
video_frames = pipe_xl(prompt, negative_prompt=negative_prompt, video=video_resized, strength=denoise_strength).frames
|
66 |
-
del pipe_xl
|
67 |
-
#torch.cuda.empty_cache()
|
68 |
-
video_path = export_to_video(video_frames, output_video_path="xl_result.mp4")
|
69 |
-
|
70 |
-
return "xl_result.mp4", gr.Group.update(visible=True)
|
71 |
-
|
72 |
-
css = """
|
73 |
-
#col-container {max-width: 510px; margin-left: auto; margin-right: auto;}
|
74 |
-
a {text-decoration-line: underline; font-weight: 600;}
|
75 |
-
.animate-spin {
|
76 |
-
animation: spin 1s linear infinite;
|
77 |
-
}
|
78 |
-
@keyframes spin {
|
79 |
-
from {
|
80 |
-
transform: rotate(0deg);
|
81 |
-
}
|
82 |
-
to {
|
83 |
-
transform: rotate(360deg);
|
84 |
-
}
|
85 |
-
}
|
86 |
-
#share-btn-container {
|
87 |
-
display: flex;
|
88 |
-
padding-left: 0.5rem !important;
|
89 |
-
padding-right: 0.5rem !important;
|
90 |
-
background-color: #000000;
|
91 |
-
justify-content: center;
|
92 |
-
align-items: center;
|
93 |
-
border-radius: 9999px !important;
|
94 |
-
max-width: 13rem;
|
95 |
-
}
|
96 |
-
#share-btn-container:hover {
|
97 |
-
background-color: #060606;
|
98 |
-
}
|
99 |
-
#share-btn {
|
100 |
-
all: initial;
|
101 |
-
color: #ffffff;
|
102 |
-
font-weight: 600;
|
103 |
-
cursor:pointer;
|
104 |
-
font-family: 'IBM Plex Sans', sans-serif;
|
105 |
-
margin-left: 0.5rem !important;
|
106 |
-
padding-top: 0.5rem !important;
|
107 |
-
padding-bottom: 0.5rem !important;
|
108 |
-
right:0;
|
109 |
-
}
|
110 |
-
#share-btn * {
|
111 |
-
all: unset;
|
112 |
-
}
|
113 |
-
#share-btn-container div:nth-child(-n+2){
|
114 |
-
width: auto !important;
|
115 |
-
min-height: 0px !important;
|
116 |
-
}
|
117 |
-
#share-btn-container .wrap {
|
118 |
-
display: none !important;
|
119 |
-
}
|
120 |
-
#share-btn-container.hidden {
|
121 |
-
display: none!important;
|
122 |
-
}
|
123 |
-
img[src*='#center'] {
|
124 |
-
display: block;
|
125 |
-
margin: auto;
|
126 |
-
}
|
127 |
-
"""
|
128 |
-
|
129 |
-
with gr.Blocks(css=css) as demo:
|
130 |
-
with gr.Column(elem_id="col-container"):
|
131 |
-
gr.Markdown(
|
132 |
-
"""
|
133 |
-
<h1 style="text-align: center;">Zeroscope XL</h1>
|
134 |
-
<p style="text-align: center;">
|
135 |
-
This space is specifically designed for upscaling content made from <br />
|
136 |
-
<a href="https://huggingface.co/spaces/fffiloni/zeroscope">the zeroscope_v2_576w space</a> using vid2vid. <br />
|
137 |
-
Remember to use the same prompt that was used to generate the original clip.<br />
|
138 |
-
For demo purpose, video length is limited to 3 seconds.
|
139 |
-
</p>
|
140 |
-
|
141 |
-
[![Duplicate this Space](https://huggingface.co/datasets/huggingface/badges/raw/main/duplicate-this-space-sm.svg#center)](https://huggingface.co/spaces/fffiloni/zeroscope-XL?duplicate=true)
|
142 |
-
|
143 |
-
"""
|
144 |
-
)
|
145 |
-
|
146 |
-
video_in = gr.Video(sources=["upload"])
|
147 |
-
prompt_in = gr.Textbox(label="Prompt", placeholder="This must be the same prompt you used for the original clip :)", elem_id="prompt-in")
|
148 |
-
denoise_strength = gr.Slider(label="Denoise strength", minimum=0.6, maximum=0.9, step=0.01, value=0.66)
|
149 |
-
#inference_steps = gr.Slider(label="Inference Steps", minimum=10, maximum=100, step=1, value=40, interactive=False)
|
150 |
-
submit_btn = gr.Button("Submit")
|
151 |
-
video_result = gr.Video(label="Video Output", elem_id="video-output")
|
152 |
-
|
153 |
-
with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
|
154 |
-
community_icon = gr.HTML(community_icon_html)
|
155 |
-
loading_icon = gr.HTML(loading_icon_html)
|
156 |
-
share_button = gr.Button("Share to community", elem_id="share-btn")
|
157 |
-
|
158 |
-
submit_btn.click(fn=infer,
|
159 |
-
inputs=[prompt_in, video_in, denoise_strength],
|
160 |
-
outputs=[video_result, share_group])
|
161 |
-
|
162 |
-
demo.queue(max_size=12).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|