prithivMLmods commited on
Commit
2727aef
·
verified ·
1 Parent(s): 413a6e6

Update deepfake_vs_real.py

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