File size: 5,590 Bytes
9154a27
 
 
 
 
b56bc50
 
9154a27
 
 
 
b56bc50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9154a27
 
 
 
 
a17fb98
9154a27
 
 
a763ea9
 
 
 
cd480d9
a763ea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad32c72
 
 
 
 
 
b56bc50
 
 
375f1b4
b56bc50
9154a27
b56bc50
22e36cf
9154a27
33379dc
b56bc50
a763ea9
 
b56bc50
 
3a0e635
9f2b6d8
b56bc50
 
 
33379dc
9154a27
 
 
 
 
33379dc
 
 
137b57c
 
9154a27
efa9908
137b57c
 
 
 
655757d
 
22e36cf
 
 
 
 
 
 
 
 
 
 
 
 
655757d
 
22e36cf
 
9154a27
 
316c194
 
fecaffd
 
9154a27
ad32c72
f1f699f
ac5e6c0
f1f699f
ac5e6c0
a763ea9
f1f699f
ac5e6c0
a763ea9
f1f699f
ac5e6c0
a763ea9
 
ad32c72
 
 
 
f1f699f
a763ea9
 
f1f699f
a763ea9
 
f1f699f
a763ea9
 
ad32c72
 
9154a27
 
 
 
 
 
33379dc
22ec348
9154a27
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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")


catToInput = {
    "Sex": ["male", "female"],
    "Embarked": ["Southampton", "Cherbourg", "Queenstown"],
    "Pclass": ["First", "Second", "Third"]
}

cityToInput = {
    "Southampton": "S",
    "Cherbourg": "C",
    "Queenstown": "Q"
}

classToInput = {
    "First": 1,
    "Second": 2,
    "Third": 3
}


# 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):
    Embarked = cityToInput[Embarked]
    Pclass = classToInput[Pclass]
    # 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={int(Age)}&gender={Sex}&minimum_age={int(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).style(
        height='100', rounded=False)

    #
    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).style(
        height='100', rounded=False)

    return img, img2


featureLabels = ["Pclass", "Name", "Sex", "Age", "SibSp",
                 "Parch", "Ticket", "Fare", "Cabin", "Embarked"]


for feature in featureLabels:
    if feature in numericalInputs:
        if feature == 'Age':
            inputs.append(gr.inputs.Slider(9, 75, 1, label='Age (years)'))
        elif feature == 'SibSp':
            inputs.append(gr.inputs.Slider(
                0, 10, 1, label='Number of siblings/spouses aboard'))
        elif feature == 'Parch':
            inputs.append(gr.inputs.Slider(
                0, 10, 1, label='Number of parents/children aboard'))
        elif feature == 'Fare':
            inputs.append(gr.inputs.Slider(0, 1000, 1, label='Ticket fare'))
        else:
            raise Exception(f'Feature: "{feature}" not found')
    elif feature in worthlessInputs:
        pass
        # inputs.append(gr.Inputs.Textbox(default='text', label=feature))
    elif feature in categoricalInputs:
        if feature == "Sex":
            inputs.append(gr.inputs.Dropdown(
                choices=catToInput.get(feature), default="male", label=feature))
        elif feature == "Embarked":
            inputs.append(gr.inputs.Dropdown(
                choices=catToInput.get(feature), default="Southampton", label='City of embarkation'))
        elif feature == "Pclass":
            inputs.append(gr.inputs.Dropdown(
                choices=catToInput.get(feature), default=3, label='Ticket class'))
    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()