How to deploy a image recognition PyTorch model as an application in hugginface? i thinking of giving a button to upload an image to the model.to recognise the image:-
this is the code i used to do the image recognition i want to convert this as a button “upload” when i click upload image will upload to model and predict image.
import torchvision
import matplotlib.pyplot as plt
from typing import Dict,List
def plot_pred_image(model:torch.nn.Module,
image_path:str,
class_names:List[str]=None,
transform=None,
device:torch.device=None):
target_image = torchvision.io.read_image(str(image_path)).type(torch.float32)
target_image = target_image/255
if transform:
target_image = transform(target_image)
model.to(device)
model.eval()
with torch.inference_mode():
target_image = target_image.unsqueeze(0)
target_image_pred = model(target_image.to(device))
target_image_pred_probs = torch.softmax(target_image_pred,dim=1)
target_image_pred_label = torch.argmax(target_image_pred_probs,dim=1)
plt.imshow(target_image.squeeze().permute(1,2,0))
if class_names:
title = f"Pred: {class_names[target_image_pred_label.cpu()]} | Prob {target_image_pred_probs.max():.2f}"
else:
title = f"{target_image_pred_label} | Prob {target_image_pred_probs,max():3f}"
plt.title(title)
plt.axis(False)
plot_pred_image(model=model,
image_path=custom_image_path,
class_names=class_list,
transform=image_transform,
device=device)