Tomas1234 commited on
Commit
59086ad
·
1 Parent(s): 39b7461

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import pandas as pd
9
+
10
+ project = hopsworks.login()
11
+ fs = project.get_feature_store()
12
+
13
+
14
+ mr = project.get_model_registry()
15
+ model = mr.get_model("titanic_modal", version=1)
16
+ model_dir = model.download()
17
+ model = joblib.load(model_dir + "/titanic_model.pkl")
18
+
19
+
20
+ def titanic(pclass,age,sibsp,parch,fare,sex,embarked):
21
+ input_list = []
22
+ input_list.append(pclass)
23
+ input_list.append(age)
24
+ input_list.append(sibsp)
25
+ input_list.append(parch)
26
+ input_list.append(fare)
27
+ if sex == "male":
28
+ input_list.append(0)
29
+ input_list.append(1)
30
+ elif sex == "female":
31
+ input_list.append(1)
32
+ input_list.append(0)
33
+
34
+ if embarked == "C":
35
+ input_list.append(1)
36
+ input_list.append(0)
37
+ input_list.append(0)
38
+ input_list.append(0)
39
+ elif embarked == "Q":
40
+ input_list.append(0)
41
+ input_list.append(1)
42
+ input_list.append(0)
43
+ input_list.append(0)
44
+ elif embarked == "S":
45
+ input_list.append(0)
46
+ input_list.append(0)
47
+ input_list.append(1)
48
+ input_list.append(0)
49
+ elif embarked == "Unknown":
50
+ input_list.append(0)
51
+ input_list.append(0)
52
+ input_list.append(0)
53
+ input_list.append(1)
54
+
55
+ # input_df = pd.DataFrame(data=input_list, columns = ['Pclass', 'Age', 'SibSp', 'Parch',
56
+ # 'Fare', 'Sex_female','Sex_male',
57
+ # 'Embarked_C', 'Embarked_Q', 'Embarked_S',
58
+ # 'Embarked_Unknown'])
59
+ # 'res' is a list of predictions returned as the label.
60
+ res = model.predict(np.asarray(input_list).reshape(1, -1))
61
+ # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
62
+ # the first element.
63
+ if res[0] == 1:
64
+ res_str = "survivor"
65
+ else:
66
+ res_str = "victim"
67
+ passenger_url = "https://raw.githubusercontent.com/daniel-rdt/serverless_ml_titanic_dr/main/assets/" + res_str + ".png"
68
+ img = Image.open(requests.get(passenger_url, stream=True).raw)
69
+ return img
70
+ # if res[0] == 1:
71
+ # return "The passenger is predicted to be a survivor."
72
+ # else:
73
+ # return "The passenger is predicted to be a victim."
74
+
75
+ demo = gr.Interface(
76
+ fn=titanic,
77
+ title="Titanic Passenger Predictive Analytics",
78
+ description="Experiment with passenger data to predict whether the passenger is a survivor or not.",
79
+ allow_flagging="never",
80
+ inputs=[
81
+ gr.inputs.Number(default=2, label="Passenger class (choose from either 1, 2 or 3)"),
82
+ gr.inputs.Number(default=30, label="Age in full years (if child younger than 1 round up to 1)"),
83
+ gr.inputs.Number(default=1, label="Number of siblings or spouses"),
84
+ gr.inputs.Number(default=0, label="Number of parents or children"),
85
+ gr.inputs.Number(default=100, label="Fare (cost between 0 and 513)"),
86
+ gr.inputs.Textbox(default="male", label="Sex (choose from either male or female)"),
87
+ gr.inputs.Textbox(default="Unknown", label="Embarked (choose from either C, Q, S or Unknown)"),
88
+ ],
89
+ # outputs=gr.outputs.Textbox())
90
+ outputs=gr.Image(type="pil"))
91
+
92
+ demo.launch()