awacke1 commited on
Commit
7691760
1 Parent(s): 9736769

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ import torch.nn as nn
4
+ import gradio as gr
5
+ from PIL import Image
6
+ import torchvision.transforms as transforms
7
+ import os # 📁 For file operations
8
+
9
+ # 🧠 Neural network layers
10
+ norm_layer = nn.InstanceNorm2d
11
+
12
+ # 🧱 Building block for the generator
13
+ class ResidualBlock(nn.Module):
14
+ def __init__(self, in_features):
15
+ super(ResidualBlock, self).__init__()
16
+
17
+ conv_block = [ nn.ReflectionPad2d(1),
18
+ nn.Conv2d(in_features, in_features, 3),
19
+ norm_layer(in_features),
20
+ nn.ReLU(inplace=True),
21
+ nn.ReflectionPad2d(1),
22
+ nn.Conv2d(in_features, in_features, 3),
23
+ norm_layer(in_features)
24
+ ]
25
+
26
+ self.conv_block = nn.Sequential(*conv_block)
27
+
28
+ def forward(self, x):
29
+ return x + self.conv_block(x)
30
+
31
+ # 🎨 Generator model for creating line drawings
32
+ class Generator(nn.Module):
33
+ def __init__(self, input_nc, output_nc, n_residual_blocks=9, sigmoid=True):
34
+ super(Generator, self).__init__()
35
+
36
+ # 🏁 Initial convolution block
37
+ model0 = [ nn.ReflectionPad2d(3),
38
+ nn.Conv2d(input_nc, 64, 7),
39
+ norm_layer(64),
40
+ nn.ReLU(inplace=True) ]
41
+ self.model0 = nn.Sequential(*model0)
42
+
43
+ # 🔽 Downsampling
44
+ model1 = []
45
+ in_features = 64
46
+ out_features = in_features*2
47
+ for _ in range(2):
48
+ model1 += [ nn.Conv2d(in_features, out_features, 3, stride=2, padding=1),
49
+ norm_layer(out_features),
50
+ nn.ReLU(inplace=True) ]
51
+ in_features = out_features
52
+ out_features = in_features*2
53
+ self.model1 = nn.Sequential(*model1)
54
+
55
+ # 🔁 Residual blocks
56
+ model2 = []
57
+ for _ in range(n_residual_blocks):
58
+ model2 += [ResidualBlock(in_features)]
59
+ self.model2 = nn.Sequential(*model2)
60
+
61
+ # 🔼 Upsampling
62
+ model3 = []
63
+ out_features = in_features//2
64
+ for _ in range(2):
65
+ model3 += [ nn.ConvTranspose2d(in_features, out_features, 3, stride=2, padding=1, output_padding=1),
66
+ norm_layer(out_features),
67
+ nn.ReLU(inplace=True) ]
68
+ in_features = out_features
69
+ out_features = in_features//2
70
+ self.model3 = nn.Sequential(*model3)
71
+
72
+ # 🎭 Output layer
73
+ model4 = [ nn.ReflectionPad2d(3),
74
+ nn.Conv2d(64, output_nc, 7)]
75
+ if sigmoid:
76
+ model4 += [nn.Sigmoid()]
77
+
78
+ self.model4 = nn.Sequential(*model4)
79
+
80
+ def forward(self, x, cond=None):
81
+ out = self.model0(x)
82
+ out = self.model1(out)
83
+ out = self.model2(out)
84
+ out = self.model3(out)
85
+ out = self.model4(out)
86
+
87
+ return out
88
+
89
+ # 🔧 Load the models
90
+ model1 = Generator(3, 1, 3)
91
+ model1.load_state_dict(torch.load('model.pth', map_location=torch.device('cpu')))
92
+ model1.eval()
93
+
94
+ model2 = Generator(3, 1, 3)
95
+ model2.load_state_dict(torch.load('model2.pth', map_location=torch.device('cpu')))
96
+ model2.eval()
97
+
98
+ # 🖼️ Function to process the image and create line drawing
99
+ def predict(input_img, ver):
100
+ input_img = Image.open(input_img)
101
+ transform = transforms.Compose([transforms.Resize(256, Image.BICUBIC), transforms.ToTensor()])
102
+ input_img = transform(input_img)
103
+ input_img = torch.unsqueeze(input_img, 0)
104
+
105
+ drawing = 0
106
+ with torch.no_grad():
107
+ if ver == 'Simple Lines':
108
+ drawing = model2(input_img)[0].detach()
109
+ else:
110
+ drawing = model1(input_img)[0].detach()
111
+
112
+ drawing = transforms.ToPILImage()(drawing)
113
+ return drawing
114
+
115
+ # 📝 Title for the Gradio interface
116
+ title="🖌️ Image to Line Drawings - Complex and Simple Portraits and Landscapes"
117
+
118
+ # 🖼️ Dynamically generate examples from images in the directory
119
+ examples = []
120
+ image_dir = '.' # Assuming images are in the current directory
121
+ for file in os.listdir(image_dir):
122
+ if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
123
+ examples.append([file, 'Simple Lines'])
124
+ examples.append([file, 'Complex Lines'])
125
+
126
+ # 🚀 Create and launch the Gradio interface
127
+ iface = gr.Interface(predict, [gr.inputs.Image(type='filepath'),
128
+ gr.inputs.Radio(['Complex Lines','Simple Lines'], type="value", default='Simple Lines', label='version')],
129
+ gr.outputs.Image(type="pil"), title=title, examples=examples)
130
+
131
+ iface.launch()