Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# Import libraries and dependencies for the UI and deep learning model
|
2 |
import streamlit as st
|
3 |
import tensorflow as tf
|
4 |
import numpy as np
|
@@ -6,85 +5,101 @@ from PIL import Image
|
|
6 |
from tensorflow import keras
|
7 |
import os
|
8 |
import warnings
|
9 |
-
import random
|
10 |
|
11 |
-
|
12 |
-
warnings.filterwarnings("ignore")
|
13 |
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
)
|
21 |
|
22 |
-
|
23 |
-
st.
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
with st.sidebar:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
keras.utils.set_random_seed(42)
|
51 |
-
model = from_pretrained_keras("ryefoxlime/PneumoniaDetection")
|
52 |
-
return model
|
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 |
-
prediction_label = class_names[np.argmax(predictions)]
|
85 |
-
|
86 |
-
if prediction_label == "Normal":
|
87 |
-
st.balloons()
|
88 |
-
st.success("Detected Disease: " + prediction_label)
|
89 |
-
else:
|
90 |
-
st.warning("Detected Disease: " + prediction_label)
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
|
|
5 |
from tensorflow import keras
|
6 |
import os
|
7 |
import warnings
|
|
|
8 |
|
9 |
+
warnings.filterwarnings('ignore')
|
|
|
10 |
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
11 |
|
12 |
+
class ChestAI:
|
13 |
+
def __init__(self):
|
14 |
+
self.setup_page_config()
|
15 |
+
self.load_model()
|
16 |
+
self.setup_sidebar()
|
17 |
+
self.run_interface()
|
18 |
|
19 |
+
def setup_page_config(self):
|
20 |
+
st.set_page_config(
|
21 |
+
page_title="ChestAI - Pneumonia Detection",
|
22 |
+
page_icon="π«",
|
23 |
+
initial_sidebar_state="expanded"
|
24 |
+
)
|
25 |
+
|
26 |
+
st.markdown("""
|
27 |
+
<style>
|
28 |
+
#MainMenu {visibility: hidden;}
|
29 |
+
footer {visibility: hidden;}
|
30 |
+
.stDeployButton {display:none;}
|
31 |
+
</style>
|
32 |
+
""", unsafe_allow_html=True)
|
33 |
|
34 |
+
@st.cache_resource
|
35 |
+
def load_model(self):
|
36 |
+
from huggingface_hub import from_pretrained_keras
|
37 |
+
keras.utils.set_random_seed(42)
|
38 |
+
self.model = from_pretrained_keras("ryefoxlime/PneumoniaDetection")
|
39 |
|
40 |
+
def setup_sidebar(self):
|
41 |
+
with st.sidebar:
|
42 |
+
st.title("π Welcome to ChestAI")
|
43 |
+
st.markdown("""
|
44 |
+
### About
|
45 |
+
ChestAI uses advanced deep learning to detect pneumonia in chest X-rays.
|
46 |
+
|
47 |
+
### How to use
|
48 |
+
1. Upload a chest X-ray image (JPG/PNG)
|
49 |
+
2. Wait for the analysis
|
50 |
+
3. View the results and confidence score
|
51 |
+
|
52 |
+
### Note
|
53 |
+
This tool is for educational purposes only. Always consult healthcare professionals for medical advice.
|
54 |
+
""")
|
55 |
|
56 |
+
def preprocess_image(self, image):
|
57 |
+
img_array = keras.preprocessing.image.img_to_array(image)
|
58 |
+
img_array = np.expand_dims(img_array, axis=0) / 255.0
|
59 |
+
return img_array
|
60 |
|
61 |
+
def get_prediction(self, image):
|
62 |
+
processed_image = self.preprocess_image(image)
|
63 |
+
prediction = self.model.predict(processed_image)
|
64 |
+
return prediction
|
|
|
|
|
|
|
65 |
|
66 |
+
def display_results(self, prediction):
|
67 |
+
class_names = ["Normal", "Pneumonia"]
|
68 |
+
prediction_label = class_names[np.argmax(prediction)]
|
69 |
+
confidence = float(max(prediction[0]) * 100)
|
70 |
|
71 |
+
st.markdown("### Analysis Results")
|
72 |
+
progress_bar = st.progress(0)
|
73 |
+
progress_bar.progress(confidence / 100)
|
74 |
+
|
75 |
+
if prediction_label == "Normal":
|
76 |
+
st.success(f"Result: {prediction_label}")
|
77 |
+
st.balloons()
|
78 |
+
else:
|
79 |
+
st.warning(f"Result: {prediction_label}")
|
80 |
+
|
81 |
+
st.info(f"Confidence: {confidence:.2f}%")
|
82 |
|
83 |
+
def run_interface(self):
|
84 |
+
st.title("π« ChestAI - Pneumonia Detection")
|
85 |
+
st.markdown("""
|
86 |
+
Upload a chest X-ray image to detect the presence of pneumonia.
|
87 |
+
The system will analyze the image and provide results with confidence scores.
|
88 |
+
""")
|
89 |
|
90 |
+
uploaded_file = st.file_uploader("Upload Chest X-ray Image", type=["jpg", "png"])
|
91 |
+
|
92 |
+
if uploaded_file is not None:
|
93 |
+
image = keras.preprocessing.image.load_img(
|
94 |
+
uploaded_file,
|
95 |
+
target_size=(224, 224),
|
96 |
+
color_mode='rgb'
|
97 |
+
)
|
98 |
+
st.image(image, caption="Uploaded X-ray Image", use_column_width=True)
|
99 |
+
|
100 |
+
with st.spinner("Analyzing image..."):
|
101 |
+
prediction = self.get_prediction(image)
|
102 |
+
self.display_results(prediction)
|
103 |
+
|
104 |
+
if __name__ == "__main__":
|
105 |
+
app = ChestAI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|