-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDT.py
126 lines (97 loc) · 4 KB
/
DT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import threading
import tensorflow_decision_forests as tfdf
import pandas
from tensorflow import keras
import tensorflow as tf
from keras.models import load_model
import numpy as np
from flask import Flask, jsonify, request
import json
from pandas.io.json import json_normalize
app = Flask(__name__)
@app.route("/index")
def index():
return "Flask app running"
class DecisionTree:
def __init__(self):
self.model_path = 'models'
def train_and_save(self):
#Training dataset
train_df = pandas.read_csv("dataset4.csv")
train_ds = tfdf.keras.pd_dataframe_to_tf_dataset(train_df, label="corona_result")
#Evaluating dataset
evaluate_data = pandas.read_csv("dataset3.csv")
evaluate_dataset = tfdf.keras.pd_dataframe_to_tf_dataset(evaluate_data, label="corona_result")
#Train Random Forest using train_ds
#model = tfdf.keras.RandomForestModel()
model=tfdf.keras.GradientBoostedTreesModel()
model.fit(train_ds)
model.summary()
# Evaluate
model.compile(metrics=["accuracy"])
print(model.evaluate(evaluate_dataset))
# >> 0.97
model.save(self.model_path)
model.make_inspector().export_to_tensorboard("tensorboard_logs")
tfdf.model_plotter.plot_model(model, tree_idx=0, max_depth=3)
# print(model)
return model
def importModel(self):
loadModel = tf.keras.models.load_model(self.model_path)
return loadModel
def predictionTest(self):
print("loading model from "+self.model_path)
loadedModel = self.importModel()
print("model loaded successfully")
Dataset = pandas.read_csv("test.csv")
ConvertedData = tfdf.keras.pd_dataframe_to_tf_dataset(Dataset)
print("Predicting from test.csv")
predictions = loadedModel.predict(ConvertedData)
print("-------------------------------------------------------------Predictions---------------------------------------------------------")
print(predictions)
def prediction(self, UserSymptoms):
print("loading model from "+self.model_path)
loadedModel = self.importModel()
print("model loaded successfully")
print("Converting input to Dataframe")
#df = pandas.DataFrame.from_dict(UserSymptoms, orient="index")
df=pandas.json_normalize(UserSymptoms)
df.to_csv("data.csv", index = False)
dataframe = pandas.read_csv("data.csv")
ConvertedData = tfdf.keras.pd_dataframe_to_tf_dataset(dataframe)
prediction = loadedModel.predict(ConvertedData)
print("------------------------Prediction------------------------")
finalPrediction = prediction[0][0]
print(finalPrediction)
if finalPrediction > 0.65:
return "True"
else:
return "False"
# @app.route('/api/predict/', methods = ['GET'])
# def assumption():
# dt = DecisionTree()
# dt.predictionTest()
# data = request.get_json()
# cough = request.args.get("cough")
# fever = data.get('fever','')
# sore_throat = data.get('sore_throat','')
# shortness_of_breath = data.get('shortness_of_breath')
# head_ache = data.get('head_ache')
# gender = data.get('gender')
# test_indication = data.get('test_indication')
# symptoms = [cough,fever, sore_throat, shortness_of_breath, head_ache, gender, test_indication]
# dt.predictionTest(symptoms)
# return "hello there"
@app.route('/api/train', methods=['GET'])
def train():
#declare object of DecisionTree class
dt = DecisionTree()
#train model and save it in 'models'
dt.train_and_save()
@app.route('/api/predict', methods=['POST'])
def userPrediction():
dt = DecisionTree()
psymptoms = request.get_json()
return dt.prediction(psymptoms)
if __name__ == '__main__':
app.run(threaded = True, port = 5006)