Spotix-API / backend /app /core /processor.py
Anish-530
Fixed Mobile support. Added a new AI media detection mechanism by Farid, that creates geometric lines. Fixed logs
817ad83
import time
import uuid
from sqlalchemy.orm import Session
from app.models.file_model import File
from app.ai.nsfw_detector import detect_ai_image
from app.ai.frequency_detector import frequency_score
from app.ai.cnn_detector import cnn_artifact_score
from app.ai.hybrid_detector import hybrid_decision
from app.ai.meta_classifier import predict_ai
from app.ai.explainer import generate_heatmap
from app.ai.reasoning import generate_reasoning
from app.ai.model_loader import model_loader
from app.ai.feature_extractor import extract_features
from app.ai.attribution import generate_attribution
from app.ai.explanation_engine import generated_structured_explanation
from app.ai.explanation_formatter import format_explanation_with_llm
from app.ai.geometry_detector import analyze_perspective
from app.core.storage import active_storage
import os
def process_file(file_id: int, db: Session):
file = db.query(File).filter(File.id == file_id).first()
if not file:
return
local_path = active_storage.download_to_temp(file.filepath)
safe_heatmap_name = f"{uuid.uuid4().hex}.png"
safe_geometry_name = f"geom_{uuid.uuid4().hex}.png"
os.makedirs("uploads/heatmaps", exist_ok=True)
local_heatmap_path = f"uploads/heatmaps/{safe_heatmap_name}"
local_geometry_path = f"uploads/heatmaps/{safe_geometry_name}"
file.status = "PROCESSING"
active_version = model_loader.get_latest_model_version()
file.model_version_used = active_version
db.commit()
try:
nsfw_result = detect_ai_image(local_path)
features = extract_features(local_path)
label, prob = predict_ai(features["frequency_score"], features["cnn_score"])
attribution_data = generate_attribution(local_path, local_heatmap_path)
# Geometry Perspective Analysis
geom_result = analyze_perspective(local_path, local_geometry_path)
features["geometry_score"] = geom_result["score"]
features["geometry_message"] = geom_result["message"]
class MockFile:
def __init__(self, f):
self.file = f
self.content_type = "image/png"
with open(local_heatmap_path, "rb") as hf:
mock_hf = MockFile(hf)
r2_heatmap_key = active_storage.save(mock_hf, f"heatmaps/{safe_heatmap_name}")
if os.path.exists(local_geometry_path):
with open(local_geometry_path, "rb") as gf:
mock_gf = MockFile(gf)
r2_geometry_key = active_storage.save(mock_gf, f"heatmaps/{safe_geometry_name}")
file.geometry_path = r2_geometry_key
file.geometry_score = geom_result["score"]
file.heatmap_path = r2_heatmap_key
structured_reasoning = generated_structured_explanation(features, prob)
natural_reasoning = format_explanation_with_llm(structured_reasoning)
file.result = f"{label}\nFREQ:{features['frequency_score']:.2f}\nCNN:{features['cnn_score']:.2f}\nNSFW: {nsfw_result}"
file.confidence = float(prob * 100)
file.ai_explanation = natural_reasoning
file.status = "COMPLETED"
except Exception as e:
file.status = "FAILED"
file.result = str(e)
finally:
if 'local_path' in locals() and os.path.exists(local_path) and getattr(active_storage, '__class__').__name__ == "R2StorageProvider":
os.remove(local_path)
if 'local_heatmap_path' in locals() and os.path.exists(local_heatmap_path):
os.remove(local_heatmap_path)
if 'local_geometry_path' in locals() and os.path.exists(local_geometry_path):
os.remove(local_geometry_path)
db.commit()
db.close()