File size: 1,320 Bytes
5139c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import uvicorn
from utils import check_score
from pydantic import BaseModel
from fastapi import FastAPI, status
from TextSimilarity import TextSimilarity
from starlette.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware


class RequestBody(BaseModel):
    text_1: str
    text_2: str


app = FastAPI(docs_url=None, redoc_url=None)
text_similarity = TextSimilarity()

origins = ['*']

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],

)


@app.post("/penilaian")
async def penilaian(data_request: RequestBody):
    if not data_request.text_2:
        return JSONResponse({
            "probability": 0,
            "score": 0
        }, status_code=status.HTTP_200_OK)
    try:
        probability = text_similarity.predict(
            data_request.text_1, data_request.text_2)
        return_value = check_score(float(probability))
        return JSONResponse(
            return_value, status_code=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        return JSONResponse({
            "errors": "Please contact your administrator"
        }, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)


if __name__ == "__main__":
    uvicorn.run(app, host="localhost", port=53640)