simran40 commited on
Commit
c0b60a4
·
verified ·
1 Parent(s): 9191d9a

Update app.py

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