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): # Resize image resized_image = keras.layers.Resizing( IMAGE_SIZE[0], IMAGE_SIZE[1], interpolation="bilinear", crop_to_aspect_ratio=True )(image) resized_image = tf.expand_dims(resized_image, axis=0) # Predict pred = model.predict(resized_image) prob = 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( """