aaravlovescodes commited on
Commit
ed6f105
·
verified ·
1 Parent(s): 8dba6ca

Update app.py

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