File size: 2,792 Bytes
d167cec
 
 
 
 
fa9e49b
d167cec
 
 
 
fa9e49b
d167cec
 
6d6b205
 
 
 
d167cec
 
ed6f105
 
 
 
d167cec
 
 
ed6f105
d167cec
 
6d6b205
ec84b54
6d6b205
 
65847f9
6d6b205
 
 
 
65847f9
6d6b205
 
 
d167cec
 
3481d08
d167cec
3481d08
 
 
1c1deb5
 
3481d08
 
ed6f105
3481d08
 
 
d167cec
 
3481d08
ed6f105
3481d08
 
d167cec
 
 
 
 
ed6f105
d167cec
 
 
 
 
 
 
3481d08
 
 
 
 
 
 
ed6f105
3481d08
ed6f105
3481d08
 
 
 
 
ed6f105
3481d08
ed6f105
1
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import streamlit as st
import tensorflow as tf
from PIL import Image
from tensorflow import keras
import numpy as np
import os

import warnings

warnings.filterwarnings("ignore")
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'

st.set_page_config(
    page_title="ChestAI - Pneumonia Detection",
    page_icon="🫁",
    initial_sidebar_state="auto",
)

hide_streamlit_style = """
    <style>
    #MainMenu {visibility: hidden;}
    footer {visibility: hidden;}
    </style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)

class_names = ["Normal", "PNEUMONIA"]

with st.sidebar:
    st.title("ChestAI")
    st.markdown("""
        ### About
        ChestAI uses advanced deep learning to detect pneumonia in chest X-rays.
                
        ### How to use
        1. Upload a chest X-ray image (JPG/PNG)
        2. Wait for the analysis
        3. View the results and confidence score
                
        ### Note
        This tool is for educational purposes only. Always consult healthcare professionals for medical advice.
    """)
st.set_option("deprecation.showfileUploaderEncoding", False)

@st.cache_resource(show_spinner=False)
def load_model():
    try:
        from huggingface_hub import from_pretrained_keras
        keras.utils.set_random_seed(42)
        # Load the model from Hugging Face
        model = from_pretrained_keras("aaravlovescodes/PneumoniaDetection")
        return model
    except Exception as e:
        st.error(f"Error loading model from Hugging Face: {str(e)}")
        return None

with st.spinner("Model is being loaded..."):
    model = load_model()

if model is None:
    st.error("Failed to load model from Hugging Face. Please check the model name or path.")
    st.stop()

file = st.file_uploader(" ", type=["jpg", "png"])

def import_and_predict(image_data, model):
    img_array = keras.preprocessing.image.img_to_array(image_data)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = img_array / 255

    predictions = model.predict(img_array)
    return predictions

if file is None:
    st.text("Please upload an image file")
else:
    try:
        image = keras.preprocessing.image.load_img(file, target_size=(224, 224), color_mode='rgb')
        st.image(image, caption="Uploaded Image.", use_column_width=True)
        predictions = import_and_predict(image, model)

        confidence = float(max(predictions[0]) * 100)
        prediction_label = class_names[np.argmax(predictions)]

        st.info(f"Confidence: {confidence:.2f}%")

        if prediction_label == "Normal":
            st.balloons()
            st.success(f"Result: {prediction_label}")
        else:
            st.warning(f"Result: {prediction_label}")

    except Exception as e:
        st.error(f"Error processing image: {str(e)}")