-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
64 lines (48 loc) · 2.09 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
from typing import List, Optional
from fastapi import FastAPI, Body, HTTPException, status
import uvicorn
from pydantic import BaseModel
from fastapi_camelcase import CamelModel
from source.spell_checker import SpellChecker
app = FastAPI()
spell_checker_handler = None
class Sentence(BaseModel):
sentences: List[str]
class SpellCheckerInnerResponse(CamelModel):
# sentence: str
correction: str
class SpellCheckerResponse(CamelModel):
response: Optional[List[SpellCheckerInnerResponse]] = None
@app.post("/spell-checker/", response_model=SpellCheckerResponse)
async def spell_checker(sentences: Sentence):
response = []
try:
for sentence in sentences.sentences:
corrected_sentence = spell_checker_handler.prediction(sentence=sentence.split(" "), k=100,
levenshtein_ratio_threshold=0.5)
response.append({
# "sentence": sentence,
"correction": " ".join(corrected_sentence)
})
except Exception as e:
raise HTTPException(status_code=status.HTTP_406_NOT_ACCEPTABLE,
detail="Any Image not uploaded")
return {
"response": response
}
# return \
# {
# "response":
# [
# {
# "sentence": "তাূরা দেখেন ঢাকার দূই সিটি করপোরেশনে মশা মারতে যে ওষধূ ছিটানো হয় তা অকাযংকর",
# "correction": "তারা দেখেন ঢাকার দুই সিটি করপোরেশনে মশা মারতে যে ওষুধ ছিটানো হয় তা অকাযংকর"
# }
# ]
# }
@app.on_event("startup")
async def startup_event():
global spell_checker_handler
spell_checker_handler = SpellChecker()
if __name__ == '__main__':
uvicorn.run(app='app:app', reload=True, port=7003, host="127.0.0.1")