fbadine commited on
Commit
6f7162d
1 Parent(s): 6468d0e

Adding application file, requirements file as well as examples

Browse files
Files changed (4) hide show
  1. app.py +92 -0
  2. examples/sample1.jpg +0 -0
  3. examples/sample2.jpg +0 -0
  4. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ import tensorflow as tf
4
+ from tensorflow import keras
5
+ from huggingface_hub import from_pretrained_keras
6
+
7
+ IMAGE_SIZE = (256, 256)
8
+
9
+ # Load model from HF
10
+ model = from_pretrained_keras(
11
+ pretrained_model_name_or_path="fbadine/image-spam-detection"
12
+ )
13
+
14
+ # This is the predict function that takes as input an array-like-image and produces
15
+ # the probabilities that this image is either spam or ham
16
+ def predict(image):
17
+ # Resize image
18
+ resized_image = keras.layers.Resizing(
19
+ IMAGE_SIZE[0],
20
+ IMAGE_SIZE[1],
21
+ interpolation="bilinear",
22
+ crop_to_aspect_ratio=True
23
+ )(image)
24
+ resized_image = tf.expand_dims(resized_image, axis=0)
25
+
26
+ # Predict
27
+ pred = model.predict(resized_image)
28
+ prob = pred[0][0]
29
+
30
+ scoring_output = {
31
+ "Spam": prob,
32
+ "Ham": 1 - prob
33
+ }
34
+
35
+ return scoring_output
36
+
37
+ # Clear Input and outpout
38
+ def clear_inputs_and_outputs():
39
+ return [None, None, None]
40
+
41
+ # Main function
42
+ if __name__ == "__main__":
43
+ demo = gr.Blocks()
44
+
45
+ with demo:
46
+ gr.Markdown(
47
+ """
48
+ <center><h1>Image Spam Detection</h1></center> \
49
+ This space is a demo of a proof of concept POC Image Spam Detection<br> \
50
+ In this space, you can upload an image to check if it's spam or not or you can use of the provided samples <br><br>
51
+ """
52
+ )
53
+
54
+ with gr.Row():
55
+ with gr.Column():
56
+ # Input
57
+ image_input = gr.Image(
58
+ shape=IMAGE_SIZE,
59
+ source="upload",
60
+ label="Upload an Image"
61
+ )
62
+ with gr.Row():
63
+ clr_btn = gr.Button(value="Clear", variant="secondary")
64
+ prd_btn = gr.Button(value="Predict")
65
+
66
+ with gr.Column():
67
+ # Output
68
+ lbl_output = gr.Label(label="Prediction")
69
+
70
+ clr_btn.click(
71
+ fn=clear_inputs_and_outputs,
72
+ inputs=[],
73
+ outputs=[image_input, lbl_output, plt_output],
74
+ )
75
+ prd_btn.click(
76
+ fn=predict,
77
+ inputs=[image_input],
78
+ outputs=[lbl_output, plt_output],
79
+ )
80
+
81
+ gr.Examples(
82
+ examples=[
83
+ os.path.join(os.path.curdir, "examples", "sample1.jpg"),
84
+ os.path.join(os.path.curdir, "examples", "sample2.jpg"),
85
+ ],
86
+ inputs=image_input,
87
+ outputs=lbl_output,
88
+ fn=predict,
89
+ cache_examples=True,
90
+ )
91
+
92
+ demo.launch(debug=True, share=True)
examples/sample1.jpg ADDED
examples/sample2.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ numpy
2
+ tensorflow