prithivMLmods commited on
Commit
8353faf
·
verified ·
1 Parent(s): a39ad4c

Update deepfake_vs_real.py

Browse files
Files changed (1) hide show
  1. deepfake_vs_real.py +15 -10
deepfake_vs_real.py CHANGED
@@ -1,12 +1,14 @@
1
  import gradio as gr
2
- from transformers import ViTForImageClassification, ViTImageProcessor
 
 
3
  from PIL import Image
4
  import torch
5
 
6
- # Load the model and processor
7
- model_name = "prithivMLmods/Deep-Fake-Detector-v2-Model"
8
- model = ViTForImageClassification.from_pretrained(model_name)
9
- processor = ViTImageProcessor.from_pretrained(model_name)
10
 
11
  def deepfake_classification(image):
12
  """Predicts whether an image is a Deepfake or Real."""
@@ -16,17 +18,20 @@ def deepfake_classification(image):
16
  with torch.no_grad():
17
  outputs = model(**inputs)
18
  logits = outputs.logits
19
- predicted_class = torch.argmax(logits, dim=1).item()
20
 
21
- # Get label mapping
22
- label = model.config.id2label[predicted_class] if hasattr(model.config, "id2label") else str(predicted_class)
23
- return {label: 1.0} # Gradio Label output expects a dictionary
 
 
 
24
 
25
  # Create Gradio interface
26
  iface = gr.Interface(
27
  fn=deepfake_classification,
28
  inputs=gr.Image(type="numpy"),
29
- outputs=gr.Label(label="Prediction"),
30
  title="Deepfake vs. Real Image Classification",
31
  description="Upload an image to determine if it's a Deepfake or a Real one."
32
  )
 
1
  import gradio as gr
2
+ from transformers import AutoImageProcessor
3
+ from transformers import SiglipForImageClassification
4
+ from transformers.image_utils import load_image
5
  from PIL import Image
6
  import torch
7
 
8
+ # Load model and processor
9
+ model_name = "prithivMLmods/Deepfake-vs-Real-8000"
10
+ model = SiglipForImageClassification.from_pretrained(model_name)
11
+ processor = AutoImageProcessor.from_pretrained(model_name)
12
 
13
  def deepfake_classification(image):
14
  """Predicts whether an image is a Deepfake or Real."""
 
18
  with torch.no_grad():
19
  outputs = model(**inputs)
20
  logits = outputs.logits
21
+ probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
22
 
23
+ labels = {
24
+ "0": "Deepfake", "1": "Real one"
25
+ }
26
+ predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
27
+
28
+ return predictions
29
 
30
  # Create Gradio interface
31
  iface = gr.Interface(
32
  fn=deepfake_classification,
33
  inputs=gr.Image(type="numpy"),
34
+ outputs=gr.Label(label="Prediction Scores"),
35
  title="Deepfake vs. Real Image Classification",
36
  description="Upload an image to determine if it's a Deepfake or a Real one."
37
  )