engrharis commited on
Commit
424c8a2
·
verified ·
1 Parent(s): 598ee99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -28
app.py CHANGED
@@ -4,52 +4,37 @@ from PIL import Image
4
  from tensorflow.keras.models import load_model
5
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
6
  import joblib
7
- from huggingface_hub import hf_hub_url, cached_download
8
 
 
 
 
9
 
10
- # Replace with your Space name (from the link)
11
- SPACE_NAME = "engrharis/Throat_Image_Classifier"
12
-
13
- # Assuming the filenames are the same as before
14
- KNN_MODEL_FILE = "knn_pharyngitis_model.pkl"
15
- EXTRACTOR_FILE = "mobilenetv2_feature_extractor.h5"
16
-
17
-
18
- def download_models(url, filename):
19
- """Downloads model files from Hugging Face space if not cached locally."""
20
- model_path = hf_hub_url(SPACE_NAME, filename=filename)
21
- if not cached_download(model_path):
22
- st.write(f"Downloading {filename}...")
23
- cached_download(model_path)
24
- st.write(f"{filename} downloaded successfully!")
25
-
26
-
27
- # Load the saved models (download if not cached)
28
- download_models(SPACE_NAME, KNN_MODEL_FILE)
29
- download_models(SPACE_NAME, EXTRACTOR_FILE)
30
-
31
- knn = joblib.load(KNN_MODEL_FILE)
32
- feature_extractor = load_model(EXTRACTOR_FILE)
33
-
34
 
 
35
  def preprocess_image(image):
36
  img = image.resize((224, 224)) # Resize to match MobileNetV2 input size
37
  img_array = np.array(img)
38
  img_array = preprocess_input(img_array) # Apply MobileNetV2 preprocessing
39
  return np.expand_dims(img_array, axis=0)
40
 
41
-
42
  def classify_image(image):
43
  processed_image = preprocess_image(image)
44
  features = feature_extractor.predict(processed_image)
45
  prediction = knn.predict(features)
46
  return "Pharyngitis" if prediction[0] == 1 else "No Pharyngitis"
47
 
48
-
49
  # Streamlit app UI
50
  st.title("Pharyngitis Classification App")
51
  st.write("Upload an image to classify it as 'Pharyngitis' or 'No Pharyngitis'.")
 
52
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
 
53
  if uploaded_file is not None:
54
  # Load the uploaded image
55
  image = Image.open(uploaded_file)
@@ -58,4 +43,4 @@ if uploaded_file is not None:
58
  # Classify the image
59
  st.write("Classifying...")
60
  prediction = classify_image(image)
61
- st.write(f"Prediction: **{prediction}**")
 
4
  from tensorflow.keras.models import load_model
5
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
6
  import joblib
 
7
 
8
+ # Paths to the saved models
9
+ KNN_MODEL_PATH = './knn_pharyngitis_model.pkl'
10
+ EXTRACTOR_PATH = './mobilenetv2_feature_extractor.h5'
11
 
12
+ # Load the saved models
13
+ st.write("Loading models...")
14
+ knn = joblib.load(KNN_MODEL_PATH)
15
+ feature_extractor = load_model(EXTRACTOR_PATH)
16
+ st.write("Models loaded successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Function to preprocess the uploaded image
19
  def preprocess_image(image):
20
  img = image.resize((224, 224)) # Resize to match MobileNetV2 input size
21
  img_array = np.array(img)
22
  img_array = preprocess_input(img_array) # Apply MobileNetV2 preprocessing
23
  return np.expand_dims(img_array, axis=0)
24
 
25
+ # Function to classify the image
26
  def classify_image(image):
27
  processed_image = preprocess_image(image)
28
  features = feature_extractor.predict(processed_image)
29
  prediction = knn.predict(features)
30
  return "Pharyngitis" if prediction[0] == 1 else "No Pharyngitis"
31
 
 
32
  # Streamlit app UI
33
  st.title("Pharyngitis Classification App")
34
  st.write("Upload an image to classify it as 'Pharyngitis' or 'No Pharyngitis'.")
35
+
36
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
37
+
38
  if uploaded_file is not None:
39
  # Load the uploaded image
40
  image = Image.open(uploaded_file)
 
43
  # Classify the image
44
  st.write("Classifying...")
45
  prediction = classify_image(image)
46
+ st.write(f"Prediction: **{prediction}**")