File size: 2,987 Bytes
d3e086c
 
 
 
 
 
 
 
 
 
 
 
 
b4a71b4
 
 
 
 
 
 
 
 
 
d3e086c
 
 
 
580cff5
d3e086c
580cff5
 
 
 
 
 
d3e086c
 
 
 
580cff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3e086c
 
645a761
 
d3e086c
 
1aeb236
d3e086c
 
 
 
 
 
 
 
 
f12fb79
d1844f5
d3e086c
 
d1844f5
d3e086c
 
 
 
 
 
 
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
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)

EVALUATION_METRIC="accuracy"  
SORT_METRICS_BY="max" # your sorting criteria

# get best model based on custom metrics
best_model = mr.get_best_model("titanic_modal",
                               EVALUATION_METRIC,
                               SORT_METRICS_BY)
model = best_model
model_dir = model.download()
model = joblib.load(model_dir + "/titanic_model.pkl")


def passenger(Pclass, Age, SibSp, Parch, Fare, Sex, Embarked):
    input_list = []
    if Pclass == "First Class":
        input_list.append(1)
    elif Pclass == "Second Class":
        input_list.append(2)
    else:
        input_list.append(3)
    input_list.append(Age)
    input_list.append(SibSp)
    input_list.append(Parch)
    input_list.append(Fare)
    if Sex == "Male":
        input_list.append(0)
        input_list.append(1)
    else:
        input_list.append(1)
        input_list.append(0)
    if Embarked == "Cherbourg":
        input_list.append(1)
        input_list.append(0)
        input_list.append(0)
    elif Embarked == "Queenstown":
        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))
    res = str(res[0])
    # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want 
    # the first element.
    passenger_url = "https://raw.githubusercontent.com/GianlucaRub/Scalable-Machine-Learning-and-Deep-Learning/main/Lab1/assets/" + res + ".png"
    img = Image.open(requests.get(passenger_url, stream=True).raw)            
    return img
        
demo = gr.Interface(
    fn=passenger,
    title="Titanic Predictive Analytics",
    description="Insert passenger class, age, number of sibilings/spouse on board of the Titanic, number of parents/children on board of the Titanic, fare, sex, port of embarkation and see if he/she survived ",
    allow_flagging="never",
    inputs=[
        gr.inputs.Radio(choices=["First Class", "Second Class", "Third Class"], label="Passenger Class"),
        gr.inputs.Number(default=20, label="Age (years)"),
        gr.inputs.Number(default=1.0, label="Number of sibilings/spouse on board of the Titanic"),
        gr.inputs.Number(default=1.0, label="Number of parents/children on board of the Titanic"),
        gr.inputs.Number(default=10.0, label="Fare (USD)"),
        gr.inputs.Radio(choices=["Male","Female"], label = "Sex"),
        gr.inputs.Radio(choices=["Cherbourg","Queenstown","Southampton"], label = "Port of embarkation")
        ],
    outputs=gr.Image(type="pil"))

demo.launch()