Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,6 @@ import torch
|
|
| 2 |
import gradio as gr
|
| 3 |
from torch import nn
|
| 4 |
import torch.nn.functional as F
|
| 5 |
-
#from torchsummary import summary
|
| 6 |
-
from tqdm import tqdm
|
| 7 |
import os
|
| 8 |
from torchvision import transforms
|
| 9 |
from torch.utils.data import DataLoader,random_split,Dataset
|
|
@@ -132,7 +130,7 @@ class CRNN(nn.Module):
|
|
| 132 |
outputs = torch.stack(outputs, dim=1) # Shape: (batch_size, 5, vocab_size)
|
| 133 |
|
| 134 |
return outputs
|
| 135 |
-
|
| 136 |
|
| 137 |
|
| 138 |
class CaptchaCrackNet(nn.Module):
|
|
@@ -178,13 +176,6 @@ optimizer=torch.optim.Adam(model.parameters())
|
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
-
# checkpoint = torch.load(r'model/final.pth', map_location=device)
|
| 182 |
-
# # Restore states
|
| 183 |
-
# #print("Checkpoint keys:", checkpoint.keys())
|
| 184 |
-
# model.load_state_dict(checkpoint['model_state_dict'])
|
| 185 |
-
|
| 186 |
-
# optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
|
| 187 |
-
# model.eval()
|
| 188 |
characters = string.ascii_letters + string.digits
|
| 189 |
idx_to_char = {idx: char for idx, char in enumerate(characters)}
|
| 190 |
|
|
@@ -199,9 +190,19 @@ def predict_captcha(image):
|
|
| 199 |
if image is None:
|
| 200 |
return "No image provided"
|
| 201 |
|
| 202 |
-
#
|
| 203 |
-
if
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
# Process image
|
| 207 |
transform = transforms.Compose([
|
|
@@ -223,12 +224,15 @@ def predict_captcha(image):
|
|
| 223 |
except Exception as e:
|
| 224 |
print(f"Error details: {str(e)}")
|
| 225 |
return f"Error processing image: {str(e)}"
|
| 226 |
-
|
| 227 |
# Ensure model is loaded and set to eval mode
|
|
|
|
| 228 |
checkpoint = torch.load(r'model/final.pth', map_location=device)
|
|
|
|
|
|
|
| 229 |
model.load_state_dict(checkpoint['model_state_dict'])
|
| 230 |
-
model.eval() # Set the model to evaluation mode after loading the state
|
| 231 |
|
|
|
|
|
|
|
| 232 |
|
| 233 |
# Update Gradio interface
|
| 234 |
iface = gr.Interface(
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from torch import nn
|
| 4 |
import torch.nn.functional as F
|
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
from torchvision import transforms
|
| 7 |
from torch.utils.data import DataLoader,random_split,Dataset
|
|
|
|
| 130 |
outputs = torch.stack(outputs, dim=1) # Shape: (batch_size, 5, vocab_size)
|
| 131 |
|
| 132 |
return outputs
|
| 133 |
+
output=CRNN(64,CRNN_KERNEL,CRNN_POOL_KERNEL,CRNN_DROPOUT,CRNN_LATENT,LSTM_HIDDEN_DIM,VOCAB_SIZE,OUTPUT_LENGTH).to(device)(torch.zeros((2,64,256,256)).to(device))
|
| 134 |
|
| 135 |
|
| 136 |
class CaptchaCrackNet(nn.Module):
|
|
|
|
| 176 |
|
| 177 |
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
characters = string.ascii_letters + string.digits
|
| 180 |
idx_to_char = {idx: char for idx, char in enumerate(characters)}
|
| 181 |
|
|
|
|
| 190 |
if image is None:
|
| 191 |
return "No image provided"
|
| 192 |
|
| 193 |
+
# Handle Gradio's FileData input: dict with 'data' and 'meta'
|
| 194 |
+
if isinstance(image, dict) and 'data' in image:
|
| 195 |
+
image = image['data']
|
| 196 |
+
|
| 197 |
+
# Convert to PIL.Image
|
| 198 |
+
if isinstance(image, str) and image.startswith('data:image'):
|
| 199 |
+
import base64
|
| 200 |
+
from io import BytesIO
|
| 201 |
+
image_data = base64.b64decode(image.split(',')[1])
|
| 202 |
+
image = Image.open(BytesIO(image_data))
|
| 203 |
+
elif not isinstance(image, Image.Image):
|
| 204 |
+
from io import BytesIO
|
| 205 |
+
image = Image.open(BytesIO(image))
|
| 206 |
|
| 207 |
# Process image
|
| 208 |
transform = transforms.Compose([
|
|
|
|
| 224 |
except Exception as e:
|
| 225 |
print(f"Error details: {str(e)}")
|
| 226 |
return f"Error processing image: {str(e)}"
|
|
|
|
| 227 |
# Ensure model is loaded and set to eval mode
|
| 228 |
+
|
| 229 |
checkpoint = torch.load(r'model/final.pth', map_location=device)
|
| 230 |
+
# Restore states
|
| 231 |
+
print("Checkpoint keys:", checkpoint.keys())
|
| 232 |
model.load_state_dict(checkpoint['model_state_dict'])
|
|
|
|
| 233 |
|
| 234 |
+
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
|
| 235 |
+
model.eval()
|
| 236 |
|
| 237 |
# Update Gradio interface
|
| 238 |
iface = gr.Interface(
|