Download app.py from kjdct/louie-api: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/kjdct/louie-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/kjdct/louie-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/kjdct/louie-api/resolve/main/app.py
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() | |