Spaces:
No application file
No application file
Upload 2 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, session, make_response
|
| 2 |
+
import sqlite3
|
| 3 |
+
from datetime import timedelta
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
from sklearn.linear_model import LogisticRegression
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Flask app setup
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
app.secret_key = "supersecretkey" # Change this to a secure key in production
|
| 11 |
+
app.permanent_session_lifetime = timedelta(days=7)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Helper function to connect to SQLite database
|
| 15 |
+
def get_db_connection():
|
| 16 |
+
conn = sqlite3.connect("users.db")
|
| 17 |
+
conn.row_factory = sqlite3.Row
|
| 18 |
+
return conn
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Initialize database with users table
|
| 22 |
+
def init_db():
|
| 23 |
+
conn = get_db_connection()
|
| 24 |
+
conn.execute("""
|
| 25 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 26 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 27 |
+
username TEXT UNIQUE NOT NULL,
|
| 28 |
+
password TEXT NOT NULL
|
| 29 |
+
)
|
| 30 |
+
""")
|
| 31 |
+
conn.commit()
|
| 32 |
+
conn.close()
|
| 33 |
+
|
| 34 |
+
# Initialize DB at startup
|
| 35 |
+
init_db()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# Home page -> always redirect to login
|
| 39 |
+
@app.route("/")
|
| 40 |
+
def home():
|
| 41 |
+
return redirect(url_for("login"))
|
| 42 |
+
|
| 43 |
+
# Load dataset and train model once
|
| 44 |
+
iris_data = sns.load_dataset("iris")
|
| 45 |
+
X = iris_data.iloc[:, 0:4].values
|
| 46 |
+
y = iris_data.iloc[:, 4].values
|
| 47 |
+
model = LogisticRegression(max_iter=300)
|
| 48 |
+
model.fit(X, y)
|
| 49 |
+
|
| 50 |
+
@app.route('/predict', methods=['POST'])
|
| 51 |
+
def predict_flower():
|
| 52 |
+
try:
|
| 53 |
+
# Get data from form
|
| 54 |
+
sl = float(request.form['sl'])
|
| 55 |
+
sw = float(request.form['sw'])
|
| 56 |
+
pl = float(request.form['pl'])
|
| 57 |
+
pw = float(request.form['pw'])
|
| 58 |
+
|
| 59 |
+
# Predict
|
| 60 |
+
prediction = model.predict([[sl, sw, pl, pw]])
|
| 61 |
+
result = prediction[0]
|
| 62 |
+
|
| 63 |
+
return render_template("form.html", result=result)
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return render_template("form.html", result=f"Error: {str(e)}")
|
| 67 |
+
|
| 68 |
+
@app.route('/form')
|
| 69 |
+
def show_form():
|
| 70 |
+
return render_template("form.html")
|
| 71 |
+
|
| 72 |
+
# Signup page
|
| 73 |
+
@app.route("/signup", methods=["GET", "POST"])
|
| 74 |
+
def signup():
|
| 75 |
+
if request.method == "POST":
|
| 76 |
+
username = request.form["username"]
|
| 77 |
+
password = request.form["password"]
|
| 78 |
+
|
| 79 |
+
conn = get_db_connection()
|
| 80 |
+
try:
|
| 81 |
+
conn.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
|
| 82 |
+
conn.commit()
|
| 83 |
+
conn.close()
|
| 84 |
+
return redirect(url_for("login"))
|
| 85 |
+
except sqlite3.IntegrityError:
|
| 86 |
+
return "Username already exists! Try another."
|
| 87 |
+
|
| 88 |
+
return render_template("signup.html")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# Login page
|
| 92 |
+
@app.route("/login", methods=["GET", "POST"])
|
| 93 |
+
def login():
|
| 94 |
+
if request.method == "POST":
|
| 95 |
+
username = request.form["username"]
|
| 96 |
+
password = request.form["password"]
|
| 97 |
+
remember = request.form.get("remember")
|
| 98 |
+
|
| 99 |
+
conn = get_db_connection()
|
| 100 |
+
user = conn.execute(
|
| 101 |
+
"SELECT * FROM users WHERE username=? AND password=?",
|
| 102 |
+
(username, password)
|
| 103 |
+
).fetchone()
|
| 104 |
+
conn.close()
|
| 105 |
+
|
| 106 |
+
if user:
|
| 107 |
+
session.permanent = (remember == "on")
|
| 108 |
+
session["username"] = username
|
| 109 |
+
|
| 110 |
+
resp = make_response(redirect(url_for("index"))) # Go to index.html after login
|
| 111 |
+
resp.set_cookie("last_visit", "Welcome back, " + username,
|
| 112 |
+
max_age=(7*24*60*60 if remember == "on" else None))
|
| 113 |
+
return resp
|
| 114 |
+
else:
|
| 115 |
+
return "Invalid username or password. Try again."
|
| 116 |
+
return render_template("login.html")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# Index page - accessible only if logged in
|
| 120 |
+
@app.route("/index")
|
| 121 |
+
def index():
|
| 122 |
+
username = session.get("username")
|
| 123 |
+
if not username:
|
| 124 |
+
return redirect(url_for("login"))
|
| 125 |
+
last_visit = request.cookies.get("last_visit", "First time visiting!")
|
| 126 |
+
return render_template("index.html", username=username, last_visit=last_visit)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# Logout route
|
| 130 |
+
@app.route("/logout")
|
| 131 |
+
def logout():
|
| 132 |
+
session.pop("username", None)
|
| 133 |
+
resp = make_response(redirect(url_for("login")))
|
| 134 |
+
resp.set_cookie("last_visit", "", expires=0)
|
| 135 |
+
return resp
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# Run the app
|
| 140 |
+
if __name__ == "__main__":
|
| 141 |
+
app.run(debug=True, host="0.0.0.0", port=5000)
|
users.db
ADDED
|
Binary file (16.4 kB). View file
|
|
|