File size: 1,210 Bytes
8e30b6a
 
 
dcb5a1a
8e30b6a
 
 
dcb5a1a
 
 
 
 
 
 
 
 
 
 
 
 
8e30b6a
 
 
 
 
 
dcb5a1a
 
 
 
 
 
 
 
8e30b6a
 
 
dcb5a1a
8e30b6a
 
 
 
 
dcb5a1a
 
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
import logging
import time
from typing import Dict, Any
from transformers import pipeline

logger = logging.getLogger(__name__)

_text_classifier = None

def _load_model():
    global _text_classifier
    if _text_classifier is None:
        logger.info("Loading XLM-RoBERTa text detector model...")
        _text_classifier = pipeline(
            "text-classification",
            model="yaya36095/xlm-roberta-text-detector",
            device=-1
        )
        logger.info("Text detector model loaded successfully")
    return _text_classifier

async def analyze_text(text: str) -> Dict[str, Any]:
    start_time = time.time()
    
    logger.info(f"Starting text analysis, length: {len(text)} chars")
    
    classifier = _load_model()
    result = classifier(text)
    
    label = result[0]["label"]
    score = result[0]["score"]
    
    is_deepfake = label.lower() == "fake"
    confidence = score
    
    analysis_time = time.time() - start_time
    
    response = {
        "is_deepfake": is_deepfake,
        "confidence": round(confidence, 3),
        "analysis_time": round(analysis_time, 3),
    }
    
    logger.info(f"Text analysis completed. Result: {response}")
    return response