Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,100 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
-
import requests
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
classifier =
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
results = classifier(image)
|
| 11 |
label = results[0]['label']
|
| 12 |
confidence = results[0]['score']
|
| 13 |
explanation = f"The model predicts **{label}** with a confidence of {confidence:.2%}."
|
| 14 |
return label, confidence, explanation
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
with gr.Tab("
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
from transformers import pipeline
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
+
# Load the Skin Cancer Image Classification model
|
| 7 |
+
classifier = gr.load("models/Anwarkh1/Skin_Cancer-Image_Classification")
|
| 8 |
|
| 9 |
+
# Functionality: Classify Skin Cancer Image
|
| 10 |
+
def classify_skin_cancer(image):
|
| 11 |
results = classifier(image)
|
| 12 |
label = results[0]['label']
|
| 13 |
confidence = results[0]['score']
|
| 14 |
explanation = f"The model predicts **{label}** with a confidence of {confidence:.2%}."
|
| 15 |
return label, confidence, explanation
|
| 16 |
|
| 17 |
+
# Functionality: Fetch Latest Cancer Research Papers
|
| 18 |
+
def fetch_cancer_research():
|
| 19 |
+
api_url = "https://api.semanticscholar.org/graph/v1/paper/search"
|
| 20 |
+
params = {
|
| 21 |
+
"query": "skin cancer research",
|
| 22 |
+
"fields": "title,abstract,url",
|
| 23 |
+
"limit": 5
|
| 24 |
+
}
|
| 25 |
+
response = requests.get(api_url, params=params)
|
| 26 |
+
if response.status_code == 200:
|
| 27 |
+
papers = response.json().get("data", [])
|
| 28 |
+
summaries = []
|
| 29 |
+
for paper in papers:
|
| 30 |
+
title = paper.get("title", "No Title")
|
| 31 |
+
abstract = paper.get("abstract", "No Abstract")
|
| 32 |
+
url = paper.get("url", "No URL")
|
| 33 |
+
summaries.append(f"**{title}**\n\n{abstract}\n\n[Read More]({url})")
|
| 34 |
+
return "\n\n---\n\n".join(summaries)
|
| 35 |
+
else:
|
| 36 |
+
return "Error fetching research papers. Please try again later."
|
| 37 |
|
| 38 |
+
# Functionality: Provide Patient-Friendly Explanation
|
| 39 |
+
def generate_explanation(label, confidence):
|
| 40 |
+
if label.lower() == "melanoma":
|
| 41 |
+
message = (
|
| 42 |
+
f"The prediction is **Melanoma**, with a confidence of **{confidence:.2%}**. "
|
| 43 |
+
f"This type of skin cancer is potentially serious and requires immediate medical attention. "
|
| 44 |
+
f"Please consult a dermatologist for further evaluation and treatment."
|
| 45 |
+
)
|
| 46 |
+
elif label.lower() == "benign keratosis-like lesions":
|
| 47 |
+
message = (
|
| 48 |
+
f"The prediction is **Benign Keratosis-like Lesion**, with a confidence of **{confidence:.2%}**. "
|
| 49 |
+
f"This is generally non-cancerous but can sometimes require medical observation. "
|
| 50 |
+
f"Consult a healthcare provider for a definitive diagnosis."
|
| 51 |
+
)
|
| 52 |
+
else:
|
| 53 |
+
message = (
|
| 54 |
+
f"The prediction is **{label}**, with a confidence of **{confidence:.2%}**. "
|
| 55 |
+
f"More detailed evaluation is recommended. Please consult a healthcare professional."
|
| 56 |
+
)
|
| 57 |
+
return message
|
| 58 |
+
|
| 59 |
+
# Gradio Multi-Application System (MAS)
|
| 60 |
+
with gr.Blocks() as mas:
|
| 61 |
+
gr.Markdown("# π AI-Powered Skin Cancer Detection and Research Assistant π©Ί")
|
| 62 |
+
gr.Markdown(
|
| 63 |
+
"This multi-functional platform provides skin cancer classification, patient-friendly explanations, "
|
| 64 |
+
"and access to the latest research papers to empower healthcare and save lives."
|
| 65 |
+
)
|
| 66 |
|
| 67 |
+
with gr.Tab("π Skin Cancer Classification"):
|
| 68 |
+
with gr.Row():
|
| 69 |
+
image = gr.Image(type="pil", label="Upload Skin Image")
|
| 70 |
+
classify_button = gr.Button("Classify Image")
|
| 71 |
+
|
| 72 |
+
label = gr.Textbox(label="Predicted Label", interactive=False)
|
| 73 |
+
confidence = gr.Slider(label="Confidence", interactive=False, minimum=0, maximum=1, step=0.01)
|
| 74 |
+
explanation = gr.Textbox(label="Patient-Friendly Explanation", interactive=False)
|
| 75 |
+
|
| 76 |
+
classify_button.click(classify_skin_cancer, inputs=image, outputs=[label, confidence, explanation])
|
| 77 |
+
|
| 78 |
+
with gr.Tab("π Latest Research Papers"):
|
| 79 |
+
with gr.Row():
|
| 80 |
+
fetch_button = gr.Button("Fetch Latest Papers")
|
| 81 |
+
|
| 82 |
+
research_papers = gr.Markdown()
|
| 83 |
+
fetch_button.click(fetch_cancer_research, inputs=[], outputs=research_papers)
|
| 84 |
+
|
| 85 |
+
with gr.Tab("π οΈ Model Information"):
|
| 86 |
+
gr.Markdown("""
|
| 87 |
+
## Skin Cancer Image Classification Model
|
| 88 |
+
- **Model Architecture:** Vision Transformer (ViT)
|
| 89 |
+
- **Trained On:** Skin Cancer Dataset (ISIC)
|
| 90 |
+
- **Classes:** Benign keratosis-like lesions, Basal cell carcinoma, Actinic keratoses, Vascular lesions, Melanocytic nevi, Melanoma, Dermatofibroma
|
| 91 |
+
- **Performance Metrics:**
|
| 92 |
+
- **Validation Accuracy:** 96.95%
|
| 93 |
+
- **Train Accuracy:** 96.14%
|
| 94 |
+
- **Loss Function:** Cross-Entropy
|
| 95 |
+
""")
|
| 96 |
|
| 97 |
+
with gr.Tab("βΉοΈ About This Project"):
|
| 98 |
+
gr.Markdown("""
|
| 99 |
+
### About
|
| 100 |
+
This project is developed by **[mgbam](https://huggingface.co/mgbam)** to revolutionize cancer detection and research
|