docker/api/src/main.py
async def translate(target_lang: str, text: List[str] = Query([]), source_lang: Optional[str] = '', beam_size: Optional[int] = 5, perform_sentence_splitting: Optional[bool] = True):
    """
    Translates the text to the given target language.
    :param text: Text that should be translated
    :param target_lang: Target language
    :param source_lang: Language of text. Optional, if empty: Automatic language detection
    :param beam_size: Beam size. Optional
    :param perform_sentence_splitting: Split longer documents into individual sentences for translation. Optional
    :return:  Returns a json with the translated text
    """

    if not IS_BACKEND:
        async_client = http3.AsyncClient()
        data = {'target_lang': target_lang, 'text': text, 'source_lang': source_lang, 'beam_size': beam_size, 'perform_sentence_splitting': perform_sentence_splitting}
        x = await async_client.post(BACKEND_URL+'/translate', json=data, timeout=3600)
        if x.status_code != 200:
            error_msg = "Error: " + x.text
            try:
                error_msg = x.json()['detail']
            except:
                pass

            raise HTTPException(403, detail=error_msg)
        return x.json()

    else:
        #Check input parameters
        if 'EASYNMT_MAX_TEXT_LEN' in os.environ and len(text) > int(os.getenv('EASYNMT_MAX_TEXT_LEN')):
            raise ValueError("Text was too long. Only texts up to {} characters are allowed".format(os.getenv('EASYNMT_MAX_TEXT_LEN')))

        if beam_size < 1 or ('EASYNMT_MAX_BEAM_SIZE' in os.environ and beam_size > int(os.getenv('EASYNMT_MAX_BEAM_SIZE'))):
            raise ValueError("Illegal beam size")

        if len(source_lang.strip()) == 0:
            source_lang = None

        #Start the translation
        start_time = time.time()
        output = {"target_lang": target_lang, "source_lang": source_lang}


        if source_lang is None:
            detected_langs = model.language_detection(text)
            output['detected_langs'] = detected_langs
            #TODO Grouping and individual translation
            #Exception: add original text


        try:
            output['translated'] = model.translate(text, target_lang=target_lang, source_lang=source_lang, beam_size=beam_size, perform_sentence_splitting=perform_sentence_splitting, batch_size=int(os.getenv('EASYNMT_BATCH_SIZE', 16)))
        except Exception as e:
            raise HTTPException(403, detail="Error: "+str(e))

        output['translation_time'] = time.time()-start_time
        return output

    Translates the text to the given target language.
    :param text: Text that should be translated
    :param target_lang: Target language
    :param source_lang: Language of text. Optional, if empty: Automatic language detection
    :param beam_size: Beam size. Optional
    :param perform_sentence_splitting: Split longer documents into individual sentences for translation. Optional
    :return:  Returns a json with the translated text
    
Translates the text to the given target language.
Translates the text to the given target language .
async def translate(target_lang: str, text: List[str] = Query([]), source_lang: Optional[str] = '', beam_size: Optional[int] = 5, perform_sentence_splitting: Optional[bool] = True):
    if not IS_BACKEND:
        async_client = http3.AsyncClient()
        data = {'target_lang': target_lang, 'text': text, 'source_lang': source_lang, 'beam_size': beam_size, 'perform_sentence_splitting': perform_sentence_splitting}
        x = await async_client.post(BACKEND_URL+'/translate', json=data, timeout=3600)
        if x.status_code != 200:
            error_msg = "Error: " + x.text
            try:
                error_msg = x.json()['detail']
            except:
                pass
            raise HTTPException(403, detail=error_msg)
        return x.json()
    else:
        if 'EASYNMT_MAX_TEXT_LEN' in os.environ and len(text) > int(os.getenv('EASYNMT_MAX_TEXT_LEN')):
            raise ValueError("Text was too long. Only texts up to {} characters are allowed".format(os.getenv('EASYNMT_MAX_TEXT_LEN')))
        if beam_size < 1 or ('EASYNMT_MAX_BEAM_SIZE' in os.environ and beam_size > int(os.getenv('EASYNMT_MAX_BEAM_SIZE'))):
            raise ValueError("Illegal beam size")
        if len(source_lang.strip()) == 0:
            source_lang = None
        start_time = time.time()
        output = {"target_lang": target_lang, "source_lang": source_lang}
        if source_lang is None:
            detected_langs = model.language_detection(text)
            output['detected_langs'] = detected_langs
        try:
            output['translated'] = model.translate(text, target_lang=target_lang, source_lang=source_lang, beam_size=beam_size, perform_sentence_splitting=perform_sentence_splitting, batch_size=int(os.getenv('EASYNMT_BATCH_SIZE', 16)))
        except Exception as e:
            raise HTTPException(403, detail="Error: "+str(e))
        output['translation_time'] = time.time()-start_time
        return output
async def translate ( target_lang : str , text : List [ str ] = Query ( [ ] ) , source_lang : Optional [ str ] = '' , beam_size : Optional [ int ] = 5 , perform_sentence_splitting : Optional [ bool ] = True ) : if not IS_BACKEND : async_client = http3 . AsyncClient ( ) data = { 'target_lang' : target_lang , 'text' : text , 'source_lang' : source_lang , 'beam_size' : beam_size , 'perform_sentence_splitting' : perform_sentence_splitting } x = await async_client . post ( BACKEND_URL + '/translate' , json = data , timeout = 3600 ) if x . status_code != 200 : error_msg = "Error: " + x . text try : error_msg = x . json ( ) [ 'detail' ] except : pass raise HTTPException ( 403 , detail = error_msg ) return x . json ( ) else : if 'EASYNMT_MAX_TEXT_LEN' in os . environ and len ( text ) > int ( os . getenv ( 'EASYNMT_MAX_TEXT_LEN' ) ) : raise ValueError ( "Text was too long. Only texts up to {} characters are allowed" . format ( os . getenv ( 'EASYNMT_MAX_TEXT_LEN' ) ) ) if beam_size < 1 or ( 'EASYNMT_MAX_BEAM_SIZE' in os . environ and beam_size > int ( os . getenv ( 'EASYNMT_MAX_BEAM_SIZE' ) ) ) : raise ValueError ( "Illegal beam size" ) if len ( source_lang . strip ( ) ) == 0 : source_lang = None start_time = time . time ( ) output = { "target_lang" : target_lang , "source_lang" : source_lang } if source_lang is None : detected_langs = model . language_detection ( text ) output [ 'detected_langs' ] = detected_langs try : output [ 'translated' ] = model . translate ( text , target_lang = target_lang , source_lang = source_lang , beam_size = beam_size , perform_sentence_splitting = perform_sentence_splitting , batch_size = int ( os . getenv ( 'EASYNMT_BATCH_SIZE' , 16 ) ) ) except Exception as e : raise HTTPException ( 403 , detail = "Error: " + str ( e ) ) output [ 'translation_time' ] = time . time ( ) - start_time return output
Translates the text to the given target language.
Translates the text to the given target language .
"""
    Translates the text to the given target language.
    :param text: Text that should be translated
    :param target_lang: Target language
    :param source_lang: Language of text. Optional, if empty: Automatic language detection
    :param beam_size: Beam size. Optional
    :param perform_sentence_splitting: Split longer documents into individual sentences for translation. Optional
    :return:  Returns a json with the translated text
    """ #Check input parameters
 #Start the translation
 #TODO Grouping and individual translation
 #Exception: add original text

Returns a json with the translated text
Returns a json with the translated text
target_lang
str
None
text
List[str]
Text that should be translated
Text that should be translated
source_lang
Optional[str]
Language of text. Optional, if empty: Automatic language detection
Language of text . Optional if empty : Automatic language detection
beam_size
Optional[int]
Beam size. Optional
Beam size . Optional
perform_sentence_splitting
Optional[bool]
Split longer documents into individual sentences for translation. Optional
Split longer documents into individual sentences for translation . Optional