Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ import numpy as np
|
|
| 13 |
from tensorflow.keras.models import load_model
|
| 14 |
from tensorflow.keras.preprocessing import image
|
| 15 |
import matplotlib.pyplot as plt
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
# ------------------------- PAGE CONFIG -------------------------
|
|
@@ -44,35 +45,37 @@ if app_mode == "π Home":
|
|
| 44 |
elif app_mode == "π©» X-ray Classifier":
|
| 45 |
st.header("π©Ί Chest X-ray COVID Prediction")
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
if uploaded_image:
|
| 50 |
-
with st.spinner("π Predicting..."):
|
| 51 |
-
# β
Show files in directory to debug model path
|
| 52 |
-
st.write("π Files in directory:", os.listdir())
|
| 53 |
-
|
| 54 |
-
try:
|
| 55 |
-
# β
Attempt to load model
|
| 56 |
-
model = load_model("covid_xray_model.keras")
|
| 57 |
-
st.success("β
Model loaded successfully!")
|
| 58 |
-
|
| 59 |
-
# β
Process image and predict
|
| 60 |
-
img = image.load_img(uploaded_image, target_size=(224, 224))
|
| 61 |
-
img_array = image.img_to_array(img) / 255.0
|
| 62 |
-
img_array = np.expand_dims(img_array, axis=0)
|
| 63 |
-
|
| 64 |
-
pred = model.predict(img_array)[0]
|
| 65 |
-
labels = ['COVID', 'NORMAL', 'Viral Pneumonia']
|
| 66 |
-
result = labels[np.argmax(pred)]
|
| 67 |
-
|
| 68 |
-
col1, col2 = st.columns([1, 2])
|
| 69 |
-
with col1:
|
| 70 |
-
st.image(uploaded_image, caption="Uploaded X-ray", use_container_width=True)
|
| 71 |
-
with col2:
|
| 72 |
-
st.success(f"π§ **Predicted Condition:** `{result}`")
|
| 73 |
-
|
| 74 |
-
except Exception as e:
|
| 75 |
-
st.error(f"π« Prediction failed. Error: `{e}`")
|
| 76 |
|
| 77 |
|
| 78 |
|
|
|
|
| 13 |
from tensorflow.keras.models import load_model
|
| 14 |
from tensorflow.keras.preprocessing import image
|
| 15 |
import matplotlib.pyplot as plt
|
| 16 |
+
from huggingface_hub import hf_hub_download
|
| 17 |
|
| 18 |
|
| 19 |
# ------------------------- PAGE CONFIG -------------------------
|
|
|
|
| 45 |
elif app_mode == "π©» X-ray Classifier":
|
| 46 |
st.header("π©Ί Chest X-ray COVID Prediction")
|
| 47 |
|
| 48 |
+
uploaded_image = st.file_uploader("π€ Upload Chest X-ray", type=["jpg", "jpeg", "png"])
|
| 49 |
+
|
| 50 |
+
if uploaded_image:
|
| 51 |
+
with st.spinner("π Predicting..."):
|
| 52 |
+
try:
|
| 53 |
+
# β
Download model from Hugging Face Hub (only once, then cached)
|
| 54 |
+
model_path = hf_hub_download(
|
| 55 |
+
repo_id="nehda10/covid-xray-model", # π Your HF model repo
|
| 56 |
+
filename="covid_xray_model.keras"
|
| 57 |
+
)
|
| 58 |
+
model = load_model(model_path)
|
| 59 |
+
st.success("β
Model loaded successfully!")
|
| 60 |
+
|
| 61 |
+
# β
Process image and predict
|
| 62 |
+
img = image.load_img(uploaded_image, target_size=(224, 224))
|
| 63 |
+
img_array = image.img_to_array(img) / 255.0
|
| 64 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 65 |
+
|
| 66 |
+
pred = model.predict(img_array)[0]
|
| 67 |
+
labels = ['COVID', 'NORMAL', 'Viral Pneumonia']
|
| 68 |
+
result = labels[np.argmax(pred)]
|
| 69 |
+
|
| 70 |
+
col1, col2 = st.columns([1, 2])
|
| 71 |
+
with col1:
|
| 72 |
+
st.image(uploaded_image, caption="Uploaded X-ray", use_container_width=True)
|
| 73 |
+
with col2:
|
| 74 |
+
st.success(f"π§ **Predicted Condition:** `{result}`")
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
st.error(f"π« Prediction failed. Error: `{e}`")
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
|