File size: 1,145 Bytes
3ca686a
b8aa51a
3ca686a
b8aa51a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from .model_loader import get_model

# Thresholds
AI_THRESHOLD = 0.55
HUMAN_THRESHOLD = 0.45


def classify_image(image_array: np.ndarray) -> dict:
    try:
        model = get_model()
        predictions = model.predict(image_array)

        if predictions.ndim != 2 or predictions.shape[1] != 1:
            raise ValueError(
                "Model output shape is invalid. Expected shape: (batch, 1)"
            )

        ai_conf = float(np.clip(predictions[0][0], 0.0, 1.0))
        human_conf = 1.0 - ai_conf

        # Classification logic
        if ai_conf > AI_THRESHOLD:
            label = "AI Generated"
        elif ai_conf < HUMAN_THRESHOLD:
            label = "Human Generated"
        else:
            label = "Uncertain (Maybe AI)"

        return {
            "label": label,
            "ai_confidence": round(ai_conf * 100, 2),
            "human_confidence": round(human_conf * 100, 2),
        }

    except Exception as e:
        return {
            "error": str(e),
            "label": "Classification Failed",
            "ai_confidence": None,
            "human_confidence": None,
        }