Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,40 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def classify_skin_cancer(image):
|
| 9 |
-
results =
|
| 10 |
label = results[0]['label']
|
| 11 |
confidence = results[0]['score']
|
| 12 |
explanation = f"The model predicts **{label}** with a confidence of {confidence:.2%}."
|
| 13 |
return label, confidence, explanation
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
|
|
|
| 16 |
def fetch_cancer_research():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
api_url = "https://api.semanticscholar.org/graph/v1/paper/search"
|
| 18 |
params = {
|
| 19 |
"query": "skin cancer research",
|
|
@@ -33,75 +54,87 @@ def fetch_cancer_research():
|
|
| 33 |
else:
|
| 34 |
return "Error fetching research papers. Please try again later."
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
message = (
|
| 46 |
-
f"The prediction is **Benign Keratosis-like Lesion**, with a confidence of **{confidence:.2%}**. "
|
| 47 |
-
f"This is generally non-cancerous but can sometimes require medical observation. "
|
| 48 |
-
f"Consult a healthcare provider for a definitive diagnosis."
|
| 49 |
-
)
|
| 50 |
-
else:
|
| 51 |
-
message = (
|
| 52 |
-
f"The prediction is **{label}**, with a confidence of **{confidence:.2%}**. "
|
| 53 |
-
f"More detailed evaluation is recommended. Please consult a healthcare professional."
|
| 54 |
-
)
|
| 55 |
-
return message
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
with gr.Row():
|
| 67 |
-
image = gr.Image(type="pil", label="Upload Skin Image")
|
| 68 |
-
classify_button = gr.Button("Classify Image")
|
| 69 |
-
|
| 70 |
-
label = gr.Textbox(label="Predicted Label", interactive=False)
|
| 71 |
-
confidence = gr.Slider(label="Confidence", interactive=False, minimum=0, maximum=1, step=0.01)
|
| 72 |
-
explanation = gr.Textbox(label="Patient-Friendly Explanation", interactive=False)
|
| 73 |
-
|
| 74 |
-
classify_button.click(classify_skin_cancer, inputs=image, outputs=[label, confidence, explanation])
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
research_papers = gr.Markdown()
|
| 81 |
-
fetch_button.click(fetch_cancer_research, inputs=[], outputs=research_papers)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
- **Model Architecture:** Vision Transformer (ViT)
|
| 87 |
-
- **Trained On:** Skin Cancer Dataset (ISIC)
|
| 88 |
-
- **Classes:** Benign keratosis-like lesions, Basal cell carcinoma, Actinic keratoses, Vascular lesions, Melanocytic nevi, Melanoma, Dermatofibroma
|
| 89 |
-
- **Performance Metrics:**
|
| 90 |
-
- **Validation Accuracy:** 96.95%
|
| 91 |
-
- **Train Accuracy:** 96.14%
|
| 92 |
-
- **Loss Function:** Cross-Entropy
|
| 93 |
-
""")
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
###
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
import requests
|
| 5 |
|
| 6 |
+
# =======================
|
| 7 |
+
# Caching the Model
|
| 8 |
+
# =======================
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
"""
|
| 12 |
+
Load the pre-trained skin cancer classification model.
|
| 13 |
+
Cached to prevent reloading on every app interaction.
|
| 14 |
+
"""
|
| 15 |
+
return pipeline("image-classification", model="Anwarkh1/Skin_Cancer-Image_Classification")
|
| 16 |
|
| 17 |
+
model = load_model()
|
| 18 |
+
|
| 19 |
+
# =======================
|
| 20 |
+
# Functionality: Classify Skin Cancer
|
| 21 |
+
# =======================
|
| 22 |
def classify_skin_cancer(image):
|
| 23 |
+
results = model(image)
|
| 24 |
label = results[0]['label']
|
| 25 |
confidence = results[0]['score']
|
| 26 |
explanation = f"The model predicts **{label}** with a confidence of {confidence:.2%}."
|
| 27 |
return label, confidence, explanation
|
| 28 |
|
| 29 |
+
# =======================
|
| 30 |
+
# Functionality: Fetch Cancer Research Papers
|
| 31 |
+
# =======================
|
| 32 |
+
@st.cache_data
|
| 33 |
def fetch_cancer_research():
|
| 34 |
+
"""
|
| 35 |
+
Fetch the latest research papers related to skin cancer.
|
| 36 |
+
Cached to avoid repeated API calls.
|
| 37 |
+
"""
|
| 38 |
api_url = "https://api.semanticscholar.org/graph/v1/paper/search"
|
| 39 |
params = {
|
| 40 |
"query": "skin cancer research",
|
|
|
|
| 54 |
else:
|
| 55 |
return "Error fetching research papers. Please try again later."
|
| 56 |
|
| 57 |
+
# =======================
|
| 58 |
+
# Streamlit Page Config
|
| 59 |
+
# =======================
|
| 60 |
+
st.set_page_config(
|
| 61 |
+
page_title="AI-Powered Skin Cancer Detection",
|
| 62 |
+
page_icon="π©Ί",
|
| 63 |
+
layout="wide",
|
| 64 |
+
initial_sidebar_state="expanded"
|
| 65 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
st.sidebar.header("Navigation")
|
| 68 |
+
app_mode = st.sidebar.radio(
|
| 69 |
+
"Choose a feature",
|
| 70 |
+
["π Skin Cancer Classification", "π Latest Research Papers", "βΉοΈ About the Model"]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# =======================
|
| 74 |
+
# Skin Cancer Classification
|
| 75 |
+
# =======================
|
| 76 |
+
if app_mode == "π Skin Cancer Classification":
|
| 77 |
+
st.title("π Skin Cancer Classification")
|
| 78 |
+
st.write(
|
| 79 |
+
"Upload an image of the skin lesion, and the AI model will classify it as one of several types, "
|
| 80 |
+
"such as melanoma, basal cell carcinoma, or benign keratosis-like lesions."
|
| 81 |
+
)
|
| 82 |
|
| 83 |
+
uploaded_image = st.file_uploader("Upload a skin lesion image", type=["png", "jpg", "jpeg"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
if uploaded_image:
|
| 86 |
+
image = Image.open(uploaded_image).convert('RGB')
|
| 87 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# Perform classification
|
| 90 |
+
st.write("Classifying...")
|
| 91 |
+
label, confidence, explanation = classify_skin_cancer(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# Display results
|
| 94 |
+
st.markdown(f"### **Prediction**: {label}")
|
| 95 |
+
st.markdown(f"### **Confidence**: {confidence:.2%}")
|
| 96 |
+
st.markdown(f"### **Explanation**: {explanation}")
|
| 97 |
+
|
| 98 |
+
# =======================
|
| 99 |
+
# Latest Research Papers
|
| 100 |
+
# =======================
|
| 101 |
+
elif app_mode == "π Latest Research Papers":
|
| 102 |
+
st.title("π Latest Research Papers")
|
| 103 |
+
st.write(
|
| 104 |
+
"Fetch the latest research papers on skin cancer to stay updated on recent findings and innovations."
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
if st.button("Fetch Papers"):
|
| 108 |
+
with st.spinner("Fetching research papers..."):
|
| 109 |
+
summaries = fetch_cancer_research()
|
| 110 |
+
st.markdown(summaries)
|
| 111 |
+
|
| 112 |
+
# =======================
|
| 113 |
+
# About the Model
|
| 114 |
+
# =======================
|
| 115 |
+
elif app_mode == "βΉοΈ About the Model":
|
| 116 |
+
st.title("βΉοΈ About the Skin Cancer Detection Model")
|
| 117 |
+
st.markdown("""
|
| 118 |
+
- **Model Architecture:** Vision Transformer (ViT)
|
| 119 |
+
- **Trained On:** Skin Cancer Dataset (ISIC)
|
| 120 |
+
- **Classes:**
|
| 121 |
+
- Benign keratosis-like lesions
|
| 122 |
+
- Basal cell carcinoma
|
| 123 |
+
- Actinic keratoses
|
| 124 |
+
- Vascular lesions
|
| 125 |
+
- Melanocytic nevi
|
| 126 |
+
- Melanoma
|
| 127 |
+
- Dermatofibroma
|
| 128 |
+
- **Performance Metrics:**
|
| 129 |
+
- **Validation Accuracy:** 96.95%
|
| 130 |
+
- **Train Accuracy:** 96.14%
|
| 131 |
+
- **Loss Function:** Cross-Entropy
|
| 132 |
+
""")
|
| 133 |
|
| 134 |
+
# =======================
|
| 135 |
+
# Footer
|
| 136 |
+
# =======================
|
| 137 |
+
st.sidebar.info("""
|
| 138 |
+
Developed by **[mgbam](https://huggingface.co/mgbam)**
|
| 139 |
+
This app leverages state-of-the-art AI models for skin cancer detection and research insights.
|
| 140 |
+
""")
|