Upload app
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import timm
|
| 6 |
+
import json
|
| 7 |
+
from torchvision import transforms
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 11 |
+
MODEL_PATH = os.path.join(BASE_DIR, "artifacts", "best_model.pt")
|
| 12 |
+
LABELS_PATH = os.path.join(BASE_DIR, "data", "style_labels.json")
|
| 13 |
+
|
| 14 |
+
class ArtStyleClassifier(nn.Module):
|
| 15 |
+
def __init__(self, num_classes):
|
| 16 |
+
super().__init__()
|
| 17 |
+
self.backbone = timm.create_model("efficientnet_b0", pretrained=False, num_classes=0)
|
| 18 |
+
num_features = self.backbone.num_features
|
| 19 |
+
self.classifier = nn.Sequential(
|
| 20 |
+
nn.Dropout(p=0.3),
|
| 21 |
+
nn.Linear(num_features, 512),
|
| 22 |
+
nn.ReLU(),
|
| 23 |
+
nn.Dropout(p=0.2),
|
| 24 |
+
nn.Linear(512, num_classes)
|
| 25 |
+
)
|
| 26 |
+
def forward(self, x):
|
| 27 |
+
features = self.backbone(x)
|
| 28 |
+
return self.classifier(features)
|
| 29 |
+
|
| 30 |
+
with open(LABELS_PATH, "r") as f:
|
| 31 |
+
style_to_idx = json.load(f)
|
| 32 |
+
idx_to_style = {int(i): s for s, i in style_to_idx.items()}
|
| 33 |
+
|
| 34 |
+
model = ArtStyleClassifier(num_classes=len(style_to_idx))
|
| 35 |
+
model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device("cpu")))
|
| 36 |
+
model.eval()
|
| 37 |
+
|
| 38 |
+
transform = transforms.Compose([
|
| 39 |
+
transforms.Resize((224, 224)),
|
| 40 |
+
transforms.ToTensor(),
|
| 41 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 42 |
+
std=[0.229, 0.224, 0.225]),
|
| 43 |
+
])
|
| 44 |
+
|
| 45 |
+
def predict(image):
|
| 46 |
+
if image is None:
|
| 47 |
+
return "No image provided."
|
| 48 |
+
pil_image = Image.fromarray(image).convert("RGB")
|
| 49 |
+
tensor = transform(pil_image).unsqueeze(0)
|
| 50 |
+
with torch.no_grad():
|
| 51 |
+
outputs = model(tensor)
|
| 52 |
+
probs = torch.softmax(outputs, dim=1)[0]
|
| 53 |
+
top_probs, top_idxs = torch.topk(probs, 3)
|
| 54 |
+
lines = []
|
| 55 |
+
medals = ["🥇", "🥈", "🥉"]
|
| 56 |
+
for i, (p, idx) in enumerate(zip(top_probs, top_idxs)):
|
| 57 |
+
style = idx_to_style[idx.item()]
|
| 58 |
+
confidence = float(p.item()) * 100
|
| 59 |
+
lines.append(f"{medals[i]} {style} — {confidence:.1f}%")
|
| 60 |
+
return "\n".join(lines)
|
| 61 |
+
|
| 62 |
+
demo = gr.Interface(
|
| 63 |
+
fn=predict,
|
| 64 |
+
inputs=gr.Image(label="Upload your artwork"),
|
| 65 |
+
outputs=gr.Text(label="Art Style Predictions"),
|
| 66 |
+
title="Art Style Classifier",
|
| 67 |
+
description="Upload a photo of your artwork to identify which art style it most resembles.",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
demo.launch()
|