Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,61 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
nn.
|
| 18 |
-
nn.
|
| 19 |
-
nn.
|
| 20 |
-
nn.
|
| 21 |
-
nn.
|
| 22 |
-
nn.
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
iface.launch()
|
| 65 |
-
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
from torchvision.transforms import ToPILImage
|
| 6 |
+
|
| 7 |
+
# === Generator Model Definition ===
|
| 8 |
+
class Generator(nn.Module):
|
| 9 |
+
def __init__(self, latent_dim, label_dim, img_shape):
|
| 10 |
+
super().__init__()
|
| 11 |
+
input_dim = latent_dim + label_dim
|
| 12 |
+
self.model = nn.Sequential(
|
| 13 |
+
nn.Linear(input_dim, 128),
|
| 14 |
+
nn.ReLU(True),
|
| 15 |
+
nn.Linear(128, 256),
|
| 16 |
+
nn.BatchNorm1d(256),
|
| 17 |
+
nn.ReLU(True),
|
| 18 |
+
nn.Linear(256, 512),
|
| 19 |
+
nn.BatchNorm1d(512),
|
| 20 |
+
nn.ReLU(True),
|
| 21 |
+
nn.Linear(512, int(torch.prod(torch.tensor(img_shape)))),
|
| 22 |
+
nn.Tanh()
|
| 23 |
+
)
|
| 24 |
+
self.img_shape = img_shape
|
| 25 |
+
|
| 26 |
+
def forward(self, noise, labels):
|
| 27 |
+
x = torch.cat((noise, labels), dim=1)
|
| 28 |
+
img = self.model(x)
|
| 29 |
+
return img.view(img.size(0), *self.img_shape)
|
| 30 |
+
|
| 31 |
+
# === Load Model ===
|
| 32 |
+
latent_dim = 100
|
| 33 |
+
label_dim = 10
|
| 34 |
+
img_shape = (1, 28, 28)
|
| 35 |
+
|
| 36 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
+
generator = Generator(latent_dim, label_dim, img_shape).to(device)
|
| 38 |
+
generator.load_state_dict(torch.load("models/mnist_generator.pth", map_location=device))
|
| 39 |
+
generator.eval()
|
| 40 |
+
|
| 41 |
+
# === Generate Function ===
|
| 42 |
+
def generate_images(digit: str):
|
| 43 |
+
digit = int(digit)
|
| 44 |
+
z = torch.randn(5, latent_dim, device=device)
|
| 45 |
+
labels = torch.eye(label_dim, device=device)[[digit] * 5]
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
gen_imgs = generator(z, labels).cpu()
|
| 48 |
+
gen_imgs = (gen_imgs + 1) / 2 # Normalize to [0, 1]
|
| 49 |
+
images = [ToPILImage()(img.squeeze(0)) for img in gen_imgs]
|
| 50 |
+
return images
|
| 51 |
+
|
| 52 |
+
# === Gradio Interface ===
|
| 53 |
+
iface = gr.Interface(
|
| 54 |
+
fn=generate_images,
|
| 55 |
+
inputs=gr.Dropdown(choices=[str(i) for i in range(10)], label="Pick a digit"),
|
| 56 |
+
outputs=[gr.Image(type="pil") for _ in range(5)],
|
| 57 |
+
title="MNIST Digit Generator",
|
| 58 |
+
description="Select a digit from 0–9 to generate 5 synthetic handwritten digits using a GAN trained on MNIST."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|