-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
165 lines (136 loc) · 5.42 KB
/
app.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from flask import Flask, request
from flask import send_file, abort, render_template
import os
from app_entity.housing_predictor import HousingData
from app_entity.housing_predictor import HousingPredictor
ROOT_DIR = os.getcwd()
LOG_FOLDER_NAME = "logs"
PIPELINE_FOLDER_NAME = "housing"
SAVED_MODELS_DIR_NAME = "saved_models"
LOG_DIR = os.path.join(ROOT_DIR, LOG_FOLDER_NAME)
PIPELINE_DIR = os.path.join(ROOT_DIR, PIPELINE_FOLDER_NAME)
MODEL_DIR = os.path.join(ROOT_DIR, SAVED_MODELS_DIR_NAME)
HOUSING_DATA_KEY = "housing_data"
MEDIAN_HOUSING_VALUE_KEY = "median_house_value"
app = Flask(__name__)
@app.route('/artifact', defaults={'req_path': 'housing'})
@app.route('/artifact/<path:req_path>')
def render_artifact_dir(req_path):
os.makedirs("housing", exist_ok=True)
# Joining the base and the requested path
print(f"req_path: {req_path}")
abs_path = os.path.join(req_path)
print(abs_path)
# Return 404 if path doesn't exist
if not os.path.exists(abs_path):
return abort(404)
# Check if path is a file and serve
if os.path.isfile(abs_path):
if ".html" in abs_path:
with open(abs_path, "r",encoding="utf-8") as file:
content = ''
for line in file.readlines():
content = f"{content}{line}"
return content
return send_file(abs_path)
# Show directory contents
files = {os.path.join(abs_path, file): file for file in os.listdir(abs_path)}
result = {
"files": files,
"parent_folder": os.path.dirname(abs_path),
"parent_label": abs_path
}
return render_template('files.html', result=result)
@app.route('/', methods=['GET', 'POST'])
def index():
try:
return render_template('index.html')
except Exception as e:
return str(e)
@app.route('/train', methods=['GET', 'POST'])
def train():
from subprocess import call
return_code = call(["python", "test.py"])
print(return_code)
return render_template('train.html')
@app.route('/predict', methods=['GET', 'POST'])
def predict():
context = {
HOUSING_DATA_KEY: None,
MEDIAN_HOUSING_VALUE_KEY: None
}
if request.method == 'POST':
longitude = float(request.form['longitude'])
latitude = float(request.form['latitude'])
housing_median_age = float(request.form['housing_median_age'])
total_rooms = float(request.form['total_rooms'])
total_bedrooms = float(request.form['total_bedrooms'])
population = float(request.form['population'])
households = float(request.form['households'])
median_income = float(request.form['median_income'])
ocean_proximity = request.form['ocean_proximity']
housing_data = HousingData(longitude=longitude,
latitude=latitude,
housing_median_age=housing_median_age,
total_rooms=total_rooms,
total_bedrooms=total_bedrooms,
population=population,
households=households,
median_income=median_income,
ocean_proximity=ocean_proximity,
)
housing_df = housing_data.get_housing_input_data_frame()
housing_predictor = HousingPredictor(model_dir=MODEL_DIR)
median_housing_value = housing_predictor.predict(X=housing_df)
context = {
HOUSING_DATA_KEY: housing_data.get_housing_data_as_dict(),
MEDIAN_HOUSING_VALUE_KEY: median_housing_value,
}
return render_template('predict.html', context=context)
return render_template("predict.html", context=context)
@app.route('/saved_models', defaults={'req_path': 'saved_models'})
@app.route('/saved_models/<path:req_path>')
def saved_models_dir(req_path):
os.makedirs("saved_models", exist_ok=True)
# Joining the base and the requested path
print(f"req_path: {req_path}")
abs_path = os.path.join(req_path)
print(abs_path)
# Return 404 if path doesn't exist
if not os.path.exists(abs_path):
return abort(404)
# Check if path is a file and serve
if os.path.isfile(abs_path):
return send_file(abs_path)
# Show directory contents
files = {os.path.join(abs_path, file): file for file in os.listdir(abs_path)}
result = {
"files": files,
"parent_folder": os.path.dirname(abs_path),
"parent_label": abs_path
}
return render_template('saved_models_files.html', result=result)
@app.route('/logs', defaults={'req_path': 'logs'})
@app.route('/logs/<path:req_path>')
def render_log_dir(req_path):
os.makedirs("logs", exist_ok=True)
# Joining the base and the requested path
print(f"req_path: {req_path}")
abs_path = os.path.join(req_path)
print(abs_path)
# Return 404 if path doesn't exist
if not os.path.exists(abs_path):
return abort(404)
# Check if path is a file and serve
if os.path.isfile(abs_path):
return send_file(abs_path)
# Show directory contents
files = {os.path.join(abs_path, file): file for file in os.listdir(abs_path)}
result = {
"files": files,
"parent_folder": os.path.dirname(abs_path),
"parent_label": abs_path
}
return render_template('log_files.html', result=result)
if __name__ == '__main__':
app.run()