-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
268 lines (220 loc) · 7.47 KB
/
server.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
#!/usr/bin/env python3.6
"""
COMS W4995 Data Analytics Pipelines
FakeTastic webserver
To run locally
python server.py
Go to http://localhost:8111 in your browser
"""
import os
from sqlalchemy import *
from sqlalchemy.pool import NullPool
from flask import Flask, request, render_template, g, redirect, Response
import numpy as np
import random
from model import get_model
import credentials
import json
import json as json
tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
app = Flask(__name__, template_folder=tmpl_dir)
app.config['TEMPLATES_AUTO_RELOAD'] = True
# XXX: The Database URI should be in the format of:
#
# postgresql://USER:PASSWORD@<IP_OF_POSTGRE_SQL_SERVER>/<DB_NAME>
#
# For example, if you had username ewu2493, password foobar, then the following line would be:
#
# DATABASEURI = "postgresql://ewu2493:foobar@<IP_OF_POSTGRE_SQL_SERVER>/postgres"
#
# For your convenience, we already set it to the class database
# Use the DB credentials you received by e-mail
# DB_USER = "YOUR_DB_"
# DB_PASSWORD = "YOUR_DB_PASSWORD_HERE"
DB_USER = credentials.username()
DB_PASSWORD = credentials.password()
# DB_SERVER = "w4111.cisxo09blonu.us-east-1.rds.amazonaws.com"
DB_SERVER = "mydbinstance.cmgiyjltohsu.us-east-1.rds.amazonaws.com"
# DATABASEURI = "postgresql://"+DB_USER+":"+DB_PASSWORD+"@"+DB_SERVER+"/w4111"
DATABASEURI = "postgresql://"+DB_USER+":"+DB_PASSWORD+"@"+DB_SERVER+"/FakeTasticdb"
#
# This line creates a database engine that knows how to connect to the URI above
#
engine = create_engine(DATABASEURI)
# Here we create a test table and insert some values in it
engine.execute("""DROP TABLE IF EXISTS test;""")
engine.execute("""CREATE TABLE IF NOT EXISTS test (
id serial,
name text
);""")
engine.execute("""INSERT INTO test(name) VALUES ('grace hopper'), ('alan turing'), ('ada lovelace');""")
model = get_model()
@app.before_request
def before_request():
"""
This function is run at the beginning of every web request
(every time you enter an address in the web browser).
We use it to setup a database connection that can be used throughout the request
The variable g is globally accessible
"""
try:
g.conn = engine.connect()
except:
print("uh oh, problem connecting to database")
import traceback; traceback.print_exc()
g.conn = None
@app.teardown_request
def teardown_request(exception):
"""
At the end of the web request, this makes sure to close the database connection.
If you don't the database could run out of memory!
"""
try:
g.conn.close()
except Exception as e:
pass
#@app.route('/')
#def index():
# """
# request is a special object that Flask provides to access web request information:
#
# request.method: "GET" or "POST"
# request.form: if the browser submitted a form, this contains the data in the form
# request.args: dictionary of URL arguments e.g., {a:1, b:2} for http://localhost?a=1&b=2
#
# See its API: http://flask.pocoo.org/docs/0.10/api/#incoming-request-data
# """
#
# # DEBUG: this is debugging code to see what request looks like
# print(request.args)
#
#
# #
# # example of a database query
# #
# cursor = g.conn.execute("SELECT name FROM test")
# names = []
# for result in cursor:
# names.append(result['name']) # can also be accessed using result[0]
# cursor.close()
#
# #
# # Flask uses Jinja templates, which is an extension to HTML where you can
# # pass data to a template and dynamically generate HTML based on the data
# # (you can think of it as simple PHP)
# # documentation: https://realpython.com/blog/python/primer-on-jinja-templating/
# #
# # You can see an example template in templates/index.html
# #
# # context are the variables that are passed to the template.
# # for example, "data" key in the context variable defined below will be
# # accessible as a variable in index.html:
# #
# # # will print: [u'grace hopper', u'alan turing', u'ada lovelace']
# # <div>{{data}}</div>
# #
# # # creates a <div> tag for each element in data
# # # will print:
# # #
# # # <div>grace hopper</div>
# # # <div>alan turing</div>
# # # <div>ada lovelace</div>
# # #
# # {% for n in data %}
# # <div>{{n}}</div>
# # {% endfor %}
# #
# context = dict(data = names)
#
#
# #
# # render_template looks in the templates/ folder for files.
# # for example, the below file reads template/index.html
# #
# return render_template("index.html", **context)
#
# This is an example of a different path. You can see it at
#
# localhost:8111/another
#
# notice that the functio name is another() rather than index()
# the functions for each app.route needs to have different names
#
@app.route('/')
def landing():
# Get list of all hashtags
# from DB
cmd = '''
SELECT *
FROM hashtags
'''
hashtag_cursor = g.conn.execute(text(cmd))
hashtags = []
for result in hashtag_cursor:
hashtags.append(result[1])
return render_template('home.html', hashtags=hashtags)
# Example of adding new data to the database
@app.route('/add', methods=['POST'])
def add():
name = request.form['name']
print(name)
cmd = 'INSERT INTO test(name) VALUES (:name1), (:name2)';
g.conn.execute(text(cmd), name1 = name, name2 = name);
return redirect('/')
@app.route('/response', methods=['POST'])
def response_create():
data = json.loads(request.data)
print(data)
cmd = 'INSERT INTO response(tweet_id, is_fake) VALUES '
for answer in data:
cmd += f"({answer['tweetId']}, {answer['answer']}),"
cmd = cmd[:-1]
print(cmd)
g.conn.execute(text(cmd))
return '{ "response": "ok" }'
@app.route('/login')
def login():
abort(401)
this_is_never_executed()
@app.route('/tweets',methods=['GET','POST'])
def tweets():
topic = request.form['topic_id']
offset = np.random.randint(0, 1000)
# cmd = "(Select tweet_id,tweet_text,is_fake from Tweets where tweet_hashtag = :topic LIMIT 10) "
cmd1 = "(Select tweet_id,tweet_text,is_fake from tweets_detailed where tweet_hashtag = :topic and is_fake=False and length(tweet_text)>100 LIMIT 10 OFFSET :offset) "
cmd2 = "UNION (Select tweet_id,tweet_text,is_fake from tweets_detailed where tweet_hashtag = :topic and is_fake=True and length(tweet_text)>100 LIMIT 10 OFFSET :offset)"
cmd = cmd1+cmd2
# print(cmd)
alltweets = g.conn.execute(text(cmd),topic=str(topic),offset=offset)
data = [dict(tweet_id=result[0], is_fake=result[2],
tweet_text=result[1]) for result in alltweets]
conf = model.predict_proba([datum['tweet_text'] for datum in data])
for i, datum in enumerate(data):
if conf[i][1] > .6:
datum['conf'] = conf[i][1]
else:
datum['conf'] = conf[i][0]
random.shuffle(data)
data = data[:10]
data_string = json.dumps(data)
context = dict(data=data, data_string=data_string, topic=topic)
return render_template('tweets.html', **context)
if __name__ == "__main__":
import click
@click.command()
@click.option('--debug', is_flag=True)
@click.option('--threaded', is_flag=True)
@click.argument('HOST', default='0.0.0.0')
@click.argument('PORT', default=8111, type=int)
def run(debug, threaded, host, port):
"""
This function handles command line parameters.
Run the server using
python server.py
Show the help text using
python server.py --help
"""
HOST, PORT = host, port
print("running on %s:%d" % (HOST, PORT))
app.run(host=HOST, port=PORT, debug=debug, threaded=threaded)
run()