titanic / app.py
Nathanotal's picture
adsf
22ec348
raw
history blame
4.32 kB
import gradio as gr
import numpy as np
from PIL import Image
import requests
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import hopsworks
import joblib
def prepare_for_write(df):
# Convert the categorical features to numerical
def sexToInt(x):
if x == "male":
return 0
elif x == "female":
return 1
else:
raise Exception("Unsupported sex value: " + x)
def embarkedToInt(x):
if x == "S":
return 0
elif x == "C":
return 1
elif x == "Q":
return 2
else:
raise Exception("Unsupported embarked value: " + x)
df["Sex"] = df["Sex"].apply(sexToInt)
df["Embarked"] = df["Embarked"].apply(embarkedToInt)
# le = preprocessing.LabelEncoder()
# df = df.apply(le.fit_transform)
df.columns = df.columns.str.lower()
return df
project = hopsworks.login()
fs = project.get_feature_store()
mr = project.get_model_registry()
model = mr.get_model("titanic_modal", version=3)
model_dir = model.download()
model = joblib.load(model_dir + "/titanic_model.pkl")
df = pd
# features = pd.read_csv(
# "https://raw.githubusercontent.com/Nathanotal/remoteFiles/main/titanicCleaned.csv")
# features = features.drop(columns=["survived"])
# featureLabels = features.columns
featureLabels = ["Pclass", "Name", "Sex", "Age", "SibSp",
"Parch", "Ticket", "Fare", "Cabin", "Embarked"]
inputs = []
numericalInputs = ["Age", "SibSp", "Parch", "Fare"]
# Maybe move cabin to categorical
worthlessInputs = ["Name", "Ticket", "Cabin", "Title"]
categoricalInputs = ["Sex", "Embarked", "Pclass"]
columnHeaders = ["Pclass", "Sex", "Age", "SibSp",
"Parch", "Fare", "Embarked", "Title"] # Todo: remove title
def titanic(Pclass, Sex, Age, SibSp, Parch, Fare, Embarked):
# Create a dataframe from the input values
input_variables = pd.DataFrame(
[[Pclass, Sex, Age, SibSp, Parch, Fare, Embarked, 1.0]], columns=columnHeaders)
df = prepare_for_write(input_variables)
# Save first row as a numpy array
input_list = df.iloc[0].to_numpy()
# '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.
intLabelToText = {0: "Died", 1: "Survived"}
survived = res[0]
# Todo: survivor, "https://fakeface.rest/face/json?maximum_age=50&gender=female&minimum_age=49"
generate_survivor_url = f'https://fakeface.rest/face/json?maximum_age={Age}&gender={Sex}&minimum_age={Age}'
randomized_face_url = requests.get(
generate_survivor_url).json()["image_url"]
survivor_url = randomized_face_url
img = Image.open(requests.get(survivor_url, stream=True).raw)
#
red_cross_url = "https://www.iconsdb.com/icons/preview/red/x-mark-xxl.png"
green_check_mark_url = "https://www.iconsdb.com/icons/preview/green/checkmark-xxl.png"
label_to_url = {
0: red_cross_url,
1: green_check_mark_url
}
url = label_to_url.get(survived)
# Save the image of the person
img2 = Image.open(requests.get(url, stream=True).raw)
return img, img2
catToInput = {
"Sex": ["male", "female"],
"Embarked": ["S", "C", "Q"],
"Pclass": [0, 1, 2]
}
featureLabels = ["Pclass", "Name", "Sex", "Age", "SibSp",
"Parch", "Ticket", "Fare", "Cabin", "Embarked"]
for feature in featureLabels:
if feature in numericalInputs:
inputs.append(gr.inputs.Number(default=1.0, label=feature))
elif feature in worthlessInputs:
pass
# inputs.append(gr.Inputs.Textbox(default='text', label=feature))
elif feature in categoricalInputs:
inputs.append(gr.inputs.Dropdown(
choices=catToInput.get(feature), default="a", label=feature))
else:
raise Exception(f'Feature: "{feature}" not found')
demo = gr.Interface(
fn=titanic,
title="Titanic Survivor Predictive Analytics",
description="Experiment with person features to predict which survivor it is.",
allow_flagging="never",
inputs=inputs,
outputs=[gr.Image(type="pil"), gr.Image(type="pil")])
demo.launch()