| import streamlit as st
|
| import tensorflow as tf
|
| from tensorflow.keras.models import load_model
|
| from PIL import Image
|
| import numpy as np
|
| from huggingface_hub import hf_hub_download
|
| import os
|
|
|
|
|
|
|
| HF_REPO_ID = "saad1BM/pneumonia-detection-system"
|
| MODEL_FILENAME = "pneumonia_detection_model.keras"
|
| MODEL_PATH = MODEL_FILENAME
|
|
|
| IMAGE_SIZE = (224, 224)
|
| CLASS_NAMES = ['NORMAL', 'PNEUMONIA']
|
|
|
|
|
|
|
| @st.cache_resource
|
| def load_pneumonia_model():
|
| """Model ko Hugging Face se download aur load karta hai."""
|
|
|
|
|
| try:
|
| with st.spinner(f"Downloading model file ({MODEL_FILENAME}) from Hugging Face..."):
|
|
|
| model_download_path = hf_hub_download(
|
| repo_id=HF_REPO_ID,
|
| filename=MODEL_FILENAME,
|
|
|
| cache_dir=os.path.join(os.getcwd(), ".hf_cache")
|
| )
|
|
|
| st.success("Model downloaded successfully!")
|
|
|
|
|
| model = load_model(model_download_path)
|
| return model
|
|
|
| except Exception as e:
|
| st.error(f"Error loading model from Hugging Face: {e}")
|
| st.info("Please make sure the repo is Public and model file name is correct.")
|
| return None
|
|
|
|
|
| model = load_pneumonia_model()
|
|
|
|
|
|
|
| st.set_page_config(page_title="Pneumonia Detection System", layout="centered")
|
|
|
| st.title("🫁 Pneumonia Detection System (AI Powered)")
|
| st.caption("Upload a chest X-ray image to predict Normal or Pneumonia.")
|
| st.markdown("---")
|
|
|
| uploaded_file = st.file_uploader("Choose a Chest X-ray Image...", type=["jpg", "jpeg", "png"])
|
|
|
| if uploaded_file is not None:
|
|
|
| image = Image.open(uploaded_file).convert("RGB")
|
| st.image(image, caption='Uploaded X-ray Image', use_column_width=True)
|
|
|
|
|
| if st.button("Detect Pneumonia"):
|
| if model is None:
|
| st.error("Model is not loaded. Cannot perform detection.")
|
| st.stop()
|
|
|
| st.subheader("📊 Prediction Result")
|
|
|
| with st.spinner('Analyzing X-ray image...'):
|
|
|
| img = image.resize(IMAGE_SIZE)
|
| img_array = np.array(img) / 255.0
|
| img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
|
| predictions = model.predict(img_array)
|
|
|
| score = tf.nn.softmax(predictions[0])
|
|
|
|
|
| predicted_class_index = np.argmax(score)
|
| predicted_class = CLASS_NAMES[predicted_class_index]
|
| confidence = np.max(score) * 100
|
|
|
|
|
| if predicted_class == 'PNEUMONIA':
|
| st.error(f"### ⚠️ Prediction: {predicted_class}")
|
| st.markdown(f"**Confidence:** **{confidence:.2f}%**")
|
|
|
| else:
|
| st.success(f"### ✅ Prediction: {predicted_class}")
|
| st.markdown(f"**Confidence:** **{confidence:.2f}%**")
|
|
|
|
|
| st.markdown("---")
|
| st.bar_chart({
|
| "Normal": score[0].numpy(),
|
| "Pneumonia": score[1].numpy()
|
| }) |