Srish117 commited on
Commit
0ab566b
·
verified ·
1 Parent(s): c70b57f
Files changed (3) hide show
  1. app.py +80 -0
  2. generator_final.pth +3 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn as nn
4
+ import torchvision.utils as vutils
5
+ from torchvision import transforms
6
+ from PIL import Image
7
+
8
+ # --- 1. Define the Brain Structure (Must match your training code!) ---
9
+ class Generator(nn.Module):
10
+ def __init__(self, ngpu):
11
+ super(Generator, self).__init__()
12
+ self.ngpu = ngpu
13
+ nz = 100 # Noise size
14
+ self.main = nn.Sequential(
15
+ # Input is Z (Noise Vector)
16
+ nn.ConvTranspose2d(nz, 512, 4, 1, 0, bias=False),
17
+ nn.BatchNorm2d(512),
18
+ nn.ReLU(True),
19
+ # 4x4 -> 8x8
20
+ nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
21
+ nn.BatchNorm2d(256),
22
+ nn.ReLU(True),
23
+ # 8x8 -> 16x16
24
+ nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False),
25
+ nn.BatchNorm2d(128),
26
+ nn.ReLU(True),
27
+ # 16x16 -> 32x32
28
+ nn.ConvTranspose2d(128, 64, 4, 2, 1, bias=False),
29
+ nn.BatchNorm2d(64),
30
+ nn.ReLU(True),
31
+ # 32x32 -> 64x64
32
+ nn.ConvTranspose2d(64, 3, 4, 2, 1, bias=False),
33
+ nn.Tanh()
34
+ )
35
+
36
+ def forward(self, input):
37
+ return self.main(input)
38
+
39
+ # --- 2. Load the Model ---
40
+ device = torch.device("cpu") # Spaces run on CPU by default
41
+ model = Generator(ngpu=0).to(device)
42
+
43
+ # Load weights (map_location is vital for CPU spaces)
44
+ model.load_state_dict(torch.load("generator_final.pth", map_location=device))
45
+ model.eval()
46
+
47
+ # --- 3. The Generation Function ---
48
+ def generate_art(seed, noise_level):
49
+ torch.manual_seed(int(seed))
50
+
51
+ # Create noise vector
52
+ noise = torch.randn(1, 100, 1, 1, device=device)
53
+
54
+ # Generate image
55
+ with torch.no_grad():
56
+ fake = model(noise).detach().cpu()
57
+
58
+ # Process image (Un-normalize from -1,1 to 0,1)
59
+ img_tensor = (fake[0] * 0.5) + 0.5
60
+ img_pil = transforms.ToPILImage()(img_tensor)
61
+
62
+ # Upscale nicely for the web
63
+ img_pil = img_pil.resize((256, 256), resample=Image.NEAREST)
64
+ return img_pil
65
+
66
+ # --- 4. The Interface ---
67
+ with gr.Blocks() as demo:
68
+ gr.Markdown("# 👾 8-Bit Hero Generator (2025 Edition)")
69
+ gr.Markdown("I trained this AI from scratch on New Year's Eve 2025 using PyTorch & DCGAN.")
70
+
71
+ with gr.Row():
72
+ with gr.Column():
73
+ seed_slider = gr.Slider(0, 9999, label="Character DNA (Seed)", value=42)
74
+ btn = gr.Button("Generate New Hero")
75
+ with gr.Column():
76
+ output_image = gr.Image(label="Result", type="pil")
77
+
78
+ btn.click(fn=generate_art, inputs=[seed_slider, seed_slider], outputs=output_image)
79
+
80
+ demo.launch()
generator_final.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1a087fa0cda3a0761a3e4a465d2a201e7804ce20c50acb47df958a8fd8a3615d
3
+ size 14323408
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio