-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
85 lines (64 loc) · 2.52 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
import os
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
from werkzeug.utils import secure_filename
import spacy
from spacy_langdetect import LanguageDetector
from components.analysis.german_analysis import analyze_german
from components.analysis.english_analysis import analyze_english
# Basic local variables for file transfer
SECRET_KEY = os.urandom(24)
LOCAL_PATH = os.path.abspath(os.path.dirname(__file__))
ALLOWED_EXTENSION = {'txt'}
# Setup flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = SECRET_KEY
cors = CORS(app)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSION
@app.route('/api/textRawUpload', methods=['POST'])
@cross_origin()
def text_raw_upload():
# noinspection PyGlobalUndefined
global results
data = request.get_json()
# Setup nlp for language detection
nlp = spacy.load("de_core_news_sm")
nlp.add_pipe(LanguageDetector(), name="language_detector", last=True)
doc = nlp(data['content'])
if doc._.language['language'] == 'de':
results = analyze_german(data['content'])
elif doc._.language['language'] == 'en':
results = analyze_english(data['content'])
nlp = None
return jsonify(results)
# noinspection PyGlobalUndefined
@app.route('/api/textFileUpload', methods=['GET', 'POST'])
@cross_origin()
def text_file_upload():
global results
# Setup nlp for language detection
nlp = spacy.load("de_core_news_sm")
nlp.add_pipe(LanguageDetector(), name="language_detector", last=True)
if request.method == 'POST':
if 'file' not in request.files:
return "No file in request"
file = request.files['file']
if file.filename == '':
return "No file in request"
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(LOCAL_PATH, "uploads", filename))
f = open(os.path.join(LOCAL_PATH, "uploads", filename), "r", encoding="utf-8")
file_content = f.read()
doc = nlp(file_content)
if doc._.language['language'] == 'de':
results = analyze_german(file_content)
elif doc._.language['language'] == 'en':
results = analyze_english(file_content)
f.close()
os.remove(os.path.join(LOCAL_PATH, "uploads", filename))
nlp = None
return jsonify(results)
if __name__ == '__main__':
app.run(port=5000, threaded=True)