Spaces:
Sleeping

louie-api / app.py
kjdct's picture
Update app.py
f9107b8 verified
Raw History Blame Contribute Delete
1.07 kB
import gradio as gr
import torch
from PIL import Image
from torchvision import transforms
import timm
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load the model architecture and weights
model = timm.create_model('convnext_tiny', pretrained=False, num_classes=1)
model.load_state_dict(torch.load("louie_model.pth", map_location=device))
model.eval().to(device)
# Image preprocessing to match training
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
])
def predict(image: Image.Image) -> str:
img_tensor = transform(image).unsqueeze(0).to(device)
with torch.no_grad():
output = torch.sigmoid(model(img_tensor))
confidence = output.item() * 100
return f"Louie confidence: {confidence:.2f}%"
# Gradio interface
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs="text",
title="Louie Image Classifier",
description="Upload a photo to see the confidence that Louie is in it."
)
if __name__ == "__main__":
iface.launch()