Santiago Roman commited on
Commit
0060bba
·
1 Parent(s): a67ece4
Files changed (2) hide show
  1. app.py +111 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ import requests
5
+
6
+ import hopsworks
7
+ import joblib
8
+
9
+ project = hopsworks.login()
10
+ fs = project.get_feature_store()
11
+
12
+
13
+ mr = project.get_model_registry()
14
+ model = mr.get_model("titanic_modal", version=4)
15
+ model_dir = model.download()
16
+ model = joblib.load(model_dir + "/titanic_model.pkl")
17
+
18
+
19
+ def iris(Pclass, Sex, SibSp, Parch, Embarked, Age, Fare_type):
20
+
21
+ input_list = []
22
+ input_list.append(Pclass)
23
+
24
+ ##############################
25
+ if Sex=='male':
26
+ input_list.append(1)
27
+ else:
28
+ input_list.append(0)
29
+ ##############################
30
+
31
+ input_list.append(SibSp)
32
+ input_list.append(Parch)
33
+
34
+ ##############################
35
+ if Embarked=='S':
36
+ input_list.append(0)
37
+ elif Embarked=='C':
38
+ input_list.append(1)
39
+ else:
40
+ input_list.append(2)
41
+ ##############################
42
+
43
+ ##############################
44
+ if Age <= 12:
45
+ one_hot_age = [1, 0, 0, 0]
46
+ for i in one_hot_age:
47
+ input_list.append(i)
48
+ elif Age <= 19:
49
+ one_hot_age = [0, 1, 0, 0]
50
+ for i in one_hot_age:
51
+ input_list.append(i)
52
+ elif Age <= 39:
53
+ one_hot_age = [0, 0, 1, 0]
54
+ for i in one_hot_age:
55
+ input_list.append(i)
56
+ else:
57
+ one_hot_age = [0, 0, 0, 1]
58
+ for i in one_hot_age:
59
+ input_list.append(i)
60
+ ##############################
61
+
62
+ ##############################
63
+ if Fare_type == "low":
64
+ one_hot_fare = [1, 0, 0, 0]
65
+ for i in one_hot_fare:
66
+ input_list.append(i)
67
+ elif Fare_type == "medium-low":
68
+ one_hot_fare = [0, 1, 0, 0]
69
+ for i in one_hot_fare:
70
+ input_list.append(i)
71
+ elif Fare_type == "medium":
72
+ one_hot_fare = [0, 0, 1, 0]
73
+ for i in one_hot_fare:
74
+ input_list.append(i)
75
+ else:
76
+ one_hot_fare = [0, 0, 0, 1]
77
+ for i in one_hot_fare:
78
+ input_list.append(i)
79
+ ##############################
80
+
81
+ # 'res' is a list of predictions returned as the label.
82
+ res = model.predict(np.asarray(input_list).reshape(1, -1))
83
+ # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
84
+ # the first element.
85
+
86
+ if res[0] == 0:
87
+ img_string = "dead"
88
+ else:
89
+ img_string = "survived"
90
+
91
+ passenger_url = "https://raw.githubusercontent.com/santroma1/id2223_lab1_titanic/main/assets/" + img_string + ".jpg"
92
+ img = Image.open(requests.get(passenger_url, stream=True).raw)
93
+ return img
94
+
95
+ demo = gr.Interface(
96
+ fn=iris,
97
+ title="Titanic Predictive Analytics",
98
+ description="Experiment to predict whether a passanger survived or not.",
99
+ allow_flagging="never",
100
+ inputs=[
101
+ gr.inputs.Number(default=1.0, label="Cabin class (1, 2, 3)"),
102
+ gr.Textbox(default='male', label="Sex (male, female)"),
103
+ gr.inputs.Number(default=1.0, label="SibSp (number of siblings/spouses aboard)"),
104
+ gr.inputs.Number(default=1.0, label="Parch (number of parents/children aboard)"),
105
+ gr.Textbox(default="S", label="Port of Embarkation (C = Cherbourg, Q = Queenstown, S = Southampton)"),
106
+ gr.inputs.Number(default=1.0, label="Age"),
107
+ gr.Textbox(default="low", label="Fare_type (low, medium-low, medium, high)"),
108
+ ],
109
+ outputs=gr.Image(type="pil"))
110
+
111
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ hopsworks
2
+ joblib
3
+ scikit-learn
4
+ numpy