translation.py
1 """ 2 Defines API paths for translation endpoints. 3 """ 4 5 from typing import List, Optional 6 7 from fastapi import APIRouter, Body 8 9 from .. import application 10 from ..route import EncodingAPIRoute 11 12 router = APIRouter(route_class=EncodingAPIRoute) 13 14 15 @router.get("/translate") 16 def translate(text: str, target: Optional[str] = "en", source: Optional[str] = None): 17 """ 18 Translates text from source language into target language. 19 20 Args: 21 text: text to translate 22 target: target language code, defaults to "en" 23 source: source language code, detects language if not provided 24 25 Returns: 26 translated text 27 """ 28 29 return application.get().pipeline("translation", (text, target, source)) 30 31 32 @router.post("/batchtranslate") 33 def batchtranslate(texts: List[str] = Body(...), target: Optional[str] = Body(default="en"), source: Optional[str] = Body(default=None)): 34 """ 35 Translates text from source language into target language. 36 37 Args: 38 texts: list of text to translate 39 target: target language code, defaults to "en" 40 source: source language code, detects language if not provided 41 42 Returns: 43 list of translated text 44 """ 45 46 return application.get().pipeline("translation", (texts, target, source))