Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import tensorflow as tf
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from tensorflow import keras
|
| 5 |
import numpy as np
|
|
@@ -17,14 +18,19 @@ st.set_page_config(
|
|
| 17 |
)
|
| 18 |
|
| 19 |
hide_streamlit_style = """
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
"""
|
| 25 |
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
with st.sidebar:
|
| 30 |
st.title("ChestAI")
|
|
@@ -42,35 +48,40 @@ with st.sidebar:
|
|
| 42 |
""")
|
| 43 |
st.set_option("deprecation.showfileUploaderEncoding", False)
|
| 44 |
|
|
|
|
| 45 |
@st.cache_resource(show_spinner=False)
|
| 46 |
def load_model():
|
| 47 |
try:
|
| 48 |
-
from huggingface_hub import from_pretrained_keras
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
| 52 |
return model
|
| 53 |
except Exception as e:
|
| 54 |
-
st.error(f"Error loading model
|
| 55 |
return None
|
| 56 |
|
|
|
|
| 57 |
with st.spinner("Model is being loaded..."):
|
| 58 |
model = load_model()
|
| 59 |
|
| 60 |
if model is None:
|
| 61 |
-
st.error("Failed to load model
|
| 62 |
st.stop()
|
| 63 |
|
| 64 |
file = st.file_uploader(" ", type=["jpg", "png"])
|
| 65 |
|
|
|
|
| 66 |
def import_and_predict(image_data, model):
|
| 67 |
img_array = keras.preprocessing.image.img_to_array(image_data)
|
| 68 |
img_array = np.expand_dims(img_array, axis=0)
|
| 69 |
-
img_array = img_array
|
| 70 |
|
| 71 |
predictions = model.predict(img_array)
|
| 72 |
return predictions
|
| 73 |
|
|
|
|
| 74 |
if file is None:
|
| 75 |
st.text("Please upload an image file")
|
| 76 |
else:
|
|
@@ -78,17 +89,22 @@ else:
|
|
| 78 |
image = keras.preprocessing.image.load_img(file, target_size=(224, 224), color_mode='rgb')
|
| 79 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 80 |
predictions = import_and_predict(image, model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
confidence = float(max(predictions[0]) * 100)
|
| 83 |
prediction_label = class_names[np.argmax(predictions)]
|
| 84 |
-
|
| 85 |
st.info(f"Confidence: {confidence:.2f}%")
|
| 86 |
-
|
| 87 |
if prediction_label == "Normal":
|
| 88 |
st.balloons()
|
| 89 |
st.success(f"Result: {prediction_label}")
|
| 90 |
else:
|
| 91 |
st.warning(f"Result: {prediction_label}")
|
| 92 |
-
|
| 93 |
except Exception as e:
|
| 94 |
-
st.error(f"Error processing image: {str(e)}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tensorflow as tf
|
| 3 |
+
import random
|
| 4 |
from PIL import Image
|
| 5 |
from tensorflow import keras
|
| 6 |
import numpy as np
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
hide_streamlit_style = """
|
| 21 |
+
<style>
|
| 22 |
+
#MainMenu {visibility: hidden;}
|
| 23 |
+
footer {visibility: hidden;}
|
| 24 |
+
</style>
|
| 25 |
"""
|
| 26 |
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.title("ChestAI")
|
|
|
|
| 48 |
""")
|
| 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, hf_hub_download
|
| 56 |
+
|
| 57 |
+
# Download the model files directly
|
| 58 |
+
model_path = hf_hub_download(repo_id="ryefoxlime/PneumoniaDetection", filename="model.keras")
|
| 59 |
+
model = keras.models.load_model(model_path)
|
| 60 |
return model
|
| 61 |
except Exception as e:
|
| 62 |
+
st.error(f"Error loading model: {str(e)}")
|
| 63 |
return None
|
| 64 |
|
| 65 |
+
|
| 66 |
with st.spinner("Model is being loaded..."):
|
| 67 |
model = load_model()
|
| 68 |
|
| 69 |
if model is None:
|
| 70 |
+
st.error("Failed to load model. Please try again.")
|
| 71 |
st.stop()
|
| 72 |
|
| 73 |
file = st.file_uploader(" ", type=["jpg", "png"])
|
| 74 |
|
| 75 |
+
|
| 76 |
def import_and_predict(image_data, model):
|
| 77 |
img_array = keras.preprocessing.image.img_to_array(image_data)
|
| 78 |
img_array = np.expand_dims(img_array, axis=0)
|
| 79 |
+
img_array = img_array/255
|
| 80 |
|
| 81 |
predictions = model.predict(img_array)
|
| 82 |
return predictions
|
| 83 |
|
| 84 |
+
|
| 85 |
if file is None:
|
| 86 |
st.text("Please upload an image file")
|
| 87 |
else:
|
|
|
|
| 89 |
image = keras.preprocessing.image.load_img(file, target_size=(224, 224), color_mode='rgb')
|
| 90 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 91 |
predictions = import_and_predict(image, model)
|
| 92 |
+
|
| 93 |
+
class_names = [
|
| 94 |
+
"Normal",
|
| 95 |
+
"PNEUMONIA",
|
| 96 |
+
]
|
| 97 |
|
| 98 |
confidence = float(max(predictions[0]) * 100)
|
| 99 |
prediction_label = class_names[np.argmax(predictions)]
|
| 100 |
+
|
| 101 |
st.info(f"Confidence: {confidence:.2f}%")
|
| 102 |
+
|
| 103 |
if prediction_label == "Normal":
|
| 104 |
st.balloons()
|
| 105 |
st.success(f"Result: {prediction_label}")
|
| 106 |
else:
|
| 107 |
st.warning(f"Result: {prediction_label}")
|
| 108 |
+
|
| 109 |
except Exception as e:
|
| 110 |
+
st.error(f"Error processing image: {str(e)}")
|