Anthony-Ml commited on
Commit
1c07639
1 Parent(s): b5e8c6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -13
app.py CHANGED
@@ -1,24 +1,12 @@
1
  import gradio as gr
 
2
  from fastai.vision.all import *
3
  from efficientnet_pytorch import EfficientNet
4
 
5
- #learn = load_learner('model/predictcovidfastaifinal18102023.pkl')
6
- learn = load_learner('model/final_20102023_eb7_model.pkl')
7
-
8
- categories = learn.dls.vocab
9
 
10
- def predict_image(get_image):
11
- pred, idx, probs = learn.predict(get_image)
12
- return dict(zip(categories, map(float, probs)))
13
 
14
  title = "COVID_19 Infection Detectation App!"
15
  head = (
16
- "<h1>"
17
- "<centre>"
18
- #"<img src='covid/Xray_Image_SuperImposed_Gradcam.png'>"
19
- "<img src='Xray_Image_SuperImposed_Gradcam.png' alt='Xray GradCam' width='400' height='400'>"
20
- "</centre>"
21
- "</h1>"
22
  "<div>"
23
  "This Space demonstrates model based on efficientnet base model. I has been trained to classify chest xray image."
24
  " "
@@ -36,6 +24,45 @@ examples = [
36
  ['covid/covid_1031.png']
37
  ]
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  interpretation="default"
40
  enable_queue=True
41
 
 
1
  import gradio as gr
2
+ import shap
3
  from fastai.vision.all import *
4
  from efficientnet_pytorch import EfficientNet
5
 
 
 
 
 
6
 
 
 
 
7
 
8
  title = "COVID_19 Infection Detectation App!"
9
  head = (
 
 
 
 
 
 
10
  "<div>"
11
  "This Space demonstrates model based on efficientnet base model. I has been trained to classify chest xray image."
12
  " "
 
24
  ['covid/covid_1031.png']
25
  ]
26
 
27
+ # Load the SHAP explainer for your model
28
+ explainer = shap.Explainer(model, data=None)
29
+
30
+ # Define a function to compute Shapley values for an image
31
+ def compute_shapley(image):
32
+ # Preprocess the input image (e.g., resize, normalize) to match the model's input requirements
33
+ # You should replace this with your specific preprocessing code
34
+ image = preprocess_image(image)
35
+
36
+ # Compute Shapley values for the input image
37
+ shap_values = explainer(image)
38
+
39
+ # Convert the Shapley values to a format that can be displayed (e.g., a heatmap)
40
+ shap_values_image = shap.image_plot(shap_values[0], image)
41
+
42
+ return shap_values_image
43
+
44
+ # Define the Gradio interface
45
+ image_input = gr.inputs.Image(shape=(224, 224)) # Adjust the shape as needed
46
+ shapley_output = gr.outputs.Image()
47
+
48
+ def interpreter_func(input_text):
49
+ if input_text == "Compute Shapley":
50
+ return compute_shapley(image_input)
51
+ else:
52
+ return None
53
+
54
+ interpreter_input = gr.inputs.Textbox(default="Type 'Compute Shapley' and click the button")
55
+ interpreter_button = "Generate Shapley"
56
+
57
+ #learn = load_learner('model/predictcovidfastaifinal18102023.pkl')
58
+ learn = load_learner('model/final_20102023_eb7_model.pkl')
59
+
60
+ categories = learn.dls.vocab
61
+
62
+ def predict_image(get_image):
63
+ pred, idx, probs = learn.predict(get_image)
64
+ return dict(zip(categories, map(float, probs)))
65
+
66
  interpretation="default"
67
  enable_queue=True
68