house_price / app.py
Krishs21
Changing App.py
6da165d
Raw History Blame Contribute Delete
2.93 kB
import gradio as gr
import joblib
import numpy as np
import pandas as pd
# Load model and scaler
model = joblib.load("linear_model.pkl")
scaler = joblib.load("scaler.pkl")
# Define a minimum living area threshold (you can adjust this as necessary)
MIN_LIVING_AREA = 300 # Example minimum threshold, adjust accordingly
# Prediction function
def predict_price(
bedrooms, bathrooms, living_area, floors,
condition, grade, built_year
):
# Check if any input is zero
if any(value == 0 for value in [bedrooms, bathrooms, living_area, floors, condition, grade, built_year]):
return "Error: None of the values should be zero."
# Check if living area is below minimum
if living_area < MIN_LIVING_AREA:
return f"Error: Living area should be greater than {MIN_LIVING_AREA} sq ft."
# Adjust bathroom count if more than bedroom count
if bathrooms > bedrooms:
bathroom_diff = bathrooms - bedrooms
bathrooms = max(0, bedrooms - bathroom_diff)
if built_year<2000 :
return "Error: House is too old for sale."
# Feature engineering
grade_poor = 1 if grade <= 5 else 0
grade_very_good = 1 if grade >= 8 else 0
condition_bad = 1 if condition <= 2 else 0
condition_good = 1 if condition >= 4 else 0
floor_type_single = 1 if floors == 1 else 0
# Final input data
input_data = np.array([[
bedrooms, bathrooms, living_area, floors,
condition, grade, built_year,
grade_poor, grade_very_good, condition_bad,
condition_good, floor_type_single
]])
# Use exact training column names
feature_names = [
"number of bedrooms", "number of bathrooms", "living area", "number of floors",
"condition of the house", "grade of the house",
"Built Year","Grade_Category_Poor", "Grade_Category_Very Good",
"Condition_Category_Bad", "Condition_Category_Good", "Floor_Type_Single"
]
# Scale input and wrap in DataFrame
input_scaled = scaler.transform(input_data)
input_scaled_df = pd.DataFrame(input_scaled, columns=feature_names)
# Predict
prediction = model.predict(input_scaled_df)[0]
if prediction<0:
return f"Error: Poor state of house , not fit for sale"
return f"Estimated Price: ₹{round(prediction, 2)}"
# Gradio UI
inputs = [
gr.Number(label="Number of Bedrooms"),
gr.Number(label="Number of Bathrooms"),
gr.Number(label="Living Area (sq ft)"),
gr.Number(label="Number of Floors"),
gr.Number(label="Condition of the House (1-5)"),
gr.Number(label="Grade of the House (1-10)"),
gr.Number(label="Built Year")
]
output = gr.Textbox(label="Predicted House Price")
# Launch Gradio app
gr.Interface(
fn=predict_price,
inputs=inputs,
outputs=output,
title="House Price Prediction App",
description="Enter the details of the house to predict the estimated price."
).launch()