Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torchvision.utils as vutils
|
| 2 |
+
import torchvision.transforms as T
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
def generate_images(num_images, z_dim=100):
|
| 8 |
+
# Generate batch of latent vectors
|
| 9 |
+
noise = torch.randn(num_images, z_dim, 1, 1)
|
| 10 |
+
|
| 11 |
+
# Generate fake image batch with G
|
| 12 |
+
generator.eval() # Set the generator to evaluation mode
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
fake_images = generator(noise).detach().cpu()
|
| 15 |
+
|
| 16 |
+
# Plot the fake images
|
| 17 |
+
img = vutils.make_grid(fake_images, padding=2, normalize=True).permute(1, 2, 0)
|
| 18 |
+
|
| 19 |
+
img = img.permute(2,0,1)
|
| 20 |
+
|
| 21 |
+
transform = T.ToPILImage()
|
| 22 |
+
|
| 23 |
+
# convert the tensor to PIL image using above transform
|
| 24 |
+
img = transform(img)
|
| 25 |
+
|
| 26 |
+
return img
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
batch_size = 16
|
| 30 |
+
z = torch.randn(batch_size, 100, 1, 1)
|
| 31 |
+
fake_images = generator(z)
|
| 32 |
+
# Create a Gradio input component for a positive integer
|
| 33 |
+
#inp = gr.Number(value=0, minimum=0, label="Enter a positive integer")
|
| 34 |
+
|
| 35 |
+
# Create a Gradio output component for an image
|
| 36 |
+
out = gr.Image(type="pil",label = "Generated Dogs")
|
| 37 |
+
inp = gr.Number(value=0, minimum=0, label="Enter number of Dogs to Generate (positive integer)")
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(fn=generate_images,inputs = inp, outputs=out, allow_flagging="never")
|
| 40 |
+
|
| 41 |
+
# Launch the Gradio interface
|
| 42 |
+
demo.launch(debug = True)
|