Nos7 commited on
Commit
78f0027
1 Parent(s): 097c2b7

Upload server2.py

Browse files
Files changed (1) hide show
  1. server2.py +136 -0
server2.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import seaborn as sns
4
+ import matplotlib.pyplot as plt
5
+ import joblib
6
+ from sklearn.tree import DecisionTreeClassifier, XGBClassifier #using sklearn decisiontreeclassifier
7
+ from sklearn.model_selection import train_test_split
8
+
9
+ import os
10
+ import shutil
11
+
12
+ # Define the directory for FHE client/server files
13
+ fhe_directory = '/tmp/fhe_client_server_files/'
14
+
15
+ # Create the directory if it does not exist
16
+ if not os.path.exists(fhe_directory):
17
+ os.makedirs(fhe_directory)
18
+ else:
19
+ # If it exists, delete its contents
20
+ shutil.rmtree(fhe_directory)
21
+ os.makedirs(fhe_directory)
22
+
23
+ data=pd.read_csv('data/heart.xls')
24
+
25
+
26
+ data.info() #checking the info
27
+
28
+ data_corr=data.corr()
29
+
30
+ plt.figure(figsize=(20,20))
31
+ sns.heatmap(data=data_corr,annot=True)
32
+ #Heatmap for data
33
+ """
34
+ # Get the Data
35
+ X_train, y_train, X_val, y_val = train_test_split()
36
+ classifier = XGBClassifier()
37
+ # Training the Model
38
+ classifier = classifier.fit(X_train, y_train)
39
+ # Trained Model Evaluation on Validation Dataset
40
+ confidence = classifier.score(X_val, y_val)
41
+ # Validation Data Prediction
42
+ y_pred = classifier.predict(X_val)
43
+ # Model Validation Accuracy
44
+ accuracy = accuracy_score(y_val, y_pred)
45
+ # Model Confusion Matrix
46
+ conf_mat = confusion_matrix(y_val, y_pred)
47
+ # Model Classification Report
48
+ clf_report = classification_report(y_val, y_pred)
49
+ # Model Cross Validation Score
50
+ score = cross_val_score(classifier, X_val, y_val, cv=3)
51
+
52
+ try:
53
+ # Load Trained Model
54
+ clf = load(str(self.model_save_path + saved_model_name + ".joblib"))
55
+ except Exception as e:
56
+ print("Model not found...")
57
+
58
+ if test_data is not None:
59
+ result = clf.predict(test_data)
60
+ print(result)
61
+ else:
62
+ result = clf.predict(self.test_features)
63
+ accuracy = accuracy_score(self.test_labels, result)
64
+ clf_report = classification_report(self.test_labels, result)
65
+ print(accuracy, clf_report)
66
+ """
67
+ ####################
68
+ feature_value=np.array(data_corr['output'])
69
+ for i in range(len(feature_value)):
70
+ if feature_value[i]<0:
71
+ feature_value[i]=-feature_value[i]
72
+
73
+ print(feature_value)
74
+
75
+ features_corr=pd.DataFrame(feature_value,index=data_corr['output'].index,columns=['correalation'])
76
+
77
+ feature_sorted=features_corr.sort_values(by=['correalation'],ascending=False)
78
+
79
+ feature_selected=feature_sorted.index
80
+
81
+ feature_selected #selected features which are very much correalated
82
+
83
+ clean_data=data[feature_selected]
84
+
85
+ #making input and output dataset
86
+ X=clean_data.iloc[:,1:]
87
+ Y=clean_data['output']
88
+
89
+ x_train,x_test,y_train,y_test=train_test_split(X,Y,test_size=0.25,random_state=0)
90
+
91
+ print(x_train.shape,y_train.shape,x_test.shape,y_test.shape) #data is splited in traing and testing dataset
92
+
93
+ # feature scaling
94
+ from sklearn.preprocessing import StandardScaler
95
+ sc=StandardScaler()
96
+ x_train=sc.fit_transform(x_train)
97
+ x_test=sc.transform(x_test)
98
+
99
+ #training our model
100
+ dt=XGBClassifier(criterion='entropy',max_depth=6)
101
+ dt.fit(x_train,y_train)
102
+ #dt.compile(x_trqin)
103
+
104
+ #predicting the value on testing data
105
+ y_pred=dt.predict(x_test)
106
+
107
+ #ploting the data
108
+ from sklearn.metrics import confusion_matrix
109
+ conf_mat=confusion_matrix(y_test,y_pred)
110
+ print(conf_mat)
111
+ accuracy=dt.score(x_test,y_test)
112
+ print("\nThe accuracy of decisiontreelassifier on Heart disease prediction dataset is "+str(round(accuracy*100,2))+"%")
113
+
114
+ joblib.dump(dt, 'heart_disease_dt_model.pkl')
115
+
116
+ from concrete.ml.sklearn import DecisionTreeClassifier as ConcreteDecisionTreeClassifier
117
+ from concrete.ml.sklearn import XGBClassifier as ConcreteXGBClassifier
118
+
119
+ fhe_compatible = ConcreteXGBClassifier.from_sklearn_model(dt, x_train, n_bits = 10) #de FHE
120
+ fhe_compatible.compile(x_train)
121
+
122
+
123
+
124
+
125
+
126
+
127
+ #### server
128
+ from concrete.ml.deployment import FHEModelDev, FHEModelClient, FHEModelServer
129
+
130
+ # Setup the development environment
131
+ dev = FHEModelDev(path_dir=fhe_directory, model=fhe_compatible)
132
+ dev.save()
133
+
134
+ # Setup the server
135
+ server = FHEModelServer(path_dir=fhe_directory)
136
+ server.load()