aaravlovescodes commited on
Commit
3481d08
·
verified ·
1 Parent(s): 6d6b205

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -31
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(): # create a dictionary of the output classes
31
- if np.argmax(prediction) == clss: # check the class
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
- from huggingface_hub import from_pretrained_keras
56
-
57
- keras.utils.set_random_seed(42)
58
- model = from_pretrained_keras("ryefoxlime/PneumoniaDetection")
59
- return model
60
-
61
-
62
- with st.spinner("Model is being loaded.."):
 
 
 
 
 
 
 
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
- image = keras.preprocessing.image.load_img(file, target_size=(224, 224), color_mode='rgb')
81
- st.image(image, caption="Uploaded Image.", use_column_width=True)
82
- predictions = import_and_predict(image, model)
83
- np.random.seed(42)
84
- x = random.randint(98, 99) + random.randint(0, 99) * 0.01
85
- st.error("Accuracy : " + str(x) + " %")
86
- print(predictions)
87
- class_names = [
88
- "Normal",
89
- "PNEUMONIA",
90
- ]
91
-
92
- string = "Detected Disease : " + class_names[np.argmax(predictions)]
93
- if class_names[np.argmax(predictions)] == "Normal":
94
- st.balloons()
95
- st.success(string)
96
-
97
- elif class_names[np.argmax(predictions)] == "PNEUMONIA":
98
- st.warning(string)
 
 
 
 
 
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)}")