SaifeldenMohamedIsmail commited on
Commit
ecf1dca
·
verified ·
1 Parent(s): 3b94006

Update app.py

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