This repository was archived by the owner on May 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (55 loc) · 1.65 KB
/
main.py
File metadata and controls
68 lines (55 loc) · 1.65 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
68
import logging
from flask import Flask, request, redirect
from flask_cors import CORS
try:
try:
from . import predict
except ImportError:
import predict
except Exception as e:
logging.critical(f"Could not import nn_predict: {e}")
def main():
"""
The main API function.
Receives all calls to http://api.hateflow.de.
:return:
"""
app = Flask(__name__) # initialize a new Flask app
CORS(app) # make it accessible from all domains
@app.route("/")
def hf_home():
"""
Redirect to hateflow.de.
Calling the root page does not correspond to any API action.
"""
return redirect("https://hateflow.de", 307)
@app.route("/<requested_action>")
def hateflow_api(requested_action):
"""
Map API actions with Python functions.
"""
res = {
'results': dict(),
'errors': [],
'warnings': [],
}
api_actions = {
'simpleCheck': predict.predict,
}
try:
results, errors, warnings, status = api_actions[requested_action](request.args)
res['results'].update(results)
res['errors'].append(errors)
res['warnings'].append(warnings)
except KeyError:
res['errors'].append(f"unknown API action: '{requested_action}'")
status = 404
except Exception as e:
res['errors'].append(f"exception while calling API action '{requested_action}': {e}")
status = 500
return res, status
return app
# run the Flask app
app = main()
if __name__ == "__main__":
app.run()