mgbam commited on
Commit
5d58e79
Β·
verified Β·
1 Parent(s): db88443

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -26
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 pre-trained model for cancer image classification
7
- classifier = pipeline("image-classification", model="Anwarkh1/Skin_Cancer-Image_Classification")
8
 
9
- def classify_image(image):
 
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
- def fetch_research():
17
- url = "https://api.semanticscholar.org/graph/v1/paper/search"
18
- params = {"query": "cancer research", "fields": "title,abstract,url", "limit": 5}
19
- response = requests.get(url, params=params)
20
- papers = response.json().get("data", [])
21
- return "\n".join([f"{paper['title']}: {paper['url']}" for paper in papers])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Gradio Interface
24
- with gr.Blocks() as demo:
25
- gr.Markdown("# AI-Powered Universal Cancer Detection and Research Assistant 🌍🩺")
26
-
27
- with gr.Tab("Cancer Detection"):
28
- image = gr.Image(label="Upload Cancer Image")
29
- label = gr.Textbox(label="Predicted Label")
30
- confidence = gr.Slider(label="Confidence")
31
- explanation = gr.Textbox(label="Explanation")
32
- detect_btn = gr.Button("Classify")
33
- detect_btn.click(classify_image, inputs=[image], outputs=[label, confidence, explanation])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- with gr.Tab("Research Papers"):
36
- fetch_btn = gr.Button("Fetch Research Papers")
37
- papers = gr.Textbox(label="Latest Research Papers", lines=5)
38
- fetch_btn.click(fetch_research, inputs=[], outputs=papers)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- demo.launch()
 
 
 
 
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