Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import yaml
|
| 4 |
+
from torchvision import models, transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
CONFIG_PATH = 'staging_config.yaml'
|
| 10 |
+
CHECKPOINT_FILENAME = 'model.pt'
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def get_model(model_name, num_classes):
|
| 14 |
+
"""Factory function to create a model shell for loading weights."""
|
| 15 |
+
model = None
|
| 16 |
+
if model_name == "efficientnet_b0":
|
| 17 |
+
model = models.efficientnet_b0(weights=None)
|
| 18 |
+
num_ftrs = model.classifier[1].in_features
|
| 19 |
+
model.classifier[1] = nn.Linear(num_ftrs, num_classes)
|
| 20 |
+
elif model_name == "resnet50":
|
| 21 |
+
model = models.resnet50(weights=None)
|
| 22 |
+
num_ftrs = model.fc.in_features
|
| 23 |
+
model.fc = nn.Linear(num_ftrs, num_classes)
|
| 24 |
+
elif model_name == "vit_b_16":
|
| 25 |
+
model = models.vit_b_16(weights=None)
|
| 26 |
+
num_ftrs = model.heads.head.in_features
|
| 27 |
+
model.heads.head = nn.Linear(num_ftrs, num_classes)
|
| 28 |
+
else:
|
| 29 |
+
raise ValueError(f"Model '{model_name}' not supported.")
|
| 30 |
+
return model
|
| 31 |
+
|
| 32 |
+
def load_checkpoint(checkpoint_path, device):
|
| 33 |
+
"""Loads a checkpoint and returns the model and class mapping."""
|
| 34 |
+
if not os.path.exists(checkpoint_path):
|
| 35 |
+
raise FileNotFoundError(f"Checkpoint file not found at: {checkpoint_path}")
|
| 36 |
+
|
| 37 |
+
checkpoint = torch.load(checkpoint_path, map_location=device)
|
| 38 |
+
model_name = checkpoint['model_name']
|
| 39 |
+
class_to_idx = checkpoint['class_to_idx']
|
| 40 |
+
|
| 41 |
+
model = get_model(model_name, num_classes=1)
|
| 42 |
+
model.load_state_dict(checkpoint['state_dict'])
|
| 43 |
+
model.to(device)
|
| 44 |
+
model.eval()
|
| 45 |
+
|
| 46 |
+
idx_to_class = {v: k for k, v in class_to_idx.items()}
|
| 47 |
+
return model, idx_to_class
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
with open(CONFIG_PATH, 'r') as f:
|
| 52 |
+
config = yaml.safe_load(f)
|
| 53 |
+
except FileNotFoundError:
|
| 54 |
+
raise RuntimeError(f"ERROR: Config file not found at '{CONFIG_PATH}'. Make sure it's uploaded to the Space.")
|
| 55 |
+
|
| 56 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 57 |
+
MODEL, IDX_TO_CLASS = load_checkpoint(CHECKPOINT_FILENAME, DEVICE)
|
| 58 |
+
print(f"Model loaded successfully on {DEVICE}.")
|
| 59 |
+
print(f"Class mapping: {IDX_TO_CLASS}")
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
IMG_SIZE = config['data_params']['image_size']
|
| 63 |
+
inference_transform = transforms.Compose([
|
| 64 |
+
transforms.Resize((IMG_SIZE, IMG_SIZE)),
|
| 65 |
+
transforms.ToTensor(),
|
| 66 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 67 |
+
])
|
| 68 |
+
|
| 69 |
+
def predict(pil_image):
|
| 70 |
+
"""
|
| 71 |
+
Runs prediction on a single PIL image and returns a dictionary of class probabilities.
|
| 72 |
+
Gradio's `Label` component expects a dictionary format for its output.
|
| 73 |
+
"""
|
| 74 |
+
if pil_image is None:
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
pil_image = pil_image.convert("RGB")
|
| 78 |
+
|
| 79 |
+
image_tensor = inference_transform(pil_image).unsqueeze(0).to(DEVICE)
|
| 80 |
+
|
| 81 |
+
with torch.no_grad():
|
| 82 |
+
output = MODEL(image_tensor)
|
| 83 |
+
prob = torch.sigmoid(output).item()
|
| 84 |
+
|
| 85 |
+
class_0_name = IDX_TO_CLASS.get(0, "Class 0")
|
| 86 |
+
class_1_name = IDX_TO_CLASS.get(1, "Class 1")
|
| 87 |
+
|
| 88 |
+
confidences = {
|
| 89 |
+
class_0_name: 1 - prob,
|
| 90 |
+
class_1_name: prob
|
| 91 |
+
}
|
| 92 |
+
return confidences
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
title = "Image Classifier API"
|
| 97 |
+
description = """
|
| 98 |
+
Upload an image and the model will predict its class.
|
| 99 |
+
This model was trained to distinguish between two classes.
|
| 100 |
+
The API returns the probabilities for each class.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
iface = gr.Interface(
|
| 104 |
+
fn=predict,
|
| 105 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 106 |
+
outputs=gr.Label(num_top_classes=2, label="Predictions"),
|
| 107 |
+
title=title,
|
| 108 |
+
description=description,
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
iface.launch()
|