-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_template.py
More file actions
111 lines (74 loc) · 2.95 KB
/
app_template.py
File metadata and controls
111 lines (74 loc) · 2.95 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
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
import os
import sys
import time
from pathlib import Path
from typing import Union, Optional, List
import uvicorn
from fastapi import FastAPI, Request
from pydantic import BaseModel
from config import CONFIG as pipeline_config
from t_res.geoparser import pipeline
geoparser = pipeline.Pipeline(**pipeline_config)
class APIQuery(BaseModel):
text: str
place: Optional[Union[str, None]] = None
place_wqid: Optional[Union[str, None]] = None
class CandidatesAPIQuery(BaseModel):
toponyms: List[dict]
class DisambiguationAPIQuery(BaseModel):
dataset: List[dict]
wk_cands: dict
place: Optional[Union[str, None]] = None
place_wqid: Optional[Union[str, None]] = None
app_config_name = os.environ["APP_CONFIG_NAME"]
app = FastAPI(title=f"Toponym Resolution Pipeline API ({app_config_name})")
@app.get("/")
async def read_root(request: Request):
return {"Welcome to T-Res!": request.app.title}
@app.get("/test")
async def test_pipeline():
resolved = geoparser.run_sentence_deprecated(
"Harvey, from London;Thomas and Elizabeth, Barnett.",
place="Manchester",
place_wqid="Q18125",
)
return resolved
@app.get("/resolve_sentence")
async def run_sentence(api_query: APIQuery, request_id: Union[str, None] = None):
place = "" if api_query.place is None else api_query.place
place_wqid = "" if api_query.place_wqid is None else api_query.place_wqid
resolved = geoparser.run_sentence_deprecated(
api_query.text, place=place, place_wqid=place_wqid
)
return resolved
@app.get("/resolve_full_text")
async def run_text(api_query: APIQuery):
place = "" if api_query.place is None else api_query.place
place_wqid = "" if api_query.place_wqid is None else api_query.place_wqid
resolved = geoparser.run_text_deprecated(api_query.text, place=place, place_wqid=place_wqid)
return resolved
@app.get("/run_ner")
async def run_ner(api_query: APIQuery):
place = "" if api_query.place is None else api_query.place
place_wqid = "" if api_query.place_wqid is None else api_query.place_wqid
ner_output = geoparser.run_text_recognition_deprecated(
api_query.text, place=place, place_wqid=place_wqid
)
return ner_output
@app.get("/run_candidate_selection")
async def run_candidate_selection(cand_api_query: CandidatesAPIQuery):
wk_cands = geoparser.run_candidate_selection_deprecated(cand_api_query.toponyms)
return wk_cands
@app.get("/run_disambiguation")
async def run_disambiguation(api_query: DisambiguationAPIQuery):
place = "" if api_query.place is None else api_query.place
place_wqid = "" if api_query.place_wqid is None else api_query.place_wqid
disamb_output = geoparser.run_disambiguation_deprecated(
api_query.dataset, api_query.wk_cands, place, place_wqid
)
return disamb_output
@app.get("/health")
async def healthcheck():
return {"status": "ok"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)