Spaces:
Runtime error
Runtime error
import streamlit as st | |
from transformers import pipeline | |
from PIL import Image | |
import requests | |
# ======================= | |
# Caching the Model | |
# ======================= | |
def load_model(): | |
""" | |
Load the pre-trained skin cancer classification model. | |
Cached to prevent reloading on every app interaction. | |
""" | |
return pipeline("image-classification", model="Anwarkh1/Skin_Cancer-Image_Classification") | |
model = load_model() | |
# ======================= | |
# Functionality: Classify Skin Cancer | |
# ======================= | |
def classify_skin_cancer(image): | |
results = model(image) | |
label = results[0]['label'] | |
confidence = results[0]['score'] | |
explanation = f"The model predicts **{label}** with a confidence of {confidence:.2%}." | |
return label, confidence, explanation | |
# ======================= | |
# Functionality: Fetch Cancer Research Papers | |
# ======================= | |
def fetch_cancer_research(): | |
""" | |
Fetch the latest research papers related to skin cancer. | |
Cached to avoid repeated API calls. | |
""" | |
api_url = "https://api.semanticscholar.org/graph/v1/paper/search" | |
params = { | |
"query": "skin cancer research", | |
"fields": "title,abstract,url", | |
"limit": 5 | |
} | |
response = requests.get(api_url, params=params) | |
if response.status_code == 200: | |
papers = response.json().get("data", []) | |
summaries = [] | |
for paper in papers: | |
title = paper.get("title", "No Title") | |
abstract = paper.get("abstract", "No Abstract") | |
url = paper.get("url", "No URL") | |
summaries.append(f"**{title}**\n\n{abstract}\n\n[Read More]({url})") | |
return "\n\n---\n\n".join(summaries) | |
else: | |
return "Error fetching research papers. Please try again later." | |
# ======================= | |
# Streamlit Page Config | |
# ======================= | |
st.set_page_config( | |
page_title="AI-Powered Skin Cancer Detection", | |
page_icon="π©Ί", | |
layout="wide", | |
initial_sidebar_state="expanded" | |
) | |
st.sidebar.header("Navigation") | |
app_mode = st.sidebar.radio( | |
"Choose a feature", | |
["π Skin Cancer Classification", "π Latest Research Papers", "βΉοΈ About the Model"] | |
) | |
# ======================= | |
# Skin Cancer Classification | |
# ======================= | |
if app_mode == "π Skin Cancer Classification": | |
st.title("π Skin Cancer Classification") | |
st.write( | |
"Upload an image of the skin lesion, and the AI model will classify it as one of several types, " | |
"such as melanoma, basal cell carcinoma, or benign keratosis-like lesions." | |
) | |
uploaded_image = st.file_uploader("Upload a skin lesion image", type=["png", "jpg", "jpeg"]) | |
if uploaded_image: | |
image = Image.open(uploaded_image).convert('RGB') | |
st.image(image, caption="Uploaded Image", use_column_width=True) | |
# Perform classification | |
st.write("Classifying...") | |
label, confidence, explanation = classify_skin_cancer(image) | |
# Display results | |
st.markdown(f"### **Prediction**: {label}") | |
st.markdown(f"### **Confidence**: {confidence:.2%}") | |
st.markdown(f"### **Explanation**: {explanation}") | |
# ======================= | |
# Latest Research Papers | |
# ======================= | |
elif app_mode == "π Latest Research Papers": | |
st.title("π Latest Research Papers") | |
st.write( | |
"Fetch the latest research papers on skin cancer to stay updated on recent findings and innovations." | |
) | |
if st.button("Fetch Papers"): | |
with st.spinner("Fetching research papers..."): | |
summaries = fetch_cancer_research() | |
st.markdown(summaries) | |
# ======================= | |
# About the Model | |
# ======================= | |
elif app_mode == "βΉοΈ About the Model": | |
st.title("βΉοΈ About the Skin Cancer Detection Model") | |
st.markdown(""" | |
- **Model Architecture:** Vision Transformer (ViT) | |
- **Trained On:** Skin Cancer Dataset (ISIC) | |
- **Classes:** | |
- Benign keratosis-like lesions | |
- Basal cell carcinoma | |
- Actinic keratoses | |
- Vascular lesions | |
- Melanocytic nevi | |
- Melanoma | |
- Dermatofibroma | |
- **Performance Metrics:** | |
- **Validation Accuracy:** 96.95% | |
- **Train Accuracy:** 96.14% | |
- **Loss Function:** Cross-Entropy | |
""") | |
# ======================= | |
# Footer | |
# ======================= | |
st.sidebar.info(""" | |
Developed by **[mgbam](https://huggingface.co/mgbam)** | |
This app leverages state-of-the-art AI models for skin cancer detection and research insights. | |
""") | |