# app.py # Import necessary libraries import torch from PIL import Image from torchvision import transforms import gradio as gr import os # Download the labels file if not present os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt") # Load ResNet model model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True).eval() # Function for model inference def inference(input_image): try: # Preprocess the input image preprocess = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) input_tensor = preprocess(input_image) input_batch = input_tensor.unsqueeze(0) # Create a mini-batch as expected by the model # Move the input and model to GPU for speed if available if torch.cuda.is_available(): input_batch = input_batch.to('cuda') model.to('cuda') with torch.no_grad(): output = model(input_batch) # The output has unnormalized scores. To get probabilities, run a softmax on it. probabilities = torch.nn.functional.softmax(output[0], dim=0) # Read the categories from the labels file with open("imagenet_classes.txt", "r") as f: categories = [s.strip() for s in f.readlines()] # Show top categories per image top5_prob, top5_catid = torch.topk(probabilities, 5) result = {categories[top5_catid[i]]: top5_prob[i].item() for i in range(top5_prob.size(0))} return result except Exception as e: return {"error": str(e)} # Gradio Interface setup inputs = gr.Image(type='pil', label="Upload Image") outputs = gr.Label(num_top_classes=5, label="Predictions") # Removed 'type' parameter # Launch Gradio Interface gr.Interface(inference, inputs, outputs, title="DeepLensExplorer", description="Classify images using ResNet", analytics_enabled=False).launch()