File size: 2,625 Bytes
a01d067
6f7162d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
511a7d1
9067926
c7789fd
6f7162d
9067926
02ff9a3
6f7162d
 
 
 
 
 
511a7d1
6f7162d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec45d6e
6f7162d
 
 
 
 
 
 
 
 
 
 
 
 
 
ec45d6e
6f7162d
 
 
 
511a7d1
6f7162d
 
 
ec45d6e
493d10c
 
 
2ed31ba
3c22726
493d10c
6f7162d
511a7d1
6f7162d
 
 
 
e9ee064
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import numpy as np
import gradio as gr
import tensorflow as tf
from tensorflow import keras
from huggingface_hub import from_pretrained_keras

IMAGE_SIZE = (256, 256)

# Load model from HF
model = from_pretrained_keras(
    pretrained_model_name_or_path="fbadine/image-spam-detection"
)

# This is the predict function that takes as input an array-like-image and produces
# the probabilities that this image is either spam or ham
def predict(image):
    # Add the batch axis
    image = tf.expand_dims(image, axis=0)
    
    # Predict
    pred = model.predict(image)
    prob = float(pred[0][0])

    scoring_output = {
        "Spam": prob,
        "Ham": 1 - prob 
    }
    
    return scoring_output

# Clear Input and outpout
def clear_inputs_and_outputs():
    return [None, None, None]

# Main function
if __name__ == "__main__":
    demo = gr.Blocks()

    with demo:
        gr.Markdown(
            """
            <center><h1>Image Spam Detection</h1></center> \
            This space is a demo of a proof of concept POC Image Spam Detection<br> \
    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>
            """
        )

        with gr.Row():
            with gr.Column():
                # Input
                image_input = gr.Image(
                    shape=(256,256),
                    source="upload",
                    label="Upload an Image"
                )
                with gr.Row():
                    clr_btn = gr.Button(value="Clear", variant="secondary")
                    prd_btn = gr.Button(value="Predict")

            with gr.Column():
                # Output
                lbl_output = gr.Label(label="Prediction")

        clr_btn.click(
            fn=clear_inputs_and_outputs,
            inputs=[],
            outputs=[image_input, lbl_output],
        )
        prd_btn.click(
            fn=predict,
            inputs=[image_input],
            outputs=[lbl_output],
        )

        gr.Examples(
            #examples=[os.path.join(os.path.dirname(__file__), "ShowLetter.jpg")],
            examples=[
                os.path.join(os.path.curdir, "examples", "sample1.jpg"),
                os.path.join(os.path.curdir, "examples", "sample2.jpg"),
                os.path.join(os.path.curdir, "examples", "sample3.jpg"),
                os.path.join(os.path.curdir, "examples", "sample4.png")
            ],
            inputs=image_input,
            outputs=lbl_output,
            fn=predict,
            cache_examples=True,
        )

    demo.launch(debug=True, share=False)