-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
215 lines (161 loc) · 6.48 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from flask import Flask, json,redirect,render_template,flash,request
from flask.globals import request, session
from flask.helpers import url_for
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from werkzeug.security import generate_password_hash,check_password_hash
from flask_login import login_required,logout_user,login_user,login_manager,LoginManager,current_user
#from flask_mail import Mail
import json
import numpy as np
import pickle
import pandas as pd
import json
import plotly
import plotly.express as px
import plotly.graph_objs as go
model = pickle.load(open('stresslevel.pkl', 'rb'))
#creation of the Flask Application named as "app"
# mydatabase connection
local_server=True
app=Flask(__name__)
app = Flask(__name__,
static_url_path='',
static_folder='static',
template_folder='templates')
# app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:@localhost/mental'
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///database.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
db=SQLAlchemy(app)
app.secret_key="tandrima"
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
id=db.Column(db.Integer,primary_key=True)
usn=db.Column(db.String(20),unique=True)
pas=db.Column(db.String(1000))
@app.route('/')
def home():
return render_template('index.html')
@app.route('/signup')
@app.route('/signup',methods=['POST','GET'])
def signup():
if request.method=="POST":
usn=request.form.get('usn')
pas=request.form.get('pas')
# print(usn,pas)
encpassword=generate_password_hash(pas)
user=User.query.filter_by(usn=usn).first()
if user:
flash("UserID is already taken","warning")
return render_template("usersignup.html")
db.engine.execute(f"INSERT INTO `user` (`usn`,`pas`) VALUES ('{usn}','{encpassword}') ")
flash("SignUp Success Please Login","success")
return render_template("userlogin.html")
return render_template("usersignup.html")
@app.route('/login',methods=['POST','GET'])
def login():
if request.method=="POST":
usn=request.form.get('usn')
pas=request.form.get('pas')
user=User.query.filter_by(usn=usn).first()
if user and check_password_hash(user.pas,pas):
login_user(user)
flash("Login Success","info")
return render_template("index.html")
else:
flash("Invalid Credentials","danger")
return render_template("userlogin.html")
return render_template("userlogin.html")
@app.route('/logout')
@login_required
def logout():
logout_user()
flash("Logout SuccessFul","warning")
return redirect(url_for('login'))
@app.route('/music')
@login_required
def music():
return render_template('music.html')
@app.route('/quizandgame')
@login_required
def quizandgame():
return render_template('quizandgame.html')
@app.route('/exercises')
@login_required
def exercises():
return render_template('exercises.html')
@app.route('/quiz')
def quiz():
return render_template('quiz.html')
@app.route('/game')
def game():
return render_template('game.html')
@app.route('/analysis',methods=['GET'])
def analysis():
#reading the dataset
train_df = pd.read_csv('dreaddit-train.csv',encoding='ISO-8859-1')
train_df.drop(['text', 'post_id' , 'sentence_range', 'id', 'social_timestamp'], axis=1, inplace=True)
values = train_df['subreddit'].value_counts()
labels = train_df['subreddit'].value_counts().index
fig = px.pie(train_df, names=labels, values=values)
fig.update_layout(title='Distribution of Subreddits')
fig.update_traces(hovertemplate='%{label}: %{value}')
#convert the plot to JSON using json.dumps() and the JSON encoder that comes with Plotly
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
train_df['label'].replace([0,1],['Not in Stress','In Stress'],inplace=True)
fig2=px.histogram(train_df,
x="label",
title='Distribution of Stress Type',
color="label"
)
fig2.update_layout(bargap=0.1)
#convert the plot to JSON using json.dumps() and the JSON encoder that comes with Plotly
graphJSON2 = json.dumps(fig2, cls=plotly.utils.PlotlyJSONEncoder)
fig3 = px.bar(train_df,
x='subreddit',
y='sentiment',
title='Car brand year resale ratio',
color='subreddit')
fig3.update_traces()
graphJSON3 = json.dumps(fig3, cls=plotly.utils.PlotlyJSONEncoder)
fig4 = px.scatter(train_df,
x='subreddit',
y='social_karma',
title='Car brand price thousand ratio',
color="subreddit")
fig4.update_traces()
graphJSON4 = json.dumps(fig4, cls=plotly.utils.PlotlyJSONEncoder)
fig5 = px.histogram(train_df,
x='confidence',
marginal='box',
title='Distribution of count reason of Mental Health issue',)
fig5.update_layout(bargap=0.1)
graphJSON5 = json.dumps(fig5, cls=plotly.utils.PlotlyJSONEncoder)
fig6=px.histogram(train_df,
x="subreddit",
title='Distribution of Vehicle Type',color='subreddit')
fig6.update_layout(bargap=0.1)
graphJSON6 = json.dumps(fig6, cls=plotly.utils.PlotlyJSONEncoder)
return render_template('analysis.html', graphJSON=graphJSON,graphJSON2=graphJSON2,graphJSON3=graphJSON3,graphJSON4=graphJSON4,
graphJSON5=graphJSON5,graphJSON6=graphJSON6)
@app.route('/i')
def i():
return render_template('stress.html')
@app.route('/stressdetect',methods=['POST'])
def stressdetect():
int_features = [int(x) for x in request.form.values()]
final_features = [np.array(int_features)]
prediction = model.predict(final_features)
#on basis of prediction displaying the desired output
if prediction=="Absence":
data="You are having Normal Stress!! Take Care of yourself"
elif prediction=="Presence":
data="You are having High Stress!! Consult a doctor and get the helpline number from our chatbot"
return render_template('stress.html', prediction_text3='Stress Level is: {}'.format(data))
if __name__=="__main__":
app.run(debug=True)