forked from daniel-dona/morph-kgc-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
66 lines (51 loc) · 1.51 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
from flask import Flask, render_template, request, send_file
from flask_cors import CORS
import morph_kgc
import zipfile
import traceback
app = Flask(__name__)
CORS(app)
app.config['UPLOAD_FOLDER'] = "/data/"
pathFiles = "/data/"
pathZipFile = pathFiles + "csv.zip"
pathRmlMapping = pathFiles + "mapping.ttl"
outputPathFile = "/output/result.ttl"
outputPathZippedFile = "/output/result.zip"
def run_morph_kgc():
config = '''
[CONFIGURATION]
# INPUT
na_values=,#N/A,N/A,#N/A N/A,n/a,NA,,#NA,NULL,null,NaN,nan
[KNOWLEDGEGRAPH]
source_type: csv
mappings:/data/mapping.ttl
'''
graph = morph_kgc.materialize(config)
graph.serialize(destination=outputPathFile, format='turtle')
def compress_result():
zipObj = zipfile.ZipFile(outputPathZippedFile, 'w')
zipObj.write(outputPathFile, "result.ttl")
zipObj.close()
@app.route('/', methods = ['POST',"GET"])
def upload_file():
try:
if request.method == 'POST':
mapping = request.form['mapping']
print(mapping)
f = open(pathRmlMapping, 'w')
f.write(mapping)
f.close()
csv = request.files['csv']
csv.save(pathZipFile)
with zipfile.ZipFile(pathZipFile, 'r') as zip_ref:
zip_ref.extractall(pathFiles)
run_morph_kgc()
compress_result()
return send_file(outputPathZippedFile, mimetype='application/x-zip')
elif request.method == "GET":
return 'Hello World!'
except Exception as e:
print(traceback.format_exc())
return "Error..."
if __name__ == '__main__':
app.run(host="0.0.0.0", port="5000",debug= True)