mgbam's picture
Update app.py
5d58e79 verified
raw
history blame
4.45 kB
import gradio as gr
import requests
from transformers import pipeline
from PIL import Image
# Load the Skin Cancer Image Classification model
classifier = gr.load("models/Anwarkh1/Skin_Cancer-Image_Classification")
# Functionality: Classify Skin Cancer Image
def classify_skin_cancer(image):
results = classifier(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 Latest Cancer Research Papers
def fetch_cancer_research():
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."
# Functionality: Provide Patient-Friendly Explanation
def generate_explanation(label, confidence):
if label.lower() == "melanoma":
message = (
f"The prediction is **Melanoma**, with a confidence of **{confidence:.2%}**. "
f"This type of skin cancer is potentially serious and requires immediate medical attention. "
f"Please consult a dermatologist for further evaluation and treatment."
)
elif label.lower() == "benign keratosis-like lesions":
message = (
f"The prediction is **Benign Keratosis-like Lesion**, with a confidence of **{confidence:.2%}**. "
f"This is generally non-cancerous but can sometimes require medical observation. "
f"Consult a healthcare provider for a definitive diagnosis."
)
else:
message = (
f"The prediction is **{label}**, with a confidence of **{confidence:.2%}**. "
f"More detailed evaluation is recommended. Please consult a healthcare professional."
)
return message
# Gradio Multi-Application System (MAS)
with gr.Blocks() as mas:
gr.Markdown("# 🌍 AI-Powered Skin Cancer Detection and Research Assistant 🩺")
gr.Markdown(
"This multi-functional platform provides skin cancer classification, patient-friendly explanations, "
"and access to the latest research papers to empower healthcare and save lives."
)
with gr.Tab("πŸ” Skin Cancer Classification"):
with gr.Row():
image = gr.Image(type="pil", label="Upload Skin Image")
classify_button = gr.Button("Classify Image")
label = gr.Textbox(label="Predicted Label", interactive=False)
confidence = gr.Slider(label="Confidence", interactive=False, minimum=0, maximum=1, step=0.01)
explanation = gr.Textbox(label="Patient-Friendly Explanation", interactive=False)
classify_button.click(classify_skin_cancer, inputs=image, outputs=[label, confidence, explanation])
with gr.Tab("πŸ“„ Latest Research Papers"):
with gr.Row():
fetch_button = gr.Button("Fetch Latest Papers")
research_papers = gr.Markdown()
fetch_button.click(fetch_cancer_research, inputs=[], outputs=research_papers)
with gr.Tab("πŸ› οΈ Model Information"):
gr.Markdown("""
## Skin Cancer Image Classification Model
- **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
""")
with gr.Tab("ℹ️ About This Project"):
gr.Markdown("""
### About
This project is developed by **[mgbam](https://huggingface.co/mgbam)** to revolutionize cancer detection and research