File size: 2,154 Bytes
d13fdbc
 
 
 
 
 
 
d30e0e9
d13fdbc
 
 
 
 
 
bb71819
d13fdbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d30e0e9
5bf3c2a
 
 
8128737
5bf3c2a
 
 
d30e0e9
 
d13fdbc
d30e0e9
d13fdbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9edefe7
d13fdbc
 
 
 
4a8b274
 
d13fdbc
 
 
 
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
import gradio as gr
import numpy as np
from PIL import Image
import requests

import hopsworks
import joblib
import pandas as pd

project = hopsworks.login()
fs = project.get_feature_store()


mr = project.get_model_registry()
model = mr.get_model("titanic", version=1)
model_dir = model.download()
model = joblib.load(model_dir + "/titanic_model.pkl")

CLASS_TO_VALUE = {
    "1st class": "1",
    "2nd class": "2",
    "3rd class": "3",
}

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


def titanic(ticket_class, sex, port, fare, age, sibsp, parch):
    data = {
        "pclass": [CLASS_TO_VALUE[ticket_class]],
        "sex": [sex],
        "embarked": [PORT_TO_VALUE[port]],
        "fare": [fare],
        "age": [age],
        "sibsp": [int(sibsp)],
        "parch": [int(parch)],
    }
    df = pd.DataFrame(data)
    # 'res' is a list of predictions returned as the label.
    res = model.predict(df)
    # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
    # the first element.

    if res:
        url = "https://m.media-amazon.com/images/I/71M6k7ZQNcL._RI_.jpg"
    else:
        url = "https://thumbs.dreamstime.com/b/allvarlig-sten-med-skallen-34707626.jpg"

    img = Image.open(requests.get(url, stream=True).raw)
    return img

demo = gr.Interface(
    fn=titanic,
    title="Titanic survival prediction",
    description="Experiment with parameters to predict if the fictional passenger survived",
    allow_flagging="never",
    inputs=[
        gr.inputs.Dropdown(["1st class", "2nd class", "3rd class"], label="Ticket class"),
        gr.inputs.Dropdown(["female", "male"], label="Sex"),
        gr.inputs.Dropdown(["Cherbourg", "Queenstown", "Southampton"], label="Port of Embarkation"),
        gr.inputs.Number(default=50.0, label="Fare"),
        gr.inputs.Number(default=20.0, label="Age"),
        gr.inputs.Number(default=0, label="Number of siblings/spouses aboard the Titanic"),
        gr.inputs.Number(default=0, label="Number of parents/children aboard the Titanic"),
        ],
    outputs=gr.Image(type="pil"))

demo.launch()