Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,163 +1,182 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import string
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import torchvision.transforms as transforms
|
| 6 |
-
from torch import nn
|
| 7 |
-
import torch.nn.functional as F
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
nn.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
nn.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
self.
|
| 51 |
-
self.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
self.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
#
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import string
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torchvision.transforms as transforms
|
| 6 |
+
from torch import nn
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
|
| 9 |
+
# Device configuration
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
# Constants
|
| 13 |
+
VOCAB_SIZE = 26*2 + 10 # Letters (upper/lower) + digits
|
| 14 |
+
OUTPUT_LENGTH = 5 # 5-character CAPTCHAs
|
| 15 |
+
AFFN_KERNEL = 5
|
| 16 |
+
AFFN_STRIDE = 1
|
| 17 |
+
AFFN_DEPTH = 4
|
| 18 |
+
CRNN_KERNEL = 5
|
| 19 |
+
CRNN_POOL_KERNEL = 2
|
| 20 |
+
CRNN_DROPOUT = 0.3
|
| 21 |
+
CRNN_LATENT = 128
|
| 22 |
+
LSTM_HIDDEN_DIM = 32
|
| 23 |
+
|
| 24 |
+
# Character mapping
|
| 25 |
+
characters = string.ascii_letters + string.digits
|
| 26 |
+
idx_to_char = {i: c for i, c in enumerate(characters)}
|
| 27 |
+
|
| 28 |
+
# --------------------------
|
| 29 |
+
# Original Model Architecture (CRNN+AFFN)
|
| 30 |
+
# --------------------------
|
| 31 |
+
class Encoder(nn.Sequential):
|
| 32 |
+
def __init__(self, n, kernel_size, stride):
|
| 33 |
+
super().__init__(
|
| 34 |
+
nn.Conv2d(4**(n-1), 4**n, kernel_size, stride),
|
| 35 |
+
nn.BatchNorm2d(4**n),
|
| 36 |
+
nn.ReLU()
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
class Decoder(nn.Sequential):
|
| 40 |
+
def __init__(self, n, kernel_size, stride):
|
| 41 |
+
super().__init__(
|
| 42 |
+
nn.ConvTranspose2d(4**n, 4**(n-1), kernel_size, stride),
|
| 43 |
+
nn.BatchNorm2d(4**(n-1)),
|
| 44 |
+
nn.ReLU()
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
class AFFN(nn.Module):
|
| 48 |
+
def __init__(self, n):
|
| 49 |
+
super().__init__()
|
| 50 |
+
self.n = n
|
| 51 |
+
self.alpha = nn.Parameter(torch.randn(n-1))
|
| 52 |
+
self.encoders = nn.ModuleList([Encoder(i, AFFN_KERNEL, AFFN_STRIDE) for i in range(1, n+1)])
|
| 53 |
+
self.decoders = nn.ModuleList([Decoder(i, AFFN_KERNEL, AFFN_STRIDE) for i in range(n, 0, -1)])
|
| 54 |
+
|
| 55 |
+
def forward(self, x):
|
| 56 |
+
residuals = []
|
| 57 |
+
for i, enc in enumerate(self.encoders):
|
| 58 |
+
x = enc(x)
|
| 59 |
+
if i < self.n - 1:
|
| 60 |
+
x = x * (1 - self.alpha[i])
|
| 61 |
+
residuals.append(x * self.alpha[i])
|
| 62 |
+
|
| 63 |
+
for i, dec in enumerate(self.decoders):
|
| 64 |
+
x = dec(x)
|
| 65 |
+
if i < self.n - 1:
|
| 66 |
+
x = x + residuals.pop()
|
| 67 |
+
return x
|
| 68 |
+
|
| 69 |
+
class CRNN(nn.Module):
|
| 70 |
+
def __init__(self):
|
| 71 |
+
super().__init__()
|
| 72 |
+
self.conv1 = nn.Sequential(
|
| 73 |
+
nn.Conv2d(64, 128, CRNN_KERNEL, padding=2),
|
| 74 |
+
nn.BatchNorm2d(128),
|
| 75 |
+
nn.ReLU(),
|
| 76 |
+
nn.MaxPool2d(CRNN_POOL_KERNEL)
|
| 77 |
+
)
|
| 78 |
+
self.conv2 = nn.Sequential(
|
| 79 |
+
nn.Conv2d(128, 256, CRNN_KERNEL, padding=2),
|
| 80 |
+
nn.BatchNorm2d(256),
|
| 81 |
+
nn.ReLU(),
|
| 82 |
+
nn.MaxPool2d(CRNN_POOL_KERNEL)
|
| 83 |
+
)
|
| 84 |
+
self.flatten = nn.Flatten()
|
| 85 |
+
self.dropout = nn.Dropout(CRNN_DROPOUT)
|
| 86 |
+
self.latent_fc = nn.LazyLinear(CRNN_LATENT)
|
| 87 |
+
self.lstm = nn.LSTM(CRNN_LATENT, LSTM_HIDDEN_DIM, batch_first=True)
|
| 88 |
+
self.output_fc = nn.Linear(LSTM_HIDDEN_DIM, VOCAB_SIZE)
|
| 89 |
+
|
| 90 |
+
def forward(self, x):
|
| 91 |
+
x = self.conv1(x)
|
| 92 |
+
x = self.conv2(x)
|
| 93 |
+
x = self.flatten(x)
|
| 94 |
+
x = self.dropout(x)
|
| 95 |
+
x = self.latent_fc(x)
|
| 96 |
+
x = x.unsqueeze(1)
|
| 97 |
+
lstm_out, _ = self.lstm(x)
|
| 98 |
+
return self.output_fc(lstm_out.squeeze(1))
|
| 99 |
+
|
| 100 |
+
class CaptchaCrackNet(nn.Module):
|
| 101 |
+
def __init__(self):
|
| 102 |
+
super().__init__()
|
| 103 |
+
self.affn = AFFN(AFFN_DEPTH)
|
| 104 |
+
self.conv1 = nn.Sequential(
|
| 105 |
+
nn.Conv2d(1, 32, 5, padding=2),
|
| 106 |
+
nn.ReLU(),
|
| 107 |
+
nn.MaxPool2d(2)
|
| 108 |
+
)
|
| 109 |
+
self.conv2 = nn.Sequential(
|
| 110 |
+
nn.Conv2d(32, 48, 5, padding=2),
|
| 111 |
+
nn.ReLU(),
|
| 112 |
+
nn.MaxPool2d(2)
|
| 113 |
+
)
|
| 114 |
+
self.conv3 = nn.Sequential(
|
| 115 |
+
nn.Conv2d(48, 64, 5, padding=2),
|
| 116 |
+
nn.ReLU(),
|
| 117 |
+
nn.MaxPool2d(2)
|
| 118 |
+
)
|
| 119 |
+
self.res = nn.Conv2d(1, 32, 5, stride=2, padding=2)
|
| 120 |
+
self.crnn = CRNN()
|
| 121 |
+
|
| 122 |
+
def forward(self, x):
|
| 123 |
+
x = self.affn(x)
|
| 124 |
+
res_out = self.res(x)
|
| 125 |
+
x = self.conv1(x)
|
| 126 |
+
x = self.conv2(x + res_out)
|
| 127 |
+
x = self.conv3(x)
|
| 128 |
+
return self.crnn(x)
|
| 129 |
+
|
| 130 |
+
# --------------------------
|
| 131 |
+
# Model Loading
|
| 132 |
+
# --------------------------
|
| 133 |
+
def load_model():
|
| 134 |
+
model = CaptchaCrackNet().to(device)
|
| 135 |
+
model.load_state_dict(torch.load('final.pth', map_location=device))
|
| 136 |
+
model.eval()
|
| 137 |
+
return model
|
| 138 |
+
|
| 139 |
+
model = load_model()
|
| 140 |
+
|
| 141 |
+
# --------------------------
|
| 142 |
+
# Prediction Logic
|
| 143 |
+
# --------------------------
|
| 144 |
+
def to_text(pred):
|
| 145 |
+
return ''.join([idx_to_char[i] for i in pred.argmax(dim=1)])
|
| 146 |
+
|
| 147 |
+
def predict(image):
|
| 148 |
+
try:
|
| 149 |
+
# Preprocess
|
| 150 |
+
transform = transforms.Compose([
|
| 151 |
+
transforms.Resize((40, 150)),
|
| 152 |
+
transforms.Grayscale(),
|
| 153 |
+
transforms.ToTensor(),
|
| 154 |
+
transforms.Normalize((0.5,), (0.5,))
|
| 155 |
+
])
|
| 156 |
+
|
| 157 |
+
img_tensor = transform(image).unsqueeze(0).to(device)
|
| 158 |
+
|
| 159 |
+
# Predict
|
| 160 |
+
with torch.no_grad():
|
| 161 |
+
output = model(img_tensor)
|
| 162 |
+
return to_text(output.squeeze(0))
|
| 163 |
+
|
| 164 |
+
except Exception as e:
|
| 165 |
+
return f"Error: {str(e)}"
|
| 166 |
+
|
| 167 |
+
# --------------------------
|
| 168 |
+
# Gradio Interface
|
| 169 |
+
# --------------------------
|
| 170 |
+
iface = gr.Interface(
|
| 171 |
+
fn=predict,
|
| 172 |
+
inputs=gr.Image(type="pil", label="Upload CAPTCHA"),
|
| 173 |
+
outputs=gr.Textbox(label="Predicted Text"),
|
| 174 |
+
title="CAPTCHA CrackNet",
|
| 175 |
+
examples=[
|
| 176 |
+
["examples/example1.png"],
|
| 177 |
+
["examples/example2.png"]
|
| 178 |
+
]
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
if __name__ == "__main__":
|
| 182 |
iface.launch()
|