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chvision import models, transforms
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
import cv2
|
| 9 |
+
from scipy import stats
|
| 10 |
+
import requests
|
| 11 |
+
from io import BytesIO
|
| 12 |
+
import base64
|
| 13 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 14 |
+
from pydantic import BaseModel
|
| 15 |
+
import uvicorn
|
| 16 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 17 |
+
import json
|
| 18 |
+
import warnings
|
| 19 |
+
import threading
|
| 20 |
+
import time
|
| 21 |
+
warnings.filterwarnings('ignore')
|
| 22 |
+
|
| 23 |
+
# ==================== KEEP-ALIVE SERVICE ====================
|
| 24 |
+
|
| 25 |
+
def keep_alive_ping():
|
| 26 |
+
"""Ping the space every 20 minutes to prevent sleeping"""
|
| 27 |
+
def ping():
|
| 28 |
+
time.sleep(10) # Wait for app to start
|
| 29 |
+
while True:
|
| 30 |
+
try:
|
| 31 |
+
# Ping your own space
|
| 32 |
+
requests.get("https://huijio-zeracap2.hf.space/api/health", timeout=10)
|
| 33 |
+
print("π Keep-alive ping sent - Preventing sleep")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"β Keep-alive ping failed: {e}")
|
| 36 |
+
time.sleep(1200) # 20 minutes
|
| 37 |
+
|
| 38 |
+
# Start in background thread
|
| 39 |
+
thread = threading.Thread(target=ping, daemon=True)
|
| 40 |
+
thread.start()
|
| 41 |
+
print("β
Keep-alive service started")
|
| 42 |
+
|
| 43 |
+
# ==================== MODEL DEFINITIONS ====================
|
| 44 |
+
|
| 45 |
+
class DualPathSiamese(nn.Module):
|
| 46 |
+
def __init__(self, embedding_dim=256):
|
| 47 |
+
super(DualPathSiamese, self).__init__()
|
| 48 |
+
|
| 49 |
+
# Deep learning path
|
| 50 |
+
resnet = models.resnet50(weights=None)
|
| 51 |
+
self.cnn_backbone = nn.Sequential(*list(resnet.children())[:-1])
|
| 52 |
+
self.cnn_embedding = nn.Sequential(
|
| 53 |
+
nn.Linear(2048, 512),
|
| 54 |
+
nn.BatchNorm1d(512),
|
| 55 |
+
nn.ReLU(),
|
| 56 |
+
nn.Dropout(0.5),
|
| 57 |
+
nn.Linear(512, embedding_dim)
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Traditional CV path
|
| 61 |
+
self.feature_embedding = nn.Sequential(
|
| 62 |
+
nn.Linear(29, 128),
|
| 63 |
+
nn.BatchNorm1d(128),
|
| 64 |
+
nn.ReLU(),
|
| 65 |
+
nn.Dropout(0.3),
|
| 66 |
+
nn.Linear(128, 64)
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Fusion layer
|
| 70 |
+
self.fusion = nn.Sequential(
|
| 71 |
+
nn.Linear(embedding_dim + 64, 256),
|
| 72 |
+
nn.BatchNorm1d(256),
|
| 73 |
+
nn.ReLU(),
|
| 74 |
+
nn.Dropout(0.3),
|
| 75 |
+
nn.Linear(256, embedding_dim)
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
def forward_once(self, img, features):
|
| 79 |
+
cnn_out = self.cnn_backbone(img)
|
| 80 |
+
cnn_out = cnn_out.view(cnn_out.size(0), -1)
|
| 81 |
+
cnn_embed = self.cnn_embedding(cnn_out)
|
| 82 |
+
|
| 83 |
+
feat_embed = self.feature_embedding(features)
|
| 84 |
+
combined = torch.cat([cnn_embed, feat_embed], dim=1)
|
| 85 |
+
output = self.fusion(combined)
|
| 86 |
+
return F.normalize(output, p=2, dim=1)
|
| 87 |
+
|
| 88 |
+
def forward(self, img1, img2, features):
|
| 89 |
+
feat1 = features[:, :29]
|
| 90 |
+
feat2 = features[:, 29:]
|
| 91 |
+
output1 = self.forward_once(img1, feat1)
|
| 92 |
+
output2 = self.forward_once(img2, feat2)
|
| 93 |
+
return output1, output2
|
| 94 |
+
|
| 95 |
+
class EnsembleSiamese:
|
| 96 |
+
def __init__(self, device='cpu'):
|
| 97 |
+
self.device = device
|
| 98 |
+
self.models = {}
|
| 99 |
+
self.model_names = ['dualpath', 'resnet50', 'efficientnet']
|
| 100 |
+
self.weights = [0.34, 0.33, 0.33]
|
| 101 |
+
self.models_loaded = False
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
# Load DualPath model
|
| 105 |
+
self.models['dualpath'] = DualPathSiamese(embedding_dim=256).to(device)
|
| 106 |
+
|
| 107 |
+
# Load ResNet50 model
|
| 108 |
+
resnet = models.resnet50(weights=None)
|
| 109 |
+
self.models['resnet50'] = self.create_resnet_siamese(resnet, 2048, 256).to(device)
|
| 110 |
+
|
| 111 |
+
# Load EfficientNet model
|
| 112 |
+
from torchvision.models import efficientnet_b3
|
| 113 |
+
efficientnet = efficientnet_b3(weights=None)
|
| 114 |
+
self.models['efficientnet'] = self.create_efficientnet_siamese(efficientnet, 256).to(device)
|
| 115 |
+
|
| 116 |
+
# Load trained weights with proper settings
|
| 117 |
+
self.load_weights()
|
| 118 |
+
self.models_loaded = True
|
| 119 |
+
print("β
Ensemble model initialized successfully!")
|
| 120 |
+
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"β Error initializing models: {e}")
|
| 123 |
+
self.models_loaded = False
|
| 124 |
+
|
| 125 |
+
def create_resnet_siamese(self, resnet, in_features, embedding_dim):
|
| 126 |
+
class ResNetSiam(nn.Module):
|
| 127 |
+
def __init__(self):
|
| 128 |
+
super(ResNetSiam, self).__init__()
|
| 129 |
+
self.backbone = nn.Sequential(*list(resnet.children())[:-1])
|
| 130 |
+
self.embedding = nn.Sequential(
|
| 131 |
+
nn.Linear(in_features, 512),
|
| 132 |
+
nn.BatchNorm1d(512),
|
| 133 |
+
nn.ReLU(),
|
| 134 |
+
nn.Dropout(0.5),
|
| 135 |
+
nn.Linear(512, embedding_dim)
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
def forward_once(self, x):
|
| 139 |
+
x = self.backbone(x)
|
| 140 |
+
x = x.view(x.size(0), -1)
|
| 141 |
+
x = self.embedding(x)
|
| 142 |
+
return F.normalize(x, p=2, dim=1)
|
| 143 |
+
|
| 144 |
+
def forward(self, img1, img2, features=None):
|
| 145 |
+
return self.forward_once(img1), self.forward_once(img2)
|
| 146 |
+
|
| 147 |
+
return ResNetSiam()
|
| 148 |
+
|
| 149 |
+
def create_efficientnet_siamese(self, efficientnet, embedding_dim):
|
| 150 |
+
class EfficientNetSiam(nn.Module):
|
| 151 |
+
def __init__(self):
|
| 152 |
+
super(EfficientNetSiam, self).__init__()
|
| 153 |
+
self.backbone = efficientnet.features
|
| 154 |
+
self.avgpool = nn.AdaptiveAvgPool2d(1)
|
| 155 |
+
self.embedding = nn.Sequential(
|
| 156 |
+
nn.Linear(1536, 512),
|
| 157 |
+
nn.BatchNorm1d(512),
|
| 158 |
+
nn.ReLU(),
|
| 159 |
+
nn.Dropout(0.4),
|
| 160 |
+
nn.Linear(512, embedding_dim)
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
def forward_once(self, x):
|
| 164 |
+
x = self.backbone(x)
|
| 165 |
+
x = self.avgpool(x)
|
| 166 |
+
x = x.view(x.size(0), -1)
|
| 167 |
+
x = self.embedding(x)
|
| 168 |
+
return F.normalize(x, p=2, dim=1)
|
| 169 |
+
|
| 170 |
+
def forward(self, img1, img2, features=None):
|
| 171 |
+
return self.forward_once(img1), self.forward_once(img2)
|
| 172 |
+
|
| 173 |
+
return EfficientNetSiam()
|
| 174 |
+
|
| 175 |
+
def load_weights(self):
|
| 176 |
+
"""Load trained model weights with proper error handling"""
|
| 177 |
+
try:
|
| 178 |
+
# Load DualPath with weights_only=False for compatibility
|
| 179 |
+
dualpath_state = torch.load('ensemble_dualpath.pth', map_location=self.device, weights_only=False)
|
| 180 |
+
self.models['dualpath'].load_state_dict(dualpath_state['model_state_dict'])
|
| 181 |
+
print("β
DualPath weights loaded")
|
| 182 |
+
|
| 183 |
+
# Load ResNet50
|
| 184 |
+
resnet_state = torch.load('ensemble_resnet50.pth', map_location=self.device, weights_only=False)
|
| 185 |
+
self.models['resnet50'].load_state_dict(resnet_state['model_state_dict'])
|
| 186 |
+
print("β
ResNet50 weights loaded")
|
| 187 |
+
|
| 188 |
+
# Load EfficientNet
|
| 189 |
+
efficient_state = torch.load('ensemble_efficientnet.pth', map_location=self.device, weights_only=False)
|
| 190 |
+
self.models['efficientnet'].load_state_dict(efficient_state['model_state_dict'])
|
| 191 |
+
print("β
EfficientNet weights loaded")
|
| 192 |
+
|
| 193 |
+
except Exception as e:
|
| 194 |
+
print(f"β οΈ Partial weight loading error: {e}")
|
| 195 |
+
# Initialize with random weights if loading fails
|
| 196 |
+
for name, model in self.models.items():
|
| 197 |
+
model.apply(self._init_weights)
|
| 198 |
+
print("π Models initialized with random weights")
|
| 199 |
+
|
| 200 |
+
def _init_weights(self, m):
|
| 201 |
+
"""Initialize weights for models"""
|
| 202 |
+
if isinstance(m, nn.Linear):
|
| 203 |
+
torch.nn.init.xavier_uniform_(m.weight)
|
| 204 |
+
if m.bias is not None:
|
| 205 |
+
m.bias.data.fill_(0.01)
|
| 206 |
+
|
| 207 |
+
def extract_handcrafted_features(self, img_array):
|
| 208 |
+
"""Extract traditional CV features from numpy array"""
|
| 209 |
+
if img_array is None:
|
| 210 |
+
return np.zeros(29)
|
| 211 |
+
|
| 212 |
+
try:
|
| 213 |
+
features = []
|
| 214 |
+
|
| 215 |
+
# Color histogram
|
| 216 |
+
for i in range(3):
|
| 217 |
+
hist = cv2.calcHist([img_array], [i], None, [8], [0, 256])
|
| 218 |
+
features.extend(hist.flatten() / (hist.sum() + 1e-6))
|
| 219 |
+
|
| 220 |
+
# HSV features
|
| 221 |
+
hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV)
|
| 222 |
+
features.extend([hsv[:,:,i].mean() for i in range(3)])
|
| 223 |
+
|
| 224 |
+
# Edge density
|
| 225 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 226 |
+
edges = cv2.Canny(gray, 50, 150)
|
| 227 |
+
features.append(edges.sum() / (edges.size + 1e-6))
|
| 228 |
+
|
| 229 |
+
# Texture
|
| 230 |
+
features.append(cv2.Laplacian(gray, cv2.CV_64F).var())
|
| 231 |
+
|
| 232 |
+
return np.array(features, dtype=np.float32)
|
| 233 |
+
except Exception as e:
|
| 234 |
+
print(f"Feature extraction error: {e}")
|
| 235 |
+
return np.zeros(29)
|
| 236 |
+
|
| 237 |
+
def predict_detailed(self, question_img, answer_imgs, threshold=0.312):
|
| 238 |
+
"""Predict similarity with detailed model breakdown"""
|
| 239 |
+
if not self.models_loaded:
|
| 240 |
+
return [{
|
| 241 |
+
'answer_index': i,
|
| 242 |
+
'model_predictions': {
|
| 243 |
+
'dualpath': {'distance': 1.0, 'confidence': 0.0, 'is_match': False},
|
| 244 |
+
'resnet50': {'distance': 1.0, 'confidence': 0.0, 'is_match': False},
|
| 245 |
+
'efficientnet': {'distance': 1.0, 'confidence': 0.0, 'is_match': False}
|
| 246 |
+
},
|
| 247 |
+
'ensemble_confidence': 0.0,
|
| 248 |
+
'ensemble_distance': 1.0,
|
| 249 |
+
'ensemble_match': False,
|
| 250 |
+
'final_decision': False
|
| 251 |
+
} for i in range(len(answer_imgs))]
|
| 252 |
+
|
| 253 |
+
transform = transforms.Compose([
|
| 254 |
+
transforms.Resize((224, 224)),
|
| 255 |
+
transforms.ToTensor(),
|
| 256 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 257 |
+
])
|
| 258 |
+
|
| 259 |
+
all_results = []
|
| 260 |
+
|
| 261 |
+
for answer_idx, answer_img in enumerate(answer_imgs):
|
| 262 |
+
try:
|
| 263 |
+
# Preprocess images
|
| 264 |
+
q_img = transform(question_img.convert('RGB')).unsqueeze(0).to(self.device)
|
| 265 |
+
a_img = transform(answer_img.convert('RGB')).unsqueeze(0).to(self.device)
|
| 266 |
+
|
| 267 |
+
# Extract features
|
| 268 |
+
q_features = self.extract_handcrafted_features(np.array(question_img))
|
| 269 |
+
a_features = self.extract_handcrafted_features(np.array(answer_img))
|
| 270 |
+
features = np.concatenate([q_features, a_features])
|
| 271 |
+
features_tensor = torch.tensor(features, dtype=torch.float32).unsqueeze(0).to(self.device)
|
| 272 |
+
|
| 273 |
+
# Get predictions from all models
|
| 274 |
+
model_predictions = {}
|
| 275 |
+
distances = []
|
| 276 |
+
confidences = []
|
| 277 |
+
|
| 278 |
+
for name, model in self.models.items():
|
| 279 |
+
model.eval()
|
| 280 |
+
with torch.no_grad():
|
| 281 |
+
if name == 'dualpath':
|
| 282 |
+
out1, out2 = model(q_img, a_img, features_tensor)
|
| 283 |
+
else:
|
| 284 |
+
out1, out2 = model(q_img, a_img)
|
| 285 |
+
|
| 286 |
+
dist = F.pairwise_distance(out1, out2)
|
| 287 |
+
confidence = max(0, 100 * (1 - dist.item()))
|
| 288 |
+
|
| 289 |
+
model_predictions[name] = {
|
| 290 |
+
'distance': float(dist.item()),
|
| 291 |
+
'confidence': float(confidence),
|
| 292 |
+
'is_match': bool(dist.item() < threshold)
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
distances.append(dist.item())
|
| 296 |
+
confidences.append(confidence)
|
| 297 |
+
|
| 298 |
+
# Weighted average
|
| 299 |
+
weighted_distance = sum(w * d for w, d in zip(self.weights, distances))
|
| 300 |
+
weighted_confidence = sum(w * c for w, c in zip(self.weights, confidences))
|
| 301 |
+
is_match = weighted_distance < threshold
|
| 302 |
+
|
| 303 |
+
answer_result = {
|
| 304 |
+
'answer_index': answer_idx,
|
| 305 |
+
'model_predictions': model_predictions,
|
| 306 |
+
'ensemble_distance': float(weighted_distance),
|
| 307 |
+
'ensemble_confidence': float(weighted_confidence),
|
| 308 |
+
'ensemble_match': bool(is_match),
|
| 309 |
+
'final_decision': is_match
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
all_results.append(answer_result)
|
| 313 |
+
|
| 314 |
+
except Exception as e:
|
| 315 |
+
print(f"Error processing answer {answer_idx}: {e}")
|
| 316 |
+
# Add fallback result
|
| 317 |
+
all_results.append({
|
| 318 |
+
'answer_index': answer_idx,
|
| 319 |
+
'model_predictions': {
|
| 320 |
+
'dualpath': {'distance': 1.0, 'confidence': 0.0, 'is_match': False},
|
| 321 |
+
'resnet50': {'distance': 1.0, 'confidence': 0.0, 'is_match': False},
|
| 322 |
+
'efficientnet': {'distance': 1.0, 'confidence': 0.0, 'is_match': False}
|
| 323 |
+
},
|
| 324 |
+
'ensemble_confidence': 0.0,
|
| 325 |
+
'ensemble_distance': 1.0,
|
| 326 |
+
'ensemble_match': False,
|
| 327 |
+
'final_decision': False
|
| 328 |
+
})
|
| 329 |
+
|
| 330 |
+
return all_results
|
| 331 |
+
|
| 332 |
+
# ==================== INITIALIZE MODEL ====================
|
| 333 |
+
|
| 334 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 335 |
+
print(f"π§ Using device: {device}")
|
| 336 |
+
ensemble_model = EnsembleSiamese(device=device)
|
| 337 |
+
|
| 338 |
+
# ==================== FASTAPI SETUP ====================
|
| 339 |
+
|
| 340 |
+
app = FastAPI(title="CAPTCHA Solver API", version="1.0")
|
| 341 |
+
|
| 342 |
+
# Add CORS middleware
|
| 343 |
+
app.add_middleware(
|
| 344 |
+
CORSMiddleware,
|
| 345 |
+
allow_origins=["*"],
|
| 346 |
+
allow_credentials=True,
|
| 347 |
+
allow_methods=["*"],
|
| 348 |
+
allow_headers=["*"],
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
class AnswerData(BaseModel):
|
| 352 |
+
captcha_id: str
|
| 353 |
+
image_base64: str
|
| 354 |
+
|
| 355 |
+
class CAPTCHAPredictionRequest(BaseModel):
|
| 356 |
+
question_base64: str
|
| 357 |
+
answers: list[AnswerData]
|
| 358 |
+
|
| 359 |
+
class Base64PredictionRequest(BaseModel):
|
| 360 |
+
question_base64: str
|
| 361 |
+
answers_base64: list[str]
|
| 362 |
+
|
| 363 |
+
def base64_to_image(base64_string):
|
| 364 |
+
"""Convert base64 string to PIL Image"""
|
| 365 |
+
try:
|
| 366 |
+
# Remove data URL prefix if present
|
| 367 |
+
if ',' in base64_string:
|
| 368 |
+
base64_string = base64_string.split(',')[1]
|
| 369 |
+
|
| 370 |
+
image_data = base64.b64decode(base64_string)
|
| 371 |
+
return Image.open(BytesIO(image_data))
|
| 372 |
+
except Exception as e:
|
| 373 |
+
print(f"Error decoding base64: {e}")
|
| 374 |
+
return None
|
| 375 |
+
|
| 376 |
+
@app.post("/api/predict")
|
| 377 |
+
async def api_predict_endpoint(request: CAPTCHAPredictionRequest):
|
| 378 |
+
"""API endpoint that preserves captcha IDs"""
|
| 379 |
+
try:
|
| 380 |
+
print(f"π₯ Received API request: {len(request.answers)} answers with captcha IDs")
|
| 381 |
+
|
| 382 |
+
# Convert base64 to images
|
| 383 |
+
question_img = base64_to_image(request.question_base64)
|
| 384 |
+
if not question_img:
|
| 385 |
+
return {"success": False, "error": "Failed to decode question image"}
|
| 386 |
+
|
| 387 |
+
answer_data = []
|
| 388 |
+
answer_imgs = []
|
| 389 |
+
|
| 390 |
+
for answer in request.answers:
|
| 391 |
+
img = base64_to_image(answer.image_base64)
|
| 392 |
+
if img:
|
| 393 |
+
answer_imgs.append(img)
|
| 394 |
+
answer_data.append({
|
| 395 |
+
'captcha_id': answer.captcha_id,
|
| 396 |
+
'image': img
|
| 397 |
+
})
|
| 398 |
+
print(f"β
Decoded answer with captcha_id: {answer.captcha_id}")
|
| 399 |
+
else:
|
| 400 |
+
print(f"β Failed to decode answer with captcha_id: {answer.captcha_id}")
|
| 401 |
+
|
| 402 |
+
if len(answer_imgs) == 0:
|
| 403 |
+
return {"success": False, "error": "No answer images could be decoded"}
|
| 404 |
+
|
| 405 |
+
# Make prediction
|
| 406 |
+
results = ensemble_model.predict_detailed(question_img, answer_imgs)
|
| 407 |
+
|
| 408 |
+
# Map results back to captcha IDs
|
| 409 |
+
predictions_with_ids = []
|
| 410 |
+
for i, result in enumerate(results):
|
| 411 |
+
if i < len(answer_data):
|
| 412 |
+
predictions_with_ids.append({
|
| 413 |
+
'captcha_id': answer_data[i]['captcha_id'],
|
| 414 |
+
'ensemble_confidence': result['ensemble_confidence'],
|
| 415 |
+
'ensemble_distance': result['ensemble_distance'],
|
| 416 |
+
'ensemble_match': result['ensemble_match'],
|
| 417 |
+
'model_predictions': result['model_predictions']
|
| 418 |
+
})
|
| 419 |
+
|
| 420 |
+
# Find best match
|
| 421 |
+
if predictions_with_ids:
|
| 422 |
+
best_prediction = max(predictions_with_ids, key=lambda x: x['ensemble_confidence'])
|
| 423 |
+
|
| 424 |
+
response_data = {
|
| 425 |
+
'success': True,
|
| 426 |
+
'predictions': predictions_with_ids,
|
| 427 |
+
'best_match': best_prediction['captcha_id'],
|
| 428 |
+
'best_confidence': best_prediction['ensemble_confidence'],
|
| 429 |
+
'best_distance': best_prediction['ensemble_distance'],
|
| 430 |
+
'models_loaded': ensemble_model.models_loaded
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
print(f"β
API Prediction complete. Best match: captcha_id {best_prediction['captcha_id']} with {best_prediction['ensemble_confidence']:.2f}% confidence")
|
| 434 |
+
return response_data
|
| 435 |
+
else:
|
| 436 |
+
return {"success": False, "error": "No valid predictions could be made"}
|
| 437 |
+
|
| 438 |
+
except Exception as e:
|
| 439 |
+
print(f"β API error: {str(e)}")
|
| 440 |
+
return {"success": False, "error": str(e)}
|
| 441 |
+
|
| 442 |
+
@app.post("/predict")
|
| 443 |
+
async def predict_endpoint(request: Base64PredictionRequest):
|
| 444 |
+
"""Alternative endpoint for backward compatibility"""
|
| 445 |
+
try:
|
| 446 |
+
print(f"π₯ Received legacy API request: {len(request.answers_base64)} answers")
|
| 447 |
+
|
| 448 |
+
# Convert to new format
|
| 449 |
+
captcha_request = CAPTCHAPredictionRequest(
|
| 450 |
+
question_base64=request.question_base64,
|
| 451 |
+
answers=[AnswerData(captcha_id=str(i), image_base64=img_base64)
|
| 452 |
+
for i, img_base64 in enumerate(request.answers_base64)]
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
return await api_predict_endpoint(captcha_request)
|
| 456 |
+
|
| 457 |
+
except Exception as e:
|
| 458 |
+
print(f"β Legacy API error: {str(e)}")
|
| 459 |
+
return {"success": False, "error": str(e)}
|
| 460 |
+
|
| 461 |
+
@app.get("/api/health")
|
| 462 |
+
async def health_check():
|
| 463 |
+
return {
|
| 464 |
+
"status": "healthy",
|
| 465 |
+
"models_loaded": ensemble_model.models_loaded,
|
| 466 |
+
"device": device,
|
| 467 |
+
"api_version": "1.0",
|
| 468 |
+
"keep_alive": "active",
|
| 469 |
+
"timestamp": time.time()
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
@app.get("/health")
|
| 473 |
+
async def health_check_alt():
|
| 474 |
+
return await health_check()
|
| 475 |
+
|
| 476 |
+
@app.get("/")
|
| 477 |
+
async def root():
|
| 478 |
+
return {
|
| 479 |
+
"message": "CAPTCHA Solver API is running!",
|
| 480 |
+
"version": "1.0",
|
| 481 |
+
"accuracy": "98.67%",
|
| 482 |
+
"models_loaded": ensemble_model.models_loaded,
|
| 483 |
+
"keep_alive": "enabled",
|
| 484 |
+
"endpoints": {
|
| 485 |
+
"api_predict": "POST /api/predict (recommended)",
|
| 486 |
+
"predict": "POST /predict (legacy)",
|
| 487 |
+
"health": "GET /api/health"
|
| 488 |
+
}
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
# ==================== GRADIO INTERFACE ====================
|
| 492 |
+
|
| 493 |
+
def format_detailed_results(results):
|
| 494 |
+
"""Format results with detailed model breakdown"""
|
| 495 |
+
if not ensemble_model.models_loaded:
|
| 496 |
+
return "β οΈ **MODELS NOT PROPERLY LOADED**\n\nPlease check that all model files are uploaded:\n- ensemble_dualpath.pth\n- ensemble_resnet50.pth\n- ensemble_efficientnet.pth\n\nCurrently using fallback mode with random weights."
|
| 497 |
+
|
| 498 |
+
output = ""
|
| 499 |
+
|
| 500 |
+
# Find best match
|
| 501 |
+
valid_results = [r for r in results if r['ensemble_confidence'] > 0]
|
| 502 |
+
if not valid_results:
|
| 503 |
+
return "β No valid predictions could be made. Please check your images."
|
| 504 |
+
|
| 505 |
+
best_match = max(valid_results, key=lambda x: x['ensemble_confidence'])
|
| 506 |
+
best_index = best_match['answer_index']
|
| 507 |
+
|
| 508 |
+
output += "π― **FINAL PREDICTION RESULTS** π―\n\n"
|
| 509 |
+
output += f"**Best Match: Answer {best_index + 1}** \n"
|
| 510 |
+
output += f"**Overall Confidence: {best_match['ensemble_confidence']:.2f}%** \n"
|
| 511 |
+
output += f"**Distance: {best_match['ensemble_distance']:.4f}** \n"
|
| 512 |
+
output += f"**Match: {'β
YES' if best_match['final_decision'] else 'β NO'}** \n\n"
|
| 513 |
+
|
| 514 |
+
output += "---\n\n"
|
| 515 |
+
output += "**π DETAILED MODEL BREAKDOWN:**\n\n"
|
| 516 |
+
|
| 517 |
+
for result in results:
|
| 518 |
+
output += f"## **Answer {result['answer_index'] + 1}**\n"
|
| 519 |
+
output += f"**Ensemble:** {result['ensemble_confidence']:.2f}% | Distance: {result['ensemble_distance']:.4f} | {'β
MATCH' if result['final_decision'] else 'β NO MATCH'}\n\n"
|
| 520 |
+
|
| 521 |
+
for model_name, prediction in result['model_predictions'].items():
|
| 522 |
+
emoji = "π’" if prediction['is_match'] else "π΄"
|
| 523 |
+
output += f" - **{model_name.upper()}:** {emoji} {prediction['confidence']:.2f}% | Distance: {prediction['distance']:.4f}\n"
|
| 524 |
+
|
| 525 |
+
output += "\n"
|
| 526 |
+
|
| 527 |
+
# Model agreement analysis
|
| 528 |
+
output += "---\n\n"
|
| 529 |
+
output += "**π€ MODEL AGREEMENT ANALYSIS:**\n\n"
|
| 530 |
+
|
| 531 |
+
for result in results:
|
| 532 |
+
matches = sum(1 for pred in result['model_predictions'].values() if pred['is_match'])
|
| 533 |
+
total_models = len(result['model_predictions'])
|
| 534 |
+
agreement = (matches / total_models) * 100
|
| 535 |
+
|
| 536 |
+
consensus_emoji = "π’" if agreement > 66 else "π‘" if agreement > 33 else "π΄"
|
| 537 |
+
output += f"**Answer {result['answer_index'] + 1}:** {consensus_emoji} {matches}/{total_models} models agree ({agreement:.1f}% consensus)\n"
|
| 538 |
+
|
| 539 |
+
return output
|
| 540 |
+
|
| 541 |
+
def predict_captcha_detailed(question_image, *answer_images):
|
| 542 |
+
"""Gradio prediction function with detailed output"""
|
| 543 |
+
# Filter out None images
|
| 544 |
+
answer_imgs = [img for img in answer_images if img is not None]
|
| 545 |
+
|
| 546 |
+
if not question_image or len(answer_imgs) == 0:
|
| 547 |
+
return "β Please upload both question and answer images"
|
| 548 |
+
|
| 549 |
+
try:
|
| 550 |
+
print(f"π Processing: 1 question + {len(answer_imgs)} answers")
|
| 551 |
+
|
| 552 |
+
# Get detailed predictions
|
| 553 |
+
results = ensemble_model.predict_detailed(question_image, answer_imgs)
|
| 554 |
+
|
| 555 |
+
# Format output
|
| 556 |
+
output = format_detailed_results(results)
|
| 557 |
+
|
| 558 |
+
# Add technical details
|
| 559 |
+
output += "\n---\n\n"
|
| 560 |
+
output += "**βοΈ TECHNICAL DETAILS:**\n\n"
|
| 561 |
+
output += f"- **Threshold:** 0.312 (optimized during training)\n"
|
| 562 |
+
output += f"- **Models:** DualPath (CNN + Handcrafted), ResNet50, EfficientNet-B3\n"
|
| 563 |
+
output += f"- **Ensemble Weights:** DualPath(34%), ResNet50(33%), EfficientNet(33%)\n"
|
| 564 |
+
output += f"- **Training Accuracy:** 98.67%\n"
|
| 565 |
+
output += f"- **Device:** {device.upper()}\n"
|
| 566 |
+
output += f"- **Models Loaded:** {'β
YES' if ensemble_model.models_loaded else 'β NO'}\n"
|
| 567 |
+
output += f"- **Keep-Alive:** β
Active (prevents sleeping)\n"
|
| 568 |
+
|
| 569 |
+
return output
|
| 570 |
+
|
| 571 |
+
except Exception as e:
|
| 572 |
+
return f"β Error during prediction: {str(e)}"
|
| 573 |
+
|
| 574 |
+
# ==================== GRADIO UI ====================
|
| 575 |
+
|
| 576 |
+
with gr.Blocks(title="CAPTCHA Solver - Ensemble AI", theme=gr.themes.Soft()) as demo:
|
| 577 |
+
gr.Markdown("""
|
| 578 |
+
# π CAPTCHA Solver - Ensemble Siamese Network
|
| 579 |
+
### **Achieved 98.67% Accuracy during Training**
|
| 580 |
+
|
| 581 |
+
**π Auto Keep-Alive Enabled** - Prevents Hugging Face from sleeping!
|
| 582 |
+
**β±οΈ 60s Timeout** - Extended timeout for better reliability
|
| 583 |
+
|
| 584 |
+
**API Endpoints:**
|
| 585 |
+
- `POST /api/predict` - **Recommended** (with captcha ID support)
|
| 586 |
+
- `POST /predict` - Legacy (order-based)
|
| 587 |
+
- `GET /api/health` - Health check
|
| 588 |
+
""")
|
| 589 |
+
|
| 590 |
+
# Status indicator
|
| 591 |
+
status_text = "β
Models Loaded Successfully" if ensemble_model.models_loaded else "β οΈ Models Not Properly Loaded - Using Fallback Mode"
|
| 592 |
+
gr.Markdown(f"**Status:** {status_text} | **Keep-Alive:** β
Active")
|
| 593 |
+
|
| 594 |
+
with gr.Row():
|
| 595 |
+
with gr.Column(scale=1):
|
| 596 |
+
gr.Markdown("### πΈ Upload Images")
|
| 597 |
+
question = gr.Image(label="Question Image", type="pil", height=200)
|
| 598 |
+
|
| 599 |
+
gr.Markdown("### π― Answer Images")
|
| 600 |
+
with gr.Row():
|
| 601 |
+
answer1 = gr.Image(label="Answer 1", type="pil", height=150)
|
| 602 |
+
answer2 = gr.Image(label="Answer 2", type="pil", height=150)
|
| 603 |
+
with gr.Row():
|
| 604 |
+
answer3 = gr.Image(label="Answer 3", type="pil", height=150)
|
| 605 |
+
answer4 = gr.Image(label="Answer 4", type="pil", height=150)
|
| 606 |
+
with gr.Row():
|
| 607 |
+
answer5 = gr.Image(label="Answer 5", type="pil", height=150)
|
| 608 |
+
|
| 609 |
+
predict_btn = gr.Button("π Analyze CAPTCHA", variant="primary", size="lg")
|
| 610 |
+
|
| 611 |
+
with gr.Column(scale=2):
|
| 612 |
+
gr.Markdown("### π Prediction Results")
|
| 613 |
+
output = gr.Markdown(
|
| 614 |
+
label="Detailed Analysis",
|
| 615 |
+
value="π Upload images and click 'Analyze CAPTCHA' to see predictions here..."
|
| 616 |
+
)
|
| 617 |
+
|
| 618 |
+
# Connect the prediction function
|
| 619 |
+
predict_btn.click(
|
| 620 |
+
fn=predict_captcha_detailed,
|
| 621 |
+
inputs=[question, answer1, answer2, answer3, answer4, answer5],
|
| 622 |
+
outputs=output
|
| 623 |
+
)
|
| 624 |
+
|
| 625 |
+
# ==================== COMBINE GRADIO AND FASTAPI ====================
|
| 626 |
+
|
| 627 |
+
@app.get("/api")
|
| 628 |
+
async def api_info():
|
| 629 |
+
return {
|
| 630 |
+
"message": "CAPTCHA Solver API",
|
| 631 |
+
"version": "1.0",
|
| 632 |
+
"features": [
|
| 633 |
+
"captcha_id_based_matching",
|
| 634 |
+
"ensemble_ai_models",
|
| 635 |
+
"base64_image_support",
|
| 636 |
+
"auto_keep_alive",
|
| 637 |
+
"extended_timeouts"
|
| 638 |
+
],
|
| 639 |
+
"endpoints": {
|
| 640 |
+
"/api/predict": "POST - Main prediction endpoint with captcha ID support",
|
| 641 |
+
"/predict": "POST - Legacy order-based endpoint",
|
| 642 |
+
"/api/health": "GET - Health check",
|
| 643 |
+
"/": "GET - API info"
|
| 644 |
+
}
|
| 645 |
+
}
|
| 646 |
+
|
| 647 |
+
# Mount Gradio app
|
| 648 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 649 |
+
|
| 650 |
+
# ==================== START KEEP-ALIVE & SERVER ====================
|
| 651 |
+
|
| 652 |
+
# Start keep-alive service
|
| 653 |
+
keep_alive_ping()
|
| 654 |
+
|
| 655 |
+
if __name__ == "__main__":
|
| 656 |
+
print("π Starting CAPTCHA Solver API Server...")
|
| 657 |
+
print("β
Keep-Alive Service: ACTIVE (prevents sleeping)")
|
| 658 |
+
print("π API URL: https://huijio-zeracap2.hf.space/api/predict")
|
| 659 |
+
print("π Health Check: https://huijio-zeracap2.hf.space/api/health")
|
| 660 |
+
print("β±οΈ Timeout: 60 seconds")
|
| 661 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, timeout_keep_alive=60)
|