This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
123 lines (90 loc) · 3.54 KB
/
app.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
119
120
121
122
123
'''
Useful references
https://programminghistorian.org/en/lessons/creating-apis-with-python-and-flask
https://auth0.com/blog/sqlalchemy-orm-tutorial-for-python-developers/
step1: working
step2: working
'''
#step1
import flask
from flask import request, jsonify
import similarities as sim
from bs4 import BeautifulSoup
#step2
from flask_sqlalchemy import SQLAlchemy
from dbswissre import Swissre
from base import Session, engine, Base
#step3
import keyvault
app = flask.Flask(__name__)
app.config["DEBUG"] = True
#step2: instantiate the connection with the DB - and create the table if not existing
#this function needs to be outside __name__ otherwise it would work when calling "python app.py" but not "flask run"
Base.metadata.create_all(engine)
@app.route('/', methods=['GET'])
def home():
url_root = request.url_root
link1 = url_root + "dole/flush"
link2 = url_root + "dole/all"
link3 = url_root + "dole?strings=ludovic is a big man, ludovic is a man, ludovic is a great man, sarah is his sister, patrizi is his mother, sarah is beautiful"
welcome = """ <h1>Welcome Ludovic Bis</h1>
<a href="{}">/dole/flush</a></br>
<a href="{}">/dole/all </a></br>
<a href="{}">/dole?strings=ludovic is a big man, ludovic is a man, ludovic is a great man, sarah is his sister, patrizi is his mother, sarah is beautiful</a>
""".format(link1, link2, link3)
return welcome
@app.route('/dole/flush', methods=['GET'])
def dole_flush():
session = Session()
session.query(Swissre).delete()
session.commit()
session.close()
return "Swissre table has been flushed"
@app.route('/dole/all', methods=['GET'])
def dole_all():
session = Session()
swissre = session.query(Swissre).all()
d = {}
for i in swissre:
reference = i.reference
sentence = i.sentence
ratio = i.ratio
t = (sentence, ratio)
if reference in d:
if t not in d[reference]:
d[reference].append(t)
else:
d[reference] = [t]
session.close()
return jsonify(d)
#http://127.0.0.1:5000/dole?strings=ludovic is a big man, ludovic is a man, ludovic is a great man, sarah is his sister, patrizi is his mother, sarah is beautiful
@app.route('/dole', methods=['GET'])
def dole():
if 'strings' in request.args:
arg_strings = request.args['strings']
if bool(BeautifulSoup(arg_strings, "html.parser").find()):#check for html tags
return 'Error: Please avoid html tags in strings'
else:
strings = [i.strip() for i in arg_strings.split(',')]
#compare strings
res = sim.sim(strings, 0.7) #list of strings and similarity ratio
#step2: populate the DB
session = Session()
for reference, l in res.items():
if len(l) == 0:
db_row = Swissre(reference, "", -1)
session.add(db_row)
else:
for item in l:
db_row = Swissre(reference, item[0], item[1])
session.add(db_row)
session.commit()
session.close()
else:
return 'Error: Please provide strings, i.e. /api/v1/dole?strings=string1, string2, string3'
return jsonify(res)
@app.route('/key', methods=['GET'])
def key():
return keyvault.retrieved_secret.value
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000)