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)