khalidey commited on
Commit
caef880
1 Parent(s): 83d113c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ mr = project.get_model_registry()
13
+ model = mr.get_model("titanic_modal", version=1)
14
+ model_dir = model.download()
15
+ model = joblib.load(model_dir + "/titanic_model.pkl")
16
+
17
+
18
+ def titanic(pclass, sex, age, sibsp, parch, fare, embarked):
19
+ input_list = []
20
+ if sex == 'female':
21
+ input_list.append(1.0)
22
+ input_list.append(0.0)
23
+ elif sex == 'male':
24
+ input_list.append(0.0)
25
+ input_list.append(1.0)
26
+ else:
27
+ print("ERROR!")
28
+ exit()
29
+ if embarked == "C":
30
+ input_list.append(1.0)
31
+ input_list.append(0.0)
32
+ input_list.append(0.0)
33
+ elif embarked == "Q":
34
+ input_list.append(0.0)
35
+ input_list.append(1.0)
36
+ input_list.append(0.0)
37
+ elif embarked == "S":
38
+ input_list.append(0.0)
39
+ input_list.append(0.0)
40
+ input_list.append(1.0)
41
+ else:
42
+ print("ERROR!")
43
+ exit()
44
+ if age < 18:
45
+ input_list.append(1.0)
46
+ elif age < 55:
47
+ input_list.append(2.0)
48
+ else:
49
+ input_list.append(3.0)
50
+ input_list.append(sibsp)
51
+ input_list.append(parch)
52
+ input_list.append(fare)
53
+ input_list.append(pclass)
54
+
55
+ # 'res' is a list of predictions returned as the label.
56
+ res = model.predict(np.asarray(input_list).reshape(1, -1))
57
+ # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
58
+ # the first element.
59
+ if res[0] == 1.0:
60
+ survive_url = "https://raw.githubusercontent.com/Hope-Liang/ID2223Lab1/main/serverless-ml-titanic/images/survived.png"
61
+ else:
62
+ survive_url = "https://raw.githubusercontent.com/Hope-Liang/ID2223Lab1/main/serverless-ml-titanic/images/died.png"
63
+ img = Image.open(requests.get(survive_url, stream=True).raw)
64
+ return img
65
+
66
+
67
+ demo = gr.Interface(
68
+ fn=titanic,
69
+ title="Titanic Survival Predictive Analytics",
70
+ description="Experiment with titanic passenger features to predict whether survived or not.",
71
+ allow_flagging="never",
72
+ inputs=[
73
+ gr.inputs.Number(default=1, label="Pclass (1,2,3)"),
74
+ gr.inputs.Textbox(default="female", label="Sex (female/male)"),
75
+ gr.inputs.Number(default=30.0, label="age (years)"),
76
+ gr.inputs.Number(default=1.0, label="SibSp"),
77
+ gr.inputs.Number(default=1.0, label="Parch"),
78
+ gr.inputs.Number(default=10.0, label="Fare (GBP)"),
79
+ gr.inputs.Textbox(default="S", label="Embarked (S,C,Q)")
80
+ ],
81
+ outputs=gr.Image(type="pil"))
82
+
83
+ demo.launch()