Spaces:
No application file
No application file
| from flask import Flask, render_template, request, redirect, url_for, session, make_response | |
| import sqlite3 | |
| from datetime import timedelta | |
| import seaborn as sns | |
| from sklearn.linear_model import LogisticRegression | |
| import numpy as np | |
| # Flask app setup | |
| app = Flask(__name__) | |
| app.secret_key = "supersecretkey" # Change this to a secure key in production | |
| app.permanent_session_lifetime = timedelta(days=7) | |
| # Helper function to connect to SQLite database | |
| def get_db_connection(): | |
| conn = sqlite3.connect("users.db") | |
| conn.row_factory = sqlite3.Row | |
| return conn | |
| # Initialize database with users table | |
| def init_db(): | |
| conn = get_db_connection() | |
| conn.execute(""" | |
| CREATE TABLE IF NOT EXISTS users ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| username TEXT UNIQUE NOT NULL, | |
| password TEXT NOT NULL | |
| ) | |
| """) | |
| conn.commit() | |
| conn.close() | |
| # Initialize DB at startup | |
| init_db() | |
| # Home page -> always redirect to login | |
| def home(): | |
| return redirect(url_for("login")) | |
| # Load dataset and train model once | |
| iris_data = sns.load_dataset("iris") | |
| X = iris_data.iloc[:, 0:4].values | |
| y = iris_data.iloc[:, 4].values | |
| model = LogisticRegression(max_iter=300) | |
| model.fit(X, y) | |
| def predict_flower(): | |
| try: | |
| # Get data from form | |
| sl = float(request.form['sl']) | |
| sw = float(request.form['sw']) | |
| pl = float(request.form['pl']) | |
| pw = float(request.form['pw']) | |
| # Predict | |
| prediction = model.predict([[sl, sw, pl, pw]]) | |
| result = prediction[0] | |
| return render_template("form.html", result=result) | |
| except Exception as e: | |
| return render_template("form.html", result=f"Error: {str(e)}") | |
| def show_form(): | |
| return render_template("form.html") | |
| # Signup page | |
| def signup(): | |
| if request.method == "POST": | |
| username = request.form["username"] | |
| password = request.form["password"] | |
| conn = get_db_connection() | |
| try: | |
| conn.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password)) | |
| conn.commit() | |
| conn.close() | |
| return redirect(url_for("login")) | |
| except sqlite3.IntegrityError: | |
| return "Username already exists! Try another." | |
| return render_template("signup.html") | |
| # Login page | |
| def login(): | |
| if request.method == "POST": | |
| username = request.form["username"] | |
| password = request.form["password"] | |
| remember = request.form.get("remember") | |
| conn = get_db_connection() | |
| user = conn.execute( | |
| "SELECT * FROM users WHERE username=? AND password=?", | |
| (username, password) | |
| ).fetchone() | |
| conn.close() | |
| if user: | |
| session.permanent = (remember == "on") | |
| session["username"] = username | |
| resp = make_response(redirect(url_for("index"))) # Go to index.html after login | |
| resp.set_cookie("last_visit", "Welcome back, " + username, | |
| max_age=(7*24*60*60 if remember == "on" else None)) | |
| return resp | |
| else: | |
| return "Invalid username or password. Try again." | |
| return render_template("login.html") | |
| # Index page - accessible only if logged in | |
| def index(): | |
| username = session.get("username") | |
| if not username: | |
| return redirect(url_for("login")) | |
| last_visit = request.cookies.get("last_visit", "First time visiting!") | |
| return render_template("index.html", username=username, last_visit=last_visit) | |
| # Logout route | |
| def logout(): | |
| session.pop("username", None) | |
| resp = make_response(redirect(url_for("login"))) | |
| resp.set_cookie("last_visit", "", expires=0) | |
| return resp | |
| # Run the app | |
| if __name__ == "__main__": | |
| app.run(debug=True, host="0.0.0.0", port=5000) | |