Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,86 @@
|
|
| 1 |
from flask import Flask, render_template, request, redirect, url_for, session, flash
|
| 2 |
-
import
|
|
|
|
| 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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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 |
-
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 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 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
conn.commit()
|
| 43 |
-
conn.close()
|
| 44 |
-
flash("Signup successful! Please log in.", "success")
|
| 45 |
-
return redirect(url_for("login"))
|
| 46 |
-
except:
|
| 47 |
flash("Username already exists. Try another one.", "error")
|
| 48 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return render_template("signup.html")
|
| 50 |
|
|
|
|
| 51 |
@app.route("/login", methods=["GET", "POST"])
|
| 52 |
def login():
|
| 53 |
if request.method == "POST":
|
| 54 |
username = request.form["username"]
|
| 55 |
password = request.form["password"]
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
user = c.fetchone()
|
| 60 |
-
conn.close()
|
| 61 |
-
if user and check_password_hash(user[2], password):
|
| 62 |
session["username"] = username
|
| 63 |
flash("Login successful!", "success")
|
| 64 |
return redirect(url_for("home"))
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
return render_template("login.html")
|
| 69 |
|
|
|
|
| 70 |
@app.route("/logout")
|
| 71 |
def logout():
|
| 72 |
session.pop("username", None)
|
| 73 |
flash("You have been logged out.", "info")
|
| 74 |
return redirect(url_for("login"))
|
| 75 |
|
|
|
|
| 76 |
@app.route("/home")
|
| 77 |
def home():
|
| 78 |
if "username" not in session:
|
| 79 |
return redirect(url_for("login"))
|
| 80 |
return render_template("home.html", username=session["username"])
|
| 81 |
|
|
|
|
| 82 |
@app.route("/predict", methods=["GET", "POST"])
|
| 83 |
def predict():
|
| 84 |
if "username" not in session:
|
| 85 |
return redirect(url_for("login"))
|
| 86 |
-
|
|
|
|
| 87 |
if request.method == "POST":
|
| 88 |
features = [
|
| 89 |
float(request.form["longitude"]),
|
|
@@ -96,10 +93,9 @@ def predict():
|
|
| 96 |
float(request.form["median_income"]),
|
| 97 |
float(request.form["rooms_per_household"])
|
| 98 |
]
|
| 99 |
-
prediction = model.predict([features])[0]
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
return render_template("predict.html", prediction=None)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from flask import Flask, render_template, request, redirect, url_for, session, flash
|
| 2 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
| 3 |
+
from supabase import create_client
|
| 4 |
import joblib
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
app.secret_key = "supersecretkey" # change in production
|
| 9 |
|
| 10 |
+
# -------- Supabase setup --------
|
| 11 |
+
SUPABASE_URL = "https://YOUR_PROJECT_ID.supabase.co"
|
| 12 |
+
SUPABASE_KEY = "YOUR_ANON_KEY"
|
| 13 |
+
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# -------- Load ML model --------
|
| 16 |
+
model = joblib.load("rf_house_price_model.joblib")
|
| 17 |
|
| 18 |
+
# -------- Routes --------
|
| 19 |
@app.route("/")
|
| 20 |
def index():
|
| 21 |
if "username" in session:
|
| 22 |
return redirect(url_for("home"))
|
| 23 |
return redirect(url_for("login"))
|
| 24 |
|
| 25 |
+
# Signup
|
| 26 |
@app.route("/signup", methods=["GET", "POST"])
|
| 27 |
def signup():
|
| 28 |
if request.method == "POST":
|
| 29 |
username = request.form["username"]
|
| 30 |
+
password_hashed = generate_password_hash(request.form["password"])
|
| 31 |
+
|
| 32 |
+
# Check if username exists
|
| 33 |
+
res = supabase.table("users").select("*").eq("username", username).execute()
|
| 34 |
+
if res.data:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
flash("Username already exists. Try another one.", "error")
|
| 36 |
+
return redirect(url_for("signup"))
|
| 37 |
+
|
| 38 |
+
# Insert new user
|
| 39 |
+
supabase.table("users").insert({"username": username, "password": password_hashed}).execute()
|
| 40 |
+
flash("Signup successful! Please log in.", "success")
|
| 41 |
+
return redirect(url_for("login"))
|
| 42 |
+
|
| 43 |
return render_template("signup.html")
|
| 44 |
|
| 45 |
+
# Login
|
| 46 |
@app.route("/login", methods=["GET", "POST"])
|
| 47 |
def login():
|
| 48 |
if request.method == "POST":
|
| 49 |
username = request.form["username"]
|
| 50 |
password = request.form["password"]
|
| 51 |
+
|
| 52 |
+
res = supabase.table("users").select("*").eq("username", username).execute()
|
| 53 |
+
if res.data and check_password_hash(res.data[0]["password"], password):
|
|
|
|
|
|
|
|
|
|
| 54 |
session["username"] = username
|
| 55 |
flash("Login successful!", "success")
|
| 56 |
return redirect(url_for("home"))
|
| 57 |
+
|
| 58 |
+
flash("Invalid credentials. Try again.", "error")
|
| 59 |
+
return redirect(url_for("login"))
|
| 60 |
+
|
| 61 |
return render_template("login.html")
|
| 62 |
|
| 63 |
+
# Logout
|
| 64 |
@app.route("/logout")
|
| 65 |
def logout():
|
| 66 |
session.pop("username", None)
|
| 67 |
flash("You have been logged out.", "info")
|
| 68 |
return redirect(url_for("login"))
|
| 69 |
|
| 70 |
+
# Home page
|
| 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 |
+
# Predict page
|
| 78 |
@app.route("/predict", methods=["GET", "POST"])
|
| 79 |
def predict():
|
| 80 |
if "username" not in session:
|
| 81 |
return redirect(url_for("login"))
|
| 82 |
+
|
| 83 |
+
prediction = None
|
| 84 |
if request.method == "POST":
|
| 85 |
features = [
|
| 86 |
float(request.form["longitude"]),
|
|
|
|
| 93 |
float(request.form["median_income"]),
|
| 94 |
float(request.form["rooms_per_household"])
|
| 95 |
]
|
| 96 |
+
prediction = round(model.predict([features])[0], 2)
|
| 97 |
+
|
| 98 |
+
return render_template("predict.html", prediction=prediction)
|
|
|
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
app.run(host="0.0.0.0", port=7860)
|