|
from fastapi.security import HTTPBearer |
|
from fastapi import FastAPI, HTTPException, Depends |
|
from functions import retrieve_similar_sentence |
|
|
|
app = FastAPI() |
|
security = HTTPBearer() |
|
|
|
@app.post("/translate") |
|
def translate_sentence(data: dict, token: str = Depends(security)): |
|
try: |
|
api_key = token.credentials |
|
sentence = data["sentence"] |
|
source_language = data["source_language"] |
|
if not sentence or not source_language: |
|
raise HTTPException(status_code=400, detail="Missing data in the request body") |
|
|
|
results = retrieve_similar_sentence(sentence, source_language, api_key) |
|
return { |
|
"top_match": { |
|
"source_sentence": results[0]["source_sentence"], |
|
"target_sentence": results[0]["target_sentence"] |
|
}, |
|
"2nd_match": { |
|
"source_sentence": results[1]["source_sentence"], |
|
"target_sentence": results[1]["target_sentence"] |
|
}, |
|
"3rd_match": { |
|
"source_sentence": results[2]["source_sentence"], |
|
"target_sentence": results[2]["target_sentence"] |
|
}, |
|
"4th_match": { |
|
"source_sentence": results[3]["source_sentence"], |
|
"target_sentence": results[3]["target_sentence"] |
|
} |
|
} |
|
|
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|