jbilcke-hf's picture
jbilcke-hf HF staff
Update app.py
e4a0319
raw
history blame
4.59 kB
#!/usr/bin/env python
from __future__ import annotations
import os
import random
import tempfile
import gradio as gr
import imageio
import numpy as np
import spaces
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
DESCRIPTION = 'This space is an API service meant to be used by VideoChain and VideoQuest.\nWant to use this space for yourself? Please use the original code: [https://huggingface.co/spaces/hysts/zeroscope-v2](https://huggingface.co/spaces/hysts/zeroscope-v2)'
if not torch.cuda.is_available():
DESCRIPTION += '\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>'
MAX_NUM_FRAMES = int(os.getenv('MAX_NUM_FRAMES', '200'))
DEFAULT_NUM_FRAMES = min(MAX_NUM_FRAMES,
int(os.getenv('DEFAULT_NUM_FRAMES', '24')))
MAX_SEED = np.iinfo(np.int32).max
SECRET_TOKEN = os.getenv('SECRET_TOKEN', 'default_secret')
if torch.cuda.is_available():
pipe = DiffusionPipeline.from_pretrained('cerspense/zeroscope_v2_576w',
torch_dtype=torch.float16)
pipe.enable_model_cpu_offload()
else:
pipe = DiffusionPipeline.from_pretrained('cerspense/zeroscope_v2_576w')
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_vae_slicing()
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
if randomize_seed:
seed = random.randint(0, MAX_SEED)
return seed
def to_video(frames: list[np.ndarray], fps: int) -> str:
out_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
writer = imageio.get_writer(out_file.name, format='FFMPEG', fps=fps)
for frame in frames:
writer.append_data(frame)
writer.close()
return out_file.name
@spaces.GPU
def generate(prompt: str, seed: int, num_frames: int,
num_inference_steps: int,
secret_token: str = '') -> str:
if secret_token != SECRET_TOKEN:
raise gr.Error(
f'Invalid secret token. Please fork the original space if you want to use it for yourself.')
generator = torch.Generator().manual_seed(seed)
frames = pipe(prompt,
num_inference_steps=num_inference_steps,
num_frames=num_frames,
width=576,
height=320,
generator=generator).frames
return to_video(frames, 8)
with gr.Blocks(css='style.css') as demo:
gr.Markdown(DESCRIPTION)
secret_token = gr.Text(
label='Secret Token',
max_lines=1,
placeholder='Enter your secret token',
)
with gr.Box():
with gr.Row():
prompt = gr.Text(label='Prompt',
show_label=False,
max_lines=1,
placeholder='Enter your prompt',
container=False)
run_button = gr.Button('Generate video', scale=0)
result = gr.Video(label='Result', show_label=False)
with gr.Accordion('Advanced options', open=False):
seed = gr.Slider(label='Seed',
minimum=0,
maximum=MAX_SEED,
step=1,
value=0)
randomize_seed = gr.Checkbox(label='Randomize seed', value=True)
num_frames = gr.Slider(
label='Number of frames',
minimum=24,
maximum=MAX_NUM_FRAMES,
step=1,
value=24,
info=
'Note that the content of the video also changes when you change the number of frames.'
)
num_inference_steps = gr.Slider(label='Number of inference steps',
minimum=10,
maximum=50,
step=1,
value=25)
inputs = [
prompt,
seed,
num_frames,
num_inference_steps,
secret_token,
]
prompt.submit(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
queue=False,
).then(
fn=generate,
inputs=inputs,
outputs=result,
api_name='run',
)
run_button.click(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
queue=False,
).then(
fn=generate,
inputs=inputs,
outputs=result,
)
demo.queue(max_size=3).launch()