File size: 1,520 Bytes
8602894 86a1ac1 455384e 8602894 3fec7e7 f468e97 6cd7b5b 3fec7e7 f468e97 a52f2f0 75a72ec 8602894 cd1f45d 07412fb e515561 d2da764 599545e d2da764 599545e 55a1bb7 02e6287 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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() |