-
Notifications
You must be signed in to change notification settings - Fork 74
/
main.py
321 lines (247 loc) · 9.56 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from typing import List, Optional, Dict, Any
import logging
import os
import json
import glob
from fastapi import FastAPI, HTTPException, Header, Response, Body
from fastapi.responses import FileResponse
from fastapi.encoders import jsonable_encoder
from app.metadata import PaperStatus, Allocation
from app.annotations import Annotation, RelationGroup, PdfAnnotation
from app.utils import StackdriverJsonFormatter
from app import pre_serve
IN_PRODUCTION = os.getenv("IN_PRODUCTION", "dev")
CONFIGURATION_FILE = os.getenv(
"PAWLS_CONFIGURATION_FILE", "/usr/local/src/skiff/app/api/config/configuration.json"
)
handlers = None
if IN_PRODUCTION == "prod":
json_handler = logging.StreamHandler()
json_handler.setFormatter(StackdriverJsonFormatter())
handlers = [json_handler]
logging.basicConfig(
level=os.environ.get("LOG_LEVEL", default=logging.INFO), handlers=handlers
)
logger = logging.getLogger("uvicorn")
# boto3 logging is _super_ verbose.
logging.getLogger("boto3").setLevel(logging.CRITICAL)
logging.getLogger("botocore").setLevel(logging.CRITICAL)
logging.getLogger("nose").setLevel(logging.CRITICAL)
logging.getLogger("s3transfer").setLevel(logging.CRITICAL)
# The annotation app requires a bit of set up.
configuration = pre_serve.load_configuration(CONFIGURATION_FILE)
app = FastAPI()
def get_user_from_header(user_email: Optional[str]) -> Optional[str]:
"""
Call this function with the X-Auth-Request-Email header value. This must
include an "@" in its value.
* In production, this is provided by Skiff after the user authenticates.
* In development, it is provided in the NGINX proxy configuration file local.conf.
If the value isn't well formed, or the user isn't allowed, an exception is
thrown.
"""
if "@" not in user_email:
raise HTTPException(403, "Forbidden")
if not user_is_allowed(user_email):
raise HTTPException(403, "Forbidden")
return user_email
def user_is_allowed(user_email: str) -> bool:
"""
Return True if the user_email is in the users file, False otherwise.
"""
try:
with open(configuration.users_file) as file:
for line in file:
entry = line.strip()
if user_email == entry:
return True
# entries like "@allenai.org" mean anyone in that domain @allenai.org is granted access
if entry.startswith("@") and user_email.endswith(entry):
return True
except FileNotFoundError:
logger.warning("file not found: %s", configuration.users_file)
pass
return False
def all_pdf_shas() -> List[str]:
pdfs = glob.glob(f"{configuration.output_directory}/*/*.pdf")
return [p.split("/")[-2] for p in pdfs]
def update_status_json(status_path: str, sha: str, data: Dict[str, Any]):
with open(status_path, "r+") as st:
status_json = json.load(st)
status_json[sha] = {**status_json[sha], **data}
st.seek(0)
json.dump(status_json, st)
st.truncate()
@app.get("/", status_code=204)
def read_root():
"""
Skiff's sonar, and the Kubernetes health check, require
that the server returns a 2XX response from it's
root URL, so it can tell the service is ready for requests.
"""
return Response(status_code=204)
@app.get("/api/doc/{sha}/pdf")
async def get_pdf(sha: str):
"""
Fetches a PDF.
sha: str
The sha of the pdf to return.
"""
pdf = os.path.join(configuration.output_directory, sha, f"{sha}.pdf")
pdf_exists = os.path.exists(pdf)
if not pdf_exists:
raise HTTPException(status_code=404, detail=f"pdf {sha} not found.")
return FileResponse(pdf, media_type="application/pdf")
@app.get("/api/doc/{sha}/title")
async def get_pdf_title(sha: str) -> Optional[str]:
"""
Fetches a PDF's title.
sha: str
The sha of the pdf title to return.
"""
pdf_info = os.path.join(configuration.output_directory, "pdf_metadata.json")
with open(pdf_info, "r") as f:
info = json.load(f)
data = info.get("sha", None)
if data is None:
return None
return data.get("title", None)
@app.post("/api/doc/{sha}/comments")
def set_pdf_comments(
sha: str, comments: str = Body(...), x_auth_request_email: str = Header(None)
):
user = get_user_from_header(x_auth_request_email)
status_path = os.path.join(configuration.output_directory, "status", f"{user}.json")
exists = os.path.exists(status_path)
if not exists:
# Not an allocated user. Do nothing.
return {}
update_status_json(status_path, sha, {"comments": comments})
return {}
@app.post("/api/doc/{sha}/junk")
def set_pdf_junk(
sha: str, junk: bool = Body(...), x_auth_request_email: str = Header(None)
):
user = get_user_from_header(x_auth_request_email)
status_path = os.path.join(configuration.output_directory, "status", f"{user}.json")
exists = os.path.exists(status_path)
if not exists:
# Not an allocated user. Do nothing.
return {}
update_status_json(status_path, sha, {"junk": junk})
return {}
@app.post("/api/doc/{sha}/finished")
def set_pdf_finished(
sha: str, finished: bool = Body(...), x_auth_request_email: str = Header(None)
):
user = get_user_from_header(x_auth_request_email)
status_path = os.path.join(configuration.output_directory, "status", f"{user}.json")
exists = os.path.exists(status_path)
if not exists:
# Not an allocated user. Do nothing.
return {}
update_status_json(status_path, sha, {"finished": finished})
return {}
@app.get("/api/doc/{sha}/annotations")
def get_annotations(
sha: str, x_auth_request_email: str = Header(None)
) -> PdfAnnotation:
user = get_user_from_header(x_auth_request_email)
annotations = os.path.join(
configuration.output_directory, sha, f"{user}_annotations.json"
)
exists = os.path.exists(annotations)
if exists:
with open(annotations) as f:
blob = json.load(f)
return blob
else:
return {"annotations": [], "relations": []}
@app.post("/api/doc/{sha}/annotations")
def save_annotations(
sha: str,
annotations: List[Annotation],
relations: List[RelationGroup],
x_auth_request_email: str = Header(None),
):
"""
sha: str
PDF sha to save annotations for.
annotations: List[Annotation]
A json blob of the annotations to save.
relations: List[RelationGroup]
A json blob of the relations between the annotations to save.
x_auth_request_email: str
This is a header sent with the requests which specifies the user login.
For local development, this will be None, because the authentication
is controlled by the Skiff Kubernetes cluster.
"""
# Update the annotations in the annotation json file.
user = get_user_from_header(x_auth_request_email)
annotations_path = os.path.join(
configuration.output_directory, sha, f"{user}_annotations.json"
)
json_annotations = [jsonable_encoder(a) for a in annotations]
json_relations = [jsonable_encoder(r) for r in relations]
# Update the annotation counts in the status file.
status_path = os.path.join(configuration.output_directory, "status", f"{user}.json")
exists = os.path.exists(status_path)
if not exists:
# Not an allocated user. Do nothing.
return {}
with open(annotations_path, "w+") as f:
json.dump({"annotations": json_annotations, "relations": json_relations}, f)
update_status_json(
status_path, sha, {"annotations": len(annotations), "relations": len(relations)}
)
return {}
@app.get("/api/doc/{sha}/tokens")
def get_tokens(sha: str):
"""
sha: str
PDF sha to retrieve tokens for.
"""
pdf_tokens = os.path.join(configuration.output_directory, sha, "pdf_structure.json")
if not os.path.exists(pdf_tokens):
raise HTTPException(status_code=404, detail="No tokens for pdf.")
with open(pdf_tokens, "r") as f:
response = json.load(f)
return response
@app.get("/api/annotation/labels")
def get_labels() -> List[Dict[str, str]]:
"""
Get the labels used for annotation for this app.
"""
return configuration.labels
@app.get("/api/annotation/relations")
def get_relations() -> List[Dict[str, str]]:
"""
Get the relations used for annotation for this app.
"""
return configuration.relations
@app.get("/api/annotation/allocation/info")
def get_allocation_info(x_auth_request_email: str = Header(None)) -> Allocation:
# In development, the app isn't passed the x_auth_request_email header,
# meaning this would always fail. Instead, to smooth local development,
# we always return all pdfs, essentially short-circuiting the allocation
# mechanism.
user = get_user_from_header(x_auth_request_email)
status_dir = os.path.join(configuration.output_directory, "status")
status_path = os.path.join(status_dir, f"{user}.json")
exists = os.path.exists(status_path)
if not exists:
# If the user doesn't have allocated papers, they can see all the
# pdfs but they can't save anything.
papers = [PaperStatus.empty(sha, sha) for sha in all_pdf_shas()]
response = Allocation(
papers=papers,
hasAllocatedPapers=False
)
else:
with open(status_path) as f:
status_json = json.load(f)
papers = []
for sha, status in status_json.items():
papers.append(PaperStatus(**status))
response = Allocation(papers=papers, hasAllocatedPapers=True)
return response