beppefolder commited on
Commit
97e0d2f
·
verified ·
1 Parent(s): 85ac8a7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torchvision.transforms as transforms
3
+ from torchvision.transforms import InterpolationMode
4
+ import torch
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ from .model import Model
8
+
9
+ # Load Model
10
+ model_path = hf_hub_download(
11
+ repo_id="itserr/exvoto_classifier_convnext_base_224",
12
+ filename="model.pt"
13
+ )
14
+
15
+ model = Model('convnext_base')
16
+ ckpt = torch.load(model_path, map_location=torch.device("cpu")) # Ensure compatibility
17
+ model.load_state_dict(ckpt['model'])
18
+
19
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
20
+ model.to(device)
21
+ model.eval()
22
+
23
+
24
+ # Image Transformations
25
+ transform = transforms.Compose([
26
+ transforms.Resize(size=(224,224), interpolation=InterpolationMode.BICUBIC),
27
+ transforms.ToTensor(),
28
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
29
+ ])
30
+
31
+ # Classification Function
32
+ def classify_img(img, threshold):
33
+ classification_threshold = threshold
34
+ img_tensor = transform(img).unsqueeze(0).to(device)
35
+
36
+ with torch.no_grad():
37
+ pred = model(img_tensor)
38
+ score = torch.sigmoid(pred).item()
39
+
40
+ # Determine Prediction
41
+ if score >= classification_threshold:
42
+ label = "✅ This is an **Ex-Voto** image!"
43
+ else:
44
+ label = "❌ This is **NOT** an Ex-Voto image."
45
+
46
+ # Format Confidence Score
47
+ confidence = f"The probability that the image is an ex-voto is: {score:.2%}"
48
+ return label, confidence
49
+
50
+
51
+ # # **🎨 Customized Interface**
52
+ demo = gr.Interface(
53
+ fn=classify_img,
54
+ inputs=[
55
+ gr.Image(type="pil"),
56
+ gr.Slider(minimum=0.5, maximum=1.0, value=0.7, step=0.1, label="Classification Threshold")
57
+ ],
58
+ outputs=[
59
+ gr.Textbox(label="Prediction", interactive=False),
60
+ gr.Textbox(label="Confidence Score", interactive=False),
61
+ ],
62
+ title="🖼️✟ Ex-Voto Image Classifier",
63
+ description="📸 **Upload an image** to check if it's an **Ex-Voto** painting!",
64
+ theme="soft",
65
+ allow_flagging="never",
66
+ live=False, # Avoids auto-updating; requires a button click
67
+ )
68
+
69
+ # Launch App
70
+ demo.launch()