titanic / app.py
tommyL99's picture
Create app.py
24646eb
import gradio as gr
import numpy as np
from PIL import Image
import requests
import hopsworks
import joblib
project = hopsworks.login()
fs = project.get_feature_store()
mr = project.get_model_registry()
model = mr.get_model("titanic_modal", version=1)
model_dir = model.download()
model = joblib.load(model_dir + "/titanic_model.pkl")
def titanic(sex, age, sibsp, parch, fare, embarked, pclass): #check if the order is the same in the feature hopsworks
input_list = []
input_list.append(sex)
input_list.append(age)
input_list.append(sibsp)
input_list.append(parch)
input_list.append(fare)
if embarked == 1:
input_list.append(1)
input_list.append(0)
input_list.append(0)
elif embarked == 2:
input_list.append(0)
input_list.append(1)
input_list.append(0)
else:
input_list.append(0)
input_list.append(0)
input_list.append(1)
if pclass == 1:
input_list.append(1)
input_list.append(0)
input_list.append(0)
elif pclass == 2:
input_list.append(0)
input_list.append(1)
input_list.append(0)
else:
input_list.append(0)
input_list.append(0)
input_list.append(1)
# 'res' is a list of predictions returned as the label.
res = model.predict(np.asarray(input_list).reshape(1, -1))
# We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
# the first element.
if res[0] == 1:
image_url = "https://i.ibb.co/0X0JTcx/survive.jpg"
else:
image_url = "https://i.ibb.co/C8SdRn2/drowning.jpg"
img = Image.open(requests.get(image_url, stream=True).raw)
return img
#return res[0]
demo = gr.Interface(
fn=titanic,
title="Titanic Predictive Analytics",
description="Experiment with titanic dataset to predicte if a passenger is survived or not",
allow_flagging="never",
inputs=[
gr.inputs.Number(default=1.0, label="sex"),
gr.inputs.Number(default=1.0, label="age"),
gr.inputs.Number(default=1.0, label="sibsp"),
gr.inputs.Number(default=1.0, label="parch"),
gr.inputs.Number(default=1.0, label="fare"),
gr.inputs.Number(default=1.0, label="embarked"),
gr.inputs.Number(default=1.0, label="pclass"),
],
outputs=gr.Image(type="pil"))
#outputs = "number"
demo.launch(debug=True)