ma4389 commited on
Commit
f5f7b11
·
verified ·
1 Parent(s): 63767aa

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +48 -0
  2. requirements.txt +4 -0
  3. tumor_model.pth +3 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torchvision import transforms, models
4
+ from PIL import Image
5
+ import gradio as gr
6
+
7
+ # === Load trained model ===
8
+ model = models.resnet18()
9
+ in_features = model.fc.in_features
10
+ model.fc = nn.Sequential(
11
+ nn.Linear(in_features, 256),
12
+ nn.ReLU(),
13
+ nn.Dropout(0.4),
14
+ nn.Linear(256, 2)
15
+ )
16
+ model.load_state_dict(torch.load("tumor_model.pth", map_location=torch.device("cpu")))
17
+ model.eval()
18
+
19
+ # === Transform (same as validation) ===
20
+ transform = transforms.Compose([
21
+ transforms.Lambda(lambda x: x.convert('RGB')),
22
+ transforms.Resize((224, 224)),
23
+ transforms.ToTensor(),
24
+ transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
25
+ ])
26
+
27
+ # === Prediction Function ===
28
+ def predict(image):
29
+ image = Image.fromarray(image)
30
+ input_tensor = transform(image).unsqueeze(0)
31
+ with torch.no_grad():
32
+ outputs = model(input_tensor)
33
+ _, pred = torch.max(outputs, 1)
34
+ prob = torch.softmax(outputs, dim=1)[0][pred.item()].item()
35
+ label = "Tumor: Yes" if pred.item() == 1 else "Tumor: No"
36
+ return f"{label} ({prob * 100:.2f}%)"
37
+
38
+ # === Gradio Interface (No examples) ===
39
+ interface = gr.Interface(
40
+ fn=predict,
41
+ inputs=gr.Image(type="numpy", label="Upload Brain Scan"),
42
+ outputs=gr.Label(label="Prediction"),
43
+ title="🧠 Tumor Detection",
44
+ description="Upload a brain MRI image to detect if a tumor is present (Yes or No)."
45
+ )
46
+
47
+ if __name__ == "__main__":
48
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio
4
+ Pillow
tumor_model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:547764ba3e5b6f4fe1187ec1a90438d2f6605de2b7d1aa9f3e5656f4d72c4dd1
3
+ size 45313338