Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, session
|
| 2 |
+
import sqlite3
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
app.secret_key = "supersecretkey" # change in production
|
| 9 |
+
|
| 10 |
+
MODEL_PATH = "rf_house_price_model.joblib"
|
| 11 |
+
model = joblib.load(MODEL_PATH)
|
| 12 |
+
|
| 13 |
+
# ---------- Database Setup ----------
|
| 14 |
+
def init_db():
|
| 15 |
+
conn = sqlite3.connect("users.db")
|
| 16 |
+
c = conn.cursor()
|
| 17 |
+
c.execute('''CREATE TABLE IF NOT EXISTS users
|
| 18 |
+
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 19 |
+
username TEXT UNIQUE,
|
| 20 |
+
password TEXT)''')
|
| 21 |
+
conn.commit()
|
| 22 |
+
conn.close()
|
| 23 |
+
|
| 24 |
+
init_db()
|
| 25 |
+
|
| 26 |
+
# ---------- Routes ----------
|
| 27 |
+
@app.route("/")
|
| 28 |
+
def index():
|
| 29 |
+
if "username" in session:
|
| 30 |
+
return redirect(url_for("home"))
|
| 31 |
+
return redirect(url_for("login"))
|
| 32 |
+
|
| 33 |
+
@app.route("/signup", methods=["GET", "POST"])
|
| 34 |
+
def signup():
|
| 35 |
+
if request.method == "POST":
|
| 36 |
+
username = request.form["username"]
|
| 37 |
+
password = generate_password_hash(request.form["password"])
|
| 38 |
+
try:
|
| 39 |
+
conn = sqlite3.connect("users.db")
|
| 40 |
+
c = conn.cursor()
|
| 41 |
+
c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
|
| 42 |
+
conn.commit()
|
| 43 |
+
conn.close()
|
| 44 |
+
return redirect(url_for("login"))
|
| 45 |
+
except:
|
| 46 |
+
return "Username already exists!"
|
| 47 |
+
return render_template("signup.html")
|
| 48 |
+
|
| 49 |
+
@app.route("/login", methods=["GET", "POST"])
|
| 50 |
+
def login():
|
| 51 |
+
if request.method == "POST":
|
| 52 |
+
username = request.form["username"]
|
| 53 |
+
password = request.form["password"]
|
| 54 |
+
conn = sqlite3.connect("users.db")
|
| 55 |
+
c = conn.cursor()
|
| 56 |
+
c.execute("SELECT * FROM users WHERE username=?", (username,))
|
| 57 |
+
user = c.fetchone()
|
| 58 |
+
conn.close()
|
| 59 |
+
if user and check_password_hash(user[2], password):
|
| 60 |
+
session["username"] = username
|
| 61 |
+
return redirect(url_for("home"))
|
| 62 |
+
else:
|
| 63 |
+
return "Invalid credentials"
|
| 64 |
+
return render_template("login.html")
|
| 65 |
+
|
| 66 |
+
@app.route("/logout")
|
| 67 |
+
def logout():
|
| 68 |
+
session.pop("username", None)
|
| 69 |
+
return redirect(url_for("login"))
|
| 70 |
+
|
| 71 |
+
@app.route("/home")
|
| 72 |
+
def home():
|
| 73 |
+
if "username" not in session:
|
| 74 |
+
return redirect(url_for("login"))
|
| 75 |
+
return render_template("home.html", username=session["username"])
|
| 76 |
+
|
| 77 |
+
@app.route("/predict", methods=["GET", "POST"])
|
| 78 |
+
def predict():
|
| 79 |
+
if "username" not in session:
|
| 80 |
+
return redirect(url_for("login"))
|
| 81 |
+
|
| 82 |
+
if request.method == "POST":
|
| 83 |
+
features = [
|
| 84 |
+
float(request.form["longitude"]),
|
| 85 |
+
float(request.form["latitude"]),
|
| 86 |
+
float(request.form["housing_median_age"]),
|
| 87 |
+
float(request.form["total_rooms"]),
|
| 88 |
+
float(request.form["total_bedrooms"]),
|
| 89 |
+
float(request.form["population"]),
|
| 90 |
+
float(request.form["households"]),
|
| 91 |
+
float(request.form["median_income"]),
|
| 92 |
+
float(request.form["rooms_per_household"])
|
| 93 |
+
]
|
| 94 |
+
prediction = model.predict([features])[0]
|
| 95 |
+
return render_template("predict.html", prediction=round(prediction, 2))
|
| 96 |
+
|
| 97 |
+
return render_template("predict.html", prediction=None)
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
app.run(host="0.0.0.0", port=7860)
|