IFMedTech commited on
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import torchfrom torchvision import models, transformsfrom PIL import Imageimport gradio as gr
class_names = [ "plaque_calculus", "caries", "plaque_gingivitis", "hypodontia", "mouth_ulcer", "tooth_discoloration"]
# Load the model and update the final fully connected layermodel = models.resnet50(weights=None)model.fc = torch.nn.Linear(model.fc.in_features, len(class_names))
# Load the model weights from tooth_model.pthmodel.load_state_dict(torch.load('tooth_model.pth', map_location=torch.device('cpu')))model.eval()
# Preprocessing steps for input imagespreprocess = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),])
def predict_image(image): # Preprocess the image and add a batch dimension processed_image = preprocess(image).unsqueeze(0)
with torch.no_grad(): outputs = model(processed_image) _, top_indices = torch.topk(outputs, 2) # Get top 2 predictions top_classes = [class_names[idx] for idx in top_indices[0]]
return ", ".join(top_classes)
# Set up the Gradio interfaceiface = gr.Interface( fn=predict_image, inputs=gr.Image(type="pil"), outputs="text", # Output will be text listing title="Medical Image Classification", description="Upload an image to predict its class.")
# Launch the interfaceiface.launch()app.pyAdded+51