-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
75 lines (64 loc) · 2.35 KB
/
Copy pathapp.py
File metadata and controls
75 lines (64 loc) · 2.35 KB
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
import pickle
import sys
import os
from dataclasses import dataclass
from src.exception import CustomException
from src.logger import logging
from flask import Flask, request, render_template
import numpy as np
import pandas as pd
from src.pipeline.predict_pipeline import CustomData, PredictPipeline
application = Flask(__name__)
app = application
## Route for a home page
@app.route('/')
def index():
return render_template('index.html')
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/health')
def health():
"""Health check endpoint for monitoring"""
try:
# Verify critical files exist
model_exists = os.path.exists('artifacts/model.pkl')
preprocessor_exists = os.path.exists('artifacts/preprocessor.pkl')
if model_exists and preprocessor_exists:
return {
"status": "healthy",
"model": "loaded",
"preprocessor": "loaded"
}, 200
else:
return {
"status": "unhealthy",
"model": "loaded" if model_exists else "missing",
"preprocessor": "loaded" if preprocessor_exists else "missing"
}, 503
except Exception as e:
return {
"status": "unhealthy",
"error": str(e)
}, 503
@app.route('/predictdata', methods=['Get','POST'])
def predict():
if request.method == 'GET':
return render_template('home.html')
else:
data = CustomData(
gender=request.form.get('gender'),
race_ethnicity=request.form.get("race_ethnicity"),
parental_level_of_education=request.form.get("parental_level_of_education"),
lunch=request.form.get("lunch"),
test_preparation_course=request.form.get("test_preparation_course"),
reading_score=float(request.form.get("reading_score")),
writing_score=float(request.form.get("writing_score")))
logging.info("Data received from form")
pred_df = data.get_data_as_dataframe()
prediction_pipeline = PredictPipeline()
results = prediction_pipeline.predict(pred_df)
return render_template('results.html', results= results[0])
if __name__ == "__main__":
logging.info("Starting the Flask server for prediction")
app.run(host='0.0.0.0', debug=True)