File size: 982 Bytes
3a330e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Layer 1 — Image source type (CLIP zero-shot)."""

SOURCE_LABELS = [
    "real photograph",
    "AI-generated image",
    "digital screenshot",
    "3D render or CGI",
]

UNCERTAIN_THRESHOLD = 0.45


def classify_source_type(image, clip_pipe):
    result = clip_pipe(image, candidate_labels=SOURCE_LABELS)
    top = result[0]
    label = top["label"]
    score = float(top["score"])

    label_map = {
        "real photograph": "Real photograph",
        "AI-generated image": "AI-generated image",
        "digital screenshot": "Digital screenshot",
        "3D render or CGI": "3D render or CGI",
    }
    source_type = label_map.get(label, label)
    uncertain = score < UNCERTAIN_THRESHOLD
    if uncertain:
        source_type = "Uncertain"

    return {
        "source_type": source_type,
        "confidence": round(score, 4),
        "uncertain": uncertain,
        "raw_label": label,
        "raw_scores": {r["label"]: float(r["score"]) for r in result[:4]},
    }