Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load the model and feature extractor
|
| 7 |
+
model_name = "dewifaj/resnet18_alzheimer_classifier"
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 9 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Define the label mapping
|
| 12 |
+
label_mapping = model.config.id2label
|
| 13 |
+
|
| 14 |
+
def predict(image):
|
| 15 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = model(**inputs)
|
| 18 |
+
logits = outputs.logits
|
| 19 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 20 |
+
return label_mapping[predicted_class_idx]
|
| 21 |
+
|
| 22 |
+
# Streamlit app
|
| 23 |
+
st.title("Alzheimer Image Classification")
|
| 24 |
+
st.write("Upload an image to classify the stage of Alzheimer's disease.")
|
| 25 |
+
|
| 26 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 27 |
+
|
| 28 |
+
if uploaded_file is not None:
|
| 29 |
+
image = Image.open(uploaded_file)
|
| 30 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 31 |
+
st.write("")
|
| 32 |
+
st.write("Classifying...")
|
| 33 |
+
|
| 34 |
+
label = predict(image)
|
| 35 |
+
st.write(f"The model predicts: **{label}**")
|