File size: 3,451 Bytes
0060bba
 
 
 
8eeeeba
0060bba
 
 
 
 
 
 
 
 
fffbb15
0060bba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be9d734
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
import gradio as gr
import numpy as np
from PIL import Image
import requests
import xgboost

import hopsworks
import joblib

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


def iris(Pclass, Sex, SibSp, Parch, Embarked, Age, Fare_type):
                
    input_list = []
    input_list.append(Pclass)
    
    ##############################
    if Sex=='male':
        input_list.append(1)
    else:
        input_list.append(0)
    ##############################
    
    input_list.append(SibSp)
    input_list.append(Parch)

    ##############################  
    if Embarked=='S':
        input_list.append(0)
    elif Embarked=='C':
        input_list.append(1)
    else:
        input_list.append(2)
    ##############################    
    
    ##############################
    if Age <= 12:
        one_hot_age = [1, 0, 0, 0]
        for i in one_hot_age:
            input_list.append(i)
    elif Age <= 19:
        one_hot_age = [0, 1, 0, 0]
        for i in one_hot_age:
            input_list.append(i)
    elif Age <= 39:
        one_hot_age = [0, 0, 1, 0]
        for i in one_hot_age:
            input_list.append(i)
    else:
        one_hot_age = [0, 0, 0, 1]
        for i in one_hot_age:
            input_list.append(i)
    ##############################
    
    ##############################
    if Fare_type == "low":
        one_hot_fare = [1, 0, 0, 0]
        for i in one_hot_fare:
            input_list.append(i)
    elif Fare_type == "medium-low":
        one_hot_fare = [0, 1, 0, 0]
        for i in one_hot_fare:
            input_list.append(i)
    elif Fare_type == "medium": 
        one_hot_fare = [0, 0, 1, 0]
        for i in one_hot_fare:
            input_list.append(i)
    else:
        one_hot_fare = [0, 0, 0, 1]
        for i in one_hot_fare:
            input_list.append(i)
    ##############################
    
    # '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.
    
    if res[0] == 0:
        img_string =  "dead"
    else:
        img_string = "survived"
        
    passenger_url = "https://raw.githubusercontent.com/santroma1/id2223_lab1_titanic/main/assets/"  + img_string + ".jpg"
    img = Image.open(requests.get(passenger_url, stream=True).raw)            
    return img
        
demo = gr.Interface(
    fn=iris,
    title="Titanic Predictive Analytics",
    description="Experiment to predict whether a passanger survived or not.",
    allow_flagging="never",
    inputs=[
        gr.inputs.Number(default=1.0, label="Cabin class (1, 2, 3)"),
        gr.Textbox(default='male', label="Sex (male, female)"),
        gr.inputs.Number(default=1.0, label="SibSp (number of siblings/spouses aboard)"),
        gr.inputs.Number(default=1.0, label="Parch (number of parents/children aboard)"),
        gr.Textbox(default="S", label="Port of Embarkation (C = Cherbourg, Q = Queenstown, S = Southampton)"),
        gr.inputs.Number(default=1.0, label="Age"),
        gr.Textbox(default="low", label="Fare_type (low, medium-low, medium, high)"),
        ],
    outputs=gr.Image(type="pil"))

demo.launch()