File size: 3,127 Bytes
9154a27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad32c72
 
 
 
 
 
9154a27
 
ad32c72
33379dc
 
ad32c72
 
 
 
 
 
 
 
 
 
 
 
 
 
33379dc
9154a27
 
 
 
 
33379dc
 
 
137b57c
 
 
33379dc
137b57c
 
 
 
 
9154a27
137b57c
 
 
 
 
9154a27
 
 
 
 
ad32c72
 
 
 
9154a27
ad32c72
 
 
 
 
 
 
 
 
 
9154a27
 
 
 
 
 
33379dc
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
import gradio as gr
import numpy as np
from PIL import Image
import requests
import pandas as pd

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")

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"]


def titanic(Pclass, Sex, Age, SibSp, Parch, Fare, Cabin, Embarked):
    input_list = []

    sexToFeature = {
        "male": 0,
        "female": 1,
    }

    # Convert inputs to features
    input_list.append(Pclass)  # Todo: Convert to feature
    input_list.append(sexToFeature.get(Sex))  # Todo: Convert to feature
    input_list.append(Age)  # !
    input_list.append(SibSp)  # Todo: Convert to feature
    input_list.append(Parch)  # Todo: Convert to feature
    input_list.append(Fare)  # Todo: Convert to feature
    input_list.append(Cabin)  # Todo: Convert to feature
    input_list.append(Embarked)  # Todo: Convert to feature

    # '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"}

    age = input_list[0]
    gender = input_list[1]
    survived = res[0]
    survivedText = intLabelToText[survived]

    # Temp:
    age = 20
    gender = "female"

    # 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={gender}&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)
    return img


inputs = []
numericalInputs = ["age", "sibsp", "parch", "fare", "pclass"]
worthlessInputs = ["name", "ticket"]
categoricalInputs = ["sex", "embarked", "cabin"]

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=["a", "b"], 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"))

demo.launch()