-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
51 lines (33 loc) · 1.19 KB
/
app.py
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
from flask import Flask,render_template,request
import pandas as pd
import pickle
app = Flask(__name__)
with open('house_price_model.pkl', 'rb')as f:
model = pickle.load(f)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST', 'GET'])
def predict():
try:
medinc = request.form['medinc'].strip()
houseage = request.form['houseage'].strip()
medinc = ''.join(filter(str.isdigit, medinc))
houseage = ''.join(filter(str.isdigit, houseage))
medinc = float(medinc) if medinc else 0
houseage = float(houseage) if houseage else 0
# Prepare the data for prediction
new_data = pd.DataFrame({
'MedInc': [medinc],
'HouseAge': [houseage]
})
# Make the prediction
predictions = model.predict(new_data)[0]
# Render the prediction result on a new page
return render_template('result.html', predictions=predictions)
except ValueError as ve:
return f"Value error: {ve}"
except Exception as e:
return f"An error occurred: {e}"
if __name__ == "__main__":
app.run(debug=True)