Jasmeet Singh commited on
application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch # for model
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.optim as optim
|
| 5 |
+
from PIL import Image #for importing images
|
| 6 |
+
import torchvision.models as models #to load vgg 19 model
|
| 7 |
+
import torchvision.transforms as transforms #to transform the images
|
| 8 |
+
from torchvision.utils import save_image #to save the generated images
|
| 9 |
+
from tqdm import tqdm # progress bar
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
from styleTransfer import style_transfer
|
| 14 |
+
from dataTransform import tensor_to_image
|
| 15 |
+
|
| 16 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 17 |
+
print(device)
|
| 18 |
+
|
| 19 |
+
def gradio_style_transfer(steps, content_image, style_image):
|
| 20 |
+
generated_tensor = style_transfer(content_image, style_image, total_steps= steps)
|
| 21 |
+
generated_image = tensor_to_image(generated_tensor)
|
| 22 |
+
return generated_image
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
interface = gr.Interface(
|
| 26 |
+
fn=gradio_style_transfer,
|
| 27 |
+
inputs=[
|
| 28 |
+
gr.Slider(minimum=100, maximum=1000, step=100, label="Number of Steps", value=500),
|
| 29 |
+
gr.Image(type='filepath', label='Content Image'),
|
| 30 |
+
gr.Image(type='filepath', label='Style Image')
|
| 31 |
+
],
|
| 32 |
+
outputs=gr.Image(type='pil', label='Generated Image'),
|
| 33 |
+
title="Neural Style Transfer",
|
| 34 |
+
description="Upload a content image and a style image, and the app will generate a stylized image.",
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
interface.launch(debug = True)
|