Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import print_function
|
| 2 |
+
from torchvision import nn
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
+
import torch.optim as optim
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
import torchvision.transforms as transforms
|
| 12 |
+
import copy
|
| 13 |
+
import torchvision.models as models
|
| 14 |
+
import torchvision.transforms.functional as TF
|
| 15 |
+
from PIL import Image
|
| 16 |
+
import numpy as np
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
#Defining the predict function
|
| 20 |
+
def style_transfer(cont_img,styl_img):
|
| 21 |
+
|
| 22 |
+
#Start the timer
|
| 23 |
+
start_time = time.time()
|
| 24 |
+
|
| 25 |
+
#transform the input image
|
| 26 |
+
style_img = image_transform(styl_img)
|
| 27 |
+
content_img =image_transform(cont_img)
|
| 28 |
+
|
| 29 |
+
#getting input image
|
| 30 |
+
input_img = content_img.clone()
|
| 31 |
+
|
| 32 |
+
#running the style transfer
|
| 33 |
+
output = run_style_transfer(cnn, cnn_normalization_mean, cnn_normalization_std,
|
| 34 |
+
content_img, style_img, input_img)
|
| 35 |
+
# output_img = output.detach().cpu().squeeze(0)
|
| 36 |
+
# output_img = TF.to_pil_image(output_img)
|
| 37 |
+
end_time=time.time()
|
| 38 |
+
|
| 39 |
+
pred_time =round(end_time- start_time, 5)
|
| 40 |
+
|
| 41 |
+
return output
|
| 42 |
+
|
| 43 |
+
##Gradio App
|
| 44 |
+
import gradio as gr
|
| 45 |
+
title= 'Style Transfer'
|
| 46 |
+
description='A model to transfer the style of one image to another'
|
| 47 |
+
article = 'Created at Pytorch Model Deployment'
|
| 48 |
+
|
| 49 |
+
#example_images
|
| 50 |
+
example_images = [["/content/content.jpg" ,"/content/style.jpg"]]
|
| 51 |
+
|
| 52 |
+
#Create the gradio demo
|
| 53 |
+
demo = gr.Interface(
|
| 54 |
+
fn=style_transfer,
|
| 55 |
+
inputs=[
|
| 56 |
+
gr.inputs.Image(label="content Image"),
|
| 57 |
+
gr.inputs.Image(label="style_image")
|
| 58 |
+
],
|
| 59 |
+
examples=example_images,
|
| 60 |
+
outputs="image",
|
| 61 |
+
allow_flagging=False,
|
| 62 |
+
title=title,
|
| 63 |
+
description=description,
|
| 64 |
+
article=article
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Launch the Gradio interface
|
| 68 |
+
demo.launch(debug=True)
|
| 69 |
+
|