File size: 1,413 Bytes
fb30cff | 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 | import torch
from transformers import CLIPProcessor, CLIPModel
from utils import device
# -------------------------------------------------
# Load CLIP
# -------------------------------------------------
clip_model = CLIPModel.from_pretrained(
"openai/clip-vit-base-patch32"
).to(device)
clip_processor = CLIPProcessor.from_pretrained(
"openai/clip-vit-base-patch32"
)
# -------------------------------------------------
# CLIP Semantic Verification
# -------------------------------------------------
def clip_predict(image):
prompts = [
"A photograph of a real human face.",
"A photograph of an AI-generated deepfake face."
]
inputs = clip_processor(
text=prompts,
images=image,
return_tensors="pt",
padding=True
)
inputs = {
k: v.to(device)
for k, v in inputs.items()
}
with torch.no_grad():
outputs = clip_model(**inputs)
probabilities = outputs.logits_per_image.softmax(dim=1)
real_score = probabilities[0][0].item()
fake_score = probabilities[0][1].item()
if fake_score > real_score:
prediction = "Fake"
confidence = fake_score
else:
prediction = "Real"
confidence = real_score
return {
"prediction": prediction,
"confidence": confidence,
"real_score": real_score,
"fake_score": fake_score
} |