Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -27,13 +27,12 @@ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
|
27 |
|
28 |
|
29 |
def prediction_cls(prediction):
|
30 |
-
for key, clss in class_names.items():
|
31 |
-
if np.argmax(prediction) == clss:
|
32 |
return key
|
33 |
|
34 |
|
35 |
with st.sidebar:
|
36 |
-
# st.image("mg.png")
|
37 |
st.title("ChestAI")
|
38 |
st.markdown("""
|
39 |
### About
|
@@ -50,18 +49,29 @@ with st.sidebar:
|
|
50 |
st.set_option("deprecation.showfileUploaderEncoding", False)
|
51 |
|
52 |
|
53 |
-
@st.cache_resource()
|
54 |
def load_model():
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
model = load_model()
|
64 |
|
|
|
|
|
|
|
|
|
65 |
file = st.file_uploader(" ", type=["jpg", "png"])
|
66 |
|
67 |
|
@@ -77,22 +87,26 @@ def import_and_predict(image_data, model):
|
|
77 |
if file is None:
|
78 |
st.text("Please upload an image file")
|
79 |
else:
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
def prediction_cls(prediction):
|
30 |
+
for key, clss in class_names.items():
|
31 |
+
if np.argmax(prediction) == clss:
|
32 |
return key
|
33 |
|
34 |
|
35 |
with st.sidebar:
|
|
|
36 |
st.title("ChestAI")
|
37 |
st.markdown("""
|
38 |
### About
|
|
|
49 |
st.set_option("deprecation.showfileUploaderEncoding", False)
|
50 |
|
51 |
|
52 |
+
@st.cache_resource(show_spinner=False)
|
53 |
def load_model():
|
54 |
+
try:
|
55 |
+
from huggingface_hub import from_pretrained_keras
|
56 |
+
config = tf.compat.v1.ConfigProto()
|
57 |
+
config.gpu_options.allow_growth = True
|
58 |
+
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))
|
59 |
+
|
60 |
+
keras.utils.set_random_seed(42)
|
61 |
+
model = from_pretrained_keras("ryefoxlime/PneumoniaDetection")
|
62 |
+
return model
|
63 |
+
except Exception as e:
|
64 |
+
st.error(f"Error loading model: {str(e)}")
|
65 |
+
return None
|
66 |
+
|
67 |
+
|
68 |
+
with st.spinner("Model is being loaded..."):
|
69 |
model = load_model()
|
70 |
|
71 |
+
if model is None:
|
72 |
+
st.error("Failed to load model. Please try again.")
|
73 |
+
st.stop()
|
74 |
+
|
75 |
file = st.file_uploader(" ", type=["jpg", "png"])
|
76 |
|
77 |
|
|
|
87 |
if file is None:
|
88 |
st.text("Please upload an image file")
|
89 |
else:
|
90 |
+
try:
|
91 |
+
image = keras.preprocessing.image.load_img(file, target_size=(224, 224), color_mode='rgb')
|
92 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
93 |
+
predictions = import_and_predict(image, model)
|
94 |
+
|
95 |
+
class_names = [
|
96 |
+
"Normal",
|
97 |
+
"PNEUMONIA",
|
98 |
+
]
|
99 |
+
|
100 |
+
confidence = float(max(predictions[0]) * 100)
|
101 |
+
prediction_label = class_names[np.argmax(predictions)]
|
102 |
+
|
103 |
+
st.info(f"Confidence: {confidence:.2f}%")
|
104 |
+
|
105 |
+
if prediction_label == "Normal":
|
106 |
+
st.balloons()
|
107 |
+
st.success(f"Result: {prediction_label}")
|
108 |
+
else:
|
109 |
+
st.warning(f"Result: {prediction_label}")
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
st.error(f"Error processing image: {str(e)}")
|