-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
308 lines (263 loc) · 12.3 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from flask import Flask, render_template, request, jsonify
## keep this commented and run ngrok from a separate terminal it generates multiple errors running it inside the code
#from flask_ngrok import run_with_ngrok
import time
import subprocess
import os
import webbrowser
import random
from flask_cors import CORS
#from question_generator_superAPI import get_response, get_Quiz
from question_generator_chatGPT import get_response, get_Quiz
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Float
from sqlalchemy.sql import func
import datetime
#global string_quiz do it inside each Flask function
# create always a new connection with ngrok using an endpoint in separate file. This file will be read by app.js
subprocess.call("curl -s localhost:4040/api/tunnels | jq -r '.tunnels[0].public_url' > static/endpoint.txt",shell=True)
subprocess.call("chmod 777 static/endpoint.txt",shell=True)
subprocess.call("chown jmm:jmm static/endpoint.txt",shell=True)
fire_path="/usr/bin/firefox"
webbrowser.register('firefox', None, webbrowser.BackgroundBrowser(fire_path), preferred=0)
browser= webbrowser.get('firefox')
string_quiz = []
string_prev = []
count_questions = 0
correct_count=0
correct_value=0
prev_questions = []
username = []
password = []
correct_ans=[]
time_start=0
time_end=0
time_values=[]
prev_text=' '
number_questions=random.randint(3,15)
app = Flask(__name__)
##database definition using SQLAlchemy
basedir = os.path.abspath(os.path.dirname(__file__))
print(basedir,'basedir')
app.config['SQLALCHEMY_DATABASE_URI'] ='sqlite:///' + os.path.join(basedir, 'database.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
## define the table of the database
class gpt_data(db.Model):
id = db.Column(db.Integer,primary_key=True)
username = db.Column(db.String(100), nullable=True)
password = db.Column(db.String(100), nullable=False)
correct_questions = db.Column(db.Integer, nullable=False)
time_replied = db.Column(db.String(100), nullable=False)
num_questions = db.Column(db.Integer, nullable=False)
correctness = db.Column(Float,nullable=False)
correct_answers = db.Column(db.PickleType, nullable=True)
time_taken = db.Column(db.PickleType, nullable=True)
questions = db.Column(db.PickleType, nullable=True)
def __hash__(self):
return hash(self.name)
def __repr__(self):
return f'<GPT {self.firstname}>'
app.secret_key = 'secret_key'
CORS(app)
#run_with_ngrok(app)
## define local variables in Flask
@app.get("/")
def index_get():
return render_template("base.html")
#@app.route('/')
#def home():
# return render_template("base.html")
@app.post("/adduser")
def adduser():
ack='none'
global username
global password
userpass = request.get_json()
print(userpass)
username=userpass.get("user")
password=userpass.get("pass")
if len(username)==0 or len(password)==0:
return(jsonify('incomplete'))
else:
return jsonify(ack)
@app.post("/ini")
def ini():
global string_quiz
global string_prev
global count_questions
global correct_count
global number_questions
global prev_questions
global correct_ans
global correct_value
global time_values
global prev_text
prev_text=' '
correct_value=0
correct_ans=[]
prev_questions = []
time_values=[]
number_questions=random.randint(3,15)
#text = request.get_json().get("message")
#string_quiz=get_Quiz()
# TODO: check if text is valid
count_questions = 0
correct_count = 0
##test the connection asking for a first question but don't attach it to main questions set
string_quiz=get_Quiz(0,prev_questions)
while string_quiz=="Error":
string_quiz=get_Quiz(0,prev_questions)
while (len(string_quiz)<2):
string_quiz=get_Quiz(0,prev_questions)
while (len(string_quiz[1])<2):
string_quiz=get_Quiz(0,prev_questions)
## analyze the type of data
str_temp=string_quiz[1].split(':')
while (len(str_temp)<2):
string_quiz=get_Quiz(correctness,prev_questions)
str_temp=string_quiz[1].split(':')
string_prev=string_quiz[1]
#prev_questions.append(string_quiz[0])
len_quiz=number_questions
message = {"answer": f"The quiz is ready! Want to start the {len_quiz} questions? reply <b>yes</b> or <b>no</b>."}
print(message,'message')
return jsonify(message)
@app.post("/predict")
def predict():
## measure the time spend by the request
global time_end
time_end = time.time()
global time_values
global time_start
global string_quiz
global string_prev
global count_questions
global correct_count
global number_questions
global prev_questions
global username
global password
global correct_ans
global correct_value
global prev_text
## initialize correctness in 0
correctness=correct_value
ids=[]
prev_questions_temp=[]
text = request.get_json().get("message")
#string_quiz=get_Quiz()
# TODO: check if text is valid
if not(text[0:3].lower() == 'yes') and not(text[0].lower() == 'y') and not(text[0:2].lower() == 'ok') and not(text[0:2].lower() == 'ye') and not(text[0:4].lower() == 'yeah') and len(text)<=4 and not(text[0:2].lower() == 'no') and not(text[0].lower() == 'n') and (text[0].lower() == 'a' or text[0].lower() == 'b' or text[0].lower() == 'c' or text[0].lower() == 'd' or text[0].lower() == 'e') and len(text)<=2:
count_questions=count_questions+1
print(time_end-time_start,'time_diff')
time_values.append(time_end-time_start)
if (text[0:3].lower() == 'yes' or text[0].lower() == 'y' or text[0:2].lower() == 'ok' or text[0:2].lower() == 'ye' or text[0:4].lower() == 'yeah') and count_questions>=number_questions:
if len(username)>0 and len(password)>0:
## check for ids
for value in db.session.query(gpt_data.id).distinct():
ids.append(value[0])
##database update
id_data=random.randint(0,5000)
while id_data in ids:
id_data=random.randint(0,5000)
time_now = datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
## update the content of previous questions
for i_data in range(0,len(prev_questions)):
prev_questions_temp.append(prev_questions[i_data].replace('\n',' '))
GPT = gpt_data(id=id_data,username=username,password=password,correct_questions=correct_count,time_replied=time_now,num_questions=number_questions,correctness=(correct_count/number_questions)*100,time_taken=time_values,correct_answers=correct_ans,questions=prev_questions_temp)
db.session.add(GPT)
db.session.commit()
number_questions=random.randint(3,15)
#prev_questions = []
count_questions = 0
correct_count = 0
time_start=0
time_end=0
string_prev=string_quiz[1]
ids = []
len_quiz=number_questions
message = {"answer": f"Quiz is ready! Want to start the {len_quiz} questions? reply <b>yes</b> or <b>no</b>!"}
print(message,'message')
return jsonify(message)
else:
message = {"answer": "Username and Password are <b>empty</b>! can't add it into the database"}
print(message,'message')
return jsonify(message)
##evaluate correctness before to process more difficult questions before
if text[0].lower() == string_prev[2].lower() or (text.lower() in string_prev.lower() and text.lower() == string_prev.lower()): ## evaluate correctness of the question before calling the request
correctness=1
else:
if len(text)<=2 and (text[0].lower() == 'a' or text[0].lower() == 'b' or text[0].lower() == 'c' or text[0].lower() == 'd' or text[0].lower() == 'e') or text[0:2].lower() == 'no' or text[0].lower() == 'n':
correctness=0
elif (not(prev_text[0:3].lower() == 'yes') and not(prev_text[0].lower() == 'y') and not(prev_text[0:2].lower() == 'ye') and not(prev_text[0:4].lower() == 'yeah')) and (text[0:3].lower() == 'yes' or text[0].lower() == 'y' or text[0:2].lower() == 'ok' or text[0:2].lower() == 'ye' or text[0:4].lower() == 'yeah'):
correct_value=correctness
if (prev_text[0:3].lower() == 'yes' or prev_text[0].lower() == 'y' or prev_text[0:2].lower() == 'ye' or prev_text[0:4].lower() == 'yeah') and (text[0:3].lower() == 'yes' or text[0].lower() == 'y' or text[0:2].lower() == 'ok' or text[0:2].lower() == 'ye' or text[0:4].lower() == 'yeah'):
message = {"answer": f"Please select a valid option!! You have repeated <b>yes</b>! \n"}
print(message,'message')
return jsonify(message)
else:
correctness=0
message = {"answer": f"Please select a valid option!! Input <b>{text}</b> is not accepted! \n"}
print(message,'message')
return jsonify(message)
correct_value=correctness
if not(text[0:2].lower() == 'no') and not(text[0].lower() == 'n') and not(text[0:3].lower() == 'end'):
## validation of the request
string_quiz=get_Quiz(correctness,prev_questions)
while string_quiz=="Error":
string_quiz=get_Quiz(correctness,prev_questions)
while (len(string_quiz)<2):
string_quiz=get_Quiz(correctness,prev_questions)
while (len(string_quiz[1])<2):
string_quiz=get_Quiz(correctness,prev_questions)
##analyze the type of data
str_temp=string_quiz[1].split(':')
while (len(str_temp)<2):
string_quiz=get_Quiz(correctness,prev_questions)
str_temp=string_quiz[1].split(':')
if text[0:2].lower() == 'no' or text[0].lower() == 'n' or text[0:3].lower() == 'end' :
prev_questions = []
count_questions = 0
correct_count = 0
correct_ans = []
time_values = []
if count_questions < number_questions and not(text[0:2].lower() == 'no') and not(text[0].lower() == 'n') and not(text[0:3].lower() == 'end'):
#print('question',string_quiz[0])
prev_questions.append(string_quiz[0])
## analyze only the prev question response
string_prev.replace('\n','')
st_prev=string_prev.split(':')
if not('yes' in text) and not('y' in text) and not('yeah' in text) and not(text[0:2].lower() == 'ok') and not(text[0:3].lower() == 'yes') and not(text[0:2].lower() == 'ye') and not(text[0:4].lower() == 'yeah') and len(text)<=4 and not(text[0:2].lower() == 'no') and not(text[0].lower() == 'n') and not(text[0:3].lower() == 'end'):
if '\n' in st_prev[1]:
temp_prev=st_prev[1].split('\n')
if len(temp_prev[0])>0 and not(temp_prev[0]=='\n') and not(temp_prev[0]==' '):
st_prev[1]=temp_prev[0]
else:
st_prev[1]=temp_prev[1]
correct_ans.append(str(correctness)+'=> reply: '+text+' correct answer: '+st_prev[1])
response, correct_count = get_response(text,string_quiz,count_questions,correct_count,number_questions,string_prev)
string_prev = string_quiz[1]
print(string_prev,'message','count',count_questions)
##add the number of questions answered correctly
if count_questions > 0 and not(text[0:3].lower() == 'yes') and not(text[0].lower() == 'y') and not(text[0:4].lower() == 'yeah') and not(text[0:2].lower() == 'no') and not(text[0].lower() == 'n') and not(text[0:3].lower() == 'end'):
response = response + f"\n You have answered {correct_count}/{number_questions} questions correctly!"
response = response.replace('\n', '<br/>') ## subtitute \n by <br\> for the html reading in the chat widget
message = {"answer": response}
print(message,text,'message')
prev_text=text
#if (len(correct_ans)<number_questions): #and not(text.lower() == 'yes') and not(text.lower() == 'y') and not(text.lower() == 'no') and not(text.lower() == 'n'):
time_start = time.time()
return jsonify(message)
if __name__ == "__main__":
## uncomment this only if you want to use a second window in your web browser in this case firefox
#if cf_port is None:
# app.run(host='127.0.0.1', port=5000, debug=True)
#else:
# app.run(host='127.0.0.1', port=int(cf_port), debug=True)
#string_quiz=get_Quiz()
#url = 'http://127.0.0.1:5000'
#browser.open(url)
#url = 'http://127.0.0.1:5000'
#browser.open_new(url)
app.run(port=5000,debug=True) #use_reloader=False)