Gradii commited on
Commit
6c9d916
·
1 Parent(s): 27e6062
backend/app/api/routes.py CHANGED
@@ -23,7 +23,7 @@ router = APIRouter()
23
 
24
  AVAILABLE_MODELS = {
25
  "text": ["yaya36095/xlm-roberta-text-detector"],
26
- "image": [],
27
  "video": [],
28
  "file": [],
29
  }
@@ -80,10 +80,10 @@ async def analyze(request: AnalysisRequest) -> AnalysisResponse:
80
  detail=f"Text content exceeds maximum length of {MAX_CONTENT_SIZES['text']} characters"
81
  )
82
 
83
- if len(request.text) < 10:
84
  raise HTTPException(
85
  status_code=400,
86
- detail="Text content must be at least 10 characters"
87
  )
88
 
89
  if not AVAILABLE_MODELS["text"]:
 
23
 
24
  AVAILABLE_MODELS = {
25
  "text": ["yaya36095/xlm-roberta-text-detector"],
26
+ "image": ["capcheck/ai-image-detection"],
27
  "video": [],
28
  "file": [],
29
  }
 
80
  detail=f"Text content exceeds maximum length of {MAX_CONTENT_SIZES['text']} characters"
81
  )
82
 
83
+ if len(request.text) < 50:
84
  raise HTTPException(
85
  status_code=400,
86
+ detail="Text content must be at least 50 characters"
87
  )
88
 
89
  if not AVAILABLE_MODELS["text"]:
backend/app/services/image_analyzer.py CHANGED
@@ -1,9 +1,54 @@
 
1
  import logging
2
  import time
3
  from typing import Dict, Any
 
 
4
 
5
  logger = logging.getLogger(__name__)
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  async def analyze_image(image_bytes: bytes) -> Dict[str, Any]:
9
- raise NotImplementedError("Image analysis models not yet configured")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
  import logging
3
  import time
4
  from typing import Dict, Any
5
+ from PIL import Image
6
+ from transformers import pipeline
7
 
8
  logger = logging.getLogger(__name__)
9
 
10
+ _image_classifier = None
11
+
12
+ def _load_model():
13
+ global _image_classifier
14
+ if _image_classifier is None:
15
+ logger.info("Loading capcheck/ai-image-detection model...")
16
+ _image_classifier = pipeline(
17
+ "image-classification",
18
+ model="capcheck/ai-image-detection",
19
+ device=-1
20
+ )
21
+ logger.info("Image detector model loaded successfully")
22
+ return _image_classifier
23
 
24
  async def analyze_image(image_bytes: bytes) -> Dict[str, Any]:
25
+ start_time = time.time()
26
+
27
+ logger.info(f"Starting image analysis, size: {len(image_bytes)} bytes")
28
+
29
+ try:
30
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
31
+ except Exception as e:
32
+ logger.error(f"Failed to parse image bytes: {str(e)}")
33
+ raise ValueError("Invalid image format or corrupted bytes") from e
34
+
35
+ classifier = _load_model()
36
+
37
+ result = classifier(image)
38
+
39
+ label = result[0]["label"]
40
+ score = result[0]["score"]
41
+
42
+ is_deepfake = label.lower() == "fake"
43
+ confidence = score
44
+
45
+ analysis_time = time.time() - start_time
46
+
47
+ response = {
48
+ "is_deepfake": is_deepfake,
49
+ "confidence": round(confidence, 3),
50
+ "analysis_time": round(analysis_time, 3),
51
+ }
52
+
53
+ logger.info(f"Image analysis completed. Result: {response}")
54
+ return response
backend/app/services/text_analyzer.py CHANGED
@@ -20,12 +20,6 @@ def _load_model():
20
  return _text_classifier
21
 
22
  async def analyze_text(text: str) -> Dict[str, Any]:
23
- if len(text) > 5000:
24
- raise ValueError("Text content exceeds maximum length of 5000 characters")
25
-
26
- if len(text) < 10:
27
- raise ValueError("Text content must be at least 10 characters")
28
-
29
  start_time = time.time()
30
 
31
  logger.info(f"Starting text analysis, length: {len(text)} chars")
 
20
  return _text_classifier
21
 
22
  async def analyze_text(text: str) -> Dict[str, Any]:
 
 
 
 
 
 
23
  start_time = time.time()
24
 
25
  logger.info(f"Starting text analysis, length: {len(text)} chars")
backend/requirements.txt CHANGED
@@ -9,3 +9,4 @@ torch==2.3.1
9
  numpy==1.26.4
10
  sentencepiece
11
  protobuf
 
 
9
  numpy==1.26.4
10
  sentencepiece
11
  protobuf
12
+ Pillow