
    Mi              	           S SK r S SKJrJr  S SKrS SKJrJr  S SKJ	r	  S SK
Jr  S\S\4S jrS	\4S
 jrSS\S\\   S\S\4S jjrS rg)    N)ListOptional)MarianMTModelMarianTokenizer)sent_tokenize)chunk_by_attention_windowsource_langtarget_langc                     SU  SU 3$ )zfConstructs the name of the MarianMT machine translation model based on the
source and target language.zHelsinki-NLP/opus-mt-- )r	   r
   s     y/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/unstructured/cleaners/translate.py_get_opus_mt_model_namer      s     #;-q>>    language_codec                 j    [        U [        5      (       a  [        U 5      S:w  a  [        SU  S35      eg )N   zInvalid language code: z,. Language codes must be two letter strings.)
isinstancestrlen
ValueError)r   s    r   _validate_language_coder      s;    mS))S-?1-D%m_4`a
 	
 .Er   textreturnc                    U R                  5       S:X  a  U $ Ub  UO[        R                  " U 5      nUR                  S5      (       a  Sn[	        U5        [	        U5        X#:X  a  U $ [        X25      n[        SU 35         [        R                  " U5      n[        R                  " U5      n[        X[        S9n/ nU H  n	UR                  [        XU5      5        M      SR!                  U5      $ ! [         a    [        SU S35      ef = f)a  Translates the foreign language text. If the source language is not specified, the
function will attempt to detect it using langdetect.

Parameters
----------
text: str
    The text to translate
target_lang: str
    The two letter language code for the target langague. Defaults to "en".
source_lang: Optional[str]
    The two letter language code for the language of the input text. If source_lang is
    not provided, the function will try to detect it.
 zhzUsing model: z2Transformers could not find the translation model z>. The requested source/target language combo is not supported.)split_function )strip
langdetectdetect
startswithr   r   printr   from_pretrainedr   OSErrorr   r   r   append_translate_textjoin)
r   r	   r
   _source_lang
model_name	tokenizermodelchunkstranslated_chunkschunks
             r   translate_textr1      s    zz|r'2'>JDUDUVZD[L t$$K(L)"(CJ	M*
&'
#33J?	--j9 2$R_`F#%  i!HI  88%&&  
@ MK K
 	

s   ,C2 2Dc                    [         R                  " 5          [         R                  " S5        UR                  " S	0 U" U /SSSS9D6nSSS5        W Vs/ s H  oBR	                  USSS9PM     snS   $ ! , (       d  f       N2= fs  snf )
z8Translates text using the specified model and tokenizer.ignoreptT)return_tensorspadding
truncationNi   )max_new_tokensskip_special_tokensr   r   )warningscatch_warningssimplefiltergeneratedecode)r   r-   r,   
translatedts        r   r(   r(   K   s    
 
	 	 	"h'^^ 
tTdS

 
#
 XbbWaRSQsMWab	  
#	"
 cs   1A3B3
B)Nen)r:   typingr   r   r!   transformersr   r   unstructured.nlp.tokenizer    unstructured.staging.huggingfacer   r   r   r   r1   r(   r   r   r   <module>rF      sb     !  7 3 F? ?3 ?
3 
0' 0'8C= 0'c 0']` 0'fr   