-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapplication.py
More file actions
executable file
·226 lines (190 loc) · 9.44 KB
/
application.py
File metadata and controls
executable file
·226 lines (190 loc) · 9.44 KB
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
# coding=utf-8
from __future__ import unicode_literals
from flask import Flask, render_template, request, redirect, url_for
import urllib
#import unirest #In an attempt to update the library, unirest requires Python 2.7
#which is getting updated to Python 3 which means we can now use requests instead
import difflib
from goose3 import Goose
import io
import os
import sys, csv
import tensorflow as tf
import requests
import json
import re
#import importlib #Used to reload modules
#importlib.reload(sys)
#sys.setdefaultencoding('utf8')
csv.field_size_limit(sys.maxsize)
import defend
import nltk
#nltk.download('punkt')
application = Flask(__name__)
SAVED_MODEL_DIR = './static/saved_models'
EMBEDDINGS_PATH = 'saved_models/glove.6B.100d.txt'
platform = 'politifact'
SAVED_MODEL_FILENAME = platform + '_Defend_model.h5'
h = defend.Defend(platform)
h.load_weights(saved_model_dir=SAVED_MODEL_DIR, saved_model_filename=SAVED_MODEL_FILENAME)
h.activation_maps('abc', ['1', '2', '3'])
graph = tf.get_default_graph()
newsdic = {}
file_title = './static/' + platform + '_title_no_ignore.tsv'
with open(file_title) as tsvfile:
reader = csv.reader(tsvfile, delimiter=str(u'\t'))
for row in reader:
id = row[0]
newsdic[id] = {}
newsdic[row[0]]['title'] = row[1]
file_comment_our = './static/' + platform + '_comment_no_ignore.tsv'
with open(file_comment_our) as tsvfile:
reader = csv.reader(tsvfile, delimiter=str(u'\t'))
for row in reader:
if row[0] in newsdic.keys():
newsdic[row[0]]['comment_our'] = row[1].split("::")
file_content = './static/' + platform + '_content_no_ignore.tsv'
with open(file_content) as tsvfile:
reader = csv.reader(tsvfile, delimiter=str(u'\t'))
for row in reader:
if row[0] in newsdic.keys():
newsdic[row[0]]['content'] = row[2]
newsdic[row[0]]['label'] = row[1]
def url2str(_title):
newtitle = _title
if _title.startswith('twitter.com') or _title.startswith('https://twitter.com'):
url = json.loads(requests.get("https://publish.twitter.com/oembed?url=" + _title).content)
url = url['html']
#remove special characters that will cause it to crash
remove_emojis = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
"]+", flags=re.UNICODE)
url = remove_emojis.sub(r'', url)
url = url.replace('*', ' ')
newtitle = re.sub(r"<([^>]*)>", "", url)
return newtitle.encode('ascii', 'ignore')[:50]
def check(_title):
newtitle = url2str(_title)
url = newtitle
url = urllib.parse.quote_plus(url)
response = requests.get(
"https://api-hoaxy.p.rapidapi.com/articles?sort_by=relevant&use_lucene_syntax=true&query=" + url,
headers={
"X-RapidAPI-Key": "API-KEY"
}
) #TODO: Replace API-KEY with a variable with file content with an api key
a = response.json()
articles = a['articles']
canonical_url = a['articles'][0]['canonical_url']
title = a['articles'][0]['title']
score = 0
for a in articles:
tmpscore = difflib.SequenceMatcher(None, a['title'], newtitle).quick_ratio()
if tmpscore > score:
canonical_url = a['canonical_url']
title = a['title']
id = a['id']
score = tmpscore
g = Goose()
article = g.extract(url=canonical_url)
article = article.cleaned_text.split("\n")
article = list(filter(None, article))
label = 0
found = 0
for key in newsdic.keys():
if difflib.SequenceMatcher(None, newsdic[key]['title'], title).quick_ratio() > 0.8 or difflib.SequenceMatcher(None, newsdic[key]['title'], _title).quick_ratio() > 0.8:
if difflib.SequenceMatcher(None, newsdic[key]['title'], _title).quick_ratio() > 0.8:
title = _title
# comment_our = newsdic[key]['comment_our']
# for i in range(len(comment_our)):
# c_val.append(comment_our[i].split('\t')[0])
# if i < 5:
# comment += "<td>User" + str(i + 1) + "</td><td>" + comment_our[i].split('\t')[
# 0] + "</td><td class=\"text-nowrap\">" + str(
# round(float(comment_our[i].split('\t')[1].replace('\n', '')), 4)) + "</td></tr>"
comment = ''
sentence = ''
article = "<p><strong>Source: </strong><a href=\"" + canonical_url + "\">" + canonical_url + "</a></p>" + '<p>' + newsdic[key]['content'] + '</p>'
if newsdic[key]['label'] == "1":
label = 1
x_val = [nltk.tokenize.sent_tokenize(newsdic[key]['content'])]
c_val = [newsdic[key]['comment_our']]
found = 1
break
if found == 0:
x_val = [article]
c_val = [' ']
article = "<p><strong>Source: </strong><a href=\"" + canonical_url + "\">" + canonical_url + "</a></p>" + "<br>".join(
[a for a in article if a != ""])
comment = "<td></td><td>The comments required to run the algorithm are not enough!</td><td class=\"text-nowrap\"></td></tr>"
sentence = ''
global graph
with graph.as_default():
global h
res_comment_weight, res_sentence_weight = h.activation_maps(x_val, c_val)
sorted_comment_weight = sorted(res_comment_weight[0], key=lambda tup: tup[1], reverse=True)
for i in range(len(sorted_comment_weight)):
if i < 5:
comment += "<td>User" + str(i + 1) + "</td><td>" + " ".join(sorted_comment_weight[i][
0]) + "</td><td class=\"text-nowrap\">" + str(
round(sorted_comment_weight[i][1], 6)) + "</td></tr>"
for pos, v in enumerate(x_val[0]):
if pos < len(res_sentence_weight[0]):
sentence += "<tr><td name=\"td2\">" + str(pos + 1) + "</td><td name=\"td0\">" + v + "</td>\
<td class=\"text-right\" name=\"td1\">\
<span class=\"badge badge-default\">" + str(
round(res_sentence_weight[0][pos][1], 8)) + "</span>\
</td>\
</tr>"
confidence = round(sum([a[1] for a in res_sentence_weight[0]]) * 100, 1)
if confidence < 50:
confidence = 100 - confidence
elif confidence > 100:
confidence = 95
if label == 1:
if confidence > 90:
image = "<img src=\"/static/demo/brand/f.jpg\" class=\"header-brand-img\" alt=\"tabler logo\">"
elif confidence > 70:
image = "<img src=\"/static/demo/brand/mf.jpg\" class=\"header-brand-img\" alt=\"tabler logo\">"
else:
image = "<img src=\"/static/demo/brand/ht.jpg\" class=\"header-brand-img\" alt=\"tabler logo\">"
label = "<div class=\"card-alert alert alert-danger mb-0\">\
Disputed by dEFEND with " + str(confidence) + "% confidence \
<a href=\"#\" onClick=\"alert(\'We have received your feedback. Thank you!\')\" class=\"btn outline-info\"><i class=\"fe fe-flag\"></i> Report detection error</a>\
</div>"
else:
if confidence > 90:
image = "<img src=\"/static/demo/brand/t.jpg\" class=\"header-brand-img\" alt=\"tabler logo\">"
elif confidence > 70:
image = "<img src=\"/static/demo/brand/mt.jpg\" class=\"header-brand-img\" alt=\"tabler logo\">"
else:
image = "<img src=\"/static/demo/brand/ht.jpg\" class=\"header-brand-img\" alt=\"tabler logo\">"
label = "<div class=\"card-alert alert alert-success mb-0\">\
Verified by dEFEND with " + str(confidence) + "% confidence \
<a href=\"#\" onClick=\"alert(\'We have received your feedback. Thank you!\')\" class=\"btn outline-info\"><i class=\"fe fe-flag\"></i> Report detection error</a>\
</div>"
return url, image + article, title, comment, sentence, label
title = "Tom Price: “It’s Better For Our Budget If Cancer Patients Die More Quickly”"
url, article, title, comment, sentence, label = check(title)
@application.route('/')
def index():
return render_template('index.html', url=url, newscontent=article, title=title, comment=comment, sentence=sentence, label=label)
@application.route('/<name>', methods=['GET'])
def link(name):
url, article, title, comment, sentence, label = check(name)
return render_template('index.html', url=url, newscontent=article, title=title, comment=comment, sentence=sentence, label=label)
@application.route('/demo', methods=['POST'])
def demo():
newtitle = request.form['url']
newtitle = url2str(newtitle)
return redirect(url_for('link', name=newtitle, _anchor='exactlocation'))
# url, article, title, comment, sentence, label = check(newtitle)
# return render_template('index.html', url=url, newscontent=article, title=title, comment=comment, sentence=sentence, label=label)
@application.route('/index')
def foobar():
return render_template('./docs/index.html')
if __name__ == '__main__':
application.run(debug=False, threaded=False, host='0.0.0.0', port=80)