File size: 4,831 Bytes
06b57f4
5d58e79
06b57f4
 
 
5d58e79
 
06b57f4
5d58e79
 
06b57f4
 
 
 
 
 
5d58e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06b57f4
5d58e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06b57f4
5d58e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06b57f4
5d58e79
 
 
17ec5d6
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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 accessibility.  
        #### Features:
        - State-of-the-art AI-powered skin cancer detection.
        - Explanations designed for patients and clinicians.
        - Access to the latest research insights for global collaboration.
        """)
        gr.Markdown("🎯 Let's work together to save lives and make healthcare accessible for all.")

# Launch the MAS
mas.launch()