abidlabs HF Staff commited on
Commit
d98db95
·
1 Parent(s): 62882bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -67
app.py CHANGED
@@ -1,67 +1 @@
1
- # This is a small and fast sklearn model, so the run-gradio script trains a model and deploys it
2
-
3
- import pandas as pd
4
- import numpy as np
5
- import sklearn
6
- import gradio as gr
7
- from sklearn import preprocessing
8
- from sklearn.model_selection import train_test_split
9
- from sklearn.ensemble import RandomForestClassifier
10
- from sklearn.metrics import accuracy_score
11
-
12
- data = pd.read_csv('https://raw.githubusercontent.com/gradio-app/titanic/master/train.csv')
13
- data.head()
14
-
15
- def encode_ages(df): # Binning ages
16
- df.Age = df.Age.fillna(-0.5)
17
- bins = (-1, 0, 5, 12, 18, 25, 35, 60, 120)
18
- categories = pd.cut(df.Age, bins, labels=False)
19
- df.Age = categories
20
- return df
21
-
22
- def encode_fares(df): # Binning fares
23
- df.Fare = df.Fare.fillna(-0.5)
24
- bins = (-1, 0, 8, 15, 31, 1000)
25
- categories = pd.cut(df.Fare, bins, labels=False)
26
- df.Fare = categories
27
- return df
28
-
29
- def encode_sex(df):
30
- mapping = {"male": 0, "female": 1}
31
- return df.replace({'Sex': mapping})
32
-
33
- def transform_features(df):
34
- df = encode_ages(df)
35
- df = encode_fares(df)
36
- df = encode_sex(df)
37
- return df
38
-
39
- train = data[['PassengerId', 'Fare', 'Age', 'Sex', 'Survived']]
40
- train = transform_features(train)
41
- train.head()
42
-
43
-
44
- X_all = train.drop(['Survived', 'PassengerId'], axis=1)
45
- y_all = train['Survived']
46
-
47
- num_test = 0.20
48
- X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=num_test, random_state=23)
49
-
50
- clf = RandomForestClassifier()
51
- clf.fit(X_train, y_train)
52
- predictions = clf.predict(X_test)
53
-
54
- def predict_survival(sex, age, fare):
55
- df = pd.DataFrame.from_dict({'Sex': [sex], 'Age': [age], 'Fare': [fare]})
56
- df = encode_sex(df)
57
- df = encode_fares(df)
58
- df = encode_ages(df)
59
- pred = clf.predict_proba(df)[0]
60
- return {'Perishes': float(pred[0]), 'Survives': float(pred[1])}
61
-
62
- sex = gr.inputs.Radio(['female', 'male'], label="Sex")
63
- age = gr.inputs.Slider(minimum=0, maximum=120, default=22, label="Age")
64
- fare = gr.inputs.Slider(minimum=0, maximum=200, default=100, label="Fare (british pounds)")
65
-
66
- gr.Interface(predict_survival, [sex, age, fare], "label", live=True, thumbnail="https://raw.githubusercontent.com/gradio-app/hub-titanic/master/thumbnail.png", analytics_enabled=False,
67
- title="Surviving the Titanic", description="What is the probability that a passenger on the Titanic would survive the famous wreck? It depends on their demographics as this live interface demonstrates.").launch();
 
1
+ 1/0