quazar002 commited on
Commit
bfb7b0d
·
verified ·
1 Parent(s): 7dc867d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py CHANGED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision.transforms as transforms
3
+ from PIL import Image
4
+ import gradio as gr
5
+ from timm import create_model
6
+ import torch.nn as nn
7
+ import os
8
+
9
+ class VisionTransformer(nn.Module):
10
+ def __init__(self, num_classes, model_name):
11
+ super(VisionTransformer, self).__init__()
12
+ self.model = create_model(model_name, pretrained=False, num_classes=num_classes)
13
+ self.model.head = nn.Sequential(
14
+ nn.Linear(self.model.num_features, 512),
15
+ nn.ReLU(),
16
+ nn.Dropout(0.5),
17
+ nn.Linear(512, num_classes)
18
+ )
19
+
20
+ def forward(self, x):
21
+ return self.model(x)
22
+
23
+ model_path = "./models/vit_small_patch16_224_final.pth"
24
+ device = torch.device("cpu")
25
+
26
+ model = VisionTransformer(num_classes=2, model_name="vit_small_patch16_224")
27
+ model.load_state_dict(torch.load(model_path, map_location=device))
28
+ model.eval()
29
+
30
+ transform = transforms.Compose([
31
+ transforms.Resize((224, 224)),
32
+ transforms.ToTensor(),
33
+ transforms.Normalize(mean=[0.5]*3, std=[0.5]*3)
34
+ ])
35
+
36
+ def classify_image(img: Image.Image):
37
+ img_tensor = transform(img).unsqueeze(0)
38
+ with torch.no_grad():
39
+ outputs = model(img_tensor)
40
+ _, predicted = torch.max(outputs, 1)
41
+ label = 'Fake' if predicted.item() == 0 else 'Real'
42
+ return label
43
+
44
+ gr.Interface(
45
+ fn=classify_image,
46
+ inputs=gr.Image(type="pil"),
47
+ outputs="label",
48
+ title="Luxury Item Authenticity Detector",
49
+ description="Upload an image to check if it's a real or fake item."
50
+ ).launch()