ronith128/malware-analysis
0
1import gradio as gr2import numpy as np3from tensorflow.keras.models import load_model4from tensorflow.keras.preprocessing import image5import tensorflow as tf6from PIL import Image7import os8 9model = load_model('model.h5')10 11def convertToImage(file):12 with open(file, "rb") as f:13 byte = f.read(1) # reading 1 byte at a time. returns a byte object.14 i, j, p = 0, 0, 015 image = np.zeros((256, 256))16 temp1 = None # Define temp1 before using it17 while byte:18 try:19 decoded_byte = byte.decode("utf-8")20 if (21 decoded_byte == " "22 or decoded_byte == "\r"23 or decoded_byte == "\n"24 or decoded_byte == "?"25 ):26 byte = f.read(1)27 continue28 if j > 8:29 try:30 image[i][p] = int(decoded_byte, 16)31 p += 132 except ValueError:33 pass34 j += 135 if p > 255:36 p = 037 j = 038 if i < 255:39 i += 140 continue41 byte = f.read(1)42 except UnicodeDecodeError:43 byte = f.read(1)44 continue45 a = np.matrix(image)46 temp1 = a47 img = Image.fromarray(image, "L")48 img.save("a.bmp")49 50def predict_gender(img):51 pathe = os.path.splitext(img.name)52 extension = pathe[1]53 filename = pathe[0] + ".bmp"54 if(not extension == ".bmp"):55 convertToImage(img.name)56 filename = "a.bmp"57 58 img = image.load_img(filename, target_size=(256, 256))59 x = image.img_to_array(img)60 x = np.expand_dims(x, axis=0)61 x = x / 255.062 63 preds = model.predict(x)64 65 if preds[0][0] > 0.5:66 return "Predicted malware"67 else:68 return "Predicted benign"69 70# Create an interface with a file input and a text output71interface = gr.Interface(72 fn=predict_gender,73 inputs=gr.inputs.File(label="Upload a file"),74 outputs="text"75)76 77# Launch the interface78interface.launch()