Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.optim as optim
|
| 4 |
+
from torch.utils.data import DataLoader, Dataset
|
| 5 |
+
import numpy as np
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
class VideoDataset(Dataset):
|
| 9 |
+
def __init__(self, videos):
|
| 10 |
+
self.videos = videos
|
| 11 |
+
|
| 12 |
+
def __len__(self):
|
| 13 |
+
return len(self.videos)
|
| 14 |
+
|
| 15 |
+
def __getitem__(self, idx):
|
| 16 |
+
return torch.tensor(self.videos[idx], dtype=torch.float32)
|
| 17 |
+
|
| 18 |
+
class Generator(nn.Module):
|
| 19 |
+
def __init__(self, latent_dim, video_shape):
|
| 20 |
+
super(Generator, self).__init__()
|
| 21 |
+
self.latent_dim = latent_dim
|
| 22 |
+
self.video_shape = video_shape
|
| 23 |
+
self.fc = nn.Sequential(
|
| 24 |
+
nn.Linear(latent_dim, 128),
|
| 25 |
+
nn.ReLU(),
|
| 26 |
+
nn.Linear(128, np.prod(video_shape)),
|
| 27 |
+
nn.Tanh()
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def forward(self, z):
|
| 31 |
+
x = self.fc(z)
|
| 32 |
+
x = x.view(x.size(0), *self.video_shape)
|
| 33 |
+
return x
|
| 34 |
+
|
| 35 |
+
# Hyperparameters
|
| 36 |
+
latent_dim = 100
|
| 37 |
+
video_shape = (10, 1, 32, 32) # 10 frames of 32x32 grayscale images
|
| 38 |
+
|
| 39 |
+
# Instantiate the Generator
|
| 40 |
+
generator = Generator(latent_dim, video_shape)
|
| 41 |
+
|
| 42 |
+
def generate_video(noise_seed):
|
| 43 |
+
torch.manual_seed(noise_seed)
|
| 44 |
+
z = torch.randn(1, latent_dim)
|
| 45 |
+
video = generator(z).detach().numpy()[0]
|
| 46 |
+
|
| 47 |
+
# Rescale the pixel values from [-1, 1] to [0, 255]
|
| 48 |
+
video = (video + 1) * 127.5
|
| 49 |
+
video = video.astype(np.uint8)
|
| 50 |
+
return [frame[0] for frame in video] # Return list of frames
|
| 51 |
+
|
| 52 |
+
demo = gr.Interface(
|
| 53 |
+
fn=generate_video,
|
| 54 |
+
inputs=gr.Slider(0, 1000, step=1, label="Noise Seed"),
|
| 55 |
+
outputs=gr.Video(label="Generated Video"),
|
| 56 |
+
title="Simple GAN Video Generator",
|
| 57 |
+
description="Generate random grayscale videos using a simple GAN generator model."
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
demo.launch()
|