Spaces:
Build error
Build error
| 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 | |
| def home(): | |
| return render_template('index.html') | |
| 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) | |