File size: 1,320 Bytes
3fe5bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import requests
from traceback import print_exc
from dotenv import load_dotenv

load_dotenv()

api_endpoint_url = os.environ.get("API_ENDPOINT_URL")
api_endpoint_basic_auth_user = os.environ.get("API_ENDPOINT_BASIC_AUTH_USER")
api_endpoint_basic_auth_password = os.environ.get("API_ENDPOINT_BASIC_AUTH_PASSWORD")
api_endpoint_enabled = os.environ.get("API_ENDPOINT_ENABLED", default=True) == "True"

def invoke_translate_endpoint(text, model):
    try:
        response = requests.request("POST", 
                                    f"{api_endpoint_url}/api/v1/translate",
                                    headers={
                                        'Content-Type': 'application/json', 
                                    },
                                    auth=(api_endpoint_basic_auth_user, api_endpoint_basic_auth_password),
                                    data=json.dumps({
                                        "src": model.get('src'),
                                        "tgt": model.get('tgt') ,
                                        "text": text
                                        })
                                    )
        response.raise_for_status()

        return response.json()['translation']

    except:
        print_exc()
        return None