import torch import timm import gradio as gr from PIL import Image import torchvision.transforms as transforms # --------- Model --------- model = timm.create_model( "efficientnetv2_s", pretrained=False, num_classes=4 ) model.load_state_dict(torch.load("efficient.pth", map_location="cpu")) model.eval() # --------- Transform --------- transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.Lambda(lambda x: x.convert("RGB")), transforms.ToTensor() ]) classes = [ "Class 0", "Class 1", "Class 2", "Class 3" ] # --------- Prediction --------- def predict(img): img = transform(img).unsqueeze(0) with torch.no_grad(): outputs = model(img) pred = outputs.argmax(1).item() return classes[pred] # --------- UI --------- demo = gr.Interface( fn=predict, inputs=gr.Image(type="pil"), outputs="text", title="OCTMNIST Medical Image Classifier", description="EfficientNetV2-S trained on OCTMNIST dataset" ) demo.launch()