File size: 1,777 Bytes
8752cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask, render_template, request
import pickle
import numpy as np

app = Flask(__name__)

# Load model and columns
model = pickle.load(open("car_price_model.pkl", "rb"))
columns = pickle.load(open("model_columns.pkl", "rb"))

# @app.route("/")
# def home():
#     return "Flask is working!"

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        try:
            present_price = float(request.form["present_price"])
            kms_driven = int(request.form["kms_driven"])
            owner = int(request.form["owner"])
            car_age = int(request.form["car_age"])
            fuel_type = request.form["fuel_type"]
            company = request.form["company"]

            # Prepare input dictionary
            input_data = {
                "kms_driven": kms_driven,
                "Owner": owner,
                "car_age": car_age,
                "company_" + company: 1,
                "fuel_type_" + fuel_type: 1
            }

            input_vector = np.zeros(len(columns))
            for i, col in enumerate(columns):
                if col in input_data:
                    input_vector[i] = input_data[col]
                elif col == 'Present_Price':
                    input_vector[i] = present_price

            predicted_price = model.predict([input_vector])[0]
            return render_template("index.html", prediction_text=f"Estimated Selling Price: ₹ {predicted_price:,.2f}")
        except Exception as e:
            return render_template("index.html", prediction_text=f"Error: {str(e)}")

    return render_template("index.html", prediction_text="")
    
if __name__ == "__main__":
    app.run(debug=True,use_reloader=False)