Brain_model / app.py
sfayn's picture
Create app.py
e3c679c verified
Raw History Blame Contribute Delete
1.01 kB
import torch
import torchvision
from torchvision import transforms as T
from loadimg import load_img
import gradio as gr
trans=T.Compose([
T.Resize((200,200)),
T.ToTensor()
])
classes = {
0 : "Glioma",
1 : "Meningioma",
2 : "No tumor",
3 : "Pitutary"
}
model = torch.load('bt_brain_model_with.pth',map_location=torch.device('cpu'))
model.eval()
def preprocess(image):
im = load_img(image, output_type="pil")
im = im.convert("RGB")
image = load_img(im)
tensor = trans(image).unsqueeze(0)
tensor = tensor.to(next(model.parameters()).device)
return tensor
def predict(input_image):
# Preprocess the image
input_tensor = preprocess(input_image)
# Run the image through the model
with torch.no_grad():
y_val = model(input_tensor)
predicted_label = torch.argmax(y_val, dim=1).item()
return classes[predicted_label]
iface = gr.Interface(fn=predict, inputs="image", outputs="label")
# Launch the interface
iface.launch()