Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,61 @@
|
|
| 1 |
-
# Importing necessary modules from Flask to create the web application
|
| 2 |
-
from flask import Flask, request, render_template
|
| 3 |
-
|
| 4 |
-
# Importing additional necessary libraries
|
| 5 |
-
import numpy as np # For numerical operations
|
| 6 |
-
import pandas as pd # For data manipulation and creating DataFrame objects
|
| 7 |
-
|
| 8 |
-
# Importing custom modules: CustomData and PredictPipeline from the 'src.pipeline.predict_pipeline' module
|
| 9 |
-
from src.pipeline.predict_pipeline import CustomData, PredictPipeline
|
| 10 |
-
|
| 11 |
-
# Initializing the Flask application
|
| 12 |
-
app = Flask(__name__)
|
| 13 |
-
|
| 14 |
-
# Defining the route for the homepage of the web application
|
| 15 |
-
@app.route('/')
|
| 16 |
-
def index():
|
| 17 |
-
# Rendering the 'index.html' template when the root URL is accessed
|
| 18 |
-
return render_template('home.html')
|
| 19 |
-
|
| 20 |
-
# Defining the route for prediction, with both GET and POST methods allowed
|
| 21 |
-
@app.route('/predictdata', methods=['GET', 'POST'])
|
| 22 |
-
def predict_datapoint():
|
| 23 |
-
# If the request method is GET, render 'home.html'
|
| 24 |
-
if request.method == 'GET':
|
| 25 |
-
return render_template('home.html')
|
| 26 |
-
else:
|
| 27 |
-
try:
|
| 28 |
-
# Capture the form data (ensure form field names match these keys)
|
| 29 |
-
data = CustomData(
|
| 30 |
-
gender=request.form.get('gender'),
|
| 31 |
-
race_ethnicity=request.form.get('ethnicity'),
|
| 32 |
-
parental_level_of_education=request.form.get('parental_level_of_education'),
|
| 33 |
-
lunch=request.form.get('lunch'),
|
| 34 |
-
test_preparation_course=request.form.get('test_preparation_course'),
|
| 35 |
-
reading_score=float(request.form.get('reading_score')), # Ensuring correct casting
|
| 36 |
-
writing_score=float(request.form.get('writing_score')) # Ensuring correct casting
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
# Convert the collected form data into a pandas DataFrame
|
| 40 |
-
pred_df = data.get_data_as_data_frame()
|
| 41 |
-
print(f"Input DataFrame: \n{pred_df}")
|
| 42 |
-
|
| 43 |
-
# Initialize the prediction pipeline
|
| 44 |
-
predict_pipeline = PredictPipeline()
|
| 45 |
-
|
| 46 |
-
# Make the prediction
|
| 47 |
-
results = predict_pipeline.predict(pred_df)
|
| 48 |
-
print(f"Prediction Result: {results}")
|
| 49 |
-
|
| 50 |
-
# Render 'home.html' and display the prediction result
|
| 51 |
-
return render_template('home.html', results=results[0])
|
| 52 |
-
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print(f"Error during prediction: {e}")
|
| 55 |
-
# If any error occurs, render the home page with an error message
|
| 56 |
-
return render_template('home.html', error="An error occurred during prediction. Please check your input.")
|
| 57 |
-
|
| 58 |
-
# Run the Flask app
|
| 59 |
-
if __name__ == "__main__":
|
| 60 |
-
# Running the app on host 0.0.0.0 (accessible from any device in the network), debug mode ON for development
|
| 61 |
-
app.run(host="0.0.0.0", debug=True)
|
|
|
|
| 1 |
+
# Importing necessary modules from Flask to create the web application
|
| 2 |
+
from flask import Flask, request, render_template
|
| 3 |
+
|
| 4 |
+
# Importing additional necessary libraries
|
| 5 |
+
import numpy as np # For numerical operations
|
| 6 |
+
import pandas as pd # For data manipulation and creating DataFrame objects
|
| 7 |
+
|
| 8 |
+
# Importing custom modules: CustomData and PredictPipeline from the 'src.pipeline.predict_pipeline' module
|
| 9 |
+
from src.pipeline.predict_pipeline import CustomData, PredictPipeline
|
| 10 |
+
|
| 11 |
+
# Initializing the Flask application
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
# Defining the route for the homepage of the web application
|
| 15 |
+
@app.route('/')
|
| 16 |
+
def index():
|
| 17 |
+
# Rendering the 'index.html' template when the root URL is accessed
|
| 18 |
+
return render_template('home.html')
|
| 19 |
+
|
| 20 |
+
# Defining the route for prediction, with both GET and POST methods allowed
|
| 21 |
+
@app.route('/predictdata', methods=['GET', 'POST'])
|
| 22 |
+
def predict_datapoint():
|
| 23 |
+
# If the request method is GET, render 'home.html'
|
| 24 |
+
if request.method == 'GET':
|
| 25 |
+
return render_template('home.html')
|
| 26 |
+
else:
|
| 27 |
+
try:
|
| 28 |
+
# Capture the form data (ensure form field names match these keys)
|
| 29 |
+
data = CustomData(
|
| 30 |
+
gender=request.form.get('gender'),
|
| 31 |
+
race_ethnicity=request.form.get('ethnicity'),
|
| 32 |
+
parental_level_of_education=request.form.get('parental_level_of_education'),
|
| 33 |
+
lunch=request.form.get('lunch'),
|
| 34 |
+
test_preparation_course=request.form.get('test_preparation_course'),
|
| 35 |
+
reading_score=float(request.form.get('reading_score')), # Ensuring correct casting
|
| 36 |
+
writing_score=float(request.form.get('writing_score')) # Ensuring correct casting
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Convert the collected form data into a pandas DataFrame
|
| 40 |
+
pred_df = data.get_data_as_data_frame()
|
| 41 |
+
print(f"Input DataFrame: \n{pred_df}")
|
| 42 |
+
|
| 43 |
+
# Initialize the prediction pipeline
|
| 44 |
+
predict_pipeline = PredictPipeline()
|
| 45 |
+
|
| 46 |
+
# Make the prediction
|
| 47 |
+
results = predict_pipeline.predict(pred_df)
|
| 48 |
+
print(f"Prediction Result: {results}")
|
| 49 |
+
|
| 50 |
+
# Render 'home.html' and display the prediction result
|
| 51 |
+
return render_template('home.html', results=results[0])
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Error during prediction: {e}")
|
| 55 |
+
# If any error occurs, render the home page with an error message
|
| 56 |
+
return render_template('home.html', error="An error occurred during prediction. Please check your input.")
|
| 57 |
+
|
| 58 |
+
# Run the Flask app
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
# Running the app on host 0.0.0.0 (accessible from any device in the network), debug mode ON for development
|
| 61 |
+
#app.run(host="0.0.0.0", debug=True)
|