Update app.py
Browse files
app.py
CHANGED
|
@@ -1,143 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import torch.nn as nn
|
| 4 |
-
from torchvision import transforms
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
# ============================================================
|
| 8 |
-
# MODEL
|
| 9 |
-
# ============================================================
|
| 10 |
-
|
| 11 |
-
class BasicBlock(nn.Module):
|
| 12 |
-
expansion = 1
|
| 13 |
-
|
| 14 |
-
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
|
| 15 |
-
super(BasicBlock, self).__init__()
|
| 16 |
-
|
| 17 |
-
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3,
|
| 18 |
-
stride=stride, padding=1, bias=False)
|
| 19 |
-
self.bn1 = nn.BatchNorm2d(out_channels)
|
| 20 |
-
|
| 21 |
-
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3,
|
| 22 |
-
stride=1, padding=1, bias=False)
|
| 23 |
-
self.bn2 = nn.BatchNorm2d(out_channels)
|
| 24 |
-
|
| 25 |
-
self.relu = nn.ReLU(inplace=True)
|
| 26 |
-
self.downsample = downsample
|
| 27 |
-
|
| 28 |
-
def forward(self, x):
|
| 29 |
-
identity = x
|
| 30 |
-
|
| 31 |
-
out = self.conv1(x)
|
| 32 |
-
out = self.bn1(out)
|
| 33 |
-
out = self.relu(out)
|
| 34 |
-
|
| 35 |
-
out = self.conv2(out)
|
| 36 |
-
out = self.bn2(out)
|
| 37 |
-
|
| 38 |
-
if self.downsample is not None:
|
| 39 |
-
identity = self.downsample(x)
|
| 40 |
-
|
| 41 |
-
out += identity
|
| 42 |
-
out = self.relu(out)
|
| 43 |
-
|
| 44 |
-
return out
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
class ResNet(nn.Module):
|
| 48 |
-
def __init__(self, block, layers, num_classes=2):
|
| 49 |
-
super(ResNet, self).__init__()
|
| 50 |
-
|
| 51 |
-
self.in_channels = 64
|
| 52 |
-
|
| 53 |
-
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
|
| 54 |
-
self.bn1 = nn.BatchNorm2d(64)
|
| 55 |
-
self.relu = nn.ReLU(inplace=True)
|
| 56 |
-
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
|
| 57 |
-
|
| 58 |
-
self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
|
| 59 |
-
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
|
| 60 |
-
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
|
| 61 |
-
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
|
| 62 |
-
|
| 63 |
-
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
|
| 64 |
-
self.fc = nn.Linear(512 * block.expansion, num_classes)
|
| 65 |
-
|
| 66 |
-
def _make_layer(self, block, out_channels, num_blocks, stride):
|
| 67 |
-
downsample = None
|
| 68 |
-
|
| 69 |
-
if stride != 1 or self.in_channels != out_channels * block.expansion:
|
| 70 |
-
downsample = nn.Sequential(
|
| 71 |
-
nn.Conv2d(self.in_channels, out_channels * block.expansion,
|
| 72 |
-
kernel_size=1, stride=stride, bias=False),
|
| 73 |
-
nn.BatchNorm2d(out_channels * block.expansion)
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
layers = []
|
| 77 |
-
layers.append(block(self.in_channels, out_channels, stride, downsample))
|
| 78 |
-
self.in_channels = out_channels * block.expansion
|
| 79 |
-
|
| 80 |
-
for _ in range(1, num_blocks):
|
| 81 |
-
layers.append(block(self.in_channels, out_channels))
|
| 82 |
-
|
| 83 |
-
return nn.Sequential(*layers)
|
| 84 |
-
|
| 85 |
-
def forward(self, x):
|
| 86 |
-
x = self.conv1(x)
|
| 87 |
-
x = self.bn1(x)
|
| 88 |
-
x = self.relu(x)
|
| 89 |
-
x = self.maxpool(x)
|
| 90 |
-
|
| 91 |
-
x = self.layer1(x)
|
| 92 |
-
x = self.layer2(x)
|
| 93 |
-
x = self.layer3(x)
|
| 94 |
-
x = self.layer4(x)
|
| 95 |
-
|
| 96 |
-
x = self.avgpool(x)
|
| 97 |
-
x = torch.flatten(x, 1)
|
| 98 |
-
x = self.fc(x)
|
| 99 |
-
|
| 100 |
-
return x
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
def resnet18(num_classes=2):
|
| 104 |
-
return ResNet(BasicBlock, [2, 2, 2, 2], num_classes=num_classes)
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
# ============================================================
|
| 108 |
-
# SETUP
|
| 109 |
# ============================================================
|
| 110 |
|
| 111 |
-
|
| 112 |
-
model =
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
transforms.Resize((256, 256)),
|
| 116 |
-
transforms.ToTensor(),
|
| 117 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 118 |
-
])
|
| 119 |
-
|
| 120 |
-
class_names = ['Fake', 'Real']
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
def load_model():
|
| 124 |
-
global model
|
| 125 |
-
|
| 126 |
-
# Load model directly from uploaded file
|
| 127 |
-
model_path = 'resnet18_deepfake_best.pth'
|
| 128 |
-
|
| 129 |
-
model = resnet18(num_classes=2)
|
| 130 |
-
checkpoint = torch.load(model_path, map_location=device)
|
| 131 |
-
model.load_state_dict(checkpoint['model_state_dict'])
|
| 132 |
-
model.to(device)
|
| 133 |
-
model.eval()
|
| 134 |
-
|
| 135 |
-
print("✓ Model loaded!")
|
| 136 |
-
return model
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
load_model()
|
| 140 |
-
|
| 141 |
|
| 142 |
# ============================================================
|
| 143 |
# PREDICTION
|
|
@@ -146,29 +19,22 @@ load_model()
|
|
| 146 |
def predict(image):
|
| 147 |
if image is None:
|
| 148 |
return {"Error": "Upload an image"}
|
| 149 |
-
|
| 150 |
if not isinstance(image, Image.Image):
|
| 151 |
image = Image.fromarray(image)
|
| 152 |
-
|
| 153 |
-
image = image.convert(
|
| 154 |
-
|
| 155 |
-
|
| 156 |
with torch.no_grad():
|
| 157 |
-
outputs = model(
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
print(f"Probabilities: {probabilities}")
|
| 163 |
-
print(f"Fake prob: {probabilities[0][0]:.4f}, Real prob: {probabilities[0][1]:.4f}")
|
| 164 |
-
|
| 165 |
-
result = {
|
| 166 |
-
class_names[0]: float(probabilities[0][0]),
|
| 167 |
-
class_names[1]: float(probabilities[0][1])
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
return result
|
| 171 |
|
|
|
|
|
|
|
| 172 |
|
| 173 |
# ============================================================
|
| 174 |
# INTERFACE
|
|
@@ -183,4 +49,4 @@ demo = gr.Interface(
|
|
| 183 |
)
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|
| 186 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoImageProcessor, SiglipForImageClassification
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
# ============================================================
|
| 7 |
+
# LOAD MODEL FROM HUGGING FACE HUB
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# ============================================================
|
| 9 |
|
| 10 |
+
model_name = "prithivMLmods/Deepfake-Detect-Siglip2"
|
| 11 |
+
model = SiglipForImageClassification.from_pretrained(model_name)
|
| 12 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 13 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# ============================================================
|
| 16 |
# PREDICTION
|
|
|
|
| 19 |
def predict(image):
|
| 20 |
if image is None:
|
| 21 |
return {"Error": "Upload an image"}
|
| 22 |
+
|
| 23 |
if not isinstance(image, Image.Image):
|
| 24 |
image = Image.fromarray(image)
|
| 25 |
+
|
| 26 |
+
image = image.convert("RGB")
|
| 27 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 28 |
+
|
| 29 |
with torch.no_grad():
|
| 30 |
+
outputs = model(**inputs)
|
| 31 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1).squeeze().tolist()
|
| 32 |
+
|
| 33 |
+
labels = model.config.id2label
|
| 34 |
+
result = {labels[i]: round(probs[i], 4) for i in range(len(probs))}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
print(f"Result: {result}")
|
| 37 |
+
return result
|
| 38 |
|
| 39 |
# ============================================================
|
| 40 |
# INTERFACE
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|