Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,53 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def calculator(num1, operation, num2):
|
4 |
-
if operation == "add":
|
5 |
-
return num1 + num2
|
6 |
-
elif operation == "subtract":
|
7 |
-
return num1 - num2
|
8 |
-
elif operation == "multiply":
|
9 |
-
return num1 * num2
|
10 |
-
elif operation == "divide":
|
11 |
-
return num1 / num2
|
12 |
-
|
13 |
-
def sampler(body, bottomwear, hair, topwear):
|
14 |
-
img_name = str(body) + str(bottomwear) + str(hair) + str(topwear)
|
15 |
-
return img_name
|
16 |
-
|
17 |
-
|
18 |
-
demo = gr.Interface(
|
19 |
-
sampler,
|
20 |
-
["number", gr.Radio(["body", "bottomwear", "hair", "topwear"]), "number"],
|
21 |
-
"number",
|
22 |
-
live=True,
|
23 |
-
)
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
from torchvision.utils import save_image
|
5 |
import gradio as gr
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
class Generator(nn.Module):
|
9 |
+
# Refer to the link below for explanations about nc, nz, and ngf
|
10 |
+
# https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html#inputs
|
11 |
+
def __init__(self, nc=4, nz=100, ngf=64):
|
12 |
+
super(Generator, self).__init__()
|
13 |
+
self.network = nn.Sequential(
|
14 |
+
nn.ConvTranspose2d(nz, ngf * 4, 3, 1, 0, bias=False),
|
15 |
+
nn.BatchNorm2d(ngf * 4),
|
16 |
+
nn.ReLU(True),
|
17 |
+
nn.ConvTranspose2d(ngf * 4, ngf * 2, 3, 2, 1, bias=False),
|
18 |
+
nn.BatchNorm2d(ngf * 2),
|
19 |
+
nn.ReLU(True),
|
20 |
+
nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 0, bias=False),
|
21 |
+
nn.BatchNorm2d(ngf),
|
22 |
+
nn.ReLU(True),
|
23 |
+
nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False),
|
24 |
+
nn.Tanh(),
|
25 |
+
)
|
26 |
+
|
27 |
+
def forward(self, input):
|
28 |
+
output = self.network(input)
|
29 |
+
return output
|
30 |
+
|
31 |
+
|
32 |
+
model = Generator()
|
33 |
+
weights_path = hf_hub_download('nateraw/cryptopunks-gan', 'generator.pth')
|
34 |
+
model.load_state_dict(torch.load(weights_path, map_location=torch.device('cpu'))) # Use 'cuda' if you have a GPU available
|
35 |
+
|
36 |
+
|
37 |
+
def predict(seed, num_punks):
|
38 |
+
torch.manual_seed(seed)
|
39 |
+
z = torch.randn(num_punks, 100, 1, 1)
|
40 |
+
punks = model(z)
|
41 |
+
save_image(punks, "punks.png", normalize=True)
|
42 |
+
return 'punks.png'
|
43 |
+
|
44 |
+
gr.Interface(
|
45 |
+
predict,
|
46 |
+
inputs=[
|
47 |
+
gr.Slider(0, 1000, label='Seed', default=42),
|
48 |
+
gr.Slider(4, 64, label='Number of Punks', step=1, default=10),
|
49 |
+
],
|
50 |
+
outputs="image",
|
51 |
+
examples=[[123, 15], [42, 29], [456, 8], [1337, 35]],
|
52 |
+
live=True,
|
53 |
+
).launch(cache_examples=True)
|