aliabd HF staff commited on
Commit
1d53d58
·
1 Parent(s): c4ad9dc

Upload with huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +1 -2
  2. run.py +113 -0
README.md CHANGED
@@ -6,7 +6,6 @@ colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 3.4.1
9
-
10
- app_file: app.py
11
  pinned: false
12
  ---
 
6
  colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 3.4.1
9
+ app_file: run.py
 
10
  pinned: false
11
  ---
run.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import pandas as pd
4
+ from sklearn.ensemble import RandomForestClassifier
5
+ from sklearn.model_selection import train_test_split
6
+
7
+ import gradio as gr
8
+
9
+ current_dir = os.path.dirname(os.path.realpath(__file__))
10
+ data = pd.read_csv(os.path.join(current_dir, "files/titanic.csv"))
11
+
12
+
13
+ def encode_age(df):
14
+ df.Age = df.Age.fillna(-0.5)
15
+ bins = (-1, 0, 5, 12, 18, 25, 35, 60, 120)
16
+ categories = pd.cut(df.Age, bins, labels=False)
17
+ df.Age = categories
18
+ return df
19
+
20
+
21
+ def encode_fare(df):
22
+ df.Fare = df.Fare.fillna(-0.5)
23
+ bins = (-1, 0, 8, 15, 31, 1000)
24
+ categories = pd.cut(df.Fare, bins, labels=False)
25
+ df.Fare = categories
26
+ return df
27
+
28
+
29
+ def encode_df(df):
30
+ df = encode_age(df)
31
+ df = encode_fare(df)
32
+ sex_mapping = {"male": 0, "female": 1}
33
+ df = df.replace({"Sex": sex_mapping})
34
+ embark_mapping = {"S": 1, "C": 2, "Q": 3}
35
+ df = df.replace({"Embarked": embark_mapping})
36
+ df.Embarked = df.Embarked.fillna(0)
37
+ df["Company"] = 0
38
+ df.loc[(df["SibSp"] > 0), "Company"] = 1
39
+ df.loc[(df["Parch"] > 0), "Company"] = 2
40
+ df.loc[(df["SibSp"] > 0) & (df["Parch"] > 0), "Company"] = 3
41
+ df = df[
42
+ [
43
+ "PassengerId",
44
+ "Pclass",
45
+ "Sex",
46
+ "Age",
47
+ "Fare",
48
+ "Embarked",
49
+ "Company",
50
+ "Survived",
51
+ ]
52
+ ]
53
+ return df
54
+
55
+
56
+ train = encode_df(data)
57
+
58
+ X_all = train.drop(["Survived", "PassengerId"], axis=1)
59
+ y_all = train["Survived"]
60
+
61
+ num_test = 0.20
62
+ X_train, X_test, y_train, y_test = train_test_split(
63
+ X_all, y_all, test_size=num_test, random_state=23
64
+ )
65
+
66
+ clf = RandomForestClassifier()
67
+ clf.fit(X_train, y_train)
68
+ predictions = clf.predict(X_test)
69
+
70
+
71
+ def predict_survival(passenger_class, is_male, age, company, fare, embark_point):
72
+ if passenger_class is None or embark_point is None:
73
+ return None
74
+ df = pd.DataFrame.from_dict(
75
+ {
76
+ "Pclass": [passenger_class + 1],
77
+ "Sex": [0 if is_male else 1],
78
+ "Age": [age],
79
+ "Company": [
80
+ (1 if "Sibling" in company else 0) + (2 if "Child" in company else 0)
81
+ ],
82
+ "Fare": [fare],
83
+ "Embarked": [embark_point + 1],
84
+ }
85
+ )
86
+ df = encode_age(df)
87
+ df = encode_fare(df)
88
+ pred = clf.predict_proba(df)[0]
89
+ return {"Perishes": float(pred[0]), "Survives": float(pred[1])}
90
+
91
+
92
+ demo = gr.Interface(
93
+ predict_survival,
94
+ [
95
+ gr.Dropdown(["first", "second", "third"], type="index"),
96
+ "checkbox",
97
+ gr.Slider(0, 80, value=25),
98
+ gr.CheckboxGroup(["Sibling", "Child"], label="Travelling with (select all)"),
99
+ gr.Number(value=20),
100
+ gr.Radio(["S", "C", "Q"], type="index"),
101
+ ],
102
+ "label",
103
+ examples=[
104
+ ["first", True, 30, [], 50, "S"],
105
+ ["second", False, 40, ["Sibling", "Child"], 10, "Q"],
106
+ ["third", True, 30, ["Child"], 20, "S"],
107
+ ],
108
+ interpretation="default",
109
+ live=True,
110
+ )
111
+
112
+ if __name__ == "__main__":
113
+ demo.launch()