Page 169 - FULL REPORT 30012024
P. 169

APPENDICES




                        APPENDIX A: FLASK APP.PY CODE



                        from flask import Flask, render_template, request, redirect,url_for,
                        session, flash, jsonify
                        from joblib import load
                        import mysql.connector
                        import pandas as pd
                        import matplotlib
                        matplotlib.use('Agg')
                        import json

                        admin_username = 'admin';
                        admin_password = "123";

                        app = Flask(__name__)
                        app.secret_key = 'anginahmarcare'
                        app.config['SESSION_TYPE'] = 'filesystem'

                        def get_db_connection():
                            return mysql.connector.connect(
                                host="localhost",
                                user="root",
                                password="",
                                database="anginahmarv2"

                            )

                        # Load the trained model and label encoders
                        model = load('trained_model.joblib')
                        le_gender = load('le_gender.joblib')
                        le_smoking_status = load('le_smoking_status.joblib')
                        le_ever_married = load('le_ever_married.joblib')
                        le_work_type = load('le_work_type.joblib')
                        le_Residence_type = load('le_Residence_type.joblib')
                        le_age_category = load('le_age_category.joblib')
                        le_bmi_category = load('le_bmi_category.joblib')


                        # Function to categorize age and BMI
                        def categorize_age(age):
                            if age <= 14:
                                return 'Child'
                            elif age <= 24:
                                return 'Youth'
                            elif age <= 64:
                                return 'Adult'
                            else:
                                return 'Senior'

                        def categorize_bmi(bmi):
                            if bmi < 18.5:
                                                               152
   164   165   166   167   168   169   170   171   172   173   174