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