quazar002 commited on
Commit
18acd7d
·
verified ·
1 Parent(s): a0acb9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py CHANGED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # ✅ Define classify_image first
37
+ def classify_image(img: Image.Image):
38
+ img_tensor = transform(img).unsqueeze(0)
39
+ with torch.no_grad():
40
+ outputs = model(img_tensor)
41
+ _, predicted = torch.max(outputs, 1)
42
+ label = 'Fake' if predicted.item() == 0 else 'Real'
43
+ return label
44
+
45
+ # ✅ Then wrap it in predict()
46
+ def predict(img: Image.Image):
47
+ return classify_image(img)
48
+
49
+ # ✅ All set to go
50
+ gr.Interface(
51
+ fn=predict,
52
+ inputs=gr.Image(type="pil"),
53
+ outputs="label",
54
+ title="Luxury Item Authenticity Detector",
55
+ description="Upload an image to check if it's a real or fake item."
56
+ ).launch()