Spaces:
Runtime error
Runtime error
Add API endpoint
Browse files- api_endpoint.py +34 -0
- app.py +14 -5
api_endpoint.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import requests
|
4 |
+
from traceback import print_exc
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
api_endpoint_url = os.environ.get("API_ENDPOINT_URL")
|
10 |
+
api_endpoint_basic_auth_user = os.environ.get("API_ENDPOINT_BASIC_AUTH_USER")
|
11 |
+
api_endpoint_basic_auth_password = os.environ.get("API_ENDPOINT_BASIC_AUTH_PASSWORD")
|
12 |
+
api_endpoint_enabled = os.environ.get("API_ENDPOINT_ENABLED", default=True) == "True"
|
13 |
+
|
14 |
+
def invoke_translate_endpoint(text, model):
|
15 |
+
try:
|
16 |
+
response = requests.request("POST",
|
17 |
+
f"{api_endpoint_url}/api/v1/translate",
|
18 |
+
headers={
|
19 |
+
'Content-Type': 'application/json',
|
20 |
+
},
|
21 |
+
auth=(api_endpoint_basic_auth_user, api_endpoint_basic_auth_password),
|
22 |
+
data=json.dumps({
|
23 |
+
"src": model.get('src'),
|
24 |
+
"tgt": model.get('tgt') ,
|
25 |
+
"text": text
|
26 |
+
})
|
27 |
+
)
|
28 |
+
response.raise_for_status()
|
29 |
+
|
30 |
+
return response.json()['translation']
|
31 |
+
|
32 |
+
except:
|
33 |
+
print_exc()
|
34 |
+
return None
|
app.py
CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
|
|
4 |
from AinaTheme import theme
|
5 |
from huggingface_hub import snapshot_download
|
6 |
import nltk
|
|
|
7 |
|
8 |
from translate import translate_text
|
9 |
|
@@ -13,7 +14,9 @@ load_dotenv()
|
|
13 |
|
14 |
MODELS_PATH = "./models"
|
15 |
HF_CACHE_DIR = "./hf_cache"
|
16 |
-
MAX_INPUT_CHARACTERS= int(os.environ.get("MAX_INPUT_CHARACTERS", default=1000))
|
|
|
|
|
17 |
|
18 |
def download_model(repo_id, revision="main"):
|
19 |
return snapshot_download(repo_id=repo_id, revision=revision, local_dir=os.path.join(MODELS_PATH, repo_id), cache_dir=HF_CACHE_DIR)
|
@@ -37,7 +40,7 @@ model_dir_pt_ca = download_model("projecte-aina/mt-aina-pt-ca", revision="main")
|
|
37 |
directions = {
|
38 |
"Catalan": {
|
39 |
"target": {
|
40 |
-
"Spanish": {"model": (f"{os.path.join(MODELS_PATH, model_dir_ca_es)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_ca_es)}")},
|
41 |
"English": {"model": (f"{os.path.join(MODELS_PATH, model_dir_ca_en)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_ca_en)}")},
|
42 |
"French": {"model": (f"{os.path.join(MODELS_PATH, model_dir_ca_fr)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_ca_fr)}")},
|
43 |
"German": {"model": (f"{os.path.join(MODELS_PATH, model_dir_ca_de)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_ca_de)}")},
|
@@ -48,7 +51,7 @@ directions = {
|
|
48 |
},
|
49 |
"Spanish": {
|
50 |
"target": {
|
51 |
-
"Catalan": {"model": (f"{os.path.join(MODELS_PATH, model_dir_es_ca)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_es_ca)}")},
|
52 |
}
|
53 |
},
|
54 |
"English": {
|
@@ -75,7 +78,8 @@ def get_target_languages(source_language):
|
|
75 |
|
76 |
|
77 |
def get_target_languege_model(source_language, target_language):
|
78 |
-
return directions.get(source_language, {}).get("target", {}).get(target_language, {}).get("model")
|
|
|
79 |
|
80 |
|
81 |
def translate(text, source_language, lang_pair):
|
@@ -101,7 +105,12 @@ def translate_input(input, source_language, target_language):
|
|
101 |
return None
|
102 |
|
103 |
target_language_model = get_target_languege_model(source_language, target_language)
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
return translation
|
107 |
|
|
|
4 |
from AinaTheme import theme
|
5 |
from huggingface_hub import snapshot_download
|
6 |
import nltk
|
7 |
+
from api_endpoint import invoke_translate_endpoint
|
8 |
|
9 |
from translate import translate_text
|
10 |
|
|
|
14 |
|
15 |
MODELS_PATH = "./models"
|
16 |
HF_CACHE_DIR = "./hf_cache"
|
17 |
+
MAX_INPUT_CHARACTERS = int(os.environ.get("MAX_INPUT_CHARACTERS", default=1000))
|
18 |
+
API_ENDPOINT_ENABLED = os.environ.get("API_ENDPOINT_ENABLED", default=True) == "True"
|
19 |
+
|
20 |
|
21 |
def download_model(repo_id, revision="main"):
|
22 |
return snapshot_download(repo_id=repo_id, revision=revision, local_dir=os.path.join(MODELS_PATH, repo_id), cache_dir=HF_CACHE_DIR)
|
|
|
40 |
directions = {
|
41 |
"Catalan": {
|
42 |
"target": {
|
43 |
+
"Spanish": {"src": "ca", "tgt":"es","model": (f"{os.path.join(MODELS_PATH, model_dir_ca_es)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_ca_es)}")},
|
44 |
"English": {"model": (f"{os.path.join(MODELS_PATH, model_dir_ca_en)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_ca_en)}")},
|
45 |
"French": {"model": (f"{os.path.join(MODELS_PATH, model_dir_ca_fr)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_ca_fr)}")},
|
46 |
"German": {"model": (f"{os.path.join(MODELS_PATH, model_dir_ca_de)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_ca_de)}")},
|
|
|
51 |
},
|
52 |
"Spanish": {
|
53 |
"target": {
|
54 |
+
"Catalan": {"src": "es", "tgt":"ca","model": (f"{os.path.join(MODELS_PATH, model_dir_es_ca)}/spm.model", f"{os.path.join(MODELS_PATH, model_dir_es_ca)}")},
|
55 |
}
|
56 |
},
|
57 |
"English": {
|
|
|
78 |
|
79 |
|
80 |
def get_target_languege_model(source_language, target_language):
|
81 |
+
# return directions.get(source_language, {}).get("target", {}).get(target_language, {}).get("model")
|
82 |
+
return directions.get(source_language, {}).get("target", {}).get(target_language, {})
|
83 |
|
84 |
|
85 |
def translate(text, source_language, lang_pair):
|
|
|
105 |
return None
|
106 |
|
107 |
target_language_model = get_target_languege_model(source_language, target_language)
|
108 |
+
|
109 |
+
if (API_ENDPOINT_ENABLED and 'src' in target_language_model):
|
110 |
+
translation = invoke_translate_endpoint(input, target_language_model)
|
111 |
+
if translation is not None:
|
112 |
+
return translation
|
113 |
+
translation = translate(input, source_language, target_language_model.get('model'))
|
114 |
|
115 |
return translation
|
116 |
|