Spaces:
Sleeping
Sleeping
| from flask import Flask, request, render_template | |
| import joblib | |
| import pandas as pd | |
| # Initialize Flask app | |
| app = Flask(__name__, template_folder="templates", static_folder="static") | |
| # Load model and preprocessor | |
| model = joblib.load("insurance_model.pkl") | |
| preprocessor = joblib.load("preprocessor.pkl") | |
| def index(): | |
| prediction = None | |
| if request.method == "POST": | |
| try: | |
| # Get form inputs | |
| age = float(request.form["age"]) | |
| sex = request.form["sex"] | |
| bmi = float(request.form["bmi"]) | |
| children = int(request.form["children"]) | |
| smoker = request.form["smoker"] | |
| region = request.form["region"] | |
| # Create DataFrame for prediction | |
| user_input = pd.DataFrame([[age, sex, bmi, children, smoker, region]], | |
| columns=["age", "sex", "bmi", "children", "smoker", "region"]) | |
| # Transform input | |
| user_input_processed = preprocessor.transform(user_input) | |
| # Predict | |
| pred = model.predict(user_input_processed) | |
| prediction = round(pred[0], 2) | |
| except Exception as e: | |
| prediction = f"Error: {str(e)}" | |
| return render_template("index.html", prediction=prediction) | |
| if __name__ == "__main__": | |
| app.run(debug=True) | |