project_python_rynekNieruch.../home_pricing/rest_api_server.py
2024-02-26 16:54:44 +01:00

47 lines
1.8 KiB
Python

from flask import Flask, request, jsonify
from flask_cors import CORS
import joblib
import pandas as pd
from DataPreprocessor.helpers.OffersCSVReader import OffersCSVReader
from DataPreprocessor.DataPreprocessor import DataPreprocessor
from pandas.core.frame import DataFrame
app = Flask(__name__)
CORS(app) # This will enable CORS for all routes
# Reading downloaded data
data_frame : DataFrame = OffersCSVReader.read_from_file("output.csv")
# Prepare data for neural network (data preprocessing)
data_preprocessor = DataPreprocessor(data_frame)
data_preprocessor.preprocess_data()
trained_model = joblib.load('trained_model.pkl')
@app.route('/calculate_price', methods=['POST'])
def calculate_price():
input_data = request.json
scaled_area = data_preprocessor.get_value('Area', pd.DataFrame({'Area': [input_data["powierzchnia"]]}))
scaled_construction_year = data_preprocessor.get_value('Construction year', pd.DataFrame({'Construction year': [input_data["rok_budowy"]]}))
encoded_location = data_preprocessor.get_value("Location", [input_data["dzielnica"]])
encoded_state = data_preprocessor.get_value("State", [input_data["stan_nieruchomosci"]])
encoded_property_form = data_preprocessor.get_value("Property form", [input_data["forma_wlasnosci"]])
floor = input_data['numer_pietra']
rooms = input_data['ilosc_pokoi']
sample_data = [[scaled_area, rooms, floor, encoded_property_form, encoded_state, encoded_location, scaled_construction_year]]
sample = pd.DataFrame(sample_data, columns=['Area', 'Rooms', 'Floor', 'Property form' , 'State', 'Location', 'Construction year'])
prediction = trained_model.predict(sample)
calculated_price = {
'estimated_price': round(float(prediction),0)
}
return jsonify(calculated_price)
if __name__ == '__main__':
app.run(debug=True, port=8081)