-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
executable file
·118 lines (99 loc) · 3.44 KB
/
main.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
#!/usr/bin/python
import os
import zipfile, io, pathlib
from flask import Flask, flash, request, redirect, url_for, render_template, send_from_directory, send_file
from werkzeug.utils import secure_filename
HOSTNAME = '0.0.0.0'
PORT = '8080'
UPLOAD_FOLDER = './ATF_INPUT/'
OUTPUT_FOLDER='./ATF_OUTPUT/'
ALLOWED_EXTENSIONS = {'atf','txt'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = os.urandom(24)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def do_translation(path):
ATF="True"
if (path.split(".")[-1]=='txt'):
ATF="False"
os.system(f'python3 pipeline.py -i {path} -a {ATF}')
return "This is a translation."
@app.route('/translate/<filename>')
def translate(filename):
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
#with open(path) as fp:
#file_contents = fp.read()
try:
translation = do_translation(path)
except Exception as error:
app.logger.error("File format is not supported file", error)
return redirect('/downloadfiles')
@app.route("/", methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file in post data')
return redirect( request.url )
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No file selected')
return redirect( request.url )
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('translate',
filename=filename))
else:
flash('Filetype not allowed')
return redirect( request.url )
return render_template("upload.html")
# Download API
@app.route("/downloadfiles", methods = ['GET'])
def download_file():
return render_template('download.html')
@app.route('/returnfiles', methods = ['GET'])
def return_files():
base_path = pathlib.Path('./ATF_OUTPUT')
data = io.BytesIO()
with zipfile.ZipFile(data, mode='w') as z:
for f_name in base_path.iterdir():
z.write(f_name)
data.seek(0)
return send_file(
data,
mimetype='application/zip',
as_attachment=True,
attachment_filename='results.zip',
cache_timeout=0
)
@app.route('/returnfilesconll', methods = ['GET'])
def return_files_conll():
base_path = pathlib.Path('./ATF_OUTPUT/output_conll')
data = io.BytesIO()
try:
with zipfile.ZipFile(data, mode='w') as z:
for f_name in base_path.iterdir():
z.write(f_name)
data.seek(0)
except Exception as error:
app.logger.error("conll-u is genrated only for atf files")
return render_template('download.html')
return send_file(
data,
mimetype='application/zip',
as_attachment=True,
attachment_filename='conll-u_results.zip',
cache_timeout=0
)
# file_path = OUTPUT_FOLDER + filename
# return send_file(file_path, as_attachment=True)
if __name__ == '__main__':
app.run(
debug=True,
host=HOSTNAME,
port=PORT,
)