-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
54 lines (47 loc) · 2.25 KB
/
application.py
File metadata and controls
54 lines (47 loc) · 2.25 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
from flask import Flask, request, render_template
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from src.pipeline.predict_pipeline import CustomData, PredictPipeline
# Initialize Flask application
application = Flask(__name__)
app = application
# Route for home page
@app.route('/')
def index():
return render_template('index.html')
# Route for prediction - handles both form display and prediction
@app.route('/predictdata', methods=['GET', 'POST'])
def predict_datapoint():
if request.method == 'GET':
# Display the prediction form
return render_template('home.html')
else:
# Collect form data and create CustomData object
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"))
)
# Convert CustomData object to DataFrame format for model input
df_pred = data.get_data_as_dataframe()
print(df_pred)
# Initialize prediction pipeline and generate math score prediction
predict_pipeline = PredictPipeline()
results = predict_pipeline.predict(df_pred)
# Round prediction to 2 decimal places and pass back form data to persist inputs
return render_template('home.html',
results=round(results[0], 2),
gender=data.gender,
race_ethnicity=data.race_ethnicity,
parental_level_of_education=data.parental_level_of_education,
lunch=data.lunch,
test_preparation_course=data.test_preparation_course,
reading_score=data.reading_score,
writing_score=data.writing_score)
if __name__ == "__main__":
app.run(host='0.0.0.0')