FridayMaster commited on
Commit
28690ba
·
verified ·
1 Parent(s): 06b68d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -21
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
- import requests
4
- from io import BytesIO
5
  from PIL import Image
 
 
6
 
7
  # Define models and their validation accuracies
8
  model_options = {
@@ -16,13 +18,35 @@ model_options = {
16
  }
17
  }
18
 
19
- # Load the model from Hugging Face repo
20
- def load_model(model_path):
21
- # Here you would use the Hugging Face `transformers` library to load your model.
22
- # However, since these are `.h5` models (likely Keras models), use the appropriate loader.
23
- # This example assumes you have a custom loader function for Keras models.
24
- from tensorflow.keras.models import load_model
25
- return load_model(model_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  def main():
28
  st.title("Pneumonia Detection App")
@@ -32,8 +56,12 @@ def main():
32
  model_accuracy = model_options[model_name]["accuracy"]
33
 
34
  # Load the selected model
35
- model = load_model(model_path)
36
-
 
 
 
 
37
  st.write(f"Model: {model_name}")
38
  st.write(f"Validation Accuracy: {model_accuracy}%")
39
 
@@ -44,15 +72,15 @@ def main():
44
  st.image(image, caption="Uploaded Image", use_column_width=True)
45
 
46
  # Perform prediction using the model
47
- # This part depends on how your model expects input.
48
- # Here, you would preprocess the image and perform prediction.
49
- # For example:
50
- # img_array = preprocess_image(image)
51
- # prediction = model.predict(img_array)
52
- # st.write("Prediction:", prediction)
53
-
54
- # Example placeholder for prediction output
55
- st.write("Prediction: [Placeholder for actual prediction]")
56
 
57
  if __name__ == "__main__":
58
  main()
 
 
1
  import streamlit as st
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.utils import CustomObjectScope
4
+ from tensorflow.keras.initializers import glorot_uniform
5
  from PIL import Image
6
+ import numpy as np
7
+ import io
8
 
9
  # Define models and their validation accuracies
10
  model_options = {
 
18
  }
19
  }
20
 
21
+ # Load the model with custom objects if necessary
22
+ def load_model_with_custom_objects(model_path):
23
+ if not os.path.isfile(model_path):
24
+ raise FileNotFoundError(f"Model file not found: {model_path}")
25
+
26
+ custom_objects = {
27
+ 'GlorotUniform': glorot_uniform
28
+ # Add other custom objects if needed
29
+ }
30
+
31
+ try:
32
+ with CustomObjectScope(custom_objects):
33
+ model = load_model(model_path)
34
+ except Exception as e:
35
+ st.error(f"Error loading model: {str(e)}")
36
+ raise
37
+
38
+ return model
39
+
40
+ # Image preprocessing (adjust as needed for your model)
41
+ def preprocess_image(image):
42
+ # Convert image to grayscale and resize
43
+ image = image.convert('L') # Convert to grayscale if necessary
44
+ image = image.resize((64, 64)) # Resize to match the model input shape
45
+ image_array = np.array(image)
46
+ image_array = image_array.astype('float32') / 255.0 # Normalize
47
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
48
+ image_array = np.expand_dims(image_array, axis=-1) # Add channel dimension if needed
49
+ return image_array
50
 
51
  def main():
52
  st.title("Pneumonia Detection App")
 
56
  model_accuracy = model_options[model_name]["accuracy"]
57
 
58
  # Load the selected model
59
+ try:
60
+ model = load_model_with_custom_objects(model_path)
61
+ except Exception as e:
62
+ st.error(f"Failed to load model: {e}")
63
+ return
64
+
65
  st.write(f"Model: {model_name}")
66
  st.write(f"Validation Accuracy: {model_accuracy}%")
67
 
 
72
  st.image(image, caption="Uploaded Image", use_column_width=True)
73
 
74
  # Perform prediction using the model
75
+ img_array = preprocess_image(image)
76
+
77
+ try:
78
+ prediction = model.predict(img_array)
79
+ predicted_class = "Pneumonia" if prediction[0][0] > 0.5 else "Normal"
80
+ st.write(f"Prediction: {predicted_class}")
81
+ except Exception as e:
82
+ st.error(f"Error during prediction: {str(e)}")
 
83
 
84
  if __name__ == "__main__":
85
  main()
86
+