-
Notifications
You must be signed in to change notification settings - Fork 5
/
Main.py
283 lines (213 loc) · 7.81 KB
/
Main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import pandas as pd
from numpy import *
import numpy as np
import numpy
import pygal
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import io
import base64
import xlrd #To read Excel File
import csv # importing csv module
from pygal.style import Style
custom_style = Style(
colors=('#E80080', '#404040', '#9BC850', '#E80017','#17E800'))
from xlwt import Workbook #To perforn Excel file I/O operation
wb=Workbook()
# Give the location of the file
loc = ("questions.xlsx")
wb = xlrd.open_workbook(loc) #Opening Excel Workbook of Our Given Location
sheet = wb.sheet_by_index(0) #Select the Sheet Number(0,1,2....)
#No. of Questions are = qnlenth
qnlenth=sheet.nrows #Length of Sheet Questions
qnlist=[] #Array of Questions
qnnumber=[] #Array of Questions Number
optn1=[] #Array of Option 1
optn2=[] #Array of Option 2
optn3=[] #Array of Option 3
optn4=[] #Array of Option 4
optn5=[] #Array of Option 5
trait=[] #Array of Trait(Extraversion, Agreeableness, Conscientiousness, Emotional Stability, Intellect )
#Reading ALL Values of from file in array format:
for i in range (qnlenth):
qnnumber=np.append(qnnumber,(sheet.cell_value(i, 0) ))
qnlist=np.append(qnlist,(sheet.cell_value(i, 1) ))
optn1=np.append(optn1,(sheet.cell_value(i, 2) ))
optn2=np.append(optn2,(sheet.cell_value(i, 3) ))
optn3=np.append(optn3,(sheet.cell_value(i, 4) ))
optn4=np.append(optn4,(sheet.cell_value(i, 5) ))
optn5=np.append(optn5,(sheet.cell_value(i, 6) ))
trait=np.append(trait,(sheet.cell_value(i, 7) ))
from flask import Flask, redirect, url_for, request ,render_template
app = Flask(__name__)
@app.route('/')
def main():
#return 'welcome to main page'
return '<br><a href="http://127.0.0.1:5000/front"><button onclick= "" align="center" >Welcome to Personality detection</button></a>'
@app.route('/front')
def front():
return render_template('front.html')
@app.route('/displaygen')
def displaygen():
return render_template('diplaygen.html')
G=0
@app.route('/nxt',methods=['POST'])
def nxt():
if request.method=='POST':
G=int(request.form['g'])
G=int(G)
print(G)
return redirect(url_for('displayqn'))
@app.route('/displayqn')
def displayqn():
f = open("responce.csv", "w")
f.truncate()
"""
if b is None:
b = -1
else:
b = temp + 1
print (b)
"""
b=0
return render_template('main.html', qno = int(qnnumber[b]), qn = qnlist[b] ,op1 = optn1[b] , op2 = optn2[b], op3 = optn3[b], op4 = optn4[b], op5 = optn5[b], trait = trait[b])
@app.route('/next',methods = ['POST'])
def next():
if request.method == 'POST':
responceqno = request.form['qno'] #Question Number
responceqno = int(responceqno)
ans = float(request.form['q']) #option selected/Value for question
ans = float(ans)
qtrait = request.form['trait'] #Trait for Question
else:
responceqno=0
ans=0
print (responceqno)
print (ans)
with open("responce.csv", "a") as recordbook:
writer = csv.writer(recordbook)
writer.writerow([responceqno,ans])
if(responceqno == 40 ): #40 is the number of questions
return redirect(url_for('result'))
else:
b=responceqno
return render_template('main.html', qno = int(qnnumber[b]), qn = qnlist[b] ,op1 = optn1[b] , op2 = optn2[b], op3 = optn3[b], op4 = optn4[b], op5 = optn5[b], trait = trait[b])
#, trait = trait[b])
ar=oro=er=nr=cr=e=a=n=c=o=E=N=A=O=C=0
@app.route('/result')
def result():
data =pd.read_csv('train dataset.csv')
array = data.values
for i in range(len(array)):
if(array[i][0]=="Male"):
array[i][0]=1
else:
array[i][0]=0
df=pd.DataFrame(array)
maindf =df[[0,1,2,3,4,5]]
mainarray=maindf.values
temp=df[6]
train_y =temp.values
#print(train_y)
train_y=temp.values
for i in range(len(train_y)):
train_y[i] =str(train_y[i])
mul_lr =DecisionTreeClassifier()
mul_lr.fit(mainarray, train_y)
e=n=c=o=a=0
File = open('responce.csv')
Reader=csv.reader(File)
Data=list(Reader)
#print(Data)
print(len(Data))
Data1= [x for x in Data if x != []]
for i in range (8):
e=e+float(Data1[i][1]) #first list ka first element and so on
er=round(e)
E=(e/8)*100
print(er)
for i in range (8,16):
a=a+float(Data1[i][1]) #first list ka first element and so on
ar=round(a)
A=(a/8)*100
print(ar)
for i in range (16,24):
c=c+float(Data1[i][1]) #first list ka first element and so on
C=(c/8)*100
cr=round(c)
print(cr)
for i in range (24,32):
o=o+float(Data1[i][1]) #first list ka first element and so on
O=(o/8)*100
oro=round(o)
print(oro)
for i in range (32,40):
n=n+float(Data1[i][1]) #first list ka first element and so on
N=(n/8)*100
nr=round(n)
print(nr)
File.close()
y_pred = mul_lr.predict([[0,oro,nr,cr,ar,er]])
print(y_pred)
f=open('responce.csv',"w")
f.truncate()
data =pd.read_csv('train dataset.csv')
array = data.values
for i in range(len(array)):
if (array[i][0]=="Male"):
array[i][0]=1
else:
array[i][0]=0
df=pd.DataFrame(array)
maindf =df[[0,1,2,3,4,5]]
mainarray=maindf.values
temp=df[6]
train_y =temp.values
#print(train_y)
train_y=temp.values
for i in range(len(train_y)):
train_y[i] =str(train_y[i])
mul_lr =DecisionTreeClassifier()
mul_lr.fit(mainarray, train_y)
y_pred = mul_lr.predict([[G,oro,nr,cr,ar,er]])
line_chart = pygal.Bar(width=500, height=400, explicit_size=True, style=custom_style)
line_chart.x_labels = map(str,['extraversion','agreeableness','conscientiousness','openness','neuroticism'] )
line_chart.add('Extraversion', [{'value': E,'style': ' stroke_width: 20', 'label': 'Your Extraversion level is'}])
line_chart.add('Agreeableness', [{'value': A,'style': ' stroke_width: 20', 'label': 'Your Agreeableness level is'}])
line_chart.add('Conscientiousness', [{'value': C,'style': ' stroke_width: 20', 'label': 'Your Conscientiouness level is'}])
line_chart.add('Openness', [{'value':O,'style': ' stroke_width: 20', 'label': 'Your Openness level is'}])
line_chart.add('Neuroticism', [{'value':N,'style': ' stroke_width: 20', 'label': 'Your Neuroticism level is'}])
line_chart = line_chart.render_data_uri()
# x-coordinates of left sides of bars
left = [1,2,3,4,5]
# heights of bars
height= [E,A,C,O,N]
#print ('Height is :')
#print (height)
# labels for bars
tick_label = ['extraversion','agreeableness','conscientiousness','openness','neuroticism']
# plotting a bar chart
plt.bar(left, height, tick_label = tick_label,width = 20.0, color = ['red', 'green','yellow', 'blue','brown'])
# naming the x-axis
plt.xlabel('x - axis')
# naming the y-axis
plt.ylabel('y - axis')
# plot title
plt.title('Personality Chart!')
# function to show the plot
#plt.show()
s = pd.Series([er,ar,cr,oro,nr])
fig, ax = plt.subplots()
s.plot.bar()
fig.savefig('my_plot.png')
def fig_to_base64(fig):
img = io.BytesIO()
fig.savefig(img, format='png',
x_inches='tight')
img.seek(0)
return base64.b64encode(img.getvalue())
encoded = fig_to_base64(fig)
return render_template('predict.html', var=y_pred[0],A=ar,O=oro,N=nr,C=cr,E=er,Ge=G, graph_data = line_chart )
#training data and prediction algorithm
if __name__ == '__main__':
app.run(debug = True)