tommyL99 commited on
Commit
24646eb
1 Parent(s): 7f6d2e9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=1)
15
+ model_dir = model.download()
16
+ model = joblib.load(model_dir + "/titanic_model.pkl")
17
+
18
+
19
+ def titanic(sex, age, sibsp, parch, fare, embarked, pclass): #check if the order is the same in the feature hopsworks
20
+ input_list = []
21
+ input_list.append(sex)
22
+ input_list.append(age)
23
+ input_list.append(sibsp)
24
+ input_list.append(parch)
25
+ input_list.append(fare)
26
+ if embarked == 1:
27
+ input_list.append(1)
28
+ input_list.append(0)
29
+ input_list.append(0)
30
+ elif embarked == 2:
31
+ input_list.append(0)
32
+ input_list.append(1)
33
+ input_list.append(0)
34
+ else:
35
+ input_list.append(0)
36
+ input_list.append(0)
37
+ input_list.append(1)
38
+
39
+ if pclass == 1:
40
+ input_list.append(1)
41
+ input_list.append(0)
42
+ input_list.append(0)
43
+ elif pclass == 2:
44
+ input_list.append(0)
45
+ input_list.append(1)
46
+ input_list.append(0)
47
+ else:
48
+ input_list.append(0)
49
+ input_list.append(0)
50
+ input_list.append(1)
51
+
52
+ # 'res' is a list of predictions returned as the label.
53
+ res = model.predict(np.asarray(input_list).reshape(1, -1))
54
+ # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
55
+ # the first element.
56
+ if res[0] == 1:
57
+ image_url = "https://i.ibb.co/0X0JTcx/survive.jpg"
58
+ else:
59
+ image_url = "https://i.ibb.co/C8SdRn2/drowning.jpg"
60
+ img = Image.open(requests.get(image_url, stream=True).raw)
61
+ return img
62
+ #return res[0]
63
+
64
+ demo = gr.Interface(
65
+ fn=titanic,
66
+ title="Titanic Predictive Analytics",
67
+ description="Experiment with titanic dataset to predicte if a passenger is survived or not",
68
+ allow_flagging="never",
69
+ inputs=[
70
+ gr.inputs.Number(default=1.0, label="sex"),
71
+ gr.inputs.Number(default=1.0, label="age"),
72
+ gr.inputs.Number(default=1.0, label="sibsp"),
73
+ gr.inputs.Number(default=1.0, label="parch"),
74
+ gr.inputs.Number(default=1.0, label="fare"),
75
+ gr.inputs.Number(default=1.0, label="embarked"),
76
+ gr.inputs.Number(default=1.0, label="pclass"),
77
+ ],
78
+ outputs=gr.Image(type="pil"))
79
+ #outputs = "number"
80
+ demo.launch(debug=True)