Delete app.py
Browse files
app.py
DELETED
@@ -1,80 +0,0 @@
|
|
1 |
-
from __future__ import print_function
|
2 |
-
import torch
|
3 |
-
import torchvision
|
4 |
-
import torch.nn as nn
|
5 |
-
import gradio as gr
|
6 |
-
import os
|
7 |
-
import time
|
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 |
-
def image_transform(image):
|
19 |
-
if isinstance(image, str):
|
20 |
-
# If image is a path to a file, open it using PIL
|
21 |
-
image = Image.open(image).convert('RGB')
|
22 |
-
else:
|
23 |
-
# If image is a NumPy array, convert it to a PIL image
|
24 |
-
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
25 |
-
# Apply the same transformations as before
|
26 |
-
image = transform(image).unsqueeze(0)
|
27 |
-
return image.to(device)
|
28 |
-
|
29 |
-
|
30 |
-
#Defining the predict function
|
31 |
-
def style_transfer(cont_img,styl_img):
|
32 |
-
|
33 |
-
#Start the timer
|
34 |
-
start_time = time.time()
|
35 |
-
|
36 |
-
#transform the input image
|
37 |
-
style_img = image_transform(styl_img)
|
38 |
-
content_img =image_transform(cont_img)
|
39 |
-
|
40 |
-
#getting input image
|
41 |
-
input_img = content_img.clone()
|
42 |
-
|
43 |
-
#running the style transfer
|
44 |
-
output = run_style_transfer(cnn, cnn_normalization_mean, cnn_normalization_std,
|
45 |
-
content_img, style_img, input_img)
|
46 |
-
# output_img = output.detach().cpu().squeeze(0)
|
47 |
-
# output_img = TF.to_pil_image(output_img)
|
48 |
-
end_time=time.time()
|
49 |
-
|
50 |
-
pred_time =round(end_time- start_time, 5)
|
51 |
-
|
52 |
-
return output
|
53 |
-
|
54 |
-
##Gradio App
|
55 |
-
import gradio as gr
|
56 |
-
title= 'Style Transfer'
|
57 |
-
description='A model to transfer the style of one image to another'
|
58 |
-
article = 'Created at Pytorch Model Deployment'
|
59 |
-
|
60 |
-
#example_images
|
61 |
-
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
62 |
-
|
63 |
-
#Create the gradio demo
|
64 |
-
demo = gr.Interface(
|
65 |
-
fn=style_transfer,
|
66 |
-
inputs=[
|
67 |
-
gr.inputs.Image(label="content image",type=pil),
|
68 |
-
gr.inputs.Image(label="style_image",type=pil)
|
69 |
-
],
|
70 |
-
examples=example_list,
|
71 |
-
outputs="image",
|
72 |
-
allow_flagging=False,
|
73 |
-
title=title,
|
74 |
-
description=description,
|
75 |
-
article=article
|
76 |
-
)
|
77 |
-
|
78 |
-
# Launch the Gradio interface
|
79 |
-
demo.launch(debug=True)
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|