import gradio as gr import numpy as np from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing import image import tensorflow as tf from PIL import Image import os model = load_model('model.h5') def convertToImage(file): with open(file, "rb") as f: byte = f.read(1) # reading 1 byte at a time. returns a byte object. i, j, p = 0, 0, 0 image = np.zeros((256, 256)) temp1 = None # Define temp1 before using it while byte: try: decoded_byte = byte.decode("utf-8") if ( decoded_byte == " " or decoded_byte == "\r" or decoded_byte == "\n" or decoded_byte == "?" ): byte = f.read(1) continue if j > 8: try: image[i][p] = int(decoded_byte, 16) p += 1 except ValueError: pass j += 1 if p > 255: p = 0 j = 0 if i < 255: i += 1 continue byte = f.read(1) except UnicodeDecodeError: byte = f.read(1) continue a = np.matrix(image) temp1 = a img = Image.fromarray(image, "L") img.save("a.bmp") def predict_gender(img): pathe = os.path.splitext(img.name) extension = pathe[1] filename = pathe[0] + ".bmp" if(not extension == ".bmp"): convertToImage(img.name) filename = "a.bmp" img = image.load_img(filename, target_size=(256, 256)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = x / 255.0 preds = model.predict(x) if preds[0][0] > 0.5: return "Predicted malware" else: return "Predicted benign" # Create an interface with a file input and a text output interface = gr.Interface( fn=predict_gender, inputs=gr.inputs.File(label="Upload a file"), outputs="text" ) # Launch the interface interface.launch()