File size: 1,304 Bytes
f9aedd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, render_template, request
import numpy as np
import joblib

app = Flask(__name__)

# Load saved model and scaler
model = joblib.load('model/population_model.pkl')           # Trained Logistic Regression model
scaler = joblib.load('model/scaler.pkl')                    # MinMaxScaler or StandardScaler
label_encoder = joblib.load('model/label_encoder.pkl')      # LabelEncoder for decoding prediction

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
    try:
        # Get form values
        area = float(request.form['area'])
        density = float(request.form['density'])
        population = float(request.form['population'])

        # Prepare input for prediction
        input_data = np.array([[population, area, density]])
        input_scaled = scaler.transform(input_data)

        # Predict
        prediction = model.predict(input_scaled)
        predicted_label = label_encoder.inverse_transform(prediction)[0]

        return render_template('index.html', prediction_text=f'Predicted Population Category: {predicted_label}')

    except Exception as e:
        return render_template('index.html', prediction_text=f'Error: {str(e)}')

if __name__ == '__main__':
    app.run(debug=True)