fbadine commited on
Commit
511a7d1
1 Parent(s): a552137

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -29
app.py CHANGED
@@ -12,21 +12,9 @@ model = from_pretrained_keras(
12
  pretrained_model_name_or_path="fbadine/image-spam-detection"
13
  )
14
 
15
- ####### To be removed
16
- def get_txt_output(arr):
17
- txt = f"Image shape: {arr.shape}\n"
18
- txt += f"Min: {np.min(arr)}, Max: {np.max(arr)}\n"
19
- txt += f"Image hash: {hash(tuple(arr.reshape(-1)))}\n\n"
20
-
21
- return txt
22
- ####### End
23
-
24
  # This is the predict function that takes as input an array-like-image and produces
25
  # the probabilities that this image is either spam or ham
26
  def predict(image):
27
- ####### To be removed
28
- txt_output = get_txt_output(image)
29
- ####### End
30
  # Resize image
31
  resized_image = keras.layers.Resizing(
32
  IMAGE_SIZE[0],
@@ -34,27 +22,20 @@ def predict(image):
34
  interpolation="bilinear",
35
  crop_to_aspect_ratio=True
36
  )(image)
37
- resized_image = tf.expand_dims(resized_image, axis=0)
38
 
39
- ####### To be removed
40
- txt_output += get_txt_output(resized_image.numpy())
41
- ####### End
42
 
43
  # Predict
44
  pred = model.predict(resized_image)
45
  prob = float(pred[0][0])
46
 
47
- ####### To be removed
48
- txt_output += f"Probability: {prob}"
49
- ####### End
50
-
51
  scoring_output = {
52
  "Spam": prob,
53
  "Ham": 1 - prob
54
  }
55
 
56
- #return scoring_output
57
- return [scoring_output, txt_output]
58
 
59
  # Clear Input and outpout
60
  def clear_inputs_and_outputs():
@@ -88,9 +69,6 @@ if __name__ == "__main__":
88
  with gr.Column():
89
  # Output
90
  lbl_output = gr.Label(label="Prediction")
91
- ####### To be removed
92
- txt_output = gr.TextArea(label="info_out")
93
- ####### End
94
 
95
  clr_btn.click(
96
  fn=clear_inputs_and_outputs,
@@ -100,8 +78,7 @@ if __name__ == "__main__":
100
  prd_btn.click(
101
  fn=predict,
102
  inputs=[image_input],
103
- #outputs=[lbl_output],
104
- outputs=[lbl_output, txt_output],
105
  )
106
 
107
  gr.Examples(
@@ -111,8 +88,7 @@ if __name__ == "__main__":
111
  os.path.join(os.path.curdir, "examples", "sample2.jpg"),
112
  ],
113
  inputs=image_input,
114
- #outputs=lbl_output,
115
- outputs=[lbl_output, txt_output],
116
  fn=predict,
117
  cache_examples=True,
118
  )
 
12
  pretrained_model_name_or_path="fbadine/image-spam-detection"
13
  )
14
 
 
 
 
 
 
 
 
 
 
15
  # This is the predict function that takes as input an array-like-image and produces
16
  # the probabilities that this image is either spam or ham
17
  def predict(image):
 
 
 
18
  # Resize image
19
  resized_image = keras.layers.Resizing(
20
  IMAGE_SIZE[0],
 
22
  interpolation="bilinear",
23
  crop_to_aspect_ratio=True
24
  )(image)
 
25
 
26
+ # Add the batch axis
27
+ resized_image = tf.expand_dims(resized_image, axis=0)
 
28
 
29
  # Predict
30
  pred = model.predict(resized_image)
31
  prob = float(pred[0][0])
32
 
 
 
 
 
33
  scoring_output = {
34
  "Spam": prob,
35
  "Ham": 1 - prob
36
  }
37
 
38
+ return scoring_output
 
39
 
40
  # Clear Input and outpout
41
  def clear_inputs_and_outputs():
 
69
  with gr.Column():
70
  # Output
71
  lbl_output = gr.Label(label="Prediction")
 
 
 
72
 
73
  clr_btn.click(
74
  fn=clear_inputs_and_outputs,
 
78
  prd_btn.click(
79
  fn=predict,
80
  inputs=[image_input],
81
+ outputs=[lbl_output],
 
82
  )
83
 
84
  gr.Examples(
 
88
  os.path.join(os.path.curdir, "examples", "sample2.jpg"),
89
  ],
90
  inputs=image_input,
91
+ outputs=lbl_output,
 
92
  fn=predict,
93
  cache_examples=True,
94
  )