Spaces:
Runtime error
Runtime error
File size: 2,228 Bytes
cc739d6 cb27489 cc739d6 | 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 | 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() |