Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
|
6 |
-
#
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
model_name = "syaha/skin_cancer_detection_model"
|
@@ -13,54 +13,55 @@ def load_model():
|
|
13 |
|
14 |
model, feature_extractor = load_model()
|
15 |
|
16 |
-
# App title
|
17 |
-
st.title("Skin Cancer Detection
|
18 |
st.write("""
|
19 |
-
Upload an image of the affected skin area, and this app will
|
20 |
""")
|
21 |
|
22 |
-
# File uploader
|
23 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
24 |
|
25 |
if uploaded_file:
|
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 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
|
6 |
+
# Cache the model and feature extractor
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
model_name = "syaha/skin_cancer_detection_model"
|
|
|
13 |
|
14 |
model, feature_extractor = load_model()
|
15 |
|
16 |
+
# App title and instructions
|
17 |
+
st.title("Skin Cancer Detection App")
|
18 |
st.write("""
|
19 |
+
Upload an image of the affected skin area, and this app will classify it based on pre-trained skin lesion types.
|
20 |
""")
|
21 |
|
22 |
+
# File uploader for user input
|
23 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
24 |
|
25 |
if uploaded_file:
|
26 |
+
try:
|
27 |
+
# Display the uploaded image
|
28 |
+
image = Image.open(uploaded_file)
|
29 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
30 |
+
|
31 |
+
# Preprocess the image
|
32 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
33 |
+
|
34 |
+
# Perform inference
|
35 |
+
with torch.no_grad():
|
36 |
+
outputs = model(**inputs)
|
37 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
38 |
+
predicted_class = probabilities.argmax().item()
|
39 |
+
confidence = probabilities.max().item()
|
40 |
|
41 |
+
# Map predicted class to human-readable labels
|
42 |
+
labels = {
|
43 |
+
0: "Melanocytic nevi",
|
44 |
+
1: "Melanoma",
|
45 |
+
2: "Benign keratosis-like lesions",
|
46 |
+
3: "Basal cell carcinoma",
|
47 |
+
4: "Actinic keratoses",
|
48 |
+
5: "Vascular lesions",
|
49 |
+
6: "Dermatofibroma"
|
50 |
+
}
|
51 |
+
diagnosis = labels.get(predicted_class, "Unknown")
|
52 |
|
53 |
+
# Display results
|
54 |
+
st.subheader("Results")
|
55 |
+
st.write(f"**Diagnosis:** {diagnosis}")
|
56 |
+
st.write(f"**Confidence Level:** {confidence:.2%}")
|
57 |
+
|
58 |
+
# Provide an option to download the report
|
59 |
+
report = f"Diagnosis: {diagnosis}\nConfidence Level: {confidence:.2%}"
|
60 |
+
st.download_button(
|
61 |
+
label="Download Report",
|
62 |
+
data=report,
|
63 |
+
file_name="report.txt",
|
64 |
+
mime="text/plain",
|
65 |
+
)
|
66 |
+
except Exception as e:
|
67 |
+
st.error(f"An error occurred while processing the image: {e}")
|