Spaces:
Runtime error
Runtime error
Download app.py from sfayn/Brain_model: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/spaces/sfayn/Brain_model/resolve/main/app.py
- Command line
-
hf download hf://spaces/sfayn/Brain_model/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sfayn/Brain_model/resolve/main/app.py
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() |