Ahsen Khaliq commited on
Commit
ba9de54
1 Parent(s): 3ecec75

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ from torchvision.io import read_video, read_video_timestamps
4
+
5
+ from videogpt import download, load_vqvae
6
+ from videogpt.data import preprocess
7
+ import imageio
8
+ import gradio as gr
9
+ from moviepy.editor import *
10
+
11
+ device = torch.device('cpu')
12
+ vqvae = load_vqvae('kinetics_stride2x4x4', device=device).to(device)
13
+
14
+ resolution, sequence_length = vqvae.hparams.resolution, 16
15
+
16
+ def vgpt(invid):
17
+ try:
18
+ os.remove("output.mp4")
19
+ except FileNotFoundError:
20
+ pass
21
+ clip = VideoFileClip(invid)
22
+ rate = clip.fps
23
+ pts = read_video_timestamps(invid, pts_unit='sec')[0]
24
+ video = read_video(invid, pts_unit='sec', start_pts=pts[0], end_pts=pts[sequence_length - 1])[0]
25
+ video = preprocess(video, resolution, sequence_length).unsqueeze(0).to(device)
26
+
27
+ with torch.no_grad():
28
+ encodings = vqvae.encode(video)
29
+ video_recon = vqvae.decode(encodings)
30
+ video_recon = torch.clamp(video_recon, -0.5, 0.5)
31
+
32
+ videos = video_recon[0].permute(1, 2, 3, 0) # CTHW -> THWC
33
+ videos = ((videos + 0.5) * 255).cpu().numpy().astype('uint8')
34
+ imageio.mimwrite('output.mp4', videos, fps=int(rate))
35
+ return './output.mp4'
36
+
37
+ inputs = gr.inputs.Video(label="Input Video")
38
+ outputs = gr.outputs.Video(label="Output Video")
39
+
40
+ title = "VideoGPT"
41
+ description = "demo for VideoGPT by University of California, Berkeley. To use it, simply upload your video, or click one of the examples to load them. Read more at the links below."
42
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2104.10157'>VideoGPT: Video Generation using VQ-VAE and Transformers</a> | <a href='https://github.com/wilson1yan/VideoGPT'>Github Repo</a></p>"
43
+
44
+ examples = [
45
+ ['gradio/bear.mp4'],
46
+ ['gradio/breakdance.mp4']
47
+ ]
48
+
49
+ gr.Interface(vgpt, inputs, outputs, title=title, description=description, article=article, examples=examples).launch(debug=True)