/
runserver.py
365 lines (301 loc) · 11.2 KB
/
runserver.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from flask import Flask, request, jsonify, make_response
from newspaper import Article, Config
from TheGadflyProject.gadfly import gap_fill_generator as gfg
from TheGadflyProject.gadfly import mcq_generator as mcq
from flask.ext.cors import CORS, cross_origin
from flask.ext.sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from urllib.parse import urlparse
from hashlib import md5
from random import shuffle
from bs4 import BeautifulSoup
import requests
import re
import os
import pprint
app = Flask(__name__)
app.config['DEBUG'] = True
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
# Newspaper Config
config = Config()
config.fetch_images = False
# Postgres Config
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
db = SQLAlchemy(app)
ma = Marshmallow(app)
from models import QuestionGenRequest, NewsArticle, Question, QuestionSchema, AnswerChoice
question_schema = QuestionSchema(many=True)
pp = pprint.PrettyPrinter(indent=4)
# use this method to get questions
@app.route('/api/gap_fill_questions', methods=['GET'])
@cross_origin()
def get_gap_fill_questions():
url = request.args.get('url')
limit = int(request.args.get('limit') or 50)
article_text = get_article_text(url)
questions = generate_gap_fill_questions(article_text)
num_questions = len(questions)
for key in ["_type", "_subtype"]:
for q in questions:
q.pop(key)
try:
db.session.add(QuestionGenRequest(url=url, questions=questions,
question_type="gap_fill"))
db.session.commit()
except Exception as e:
print(e)
try:
parsed_url = urlparse(url)
article_id = md5(article_text.strip().encode('utf-8'))
if not NewsArticle.query.get(article_id.hexdigest()):
db.session.add(
NewsArticle(id=article_id.hexdigest(), url=url,
article_text=article_text.strip(),
domain=parsed_url.netloc)
)
db.session.commit()
except Exception as e:
print(e)
for q in questions:
q_id = md5(q.get('question').encode('utf-8'))
if not Question.query.get(q_id.hexdigest()):
try:
db.session.add(
Question(id=q_id.hexdigest(),
question_text=q.get("question"),
source_sentence=q.get("source_sentence"),
correct_answer=q.get("answer"),
good_question_votes=0,
bad_question_votes=0,
news_article_id=article_id.hexdigest())
)
db.session.commit()
except Exception as e:
print("Unable to add item to database.")
print(e)
news_article = NewsArticle.query.get(article_id.hexdigest())
questions = question_schema.dump(news_article.questions.all()).data
shuffle(questions)
return jsonify({
'num_questions': min(limit, num_questions),
'questions': questions[:limit]
})
# use this method to get questions
@app.route('/api/multiple_choice_questions', methods=['GET'])
@cross_origin()
def get_multiple_choice_questions():
url = request.args.get('url')
limit = int(request.args.get('limit') or 50)
article_text = get_article_text(url)
questions = generate_multiple_choice_questions(article_text)
num_questions = len(questions)
for key in ["_type", "_subtype"]:
for q in questions:
q.pop(key)
try:
db.session.add(QuestionGenRequest(
url=url,
questions=questions,
question_type="multiple_choice",
))
db.session.commit()
except Exception as e:
print(e)
try:
parsed_url = urlparse(url)
article_id = md5(article_text.strip().encode('utf-8'))
if not NewsArticle.query.get(article_id.hexdigest()):
db.session.add(
NewsArticle(
id=article_id.hexdigest(),
url=url,
article_text=article_text.strip(),
domain=parsed_url.netloc
))
db.session.commit()
except Exception as e:
print(e)
for q in questions:
q_id = md5(q.get('question').encode('utf-8'))
if not Question.query.get(q_id.hexdigest()):
try:
db.session.add(
Question(
id=q_id.hexdigest(),
question_text=q.get("question"),
source_sentence=q.get("source_sentence"),
correct_answer=q.get("answer"),
# reactions=[],
good_question_votes=0,
bad_question_votes=0,
news_article_id=article_id.hexdigest()
)
)
db.session.commit()
for answer_choice in q.get("answer_choices"):
ac = AnswerChoice(
question_id=q_id.hexdigest(),
answer=answer_choice
)
db.session.add(ac)
db.session.commit()
except Exception as e:
print("Unable to add item to database.")
print(e)
# news_article = NewsArticle.query.get(article_id.hexdigest())
# questions = question_schema.dump(news_article.questions.all()).data
shuffle(questions)
return jsonify({
'num_questions': min(limit, num_questions),
'questions': questions[:limit]
})
@app.route('/api/question/<q_id>', methods=['GET'])
@cross_origin()
def question(q_id):
question = {}
try:
question = Question.query.get(q_id)
question = question_schema.dump([question]).data
except Exception as e:
print(e)
print("unable to find {} in database".format(q_id))
return jsonify({"question": question})
@app.route('/api/good_question/<q_id>', methods=['POST'])
@cross_origin()
def good_question(q_id):
try:
question = Question.query.get(q_id)
question.good_question_votes += 1
db.session.commit()
return jsonify({'status': "success"})
except Exception as e:
print(e)
print("unable to find {} in database".format(q_id))
return jsonify({"status": "failed"})
@app.route('/api/bad_question/<q_id>', methods=['POST'])
@cross_origin()
def bad_question(q_id):
try:
question = Question.query.get(q_id)
question.bad_question_votes += 1
db.session.commit()
return jsonify({'status': "success"})
except Exception as e:
print(e)
print("unable to find {} in database".format(q_id))
return jsonify({"status": "failed"})
@app.route('/api/article', methods=['GET'])
@cross_origin()
def get_article():
url = request.args.get('url')
article_text = get_article_text(url)
return (article_text)
@app.route('/api/raw_article', methods=['GET'])
@cross_origin()
def get_raw_article():
url = request.args.get('url')
up = urlparse(url)
if (up.netloc == "www.nytimes.com") or (up.netloc == "mobile.nytimes.com"):
article = []
r = requests.get(url)
soup = BeautifulSoup(r.text, "lxml")
known_text = False
for each in soup.findAll("p", class_="story-body-text story-content"):
full_string = []
for string in each.stripped_strings:
full_string.append(string)
article.append(" ".join(full_string))
known_text = True
title = soup.title.string
if known_text:
article = " ".join(article)
return (article)
article = Article(url, config)
article.download()
article.parse()
return article.text
@app.route('/api/sentences', methods=['GET'])
@cross_origin()
def get_sentences():
url = request.args.get('url')
article_text = get_article_text(url)
g = mcq.MCQGenerator(article_text)
sents = [sent.text for sent in g.sents]
top_sent_idx = []
top_sents = []
for top_sent in [sent.text for sent in g.top_sents]:
for i, sent in enumerate(g.sents):
if sent.text == top_sent:
top_sent_idx.append(i)
top_sents.append(top_sent)
return jsonify({"sents": sents, "top_sent_idx": top_sent_idx, "top_sents": top_sents})
@app.route('/api/entities', methods=['GET'])
@cross_origin()
def get_entities():
url = request.args.get('url')
article_text = get_article_text(url)
g = mcq.MCQGenerator(article_text)
ents = [(ent.text, ent.label_) for ent in g.parsed_text.ents]
ent_dict = {}
for ent in ents:
if ent[1] in ent_dict:
ent_dict[ent[1]].append(ent[0])
else:
ent_dict[ent[1]] = [ent[0]]
return jsonify({"entities": ent_dict})
@app.route('/api/article_info', methods=['GET'])
@cross_origin()
def get_article_info():
url = request.args.get('url')
article = Article(url, config)
article.download()
article.parse()
return jsonify({"article_info": {"title": article.title }})
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
def generate_gap_fill_questions(article_text):
q_gen = gfg.GapFillGenerator(article_text)
return q_gen.output_questions()
def generate_multiple_choice_questions(article_text):
q_gen = mcq.MCQGenerator(article_text)
return q_gen.output_questions()
def get_article_text(url):
up = urlparse(url)
if (up.netloc == "www.nytimes.com") or (up.netloc == "mobile.nytimes.com"):
article = []
r = requests.get(url)
soup = BeautifulSoup(r.text, "lxml")
known_text = False
for each in soup.findAll("p", class_="story-body-text story-content"):
full_string = []
for string in each.stripped_strings:
full_string.append(string)
article.append(" ".join(full_string))
known_text = True
title = soup.title.string
if known_text:
article = " ".join(article)
return clean_text(article)
article = Article(url, config)
article.download()
article.parse()
article = article.text
return clean_text(article)
def clean_text(article):
article = re.sub("“", '"', article)
article = re.sub("”", '"', article)
article = re.sub("’", "'", article)
article = re.sub("Advertisement ", "", article)
article = re.sub("Continue reading the main story", "", article)
# Should replace "Photo RIO DE JANEIRO — BRAZIL" with "Photo - BRAZIL"
article = re.sub("^(([A-Za-z,]|\s)+\—)+?", "", article)
# Should remove "Reporting by David Chance ; Editing by Peter Cooney"
article = re.sub("(Reporting|Editing)\sby(\s\w+)+", "", article)
article = re.sub("Photo\s", "", article)
article = re.sub("Video\s", "", article)
return re.sub("[\s*]", " ", article)
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=8081)