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