Gagan0141 commited on
Commit
e009751
·
verified ·
1 Parent(s): 2e46c05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -10
app.py CHANGED
@@ -1,21 +1,90 @@
1
- from flask import Flask, request, render_template
2
  import seaborn as sns
3
  from sklearn.linear_model import LogisticRegression
 
4
 
5
- # Load dataset
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  df = sns.load_dataset("iris")
7
  X = df.iloc[:, :4].values
8
  y = df.iloc[:, 4].values
9
 
10
- # Train model
11
  model = LogisticRegression(max_iter=200, multi_class="auto")
12
  model.fit(X, y)
13
 
14
- # Flask app
15
- app = Flask(__name__)
16
 
17
- @app.route("/", methods=["GET", "POST"])
18
  def home():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  if request.method == "POST":
20
  try:
21
  sepal_length = float(request.form["sepal_length"])
@@ -27,11 +96,57 @@ def home():
27
  [[sepal_length, sepal_width, petal_length, petal_width]]
28
  )[0]
29
 
30
- return render_template("index.html", prediction_text=f"🌸 Predicted Flower: {prediction}")
31
  except Exception as e:
32
- return render_template("index.html", prediction_text=f"⚠️ Error: {e}")
 
 
33
 
34
- return render_template("index.html", prediction_text="")
 
 
 
 
35
 
36
  if __name__ == "__main__":
37
- app.run(host="0.0.0.0", port=7860, debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template, redirect, url_for, session, flash
2
  import seaborn as sns
3
  from sklearn.linear_model import LogisticRegression
4
+ import sqlite3, os
5
 
6
+ app = Flask(__name__)
7
+ app.secret_key = "supersecretkey" # change in production!
8
+
9
+ # ------------------- Database Setup -------------------
10
+ def init_db():
11
+ if not os.path.exists("users.db"):
12
+ conn = sqlite3.connect("users.db")
13
+ c = conn.cursor()
14
+ c.execute("""
15
+ CREATE TABLE users (
16
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
17
+ username TEXT UNIQUE NOT NULL,
18
+ password TEXT NOT NULL
19
+ )
20
+ """)
21
+ conn.commit()
22
+ conn.close()
23
+
24
+ init_db()
25
+
26
+ # ------------------- ML Model -------------------
27
  df = sns.load_dataset("iris")
28
  X = df.iloc[:, :4].values
29
  y = df.iloc[:, 4].values
30
 
 
31
  model = LogisticRegression(max_iter=200, multi_class="auto")
32
  model.fit(X, y)
33
 
34
+ # ------------------- Routes -------------------
 
35
 
36
+ @app.route("/")
37
  def home():
38
+ if "user" in session:
39
+ return redirect(url_for("predict"))
40
+ return redirect(url_for("login"))
41
+
42
+ @app.route("/signup", methods=["GET", "POST"])
43
+ def signup():
44
+ if request.method == "POST":
45
+ username = request.form["username"]
46
+ password = request.form["password"]
47
+
48
+ try:
49
+ conn = sqlite3.connect("users.db")
50
+ c = conn.cursor()
51
+ c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
52
+ conn.commit()
53
+ conn.close()
54
+ flash("Signup successful! Please login.", "success")
55
+ return redirect(url_for("login"))
56
+ except sqlite3.IntegrityError:
57
+ flash("Username already taken!", "danger")
58
+
59
+ return render_template("signup.html")
60
+
61
+ @app.route("/login", methods=["GET", "POST"])
62
+ def login():
63
+ if request.method == "POST":
64
+ username = request.form["username"]
65
+ password = request.form["password"]
66
+
67
+ conn = sqlite3.connect("users.db")
68
+ c = conn.cursor()
69
+ c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
70
+ user = c.fetchone()
71
+ conn.close()
72
+
73
+ if user:
74
+ session["user"] = username
75
+ flash("Login successful!", "success")
76
+ return redirect(url_for("predict"))
77
+ else:
78
+ flash("Invalid credentials!", "danger")
79
+
80
+ return render_template("login.html")
81
+
82
+ @app.route("/predict", methods=["GET", "POST"])
83
+ def predict():
84
+ if "user" not in session:
85
+ return redirect(url_for("login"))
86
+
87
+ prediction_text = ""
88
  if request.method == "POST":
89
  try:
90
  sepal_length = float(request.form["sepal_length"])
 
96
  [[sepal_length, sepal_width, petal_length, petal_width]]
97
  )[0]
98
 
99
+ prediction_text = f"Predicted Flower: {prediction}"
100
  except Exception as e:
101
+ prediction_text = f"Error: {e}"
102
+
103
+ return render_template("index.html", prediction_text=prediction_text)
104
 
105
+ @app.route("/logout")
106
+ def logout():
107
+ session.pop("user", None)
108
+ flash("Logged out successfully.", "info")
109
+ return redirect(url_for("login"))
110
 
111
  if __name__ == "__main__":
112
+ app.run(host="0.0.0.0", port=7860, debug=True)
113
+
114
+
115
+
116
+ # from flask import Flask, request, render_template
117
+ # import seaborn as sns
118
+ # from sklearn.linear_model import LogisticRegression
119
+
120
+ # # Load dataset
121
+ # df = sns.load_dataset("iris")
122
+ # X = df.iloc[:, :4].values
123
+ # y = df.iloc[:, 4].values
124
+
125
+ # # Train model
126
+ # model = LogisticRegression(max_iter=200, multi_class="auto")
127
+ # model.fit(X, y)
128
+
129
+ # # Flask app
130
+ # app = Flask(__name__)
131
+
132
+ # @app.route("/", methods=["GET", "POST"])
133
+ # def home():
134
+ # if request.method == "POST":
135
+ # try:
136
+ # sepal_length = float(request.form["sepal_length"])
137
+ # sepal_width = float(request.form["sepal_width"])
138
+ # petal_length = float(request.form["petal_length"])
139
+ # petal_width = float(request.form["petal_width"])
140
+
141
+ # prediction = model.predict(
142
+ # [[sepal_length, sepal_width, petal_length, petal_width]]
143
+ # )[0]
144
+
145
+ # return render_template("index.html", prediction_text=f"🌸 Predicted Flower: {prediction}")
146
+ # except Exception as e:
147
+ # return render_template("index.html", prediction_text=f"⚠️ Error: {e}")
148
+
149
+ # return render_template("index.html", prediction_text="")
150
+
151
+ # if __name__ == "__main__":
152
+ # app.run(host="0.0.0.0", port=7860, debug=True)