-
Notifications
You must be signed in to change notification settings - Fork 13
/
api.py
91 lines (71 loc) · 2.21 KB
/
api.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
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
import functools
import uvicorn
from starlette.middleware.cors import CORSMiddleware
from summa_score_sentences import summarize as summarize_textrank
from summa_score_sentences_xling import summarize_xling
from summa_score_sentences_laser import summarize as summarize_laser
class HighlightRequest(BaseModel):
text: str
model: str = "textrank"
class Sentence(BaseModel):
paragraph: int
index: int
text: str
score: float
class HighlightResults(BaseModel):
success: bool
message: str = ""
sentences: List[Sentence] = []
app = FastAPI()
origins = [
# "http:localhost",
# "http:localhost:8080",
# "http:localhost:3000",
"*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def read_root():
return {"Hello": "World"}
def sentence_sort_function(sent_1, sent_2) -> bool:
if sent_1.paragraph == sent_2.paragraph:
return sent_1.index - sent_2.index
return sent_1.paragraph - sent_2.paragraph
@app.post("/highlight/", response_model=HighlightResults)
def read_item(highlight_request: HighlightRequest):
if highlight_request.model == "textrank":
sentences, _, _ = summarize_textrank(highlight_request.text)
elif highlight_request.model == "use-xling":
sentences, _, _ = summarize_xling(highlight_request.text)
elif highlight_request.model == "laser":
sentences, _, _ = summarize_laser(highlight_request.text)
else:
return HighlightResults(
success=False,
message=f"'{highlight_request.model}' is not supported."
)
# Sort sentences
sentences = sorted(
sentences, key=functools.cmp_to_key(sentence_sort_function))
# Create sentence obj
sentence_objs = [
Sentence(paragraph=x.paragraph, score=x.score,
text=x.text, index=x.index)
for x in sentences
]
# print(sentence_objs)
return HighlightResults(
success=True,
sentences=sentence_objs
)
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8000)