File size: 2,652 Bytes
fe65d04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17a6de7
fe65d04
 
 
17a6de7
fe65d04
 
 
17a6de7
fe65d04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17a6de7
fe65d04
 
 
 
17a6de7
fe65d04
 
 
 
 
 
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
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)
model_dir = model.download()
model = joblib.load(model_dir + "/titanic_model.pkl")


def titanic(pclass, sex, age, sibsp, parch, fare, embarked):
    input_list = []
    if sex == 'female':
        input_list.append(1.0)
        input_list.append(0.0)
    elif sex == 'male':
        input_list.append(0.0)
        input_list.append(1.0)
    else:
        print("ERROR!")
        exit()
    if embarked == "C":
        input_list.append(1.0)
        input_list.append(0.0)
        input_list.append(0.0)
    elif embarked == "Q":
        input_list.append(0.0)
        input_list.append(1.0)
        input_list.append(0.0)
    elif embarked == "S":
        input_list.append(0.0)
        input_list.append(0.0)
        input_list.append(1.0)
    else:
        print("ERROR!")
        exit()
    if age < 18:
        input_list.append(1.0)
    elif age < 55:
        input_list.append(2.0)
    else:
        input_list.append(3.0)
    input_list.append(sibsp)
    input_list.append(parch)
    input_list.append(fare)
    input_list.append(pclass)

    # '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] == 1.0:
        survive_url = "https://raw.githubusercontent.com/Hope-Liang/ID2223Lab1/main/serverless-ml-titanic/images/survived.png"
    else:
        survive_url = "https://raw.githubusercontent.com/Hope-Liang/ID2223Lab1/main/serverless-ml-titanic/images/died.png"
    img = Image.open(requests.get(survive_url, stream=True).raw)            
    return img
        
demo = gr.Interface(
    fn=titanic,
    title="Titanic Survival Predictive Analytics",
    description="Experiment with titanic passenger features to predict whether survived or not.",
    allow_flagging="never",
    inputs=[
        gr.inputs.Number(default=1, label="Pclass (1,2,3)"),
        gr.inputs.Textbox(default="female", label="Sex (female/male)"),
        gr.inputs.Number(default=30.0, label="age (years)"),
        gr.inputs.Number(default=1.0, label="SibSp"),
        gr.inputs.Number(default=1.0, label="Parch"),
        gr.inputs.Number(default=10.0, label="Fare (GBP)"),
        gr.inputs.Textbox(default="S", label="Embarked (S,C,Q)")
        ],
    outputs=gr.Image(type="pil"))

demo.launch()