Spaces:
Running
Running
testing version
Browse files
app.py
CHANGED
@@ -16,48 +16,31 @@ processor = ViTImageProcessor.from_pretrained("prithivMLmods/Deep-Fake-Detector-
|
|
16 |
logger.info(f"Model label mapping: {model.config.id2label}")
|
17 |
|
18 |
def detect(image, confidence_threshold=0.5):
|
19 |
-
"""Detect deepfake content using prithivMLmods/Deep-Fake-Detector-v2-Model"""
|
20 |
if image is None:
|
21 |
raise gr.Error("Please upload an image to analyze")
|
22 |
|
23 |
try:
|
24 |
-
# Convert Gradio image (filepath) to PIL Image
|
25 |
pil_image = Image.open(image).convert("RGB")
|
26 |
-
|
27 |
-
# Resize to match ViT input requirements (224x224)
|
28 |
pil_image = pil_image.resize((224, 224), Image.Resampling.LANCZOS)
|
29 |
-
|
30 |
-
# Preprocess the image
|
31 |
inputs = processor(images=pil_image, return_tensors="pt")
|
32 |
|
33 |
-
# Perform inference
|
34 |
with torch.no_grad():
|
35 |
outputs = model(**inputs)
|
36 |
logits = outputs.logits
|
37 |
probabilities = torch.softmax(logits, dim=1)[0]
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
confidence_fake = probabilities[1].item() * 100 # Assuming 1 is Fake
|
42 |
|
43 |
-
# Verify label mapping from model config
|
44 |
id2label = model.config.id2label
|
45 |
predicted_class = torch.argmax(logits, dim=1).item()
|
46 |
predicted_label = id2label[predicted_class]
|
47 |
-
|
48 |
-
# Adjust prediction based on threshold and label
|
49 |
threshold_predicted = "Fake" if confidence_fake / 100 >= confidence_threshold else "Real"
|
50 |
confidence_score = max(confidence_real, confidence_fake)
|
51 |
|
52 |
-
#
|
53 |
-
logger.info(f"Logits: {logits.tolist()}")
|
54 |
-
logger.info(f"Probabilities - Real: {confidence_real:.1f}%, Fake: {confidence_fake:.1f}%")
|
55 |
-
logger.info(f"Predicted Class: {predicted_class}, Label: {predicted_label}")
|
56 |
-
logger.info(f"Threshold ({confidence_threshold}): {threshold_predicted}")
|
57 |
-
|
58 |
-
# Prepare output
|
59 |
overall = f"{confidence_score:.1f}% Confidence ({threshold_predicted})"
|
60 |
-
aigen = f"{confidence_fake:.1f}% (AI-Generated Content Likelihood)"
|
61 |
deepfake = f"{confidence_fake:.1f}% (Face Manipulation Likelihood)"
|
62 |
|
63 |
return overall, aigen, deepfake
|
@@ -65,7 +48,6 @@ def detect(image, confidence_threshold=0.5):
|
|
65 |
except Exception as e:
|
66 |
logger.error(f"Error during analysis: {str(e)}")
|
67 |
raise gr.Error(f"Analysis error: {str(e)}")
|
68 |
-
|
69 |
# Custom CSS (unchanged)
|
70 |
custom_css = """
|
71 |
.container {
|
|
|
16 |
logger.info(f"Model label mapping: {model.config.id2label}")
|
17 |
|
18 |
def detect(image, confidence_threshold=0.5):
|
|
|
19 |
if image is None:
|
20 |
raise gr.Error("Please upload an image to analyze")
|
21 |
|
22 |
try:
|
|
|
23 |
pil_image = Image.open(image).convert("RGB")
|
|
|
|
|
24 |
pil_image = pil_image.resize((224, 224), Image.Resampling.LANCZOS)
|
|
|
|
|
25 |
inputs = processor(images=pil_image, return_tensors="pt")
|
26 |
|
|
|
27 |
with torch.no_grad():
|
28 |
outputs = model(**inputs)
|
29 |
logits = outputs.logits
|
30 |
probabilities = torch.softmax(logits, dim=1)[0]
|
31 |
|
32 |
+
confidence_real = probabilities[0].item() * 100
|
33 |
+
confidence_fake = probabilities[1].item() * 100
|
|
|
34 |
|
|
|
35 |
id2label = model.config.id2label
|
36 |
predicted_class = torch.argmax(logits, dim=1).item()
|
37 |
predicted_label = id2label[predicted_class]
|
|
|
|
|
38 |
threshold_predicted = "Fake" if confidence_fake / 100 >= confidence_threshold else "Real"
|
39 |
confidence_score = max(confidence_real, confidence_fake)
|
40 |
|
41 |
+
# Differentiate outputs (example heuristic)
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
overall = f"{confidence_score:.1f}% Confidence ({threshold_predicted})"
|
43 |
+
aigen = f"{confidence_fake * 0.9:.1f}% (AI-Generated Content Likelihood)" # Arbitrary scaling
|
44 |
deepfake = f"{confidence_fake:.1f}% (Face Manipulation Likelihood)"
|
45 |
|
46 |
return overall, aigen, deepfake
|
|
|
48 |
except Exception as e:
|
49 |
logger.error(f"Error during analysis: {str(e)}")
|
50 |
raise gr.Error(f"Analysis error: {str(e)}")
|
|
|
51 |
# Custom CSS (unchanged)
|
52 |
custom_css = """
|
53 |
.container {
|