predictor ui
Browse files
alive.png
ADDED
app.py
CHANGED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
import re
|
6 |
+
|
7 |
+
import hopsworks
|
8 |
+
import joblib
|
9 |
+
from consts import *
|
10 |
+
|
11 |
+
project = hopsworks.login()
|
12 |
+
fs = project.get_feature_store()
|
13 |
+
|
14 |
+
mr = project.get_model_registry()
|
15 |
+
model = mr.get_model("titanic", version=2)
|
16 |
+
model_dir = model.download()
|
17 |
+
model = joblib.load(model_dir + "/titanic_model.pkl")
|
18 |
+
|
19 |
+
|
20 |
+
def get_deck(cabin):
|
21 |
+
if cabin is None:
|
22 |
+
return 0
|
23 |
+
deck = re.compile("([a-zA-Z]+)").search(cabin)
|
24 |
+
if deck is not None:
|
25 |
+
deck = deck.group()
|
26 |
+
if deck in DECK:
|
27 |
+
deck = DECK[deck]
|
28 |
+
else:
|
29 |
+
deck = 0
|
30 |
+
return deck
|
31 |
+
|
32 |
+
|
33 |
+
def get_sex(sex):
|
34 |
+
return GENDERS[sex]
|
35 |
+
|
36 |
+
|
37 |
+
def get_embarked(embarked):
|
38 |
+
return PORTS[embarked]
|
39 |
+
|
40 |
+
|
41 |
+
def get_title(title):
|
42 |
+
if title in TITLES_RARE:
|
43 |
+
title = "Rare"
|
44 |
+
if title not in TITLES:
|
45 |
+
return 0
|
46 |
+
return TITLES[title]
|
47 |
+
|
48 |
+
|
49 |
+
def get_age_class(age, p_class):
|
50 |
+
return age * p_class
|
51 |
+
|
52 |
+
|
53 |
+
def get_relatives(sib_sp, parch):
|
54 |
+
return sib_sp + parch
|
55 |
+
|
56 |
+
|
57 |
+
def get_not_alone(relatives):
|
58 |
+
return 1 if relatives > 0 else 0
|
59 |
+
|
60 |
+
|
61 |
+
def get_fare_per_person(fare, relatives):
|
62 |
+
return fare / (relatives + 1)
|
63 |
+
|
64 |
+
|
65 |
+
def titanic(p_class, sex, age, sib_sp, parch, fare, cabin, embarked, title):
|
66 |
+
# Model input:
|
67 |
+
# Pclass, Sex, Age, SibSp, Parch, Fare, Embarked, Deck,
|
68 |
+
# Title, Age_Class, Relatives, Not_alone, Fare_Per_Person
|
69 |
+
p_class = p_class + 1
|
70 |
+
deck = get_deck(cabin)
|
71 |
+
sex = get_sex(sex)
|
72 |
+
embarked = get_embarked(embarked)
|
73 |
+
title = get_title(title)
|
74 |
+
age_class = get_age_class(age, p_class)
|
75 |
+
relatives = get_relatives(sib_sp, parch)
|
76 |
+
not_alone = get_not_alone(relatives)
|
77 |
+
fare_per_person = get_fare_per_person(fare, relatives)
|
78 |
+
input_list = [p_class, sex, age, sib_sp, parch, fare, embarked,
|
79 |
+
deck, title, age_class, relatives, not_alone, fare_per_person]
|
80 |
+
# 'res' is a list of predictions returned as the label.
|
81 |
+
res = model.predict(np.asarray(input_list).reshape(1, -1))
|
82 |
+
# We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
|
83 |
+
# the first element.
|
84 |
+
img_name = "alive.png" if res[0] == 1 else "dead.png"
|
85 |
+
img = Image.open(img_name)
|
86 |
+
return img
|
87 |
+
|
88 |
+
|
89 |
+
demo = gr.Interface(
|
90 |
+
fn=titanic,
|
91 |
+
title="Titanic survival predictor",
|
92 |
+
description="Experiment with the parameters to predict if the person would or would not survive.",
|
93 |
+
allow_flagging="never",
|
94 |
+
inputs=[
|
95 |
+
gr.inputs.Dropdown(choices=["Class 1", "Class 2", "Class 3"], type="index", label="Class", default="Class 1"),
|
96 |
+
gr.inputs.Dropdown(choices=["male", "female"], type="value", label="Gender", default="male"),
|
97 |
+
gr.inputs.Number(label="Age"),
|
98 |
+
gr.inputs.Number(default=0, label="Number of sibling and spouses on board"),
|
99 |
+
gr.inputs.Number(default=0, label="Number of children/parents on board"),
|
100 |
+
gr.inputs.Number(label="Fare"),
|
101 |
+
gr.inputs.Textbox(label="Cabin"),
|
102 |
+
gr.inputs.Dropdown(choices=['S', 'C', 'Q'], label="Port of embarkation", default="S"),
|
103 |
+
gr.inputs.Textbox(label="Title")
|
104 |
+
],
|
105 |
+
outputs=gr.Image(type="pil"))
|
106 |
+
|
107 |
+
demo.launch()
|
108 |
+
|
109 |
+
titanic(1, "male", 22, 1, 0, 150, "C85", "S", "Mr")
|
consts.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
TITLES = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
|
2 |
+
TITLES_RARE = ['Lady', 'Countess', 'Capt', 'Col', 'Don', 'Dr',
|
3 |
+
'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona']
|
4 |
+
DECK = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "U": 8}
|
5 |
+
GENDERS = {"male": 0, "female": 1}
|
6 |
+
PORTS = {"S": 0, "C": 1, "Q": 2}
|
dead.png
ADDED