Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline, AutoModelForImageClassification, AutoFeatureExtractor
|
3 |
from PIL import Image
|
4 |
-
import openai
|
5 |
-
import os
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
|
8 |
-
# =======================
|
9 |
-
# Load Environment Variables from .env File
|
10 |
-
# =======================
|
11 |
-
load_dotenv() # Explicitly load the .env file
|
12 |
-
|
13 |
-
# Set OpenAI API key
|
14 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
15 |
-
|
16 |
-
# Debugging: Check if API key is loaded
|
17 |
-
if not openai.api_key or not openai.api_key.startswith("sk-"):
|
18 |
-
st.error("OpenAI API key is not set or is invalid. Please check the `.env` file or your environment variable setup.")
|
19 |
-
st.stop()
|
20 |
|
21 |
# =======================
|
22 |
# Streamlit Page Config
|
@@ -35,7 +19,6 @@ st.set_page_config(
|
|
35 |
def load_model():
|
36 |
"""
|
37 |
Load the pre-trained skin cancer classification model using PyTorch.
|
38 |
-
Use the AutoModelForImageClassification and AutoFeatureExtractor for explicit local caching.
|
39 |
"""
|
40 |
try:
|
41 |
extractor = AutoFeatureExtractor.from_pretrained("Anwarkh1/Skin_Cancer-Image_Classification")
|
@@ -48,28 +31,33 @@ def load_model():
|
|
48 |
model = load_model()
|
49 |
|
50 |
# =======================
|
51 |
-
#
|
52 |
# =======================
|
53 |
-
def
|
54 |
"""
|
55 |
-
Generate a
|
56 |
"""
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
69 |
)
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
73 |
|
74 |
# =======================
|
75 |
# Streamlit App Title and Sidebar
|
@@ -116,8 +104,7 @@ if uploaded_image:
|
|
116 |
st.error("Low confidence in the prediction. Results should be interpreted with caution.")
|
117 |
|
118 |
# Generate explanation
|
119 |
-
|
120 |
-
explanation = generate_openai_explanation(label, confidence)
|
121 |
|
122 |
st.markdown("### Explanation")
|
123 |
st.write(explanation)
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline, AutoModelForImageClassification, AutoFeatureExtractor
|
3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# =======================
|
6 |
# Streamlit Page Config
|
|
|
19 |
def load_model():
|
20 |
"""
|
21 |
Load the pre-trained skin cancer classification model using PyTorch.
|
|
|
22 |
"""
|
23 |
try:
|
24 |
extractor = AutoFeatureExtractor.from_pretrained("Anwarkh1/Skin_Cancer-Image_Classification")
|
|
|
31 |
model = load_model()
|
32 |
|
33 |
# =======================
|
34 |
+
# Local Explanation Generator
|
35 |
# =======================
|
36 |
+
def generate_local_explanation(label, confidence):
|
37 |
"""
|
38 |
+
Generate a simple explanation for the classification result.
|
39 |
"""
|
40 |
+
explanations = {
|
41 |
+
"Melanoma": (
|
42 |
+
"Melanoma is a serious type of skin cancer that develops in the cells that produce melanin. "
|
43 |
+
"If detected early, it is often treatable. You should consult a dermatologist immediately."
|
44 |
+
),
|
45 |
+
"Basal Cell Carcinoma": (
|
46 |
+
"Basal Cell Carcinoma is a common form of skin cancer that grows slowly and is typically not life-threatening. "
|
47 |
+
"Still, it requires medical attention to prevent further complications."
|
48 |
+
),
|
49 |
+
"Benign Lesion": (
|
50 |
+
"A benign lesion is a non-cancerous growth on the skin. While it is usually harmless, "
|
51 |
+
"consulting a dermatologist can help ensure no further treatment is needed."
|
52 |
+
),
|
53 |
+
"Other": (
|
54 |
+
"The AI could not confidently classify the lesion. It's strongly recommended to consult a dermatologist for further evaluation."
|
55 |
)
|
56 |
+
}
|
57 |
+
|
58 |
+
explanation = explanations.get(label, explanations["Other"])
|
59 |
+
confidence_msg = f"The model is {confidence:.2%} confident in this prediction. "
|
60 |
+
return confidence_msg + explanation
|
61 |
|
62 |
# =======================
|
63 |
# Streamlit App Title and Sidebar
|
|
|
104 |
st.error("Low confidence in the prediction. Results should be interpreted with caution.")
|
105 |
|
106 |
# Generate explanation
|
107 |
+
explanation = generate_local_explanation(label, confidence)
|
|
|
108 |
|
109 |
st.markdown("### Explanation")
|
110 |
st.write(explanation)
|