Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# syntax=docker/dockerfile:1

ARG PYTHON_VERSION=3.10.12
FROM python:${PYTHON_VERSION}-slim as base
FROM python:3.10.12-slim AS base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1
Expand All @@ -11,8 +10,8 @@ ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1


ENV MT_API_CONFIG /app/config.json
ENV MODELS_ROOT /app/models
ENV MT_API_CONFIG=/app/config.json
ENV MODELS_ROOT=/app/models

ENV HUGGINGFACE_HUB_CACHE=/app/models \
HF_HUB_ENABLE_HF_TRANSFER=1 \
Expand Down
18 changes: 9 additions & 9 deletions app/views/v1/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

translate_v1 = APIRouter(prefix='/api/v1/translate')

DEVDEBUG = True
logger = logging.getLogger('console_logger')
logger.setLevel(logging.INFO)

def fetch_model_data_from_request(request):
config = Config()
Expand All @@ -39,10 +39,9 @@ def fetch_model_data_from_request(request):
detail=f'Language pair {model_id} is not supported.',
)

if DEVDEBUG:
logger.debug(f'compatible_model_ids {compatible_model_ids}')
if use_multi:
logger.debug(f'use_multi {use_multi}')
logger.debug(f'compatible_model_ids {compatible_model_ids}')
if use_multi:
logger.debug(f'use_multi {use_multi}')

regular_model_exists = model_id in config.loaded_models
multilingual_model_exists_for_pair = any([mid.startswith(MULTIMODALCODE) for mid in compatible_model_ids])
Expand All @@ -56,17 +55,18 @@ def fetch_model_data_from_request(request):
# model_id = get_model_id(src=MULTIMODALCODE,
# tgt=MULTIMODALCODE,
# alt_id=request.alt)
model_id = config._pair_to_model_id(compatible_model_ids[0])
if len(compatible_model_ids) > 1:
logger.warning(f"More than one compatible model. Choosing {compatible_model_ids[0]} among {compatible_model_ids}")
compatible_multi_model_ids = [mid for mid in compatible_model_ids if mid.startswith(MULTIMODALCODE)]
model_id = config._pair_to_model_id(compatible_multi_model_ids[0])
if len(compatible_multi_model_ids) > 1:
logger.warning(f"More than one compatible model. Choosing {compatible_multi_model_ids[0]} among {compatible_multi_model_ids}")

else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f'No multilingual model support for pair {src}-{tgt}. Remove flag `use_multi` from request',
)

if DEVDEBUG: logger.debug(f'model_id {model_id}')
logger.debug(f'model_id {model_id}')

return model_id, src, tgt

Expand Down
11 changes: 6 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
import uvicorn
from app import create_app


if __name__ == "__main__":
load_models = [item.strip() for item in os.environ.get("LOAD", "es-ca,ca-es").split(",")]

parser = argparse.ArgumentParser(description="An API designed to provide translation services for text between different languages.")
parser.add_argument("-m", "--models", type=str, default="./models", help="Directory path of models", required=False)
parser.add_argument("-l", "--load", type=str, nargs="+", help="Option to load models, if it contains 'all' it will download all models", default=["es-ca", "ca-es"])
parser.add_argument("-l", "--load", type=str, nargs="+", help="Option to load models, if it contains 'all' it will download all models", default=load_models)

args = parser.parse_args()
os.environ['MODELS_ROOT'] = args.models

models_to_load = args.load
if 'all' in models_to_load:
load_all_models = True
else:
load_all_models = False
load_all_models = 'all' in models_to_load

app = create_app(load_all_models, models_to_load)
uvicorn.run(app, host="0.0.0.0", port=8000, log_config = "logging.yml")