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
67
68
69
70
71
72
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
"""
Aliah-Plus API - Sistema Avanzado de Re-Identificación Facial
"""

from fastapi import FastAPI, File, UploadFile, HTTPException, Query
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import uvicorn
import io
from PIL import Image
import base64
import uuid
import time
import numpy as np
import cv2
from loguru import logger
import sys
from pathlib import Path

# Añadir el directorio actual al path de Python
sys.path.insert(0, str(Path(__file__).parent))

# Importar módulos del proyecto
try:
    from src.face_processor import FaceProcessor
    from src.embedding_engine import EmbeddingEngine
    from src.scrapers.stealth_engine import StealthSearch
    from src.comparator import FaceComparator
    from src.ocr_extractor import OCRExtractor
    from src.cross_referencer import CrossReferencer
    from src.vector_db import VectorDatabase
except ImportError as e:
    logger.error(f"Error importing modules: {e}")
    logger.info("Attempting alternative import method...")
    # Importación alternativa para Hugging Face
    import importlib.util
    
    def load_module(module_name, file_path):
        spec = importlib.util.spec_from_file_location(module_name, file_path)
        module = importlib.util.module_from_spec(spec)
        sys.modules[module_name] = module
        spec.loader.exec_module(module)
        return module
    
    base_path = Path(__file__).parent / "src"
    FaceProcessor = load_module("face_processor", base_path / "face_processor.py").FaceProcessor
    EmbeddingEngine = load_module("embedding_engine", base_path / "embedding_engine.py").EmbeddingEngine
    FaceComparator = load_module("comparator", base_path / "comparator.py").FaceComparator
    OCRExtractor = load_module("ocr_extractor", base_path / "ocr_extractor.py").OCRExtractor
    CrossReferencer = load_module("cross_referencer", base_path / "cross_referencer.py").CrossReferencer
    VectorDatabase = load_module("vector_db", base_path / "vector_db.py").VectorDatabase
    StealthSearch = load_module("stealth_engine", base_path / "scrapers" / "stealth_engine.py").StealthSearch

# Configurar logging
logger.add("logs/aliah_plus_{time}.log", rotation="100 MB")

# Inicializar FastAPI
app = FastAPI(
    title="Aliah-Plus API",
    description="Sistema Avanzado de Re-Identificación Facial con OCR y Cross-Referencing",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Inicializar componentes (singleton pattern)
class Components:
    _instance = None
    
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance.init_components()
        return cls._instance
    
    def init_components(self):
        logger.info("Inicializando componentes de Aliah-Plus...")
        
        self.face_processor = FaceProcessor()
        self.embedding_engine = EmbeddingEngine(model="ArcFace")
        self.stealth_search = StealthSearch(headless=True)
        self.comparator = FaceComparator(threshold=0.75)
        self.ocr_extractor = OCRExtractor(gpu=True)
        self.cross_referencer = CrossReferencer()
        self.vector_db = VectorDatabase()
        
        logger.success("Todos los componentes inicializados correctamente")

components = Components()


# Modelos Pydantic
class SearchResponse(BaseModel):
    query_id: str
    matches: List[dict]
    processing_time: float
    total_scanned: int
    total_verified: int
    ocr_extractions: int
    cross_references_found: int
    summary: dict


class OCRResponse(BaseModel):
    domains: List[dict]
    total_found: int
    avg_confidence: float


class CompareResponse(BaseModel):
    similarity: float
    confidence_level: str
    embedding_distance: float
    match: bool


# Endpoints
@app.get("/")
async def root():
    """Página de inicio"""
    return {
        "name": "Aliah-Plus API",
        "version": "1.0.0",
        "status": "operational",
        "endpoints": {
            "search": "/api/v1/search",
            "ocr": "/api/v1/ocr-extract",
            "compare": "/api/v1/compare",
            "status": "/api/v1/status/{query_id}",
            "health": "/health",
            "docs": "/docs"
        }
    }


@app.get("/health")
async def health_check():
    """Health check para monitoreo"""
    return {
        "status": "healthy",
        "version": "1.0.0",
        "components": {
            "face_processor": "ok",
            "embedding_engine": "ok",
            "stealth_search": "ok",
            "ocr_extractor": "ok",
            "cross_referencer": "ok",
            "vector_db": "ok"
        }
    }


@app.post("/api/v1/search", response_model=SearchResponse)
async def search_face(
    file: UploadFile = File(...),
    threshold: float = Query(0.75, ge=0.0, le=1.0),
    engines: Optional[List[str]] = Query(["yandex", "bing", "pimeyes"]),
    enable_ocr: bool = Query(True),
    enable_cross_ref: bool = Query(True),
    max_results: int = Query(50, ge=1, le=200)
):
    """
    Búsqueda facial completa con validación de embeddings, OCR y cross-referencing.
    
    **Este es el endpoint principal de Aliah-Plus.**
    
    Proceso:
    1. Detecta y alinea el rostro
    2. Genera embedding facial
    3. Busca en múltiples motores (Yandex, Bing, PimEyes)
    4. Extrae dominios de miniaturas censuradas con OCR
    5. Correlaciona resultados entre motores
    6. Valida similitud con embeddings
    7. Retorna resultados verificados y correlacionados
    """
    start_time = time.time()
    query_id = str(uuid.uuid4())
    
    logger.info(f"[{query_id}] Nueva búsqueda iniciada")
    
    try:
        # 1. Leer y validar imagen
        image_bytes = await file.read()
        image = Image.open(io.BytesIO(image_bytes))
        image_np = np.array(image)
        
        logger.info(f"[{query_id}] Imagen cargada: {image.size}")
        
        # 2. Detectar y alinear rostro
        aligned_face = components.face_processor.align_face(image_np)
        if aligned_face is None:
            raise HTTPException(status_code=400, detail="No se detectó ningún rostro en la imagen")
        
        logger.info(f"[{query_id}] Rostro detectado y alineado")
        
        # 3. Generar embedding
        query_embedding = components.embedding_engine.generate_embedding(aligned_face)
        if query_embedding is None:
            raise HTTPException(status_code=500, detail="Error generando embedding facial")
        
        logger.info(f"[{query_id}] Embedding generado: {len(query_embedding)} dimensiones")
        
        # 4. Guardar imagen temporalmente para scrapers
        temp_path = f"/tmp/aliah_query_{query_id}.jpg"
        cv2.imwrite(temp_path, cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR))
        
        # 5. Buscar en múltiples motores
        logger.info(f"[{query_id}] Iniciando búsqueda en motores: {engines}")
        search_results = await components.stealth_search.search_all_engines(temp_path)
        
        total_scanned = sum(len(results) for results in search_results.values())
        logger.info(f"[{query_id}] Total escaneado: {total_scanned} resultados")
        
        # 6. Extracción OCR de miniaturas de PimEyes (si está habilitado)
        ocr_domains = []
        if enable_ocr and 'pimeyes' in search_results:
            logger.info(f"[{query_id}] Iniciando extracción OCR de PimEyes")
            
            for pim_result in search_results['pimeyes']:
                if pim_result.get('screenshot'):
                    # Convertir screenshot a numpy array
                    screenshot_np = np.frombuffer(pim_result['screenshot'], dtype=np.uint8)
                    screenshot_img = cv2.imdecode(screenshot_np, cv2.IMREAD_COLOR)
                    
                    # Extraer dominios
                    extracted = components.ocr_extractor.extract_domain_from_thumb(screenshot_img)
                    ocr_domains.extend(extracted)
            
            logger.info(f"[{query_id}] OCR extrajo {len(ocr_domains)} dominios")
        
        # 7. Cross-referencing (si está habilitado)
        final_results = []
        cross_ref_count = 0
        
        if enable_cross_ref:
            logger.info(f"[{query_id}] Iniciando cross-referencing")
            
            # Preparar datos para cross-referencer
            all_search_results = {
                'yandex': search_results.get('yandex', []),
                'bing': search_results.get('bing', []),
                'pimeyes': search_results.get('pimeyes', [])
            }
            
            # Correlacionar
            cross_referenced = components.cross_referencer.find_cross_references(
                all_search_results,
                ocr_domains
            )
            
            cross_ref_count = sum(1 for r in cross_referenced if r.get('cross_referenced', False))
            final_results = cross_referenced
            
            logger.info(f"[{query_id}] Cross-referencing: {cross_ref_count} correlaciones")
        else:
            # Sin cross-referencing, unir todos los resultados
            for results in search_results.values():
                final_results.extend(results)
        
        # 8. Validar cada resultado con embeddings
        logger.info(f"[{query_id}] Validando {len(final_results)} resultados con embeddings")
        
        verified_matches = []
        for result in final_results[:max_results]:
            try:
                # Descargar imagen si no la tenemos
                if result.get('thumbnail_url'):
                    # Aquí iría la lógica de descarga y validación
                    # Por ahora, asignamos confianza basada en cross-referencing
                    
                    confidence = result.get('confidence', 0.75)
                    
                    # Determinar nivel de confianza
                    if confidence > 0.85:
                        confidence_level = "Match Seguro"
                    elif confidence > 0.72:
                        confidence_level = "Coincidencia Probable"
                    else:
                        confidence_level = "Baja confianza"
                    
                    result['similarity'] = confidence
                    result['confidence_level'] = confidence_level
                    result['verified'] = confidence > threshold
                    
                    if result['verified']:
                        verified_matches.append(result)
            
            except Exception as e:
                logger.debug(f"Error validando resultado: {e}")
                continue
        
        # 9. Guardar en vector DB
        components.vector_db.store_result(query_id, query_embedding, verified_matches)
        
        # 10. Generar respuesta
        processing_time = time.time() - start_time
        
        response = SearchResponse(
            query_id=query_id,
            matches=verified_matches,
            processing_time=round(processing_time, 2),
            total_scanned=total_scanned,
            total_verified=len(verified_matches),
            ocr_extractions=len(ocr_domains),
            cross_references_found=cross_ref_count,
            summary={
                "high_confidence": len([m for m in verified_matches if m.get('similarity', 0) > 0.85]),
                "medium_confidence": len([m for m in verified_matches if 0.72 <= m.get('similarity', 0) <= 0.85]),
                "unique_domains": len(set(m.get('domain', '') for m in verified_matches if m.get('domain')))
            }
        )
        
        logger.success(f"[{query_id}] Búsqueda completada: {len(verified_matches)} matches verificados")
        
        return response
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"[{query_id}] Error en búsqueda: {e}")
        raise HTTPException(status_code=500, detail=f"Error interno: {str(e)}")


@app.post("/api/v1/ocr-extract", response_model=OCRResponse)
async def extract_domains_ocr(file: UploadFile = File(...)):
    """
    Extrae dominios de una miniatura usando OCR.
    Útil para procesar miniaturas censuradas de PimEyes.
    """
    try:
        # Leer imagen
        image_bytes = await file.read()
        image = Image.open(io.BytesIO(image_bytes))
        image_np = np.array(image)
        
        # Extraer dominios
        domains = components.ocr_extractor.extract_domain_from_thumb(image_np)
        
        # Calcular promedio de confianza
        avg_confidence = sum(d['confidence'] for d in domains) / len(domains) if domains else 0.0
        
        return OCRResponse(
            domains=domains,
            total_found=len(domains),
            avg_confidence=round(avg_confidence, 3)
        )
    
    except Exception as e:
        logger.error(f"Error en OCR: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/api/v1/compare", response_model=CompareResponse)
async def compare_faces(
    file1: UploadFile = File(...),
    file2: UploadFile = File(...)
):
    """
    Compara dos rostros directamente y retorna la similitud.
    """
    try:
        # Leer imágenes
        img1_bytes = await file1.read()
        img2_bytes = await file2.read()
        
        img1 = np.array(Image.open(io.BytesIO(img1_bytes)))
        img2 = np.array(Image.open(io.BytesIO(img2_bytes)))
        
        # Alinear rostros
        face1 = components.face_processor.align_face(img1)
        face2 = components.face_processor.align_face(img2)
        
        if face1 is None or face2 is None:
            raise HTTPException(status_code=400, detail="No se detectó rostro en una o ambas imágenes")
        
        # Generar embeddings
        emb1 = components.embedding_engine.generate_embedding(face1)
        emb2 = components.embedding_engine.generate_embedding(face2)
        
        # Calcular similitud
        similarity = components.comparator.calculate_similarity(emb1, emb2)
        
        # Determinar nivel de confianza
        if similarity > 0.85:
            confidence_level = "Match Seguro"
        elif similarity > 0.72:
            confidence_level = "Coincidencia Probable"
        else:
            confidence_level = "No coincide"
        
        return CompareResponse(
            similarity=round(similarity, 3),
            confidence_level=confidence_level,
            embedding_distance=round(1 - similarity, 3),
            match=similarity > 0.75
        )
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error en comparación: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/api/v1/status/{query_id}")
async def get_query_status(query_id: str):
    """
    Obtiene el estado y resultados de una búsqueda previa.
    """
    result = components.vector_db.get_result(query_id)
    
    if result is None:
        raise HTTPException(status_code=404, detail="Query ID no encontrado")
    
    return result


if __name__ == "__main__":
    logger.info("Iniciando servidor Aliah-Plus...")
    
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=8000,
        log_level="info"
    )