Skip to content

Commit

Permalink
enable API mode
Browse files Browse the repository at this point in the history
  • Loading branch information
izuna385 committed Jul 25, 2021
1 parent c616d8d commit 4228df8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 51 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,20 @@ $ python -m spacy download ja_core_news_md

## Run as API
```
$ docker build -t jel:latest .
$ docker run -d -itd -p 8000:8000 jel
$ uvicorn jel.api.server:app --reload --port 8000 --host 0.0.0.0 --log-level trace
```

### Example
```
# link
$ curl localhost:8000/api/v1/link -X POST -H "Content-Type: application/json" \
$ curl localhost:8000/link -X POST -H "Content-Type: application/json" \
-d '{"sentence": "日本の総理は菅総理だ。"}'

# question
$ curl localhost:8000/api/v1/question -X POST -H "Content-Type: application/json" \
$ curl localhost:8000/question -X POST -H "Content-Type: application/json" \
-d '{"sentence": "日本で有名な総理は?"}
```


## Test
`$ python pytest`

Expand Down
60 changes: 54 additions & 6 deletions jel/api/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@
import logging
from jel.api.v1 import predictor
from fastapi import FastAPI

FORMAT = "%(message)s"
logging.basicConfig(
level="NOTSET", format=FORMAT, datefmt="[%X]"
)
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
import uvicorn
from fastapi import BackgroundTasks, FastAPI
from fastapi import HTTPException


def app() -> FastAPI:
app = FastAPI()
app.state.cache = {}
app.include_router(predictor.router)
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


from jel.mention_predictor import EntityLinker

class Sentence(BaseModel):
sentence: str

logger = logging.getLogger(__file__)
logger.info("loading linker...")
el = EntityLinker()
logger.info("loading finished!")

@app.post("/link")
async def link(params: Sentence):
if params.sentence is None:
raise HTTPException(status_code=400, detail="Sentence is required.")

try:
result = el.link(sentence=params.sentence)
except Exception:
raise HTTPException(status_code=400, detail="fail to link")

return {"result": result}

@app.post("/question")
async def question(params: Sentence):
if params.sentence is None:
raise HTTPException(status_code=400, detail="Sentence is required.")

try:
result = el.question(sentence=params.sentence)
except Exception:
raise HTTPException(status_code=400, detail="fail to link")

return {"result": result}


return app
if __name__ == '__main__':
uvicorn.run("app:app", host='0.0.0.0', port=8000,
log_level="debug", debug=True)
Empty file removed jel/api/v1/__init__.py
Empty file.
40 changes: 0 additions & 40 deletions jel/api/v1/predictor.py

This file was deleted.

1 change: 1 addition & 0 deletions jel/mention_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self):
self.mention_predictor, _ = predictors_loader()
print('Loading kb. This might take few minutes.')
self.kb = TitleIndexerWithFaiss()
print('Loading kb finished!')
self.prior_dict = self._mention2cand_entity_dict_loader()
self.candidate_ent_max_num = 10

Expand Down

0 comments on commit 4228df8

Please sign in to comment.