generated from kjpou1/ml_project_template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredict_app.py
More file actions
68 lines (51 loc) · 2.14 KB
/
predict_app.py
File metadata and controls
68 lines (51 loc) · 2.14 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
import numpy as np
import pandas as pd
from flask import Flask, render_template, request
from sklearn.preprocessing import StandardScaler
from src.schemas.prediction_input_schema import PredictionInputSchema
from src.pipeline.predict_pipeline import PredictPipeline
from src.logger_manager import LoggerManager
from pydantic import ValidationError
logging = LoggerManager.get_logger(__name__)
application = Flask(__name__)
app = application
## Route for a home page
@app.route("/")
def index():
return render_template("index.html")
@app.route("/predictdata", methods=["GET", "POST"])
def predict_datapoint():
if request.method == "GET":
return render_template("home.html")
else:
try:
# Parse and validate the input data
validated_data = PredictionInputSchema(
gender=request.form.get("gender"),
race_ethnicity=request.form.get("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 to DataFrame
pred_df = validated_data.to_dataframe()
logging.info(pred_df)
# Perform prediction
predict_pipeline = PredictPipeline()
results = predict_pipeline.predict(pred_df)
logging.info("Prediction successful.")
return render_template("home.html", results=results[0])
except ValidationError as e:
logging.error(f"Validation Error: {e}")
return render_template("home.html", error=f"Validation Error: {e.errors()}")
except Exception as e:
logging.error(f"Prediction Error: {e}")
return render_template(
"home.html", error="An error occurred during prediction."
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8097)