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 |
+
import numpy as np
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import requests
|
| 13 |
+
|
| 14 |
+
import hopsworks
|
| 15 |
+
import joblib
|
| 16 |
+
|
| 17 |
+
project = hopsworks.login()
|
| 18 |
+
fs = project.get_feature_store()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
mr = project.get_model_registry()
|
| 22 |
+
model = mr.get_model("titanic_modal", version=6)
|
| 23 |
+
model_dir = model.download()
|
| 24 |
+
model = joblib.load(model_dir + "/titanic_model.pkl")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def titanic(pclass, sex, fare, embarked, familysize, family, appellation, cabin):
|
| 28 |
+
input_list = []
|
| 29 |
+
# PClass
|
| 30 |
+
if pclass == "1":
|
| 31 |
+
input_list.extend([1,0,0])
|
| 32 |
+
elif pclass == "2":
|
| 33 |
+
input_list.extend([0,1,0])
|
| 34 |
+
elif pclass == "3":
|
| 35 |
+
input_list.extend([0,0,1])
|
| 36 |
+
|
| 37 |
+
# Gender
|
| 38 |
+
if sex == "Male":
|
| 39 |
+
input_list.append(0)
|
| 40 |
+
else:
|
| 41 |
+
input_list.append(1)
|
| 42 |
+
|
| 43 |
+
# Age
|
| 44 |
+
#input_list.append(age)
|
| 45 |
+
|
| 46 |
+
# Fare
|
| 47 |
+
input_list.append(fare)
|
| 48 |
+
|
| 49 |
+
# Embarked
|
| 50 |
+
if embarked == "S":
|
| 51 |
+
input_list.extend([1,0,0])
|
| 52 |
+
elif embarked == "C":
|
| 53 |
+
input_list.extend([0,1,0])
|
| 54 |
+
elif embarked == "Q":
|
| 55 |
+
input_list.extend([0,0,1])
|
| 56 |
+
|
| 57 |
+
# Family Size
|
| 58 |
+
input_list.append(familysize)
|
| 59 |
+
|
| 60 |
+
if family == "Family_Single":
|
| 61 |
+
input_list.extend([1,0,0])
|
| 62 |
+
elif family == "Family_Small":
|
| 63 |
+
input_list.extend([0,1,0])
|
| 64 |
+
elif family == "Family_Large":
|
| 65 |
+
input_list.extend([0,0,1])
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# Appellation
|
| 70 |
+
if appellation == "master":
|
| 71 |
+
input_list.extend([1,0,0,0,0,0])
|
| 72 |
+
elif appellation == "miss":
|
| 73 |
+
input_list.extend([0,1,0,0,0,0])
|
| 74 |
+
elif appellation == "mr":
|
| 75 |
+
input_list.extend([0,0,1,0,0,0])
|
| 76 |
+
elif appellation == "mrs":
|
| 77 |
+
input_list.extend([0,0,0,1,0,0])
|
| 78 |
+
elif appellation == "officer":
|
| 79 |
+
input_list.extend([0,0,0,0,1,0])
|
| 80 |
+
elif appellation == "royalty":
|
| 81 |
+
input_list.extend([0,0,0,0,0,1])
|
| 82 |
+
|
| 83 |
+
# Cabin
|
| 84 |
+
if cabin == "A":
|
| 85 |
+
input_list.extend([1,0,0,0,0,0,0,0,0])
|
| 86 |
+
elif cabin == "B":
|
| 87 |
+
input_list.extend([0,1,0,0,0,0,0,0,0])
|
| 88 |
+
elif cabin == "C":
|
| 89 |
+
input_list.extend([0,0,1,0,0,0,0,0,0])
|
| 90 |
+
elif cabin == "D":
|
| 91 |
+
input_list.extend([0,0,0,1,0,0,0,0,0])
|
| 92 |
+
elif cabin == "E":
|
| 93 |
+
input_list.extend([0,0,0,0,1,0,0,0,0])
|
| 94 |
+
elif cabin == "F":
|
| 95 |
+
input_list.extend([0,0,0,0,0,1,0,0,0])
|
| 96 |
+
elif cabin == "G":
|
| 97 |
+
input_list.extend([0,0,0,0,0,0,1,0,0])
|
| 98 |
+
elif cabin == "T":
|
| 99 |
+
input_list.extend([0,0,0,0,0,0,0,1,0])
|
| 100 |
+
else:
|
| 101 |
+
input_list.extend([0,0,0,0,0,0,0,0,1])
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# 'res' is a list of predictions returned as the label.
|
| 105 |
+
res = model.predict(np.asarray(input_list).reshape(1, -1))
|
| 106 |
+
res = res.astype(int)
|
| 107 |
+
# We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
|
| 108 |
+
# the first element.
|
| 109 |
+
titanic_url = "https://raw.githubusercontent.com/M75583/Machinelearning/main/" + str(res[0]) + ".png"
|
| 110 |
+
#titanic_url = "https://github.com/Qinglin2000/ID2223/blob/main/" + str(res[0]) + ".png?raw=true"
|
| 111 |
+
img = Image.open(requests.get(titanic_url, stream=True).raw)
|
| 112 |
+
return img
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
demo = gr.Interface(
|
| 116 |
+
fn=titanic,
|
| 117 |
+
title="Titanic Predictive Analytics",
|
| 118 |
+
description="Experiment with titanic dataset values.",
|
| 119 |
+
allow_flagging="never",
|
| 120 |
+
inputs=[
|
| 121 |
+
gr.Dropdown(choices=["1", "2", "3"], label="PClass", value="1"),
|
| 122 |
+
gr.Radio(choices=["Male", "Female"], label="Gender", value="Male"),
|
| 123 |
+
#gr.inputs.Number(default=30.0, label="Age"),
|
| 124 |
+
gr.inputs.Number(default=40.99, label="Fare"),
|
| 125 |
+
gr.Dropdown(choices=["S","C","Q"], label="Embarked", value="S"),
|
| 126 |
+
gr.Number(label="Family Size", precision=0, value=1),
|
| 127 |
+
gr.Dropdown(choices=["Family_Single","Family_Small","Family_Large"], label="Family", value="Family_Single"),
|
| 128 |
+
gr.Dropdown(choices=["master", "miss", "mr", "mrs", "officer", "royalty"], label="Appellation", value="master"),
|
| 129 |
+
gr.Dropdown(choices=["A", "B", "C", "D", "E", "F", "G", "T", "U"], label="Cabin", value="A"),
|
| 130 |
+
],
|
| 131 |
+
outputs=gr.Image(type="pil"))
|
| 132 |
+
demo.launch()
|
| 133 |
+
|
| 134 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
hopsworks
|
| 2 |
+
joblib
|
| 3 |
+
scikit-learn
|