rufaidahaiman42 commited on
Commit
009200a
·
verified ·
1 Parent(s): 5961733

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -55
app.py CHANGED
@@ -1,65 +1,31 @@
1
- import gradio as gr
2
- from transformers import AutoModelForImageClassification, AutoImageProcessor
3
  from PIL import Image
4
  import torch
5
 
6
- # Load multiple models
7
- model_names = [
8
- "dima806/deepfake_vs_real_image_detection",
9
- "BuzzFeedNews/Deepfake-Detection",
10
- "umarlai/deepfake-detection-vit"
11
- ]
12
 
13
- models = []
14
- processors = []
 
15
 
16
- for name in model_names:
17
- processors.append(AutoImageProcessor.from_pretrained(name))
18
- models.append(AutoModelForImageClassification.from_pretrained(name))
19
 
20
- def predict(image):
21
- votes = {"Real": 0, "Fake": 0}
22
- probs_list = []
23
 
24
- for model, processor in zip(models, processors):
25
- inputs = processor(images=image, return_tensors="pt")
26
- with torch.no_grad():
27
- outputs = model(**inputs)
28
- probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
29
-
30
- # Map outputs depending on model’s labels
31
- labels = model.config.id2label
32
- result = {labels[i]: float(probs[i]) for i in range(len(labels))}
33
 
34
- # Normalize labels to Real/Fake
35
- real_score = result.get("Real", result.get("REAL", result.get("0", 0)))
36
- fake_score = result.get("Fake", result.get("FAKE", result.get("1", 0)))
 
37
 
38
- probs_list.append({"Real": real_score, "Fake": fake_score})
39
 
40
- if fake_score > real_score:
41
- votes["Fake"] += 1
42
- else:
43
- votes["Real"] += 1
44
-
45
- # Majority voting
46
- final_label = "Fake" if votes["Fake"] > votes["Real"] else "Real"
47
-
48
- # Average probability
49
- avg_real = sum([p["Real"] for p in probs_list]) / len(probs_list)
50
- avg_fake = sum([p["Fake"] for p in probs_list]) / len(probs_list)
51
-
52
- return {final_label: max(avg_real, avg_fake), "Real": avg_real, "Fake": avg_fake}
53
-
54
- # UI
55
- demo = gr.Interface(
56
- fn=predict,
57
- inputs=gr.Image(type="pil"),
58
- outputs="label",
59
- title="🕵️ Deepfake Detector (Ensemble)",
60
- description="Uploads an image and checks if it's REAL or FAKE using 3 different models combined."
61
- )
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
65
-
 
1
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
 
2
  from PIL import Image
3
  import torch
4
 
5
+ # Use a valid Hugging Face model repo
6
+ MODEL_NAME = "selimsef/dfdc_deepfake_challenge"
 
 
 
 
7
 
8
+ # Load processor & model
9
+ processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
10
+ model = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
11
 
12
+ # Load your image
13
+ image_path = "your_image.jpg" # replace with your raw image path
14
+ image = Image.open(image_path).convert("RGB")
15
 
16
+ # Preprocess
17
+ inputs = processor(images=image, return_tensors="pt")
 
18
 
19
+ # Inference
20
+ with torch.no_grad():
21
+ outputs = model(**inputs)
22
+ logits = outputs.logits
23
+ probs = torch.nn.functional.softmax(logits, dim=-1)[0]
 
 
 
 
24
 
25
+ # Get labels
26
+ labels = model.config.id2label
27
+ real_prob = probs[labels["0"]] if "0" in labels else probs[0]
28
+ fake_prob = probs[labels["1"]] if "1" in labels else probs[1]
29
 
30
+ print(f"Prediction: Real={real_prob:.4f}, Fake={fake_prob:.4f}")
31