Spaces:
Running
Running
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 | |