Hope-Liang commited on
Commit
fe65d04
1 Parent(s): 7caf538
Files changed (3) hide show
  1. README.md +5 -5
  2. app.py +84 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: Huggingface Spaces Titanic
3
- emoji: 💻
4
- colorFrom: blue
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 3.9.1
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
1
  ---
2
+ title: Iris
3
+ emoji: 🐢
4
+ colorFrom: purple
5
+ colorTo: green
6
  sdk: gradio
7
+ sdk_version: 3.5
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(pclass, sex, age, sibsp, parch, fare, embarked):
20
+ input_list = []
21
+ if sex == 'female':
22
+ input_list.append(1.0)
23
+ input_list.append(0.0)
24
+ elif sex == 'male':
25
+ input_list.append(0.0)
26
+ input_list.append(1.0)
27
+ else:
28
+ print("ERROR!")
29
+ exit()
30
+ if embarked = "C":
31
+ input_list.append(1.0)
32
+ input_list.append(0.0)
33
+ input_list.append(0.0)
34
+ elif embarked = "Q":
35
+ input_list.append(0.0)
36
+ input_list.append(1.0)
37
+ input_list.append(0.0)
38
+ elif embarked = "S":
39
+ input_list.append(0.0)
40
+ input_list.append(0.0)
41
+ input_list.append(1.0)
42
+ else:
43
+ print("ERROR!")
44
+ exit()
45
+ if age < 18:
46
+ input_list.append(1.0)
47
+ elif age < 55:
48
+ input_list.append(2.0)
49
+ else:
50
+ input_list.append(3.0)
51
+ input_list.append(sibsp)
52
+ input_list.append(parch)
53
+ input_list.append(fare)
54
+ input_list.append(pclass)
55
+
56
+ # 'res' is a list of predictions returned as the label.
57
+ res = model.predict(np.asarray(input_list).reshape(1, -1))
58
+ # We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
59
+ # the first element.
60
+ if res[0] == 1.0:
61
+ survive_url = "https://raw.githubusercontent.com/Hope-Liang/ID2223Lab1/main/serverless-ml-titanic/images/survived.png"
62
+ else:
63
+ survive_url = "https://raw.githubusercontent.com/Hope-Liang/ID2223Lab1/main/serverless-ml-titanic/images/died.png"
64
+ img = Image.open(requests.get(survive_url, stream=True).raw)
65
+ return img
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()
84
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ hopsworks
2
+ joblib
3
+ scikit-learn