Download app.py from potello/Santa: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/potello/Santa/resolve/main/app.py
- Command line
-
hf download hf://spaces/potello/Santa/app.py
-
curl -L -o app.py https://huggingface.co/spaces/potello/Santa/resolve/main/app.py
1.52 kB
| import gradio as gr | |
| import numpy as np | |
| import torch | |
| from torchvision.models import resnet18 | |
| from torchvision.transforms import transforms | |
| MODEL_PATH = "model.pth" | |
| model = resnet18(num_classes=2) | |
| model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device('cpu'))) | |
| model.eval() | |
| # Preprocess | |
| tfm = transforms.Compose([ | |
| transforms.Resize((224, 224)), | |
| transforms.RandomHorizontalFlip(), | |
| transforms.RandomRotation(degrees=10), | |
| transforms.ToTensor(), | |
| transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) | |
| ]) | |
| def predict(img): | |
| img = img.convert("RGB") | |
| img_tensor = tfm(img) | |
| img_tensor = img_tensor[np.newaxis, :] | |
| pred_prob = model(img_tensor) | |
| pred = torch.max(pred_prob,1).indices | |
| pred = pred.item() | |
| if pred == 1: | |
| return "Santa", img | |
| else: | |
| return "Not Santa", img | |
| with gr.Blocks() as demo: | |
| # Title | |
| gr.HTML( | |
| """ | |
| <img src="file/img_1.jpg" alt="Image"> | |
| """) | |
| # # Instructions | |
| # gr.Text( | |
| # """ | |
| # ### Instructions | |
| # 1. Upload an image of the cafeteria item. | |
| # 2. The model will predict the item and estimate its price. | |
| # 3. View the image prediction and the estimated price in the interface. | |
| # """) | |
| # Model Evaluation | |
| gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil"), | |
| outputs=[gr.Textbox(type="text", label="Price Prediction"), | |
| gr.Image(type="pil", label="Image Prediction")] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |