Spaces:
Runtime error
Runtime error
File size: 8,049 Bytes
4fc93b8 8e30b6a 4fc93b8 8e30b6a 4fc93b8 dcb5a1a 6c9d916 dcb5a1a 4fc93b8 8e30b6a 4fc93b8 dcb5a1a 8e30b6a 4fc93b8 8e30b6a 4fc93b8 8e30b6a 4fc93b8 8e30b6a dcb5a1a 6c9d916 dcb5a1a 6c9d916 dcb5a1a 27e6062 dcb5a1a 27e6062 dcb5a1a 27e6062 dcb5a1a 8e30b6a dcb5a1a 8e30b6a dcb5a1a 4fc93b8 8e30b6a dcb5a1a 8e30b6a d7322bf 8e30b6a dcb5a1a 8e30b6a 27e6062 dcb5a1a 27e6062 dcb5a1a 27e6062 dcb5a1a 4fc93b8 8e30b6a dcb5a1a 8e30b6a 4fc93b8 dcb5a1a 4fc93b8 8e30b6a 4fc93b8 8e30b6a dcb5a1a 8e30b6a 4fc93b8 8e30b6a dcb5a1a 8e30b6a 27e6062 dcb5a1a 27e6062 dcb5a1a 27e6062 dcb5a1a 8e30b6a dcb5a1a 8e30b6a dcb5a1a 8e30b6a 4fc93b8 dcb5a1a 8e30b6a 4fc93b8 8e30b6a dcb5a1a 27e6062 dcb5a1a 27e6062 dcb5a1a 27e6062 dcb5a1a 8e30b6a dcb5a1a 8e30b6a dcb5a1a 8e30b6a dcb5a1a 8e30b6a 4fc93b8 8e30b6a | 1 2 3 4 5 6 7 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 | import logging
from fastapi import APIRouter, HTTPException
from app.models.schemas import (
AnalysisRequest,
AnalysisResponse,
ErrorResponse,
HealthResponse,
TextAnalysisRequest,
ImageAnalysisRequest,
VideoAnalysisRequest,
FileAnalysisRequest,
)
from app.services.download import download_file
from app.services.text_analyzer import analyze_text
from app.services.image_analyzer import analyze_image
from app.core.config import get_settings
from app.utils.exceptions import DeepfakeDetectionError
logger = logging.getLogger(__name__)
router = APIRouter()
AVAILABLE_MODELS = {
"text": ["yaya36095/xlm-roberta-text-detector"],
"image": ["capcheck/ai-image-detection"],
"video": [],
"file": [],
}
MAX_CONTENT_SIZES = {
"text": 5000,
"image": 100 * 1024 * 1024,
"video": 100 * 1024 * 1024,
"file": 100 * 1024 * 1024,
}
@router.get(
"/",
response_model=HealthResponse,
tags=["Health"],
summary="Health check endpoint",
)
async def health_check() -> HealthResponse:
settings = get_settings()
logger.info("Health check endpoint accessed")
supported_types = ["text", "image", "video", "file"]
return HealthResponse(
status="ok",
service="Deepfake Detection Service",
version=settings.APP_VERSION,
available_models=AVAILABLE_MODELS,
supported_types=supported_types,
)
@router.post(
"/analyze",
response_model=AnalysisResponse,
responses={
400: {"model": ErrorResponse, "description": "Bad request"},
408: {"model": ErrorResponse, "description": "Request timeout"},
500: {"model": ErrorResponse, "description": "Internal server error"},
},
tags=["Analysis"],
summary="Analyze content for deepfake detection",
)
async def analyze(request: AnalysisRequest) -> AnalysisResponse:
settings = get_settings()
if isinstance(request, TextAnalysisRequest):
content_type = "text"
if len(request.text) > MAX_CONTENT_SIZES["text"]:
raise HTTPException(
status_code=400,
detail=f"Text content exceeds maximum length of {MAX_CONTENT_SIZES['text']} characters"
)
if len(request.text) < 50:
raise HTTPException(
status_code=400,
detail="Text content must be at least 50 characters"
)
if not AVAILABLE_MODELS["text"]:
raise HTTPException(
status_code=400,
detail="No model available for text analysis"
)
model = AVAILABLE_MODELS["text"][0]
logger.info(f"Received text analysis request, length: {len(request.text)} chars, model: {model}")
try:
analysis_result = await analyze_text(request.text)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error(f"Text analysis error: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to analyze text")
logger.info(f"Text analysis completed. Result: {analysis_result}")
return AnalysisResponse(
is_deepfake=analysis_result["is_deepfake"],
confidence=analysis_result["confidence"],
analysis_time=analysis_result["analysis_time"],
model_used=model,
content_type="text",
)
elif isinstance(request, ImageAnalysisRequest):
content_type = "image"
if not AVAILABLE_MODELS["image"]:
raise HTTPException(
status_code=400,
detail="No model available for image analysis"
)
model = AVAILABLE_MODELS["image"][0]
logger.info(f"Received image analysis request for URL: {request.image_url}, model: {model}")
try:
image_bytes = await download_file(str(request.image_url))
if not image_bytes:
raise HTTPException(status_code=500, detail="Failed to download image")
if len(image_bytes) > MAX_CONTENT_SIZES["image"]:
raise HTTPException(
status_code=400,
detail=f"Image size exceeds maximum of {MAX_CONTENT_SIZES['image']} bytes"
)
except DeepfakeDetectionError as e:
raise HTTPException(status_code=e.status_code, detail=e.message)
analysis_result = await analyze_image(image_bytes)
logger.info(f"Image analysis completed. Result: {analysis_result}")
return AnalysisResponse(
is_deepfake=analysis_result["is_deepfake"],
confidence=analysis_result["confidence"],
analysis_time=analysis_result["analysis_time"],
model_used=model,
content_type="image",
)
elif isinstance(request, VideoAnalysisRequest):
content_type = "video"
if not AVAILABLE_MODELS["video"]:
raise HTTPException(
status_code=400,
detail="No model available for video analysis"
)
model = AVAILABLE_MODELS["video"][0]
logger.info(f"Received video analysis request for URL: {request.video_url}, model: {model}")
try:
video_bytes = await download_file(str(request.video_url))
if not video_bytes:
raise HTTPException(status_code=500, detail="Failed to download video")
if len(video_bytes) > MAX_CONTENT_SIZES["video"]:
raise HTTPException(
status_code=400,
detail=f"Video size exceeds maximum of {MAX_CONTENT_SIZES['video']} bytes"
)
except DeepfakeDetectionError as e:
raise HTTPException(status_code=e.status_code, detail=e.message)
analysis_result = await analyze_image(video_bytes)
logger.info(f"Video analysis completed. Result: {analysis_result}")
return AnalysisResponse(
is_deepfake=analysis_result["is_deepfake"],
confidence=analysis_result["confidence"],
analysis_time=analysis_result["analysis_time"],
model_used=model,
content_type="video",
)
elif isinstance(request, FileAnalysisRequest):
content_type = "file"
if not AVAILABLE_MODELS["file"]:
raise HTTPException(
status_code=400,
detail="No model available for file analysis"
)
model = AVAILABLE_MODELS["file"][0]
logger.info(f"Received file analysis request for URL: {request.file_url}, model: {model}")
try:
file_bytes = await download_file(str(request.file_url))
if not file_bytes:
raise HTTPException(status_code=500, detail="Failed to download file")
if len(file_bytes) > MAX_CONTENT_SIZES["file"]:
raise HTTPException(
status_code=400,
detail=f"File size exceeds maximum of {MAX_CONTENT_SIZES['file']} bytes"
)
except DeepfakeDetectionError as e:
raise HTTPException(status_code=e.status_code, detail=e.message)
analysis_result = await analyze_image(file_bytes)
logger.info(f"File analysis completed. Result: {analysis_result}")
return AnalysisResponse(
is_deepfake=analysis_result["is_deepfake"],
confidence=analysis_result["confidence"],
analysis_time=analysis_result["analysis_time"],
model_used=model,
content_type="file",
)
else:
raise HTTPException(status_code=400, detail="Unsupported content type")
|