File size: 1,206 Bytes
e9fa8d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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))