WebashalarForML's picture
Update app.py
d1d6125 verified
from flask import Flask, render_template, request, redirect, url_for, flash
import os
from joblib import load
import numpy as np
app = Flask(__name__)
# Set the secret key for session management
app.secret_key = os.urandom(24)
# Configurations
UPLOAD_FOLDER = "uploads/"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DEBUG'] = True # Enable debugging during development
app.config['ENV'] = 'development'
# Load models
gia_model = load("Model/linear_regression_model_gia_price.joblib")
grade_model = load("Model/linear_regression_model_grade_price.joblib")
bygrade_model = load("Model/linear_regression_model_bygrade_price.joblib")
makable_model = load("Model/linear_regression_model_makable_price.joblib")
# Load label encoders for categorical features (renamed variable to avoid conflict with built-in list)
encoder_list = ['Tag', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngLab']
loaded_label_encoder = {}
for val in encoder_list:
encoder_path = f"Label_encoders/label_encoder_{val}.joblib"
loaded_label_encoder[val] = load(encoder_path)
@app.route('/', methods=['GET'])
def home():
return render_template('home.html')
@app.route('/predict', methods=['GET', 'POST'])
def predict():
# Redirect GET requests back to the home page
if request.method == 'GET':
return redirect(url_for('home'))
try:
# Retrieve form data and strip any extra spaces ['Tag', 'EngCts', 'EngShp', 'EngQua', 'EngCol', 'EngCut', 'EngPol', 'EngSym', 'EngFlo', 'EngNts', 'EngMikly', 'EngLab', 'EngAmt',
Tag = request.form.get('Tag', '').strip()
# ICarat = request.form.get('ICarat', '').strip()
# MkblCarat = request.form.get('MkblCarat', '').strip()
# SawLossCarat = request.form.get('SawLossCarat', '').strip()
# CrapsCarat = request.form.get('CrapsCarat', '').strip()
# EngGraphCts = request.form.get('EngGraphCts', '').strip()
EngCts = request.form.get('EngCts', '').strip()
EngShp = request.form.get('EngShp', '').strip()
EngQua = request.form.get('EngQua', '').strip()
EngCol = request.form.get('EngCol', '').strip()
EngCut = request.form.get('EngCut', '').strip()
EngPol = request.form.get('EngPol', '').strip()
EngSym = request.form.get('EngSym', '').strip()
EngFlo = request.form.get('EngFlo', '').strip()
EngNts = request.form.get('EngNts', '').strip()
EngMikly = request.form.get('EngMikly', '').strip()
EngLab = request.form.get('EngLab', '').strip()
EngAmt = request.form.get('EngAmt', '').strip()
if EngLab == "nan": # or if not EngLab:
EngLab = np.nan
else:
EngLab = EngLab # remains the same
# Check if any required field is empty
if not all([Tag,
#ICarat, MkblCarat, SawLossCarat, CrapsCarat, EngGraphCts,
EngCts, EngShp, EngQua, EngCol,
EngCut, EngPol, EngSym, EngFlo, EngNts, EngMikly,
EngLab, EngAmt]):
flash("Please fill all the fields", "error")
return redirect(url_for('home'))
# Transform categorical features using loaded label encoders
Tag = loaded_label_encoder['Tag'].transform([Tag])[0]
EngShp = loaded_label_encoder['EngShp'].transform([EngShp])[0]
EngQua = loaded_label_encoder['EngQua'].transform([EngQua])[0]
EngCol = loaded_label_encoder['EngCol'].transform([EngCol])[0]
EngCut = loaded_label_encoder['EngCut'].transform([EngCut])[0]
EngPol = loaded_label_encoder['EngPol'].transform([EngPol])[0]
EngSym = loaded_label_encoder['EngSym'].transform([EngSym])[0]
EngFlo = loaded_label_encoder['EngFlo'].transform([EngFlo])[0]
EngNts = loaded_label_encoder['EngNts'].transform([EngNts])[0]
EngMikly = loaded_label_encoder['EngMikly'].transform([EngMikly])[0]
EngLab = loaded_label_encoder['EngLab'].transform([EngLab])[0]
# Convert numeric fields to float
# ICarat = float(ICarat)
# MkblCarat = float(MkblCarat)
# SawLossCarat = float(SawLossCarat)
# CrapsCarat = float(CrapsCarat)
# EngGraphCts = float(EngGraphCts)
EngCts = float(EngCts)
EngAmt = float(EngAmt)
# Prepare input data for prediction
input_features = [[Tag,
#ICarat, MkblCarat, SawLossCarat, CrapsCarat, EngGraphCts,
EngCts, EngShp, EngQua, EngCol, EngCut,
EngPol, EngSym, EngFlo, EngNts, EngMikly, EngLab, EngAmt]]
# Make predictions
gia_price = gia_model.predict(input_features)[0]
grade_price = grade_model.predict(input_features)[0]
bygrade_price = bygrade_model.predict(input_features)[0]
makable_price = makable_model.predict(input_features)[0]
gia_diff = EngAmt - gia_price
grade_diff = EngAmt - grade_price
bygrade_diff = EngAmt - bygrade_price
makable_diff = EngAmt - makable_price
except Exception as e:
flash(f"Error in calculating the price: {e}", "error")
return redirect(url_for('home'))
return render_template('results.html', gia_price=gia_price, grade_price=grade_price,
bygrade_price=bygrade_price, makable_price=makable_price,
gia_diff=gia_diff,grade_diff=grade_diff,bygrade_diff=bygrade_diff,
makable_diff=makable_diff)
if __name__ == "__main__":
app.run(debug=True, use_reloader=False)