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