asmaa1 commited on
Commit
34b2cbb
·
verified ·
1 Parent(s): a1b3f81

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import gradio as gr
3
+ import numpy as np
4
+ from PIL import Image
5
+ from tensorflow.keras.models import load_model
6
+ # Load your trained model and label map
7
+ path="skinDiseaseDetection (1).h5"
8
+ model = load_model(path)
9
+ label_map={0: 'pigmented benign keratosis',
10
+ 1: 'melanoma',
11
+ 2: 'vascular lesion',
12
+ 3: 'actinic keratosis',
13
+ 4: 'squamous cell carcinoma',
14
+ 5: 'basal cell carcinoma',
15
+ 6: 'seborrheic keratosis',
16
+ 7: 'dermatofibroma',
17
+ 8: 'nevus'}
18
+
19
+ def predict_image(image):
20
+ """
21
+ Predict the class of an uploaded image of a skin lesion.
22
+
23
+ Parameters:
24
+ - image: The uploaded image in PIL format.
25
+
26
+ Returns:
27
+ - predicted_class: The name of the predicted class or an error message.
28
+ - confidence: The confidence score of the prediction (0 to 1) or None.
29
+ - prediction_type: The type of prediction or an error message.
30
+ """
31
+ if image is None:
32
+ return "Please upload an image.", None, "No Diagnosis"
33
+
34
+ # Preprocess the image
35
+ image = image.resize((100, 75)) # Resize to model's expected input size
36
+ image_array = np.asarray(image)
37
+ image_array = (image_array - np.mean(image_array)) / np.std(image_array) # Normalize
38
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
39
+
40
+ # Make prediction using the loaded model
41
+ predictions = model.predict(image_array)
42
+ predicted_index = np.argmax(predictions, axis=1)[0]
43
+ confidence = np.max(predictions)
44
+ predicted_class = label_map[predicted_index]
45
+
46
+ # Define classes for benign and malignant lesions
47
+ benign_classes = [
48
+ 'pigmented benign keratosis',
49
+ 'vascular lesion',
50
+ 'actinic keratosis',
51
+ 'seborrheic keratosis',
52
+ 'dermatofibroma',
53
+ 'nevus'
54
+ ]
55
+ malignant_classes = [
56
+ 'melanoma',
57
+ 'squamous cell carcinoma',
58
+ 'basal cell carcinoma'
59
+ ]
60
+
61
+ # Determine the type of prediction
62
+ if predicted_class in benign_classes:
63
+ prediction_type = 'Benign Neoplasm'
64
+ elif predicted_class in malignant_classes:
65
+ prediction_type = 'Malignant Neoplasm'
66
+ else:
67
+ prediction_type = 'Unknown Neoplasm'
68
+
69
+ return predicted_class, float(f"{confidence:.2f}"), prediction_type # Format confidence here
70
+
71
+ # Example images and their descriptions
72
+ examples = [
73
+ ["/content/sample_data/bcc.jpg"],
74
+ ["/content/sample_data/d.jpg"],
75
+ ["/content/sample_data/m.PNG"],
76
+ ["/content/sample_data/sck.jpg"]
77
+ ]
78
+
79
+ # Example images and their descriptions
80
+ examples = [
81
+ ["/content/sample_data/bcc.jpg"],
82
+ ["/content/sample_data/d.jpg"],
83
+ ["/content/sample_data/m.PNG"],
84
+ ["/content/sample_data/sck.jpg"]
85
+ ]
86
+
87
+ # Set up the Gradio interface
88
+ iface = gr.Interface(
89
+ fn=predict_image,
90
+ inputs=gr.Image(type="pil", label="Upload Skin Lesion Image"),
91
+ outputs=[
92
+ gr.Label(label="Predicted Class"),
93
+ gr.Number(label="Confidence Score", precision=2),
94
+ gr.Textbox(label="Diagnosis Type", interactive=False)
95
+ ],
96
+ title="Skin Cancer Image Classification",
97
+ description="Upload an image of a skin lesion to predict its type and determine if the diagnosis is Benign or Malignant.",
98
+ theme="default", # Use a valid theme like "default" or "compact"
99
+ allow_flagging="never", # Optional: Disable flagging for a cleaner UI
100
+ examples=examples ,
101
+ css="""
102
+ .gradio-container {
103
+ font-family: 'Arial', sans-serif;
104
+ background-color: #ddebf7;
105
+ border-radius: 10px;
106
+ padding: 20px;
107
+ }
108
+ .gr-button {
109
+ background-color: #4CAF50;
110
+ color: white;
111
+ }
112
+ .gr-image {
113
+ border: 2px solid #ddd;
114
+ border-radius: 10px;
115
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
116
+ }
117
+ .output-textbox {
118
+ font-size: 1.2em;
119
+ text-align: center;
120
+ color: #333;
121
+ }
122
+ """,
123
+ )
124
+
125
+ # Launch the Gradio interface
126
+ iface.launch()