-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractiveServer_flask.py
More file actions
67 lines (54 loc) · 1.99 KB
/
extractiveServer_flask.py
File metadata and controls
67 lines (54 loc) · 1.99 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
#pip install newspaper3k
#pip install bert-extractive-summarizer
#pip install transformers==2.2.0
#conda install pytorch -c pytorch
#conda install flask
#
# Date Created: 1 March 2020
#
# To run:
# python extractiveServer_flask.py
#
# Flask server app to process the request from Streamlit webapp.
#
# /url - to get a summary of the news article when a URL is being passed
# /update - update the summarizer machine with the indexes
#
import os
import pandas as pd
import summaryEngine
from flask import Flask, render_template, request, jsonify
app = Flask(__name__, template_folder='.')
# to process the request and then return the summary
@app.route('/url', methods=['GET', 'POST'])
def url_request():
url_req = request.json
print('\n-----------')
print('The URL is => ', url_req['search_url'])
fileSaved = summaryEngine.getSummary(url_req['search_url'])
print('File saved: ', fileSaved)
print('-----------\n')
return jsonify(filename=fileSaved)
# to update the indexes of the summary
@app.route('/update', methods=['GET', 'POST'])
def update_request():
url_req = request.json
print('\n-----------')
print('File to replace: ', url_req['filename'])
print('Indexes updated by user: ', url_req['indexes'])
# read the 'before' file content
df = pd.read_csv(url_req['filename'])
# convert the indexes to be change to a string of comma separated int
df.iloc[0][0] = ','.join(str(i) for i in url_req['indexes'])
# rename the filename by adding a '_changed' followed by '.csv' extension
updated_filename = os.path.splitext(url_req['filename'])[0] + '_changed.csv'
print('Updated filename: ', updated_filename)
# save the dataframe as csv file
df.to_csv(updated_filename, index = False, header=True, encoding="utf-8")
print('-----------\n')
return jsonify(success=True)
@app.route('/')
def index():
return('<h1>Hello "extractiveServer_flask.py"</h1>')
if __name__ == '__main__':
app.run(debug=True, port=5500)