Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,81 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# β
Fix permission issues on Hugging Face Spaces
|
| 4 |
+
os.environ["STREAMLIT_HOME"] = "/home/user/app"
|
| 5 |
+
os.environ["XDG_CONFIG_HOME"] = "/home/user/app"
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
import torch
|
| 9 |
+
from torchvision import transforms
|
| 10 |
+
from PIL import Image
|
| 11 |
+
from TumorModel import TumorClassification, GliomaStageModel
|
| 12 |
+
|
| 13 |
+
# π― Load classification model
|
| 14 |
+
tumor_model = TumorClassification()
|
| 15 |
+
tumor_model.load_state_dict(torch.load("BTD_model.pth", map_location=torch.device("cpu")))
|
| 16 |
+
tumor_model.eval()
|
| 17 |
+
|
| 18 |
+
# π― Load glioma stage model
|
| 19 |
+
glioma_model = GliomaStageModel()
|
| 20 |
+
glioma_model.load_state_dict(torch.load("glioma_stages.pth", map_location=torch.device("cpu")))
|
| 21 |
+
glioma_model.eval()
|
| 22 |
+
|
| 23 |
+
# π Class labels
|
| 24 |
+
tumor_labels = ['glioma', 'meningioma', 'notumor', 'pituitary']
|
| 25 |
+
stage_labels = ['Stage 1', 'Stage 2', 'Stage 3', 'Stage 4']
|
| 26 |
+
|
| 27 |
+
# π Image Transform
|
| 28 |
+
transform = transforms.Compose([
|
| 29 |
+
transforms.Grayscale(),
|
| 30 |
+
transforms.Resize((224, 224)),
|
| 31 |
+
transforms.ToTensor(),
|
| 32 |
+
transforms.Normalize(mean=[0.5], std=[0.5])
|
| 33 |
+
])
|
| 34 |
+
|
| 35 |
+
# π§ Tumor prediction
|
| 36 |
+
def predict_tumor(image: Image.Image) -> str:
|
| 37 |
+
image = transform(image).unsqueeze(0)
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
output = tumor_model(image)
|
| 40 |
+
pred = torch.argmax(output, dim=1).item()
|
| 41 |
+
return tumor_labels[pred]
|
| 42 |
+
|
| 43 |
+
# 𧬠Glioma stage prediction
|
| 44 |
+
def predict_stage(gender, age, idh1, tp53, atrx, pten, egfr, cic, pik3ca):
|
| 45 |
+
gender_val = 0 if gender == "Male" else 1
|
| 46 |
+
features = [gender_val, age, idh1, tp53, atrx, pten, egfr, cic, pik3ca]
|
| 47 |
+
x = torch.tensor(features).float().unsqueeze(0)
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
out = glioma_model(x)
|
| 50 |
+
pred = torch.argmax(out, dim=1).item()
|
| 51 |
+
return stage_labels[pred]
|
| 52 |
+
|
| 53 |
+
# π¨ Streamlit UI
|
| 54 |
+
st.title("π§ Brain Tumor Detection & Glioma Stage Predictor")
|
| 55 |
+
|
| 56 |
+
tab1, tab2 = st.tabs(["Tumor Type Detector", "Glioma Stage Predictor"])
|
| 57 |
+
|
| 58 |
+
with tab1:
|
| 59 |
+
st.header("πΌοΈ Upload an MRI Image")
|
| 60 |
+
uploaded_file = st.file_uploader("Choose an MRI image", type=["jpg", "jpeg", "png"])
|
| 61 |
+
if uploaded_file:
|
| 62 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 63 |
+
st.image(image, caption="Uploaded MRI", use_column_width=True)
|
| 64 |
+
prediction = predict_tumor(image)
|
| 65 |
+
st.success(f"π§ Predicted Tumor Type: **{prediction.upper()}**")
|
| 66 |
+
|
| 67 |
+
with tab2:
|
| 68 |
+
st.header("𧬠Patient Genetic and Demographic Information")
|
| 69 |
+
gender = st.radio("Gender", ["Male", "Female"])
|
| 70 |
+
age = st.slider("Age", 1, 100, 30)
|
| 71 |
+
idh1 = st.slider("IDH1 Mutation", 0, 1, 0)
|
| 72 |
+
tp53 = st.slider("TP53 Mutation", 0, 1, 0)
|
| 73 |
+
atrx = st.slider("ATRX Mutation", 0, 1, 0)
|
| 74 |
+
pten = st.slider("PTEN Mutation", 0, 1, 0)
|
| 75 |
+
egfr = st.slider("EGFR Mutation", 0, 1, 0)
|
| 76 |
+
cic = st.slider("CIC Mutation", 0, 1, 0)
|
| 77 |
+
pik3ca = st.slider("PIK3CA Mutation", 0, 1, 0)
|
| 78 |
+
|
| 79 |
+
if st.button("Predict Glioma Stage"):
|
| 80 |
+
stage = predict_stage(gender, age, idh1, tp53, atrx, pten, egfr, cic, pik3ca)
|
| 81 |
+
st.success(f"π Predicted Glioma Stage: **{stage}**")
|