truens66 commited on
Commit
a518ecb
·
verified ·
1 Parent(s): 109e2cd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+ from torchvision import transforms
5
+ import os
6
+ import numpy as np
7
+ import random
8
+ from resnet import resnet50
9
+
10
+ def seed_torch(seed=1029):
11
+ random.seed(seed)
12
+ os.environ['PYTHONHASHSEED'] = str(seed)
13
+ np.random.seed(seed)
14
+ torch.manual_seed(seed)
15
+ torch.cuda.manual_seed(seed)
16
+ torch.cuda.manual_seed_all(seed)
17
+ torch.backends.cudnn.benchmark = False
18
+ torch.backends.cudnn.deterministic = True
19
+ torch.backends.cudnn.enabled = False
20
+
21
+ seed_torch(100)
22
+
23
+ def load_model(model_path):
24
+ model = resnet50(num_classes=1)
25
+ state_dict = torch.load(model_path, map_location='cpu')
26
+ model.load_state_dict(state_dict, strict=True)
27
+ if torch.cuda.is_available():
28
+ model.cuda()
29
+ model.eval()
30
+ return model
31
+
32
+ def preprocess_image(image):
33
+ transform = transforms.Compose([
34
+ transforms.Resize((224, 224)),
35
+ transforms.ToTensor(),
36
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
37
+ ])
38
+ image = transform(image).unsqueeze(0)
39
+ return image
40
+
41
+ def predict_image(model, image):
42
+ if torch.cuda.is_available():
43
+ image = image.cuda()
44
+ with torch.no_grad():
45
+ output = model(image)
46
+ # Apply sigmoid to get probability between 0 and 1
47
+ prediction = torch.sigmoid(output).item()
48
+
49
+ # Clamp prediction between 0 and 1
50
+ prediction = max(0, min(prediction, 1))
51
+
52
+ # Convert to percentages
53
+ real_prob = round(prediction * 1, 2) # Rounded to 2 decimal places
54
+ fake_prob = round(1 - real_prob, 2) # Complementary probability
55
+
56
+ return real_prob, fake_prob
57
+
58
+
59
+ # def predict_image(model, image):
60
+ # if torch.cuda.is_available():
61
+ # image = image.cuda()
62
+ # with torch.no_grad():
63
+ # output = model(image)
64
+ # prediction = torch.sigmoid(output).item()
65
+ # real_prob = gr.number(min(max(prediction * 100, 0), 100)) # Convert to integer
66
+ # fake_prob = int(100 - real_prob) # Ensure complementary probability
67
+ # return real_prob, fake_prob
68
+
69
+ # Load the model once at the start
70
+ model_path = "model_epoch_last_3090.pth" # Update with the correct path to your model
71
+ model = load_model(model_path)
72
+
73
+ def detect_deepfake(image):
74
+ image = Image.fromarray(image).convert("RGB")
75
+ preprocessed_image = preprocess_image(image)
76
+ real_prob, fake_prob = predict_image(model, preprocessed_image)
77
+ print("real_prob", real_prob)
78
+ print("fake_prob", fake_prob)
79
+
80
+ return {"Real Confidence": real_prob, "Fake Confidence": fake_prob}
81
+
82
+
83
+ iface = gr.Interface(
84
+ fn=detect_deepfake,
85
+ inputs=gr.Image(type="numpy", label="Upload Image"),
86
+ outputs=gr.Label(num_top_classes=2, label="Confidence Scores"),
87
+ title="Deepfake Detection",
88
+ description="Upload an image to determine its confidence scores for being real or fake."
89
+ )
90
+
91
+ if __name__ == "__main__":
92
+ iface.launch()