Spaces:
Runtime error
Runtime error
Commit
·
ef851dc
1
Parent(s):
c32777e
comments
Browse files
app.py
CHANGED
@@ -9,6 +9,8 @@ import numpy as np
|
|
9 |
import hopsworks
|
10 |
import joblib
|
11 |
|
|
|
|
|
12 |
|
13 |
def prepare_for_write(df):
|
14 |
# Convert the categorical features to numerical
|
@@ -38,16 +40,17 @@ def prepare_for_write(df):
|
|
38 |
return df
|
39 |
|
40 |
|
|
|
41 |
project = hopsworks.login()
|
42 |
fs = project.get_feature_store()
|
43 |
|
44 |
-
|
45 |
mr = project.get_model_registry()
|
46 |
model = mr.get_model("titanic_modal", version=3)
|
47 |
model_dir = model.download()
|
48 |
model = joblib.load(model_dir + "/titanic_model.pkl")
|
49 |
|
50 |
-
|
51 |
catToInput = {
|
52 |
"Sex": ["male", "female"],
|
53 |
"Embarked": ["Southampton", "Cherbourg", "Queenstown"],
|
@@ -67,15 +70,9 @@ classToInput = {
|
|
67 |
}
|
68 |
|
69 |
|
70 |
-
# features = pd.read_csv(
|
71 |
-
# "https://raw.githubusercontent.com/Nathanotal/remoteFiles/main/titanicCleaned.csv")
|
72 |
-
# features = features.drop(columns=["survived"])
|
73 |
-
# featureLabels = features.columns
|
74 |
-
featureLabels = ["Pclass", "Name", "Sex", "Age", "SibSp",
|
75 |
-
"Parch", "Ticket", "Fare", "Cabin", "Embarked"]
|
76 |
inputs = []
|
77 |
numericalInputs = ["Age", "SibSp", "Parch", "Fare"]
|
78 |
-
# Maybe move cabin to categorical
|
79 |
worthlessInputs = ["Name", "Ticket", "Cabin", "Title"]
|
80 |
categoricalInputs = ["Sex", "Embarked", "Pclass"]
|
81 |
|
@@ -84,6 +81,7 @@ columnHeaders = ["Pclass", "Sex", "Age", "SibSp",
|
|
84 |
|
85 |
|
86 |
def titanic(Pclass, Sex, Age, SibSp, Parch, Fare, Embarked):
|
|
|
87 |
Embarked = cityToInput[Embarked]
|
88 |
Pclass = classToInput[Pclass]
|
89 |
# Create a dataframe from the input values
|
@@ -100,11 +98,16 @@ def titanic(Pclass, Sex, Age, SibSp, Parch, Fare, Embarked):
|
|
100 |
# We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
|
101 |
# the first element.
|
102 |
|
103 |
-
intLabelToText = {0: "Died", 1: "Survived"}
|
104 |
|
105 |
survived = res[0]
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
-
#
|
108 |
generate_survivor_url = f'https://fakeface.rest/face/json?maximum_age={int(Age)}&gender={Sex}&minimum_age={int(Age)}'
|
109 |
randomized_face_url = requests.get(
|
110 |
generate_survivor_url).json()["image_url"]
|
@@ -112,7 +115,7 @@ def titanic(Pclass, Sex, Age, SibSp, Parch, Fare, Embarked):
|
|
112 |
survivor_url = randomized_face_url
|
113 |
img = Image.open(requests.get(survivor_url, stream=True).raw)
|
114 |
|
115 |
-
#
|
116 |
red_cross_url = "https://www.iconsdb.com/icons/preview/red/x-mark-xxl.png"
|
117 |
green_check_mark_url = "https://www.iconsdb.com/icons/preview/green/checkmark-xxl.png"
|
118 |
|
@@ -129,10 +132,11 @@ def titanic(Pclass, Sex, Age, SibSp, Parch, Fare, Embarked):
|
|
129 |
return img, img2
|
130 |
|
131 |
|
|
|
132 |
featureLabels = ["Pclass", "Name", "Sex", "Age", "SibSp",
|
133 |
"Parch", "Ticket", "Fare", "Cabin", "Embarked"]
|
134 |
|
135 |
-
|
136 |
for feature in featureLabels:
|
137 |
if feature in numericalInputs:
|
138 |
if feature == 'Age':
|
@@ -163,6 +167,7 @@ for feature in featureLabels:
|
|
163 |
else:
|
164 |
raise Exception(f'Feature: "{feature}" not found')
|
165 |
|
|
|
166 |
demo = gr.Interface(
|
167 |
fn=titanic,
|
168 |
title="Titanic Survivor Predictive Analytics",
|
|
|
9 |
import hopsworks
|
10 |
import joblib
|
11 |
|
12 |
+
# Convert the input to the format the model expects
|
13 |
+
|
14 |
|
15 |
def prepare_for_write(df):
|
16 |
# Convert the categorical features to numerical
|
|
|
40 |
return df
|
41 |
|
42 |
|
43 |
+
# Login to hopsworks and get the feature store
|
44 |
project = hopsworks.login()
|
45 |
fs = project.get_feature_store()
|
46 |
|
47 |
+
# Get the model from Hopsworks
|
48 |
mr = project.get_model_registry()
|
49 |
model = mr.get_model("titanic_modal", version=3)
|
50 |
model_dir = model.download()
|
51 |
model = joblib.load(model_dir + "/titanic_model.pkl")
|
52 |
|
53 |
+
# For generating the input form
|
54 |
catToInput = {
|
55 |
"Sex": ["male", "female"],
|
56 |
"Embarked": ["Southampton", "Cherbourg", "Queenstown"],
|
|
|
70 |
}
|
71 |
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
inputs = []
|
74 |
numericalInputs = ["Age", "SibSp", "Parch", "Fare"]
|
75 |
+
# Maybe move cabin to categorical (or just remove it)
|
76 |
worthlessInputs = ["Name", "Ticket", "Cabin", "Title"]
|
77 |
categoricalInputs = ["Sex", "Embarked", "Pclass"]
|
78 |
|
|
|
81 |
|
82 |
|
83 |
def titanic(Pclass, Sex, Age, SibSp, Parch, Fare, Embarked):
|
84 |
+
# Parse the unput and save it so we can run it through the model
|
85 |
Embarked = cityToInput[Embarked]
|
86 |
Pclass = classToInput[Pclass]
|
87 |
# Create a dataframe from the input values
|
|
|
98 |
# We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
|
99 |
# the first element.
|
100 |
|
101 |
+
intLabelToText = {0: "Died", 1: "Survived"} # Debug
|
102 |
|
103 |
survived = res[0]
|
104 |
+
# The API we are using only supports this age range
|
105 |
+
if Age < 9:
|
106 |
+
Age = 9
|
107 |
+
if Age > 75:
|
108 |
+
Age = 75
|
109 |
|
110 |
+
# Generate a face of the inputted person
|
111 |
generate_survivor_url = f'https://fakeface.rest/face/json?maximum_age={int(Age)}&gender={Sex}&minimum_age={int(Age)}'
|
112 |
randomized_face_url = requests.get(
|
113 |
generate_survivor_url).json()["image_url"]
|
|
|
115 |
survivor_url = randomized_face_url
|
116 |
img = Image.open(requests.get(survivor_url, stream=True).raw)
|
117 |
|
118 |
+
# Show a green check mark if the person is predicted to survive, otherwise show a red x
|
119 |
red_cross_url = "https://www.iconsdb.com/icons/preview/red/x-mark-xxl.png"
|
120 |
green_check_mark_url = "https://www.iconsdb.com/icons/preview/green/checkmark-xxl.png"
|
121 |
|
|
|
132 |
return img, img2
|
133 |
|
134 |
|
135 |
+
# All features present in the titanic dataset
|
136 |
featureLabels = ["Pclass", "Name", "Sex", "Age", "SibSp",
|
137 |
"Parch", "Ticket", "Fare", "Cabin", "Embarked"]
|
138 |
|
139 |
+
# Generate the input form
|
140 |
for feature in featureLabels:
|
141 |
if feature in numericalInputs:
|
142 |
if feature == 'Age':
|
|
|
167 |
else:
|
168 |
raise Exception(f'Feature: "{feature}" not found')
|
169 |
|
170 |
+
# Create the interface
|
171 |
demo = gr.Interface(
|
172 |
fn=titanic,
|
173 |
title="Titanic Survivor Predictive Analytics",
|