
    Ti:\                       % S SK Jr  S SKrS SKJr  S SKrS SKJr  S SKJrJ	r	J
r
JrJr  S SKJr  S SKJr  S SKJrJrJr  S S	KJr   " S
 S5      r " S S5      r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r\r " S S\5      r  S&           S'S jjr! " S S5      r"/ r#S\$S '   \RJ                  " 5       S(S! j5       r&S(S" jr'S)S# jr(S*S$ jr)S+S% jr*g),    )annotationsN)List)
Translator)apisfewshotpackagesbdsettings)ILanguageModel)Package)SpacySentencizerSmallStanzaSentencizerMiniSBDSentencizer)infoc                  J    \ rS rSr% SrS\S'   S\S'   SS jrS rS	 rS
 r	Sr
g)
Hypothesis   zRepresents a translation hypothesis

Attributes:
    value: The hypothetical translation value
    score: The score representing the quality of the translation
strvaluefloatscorec                    Xl         X l        g N)r   r   )selfr   r   s      j/var/www/html/backend/Backoffice_Marketplace/venv/lib/python3.13/site-packages/argostranslate/translate.py__init__Hypothesis.__init__   s    

    c                4    U R                   UR                   :  $ r   )r   )r   others     r   __lt__Hypothesis.__lt__    s    zzEKK''r   c                N    S[        U R                  5       SU R                   S3$ )N(z, ))reprr   r   r   s    r   __repr__Hypothesis.__repr__#   s%    4

#$Btzzl!44r   c                    [        U 5      $ r   )r&   r'   s    r   __str__Hypothesis.__str__&   s    Dzr   )r   r   N)r   r   r   r   )__name__
__module____qualname____firstlineno____doc____annotations__r   r!   r(   r+   __static_attributes__ r   r   r   r      s'     JL(5r   r   c                  z    \ rS rSr% SrS\S'   S\S'   SS jrSSS jjr\SS j5       r	\SS	 j5       r
S
 rS rSrg)ITranslation*   zRepresents a translation between two Languages

Attributes:
    from_lang: The Language this Translation translates from.
    to_lang: The Language this Translation translates to.

Language	from_langto_langc                <    U R                  USS9S   R                  $ )zTranslates a string from self.from_lang to self.to_lang

Args:
    input_text: The text to be translated.

Returns:
    input_text translated.

   )num_hypothesesr   )
hypothesesr   )r   
input_texts     r   	translateITranslation.translate6   s"     z!<Q?EEEr   c                    [        5       e)zTranslates a string from self.from_lang to self.to_lang

Args:
    input_text: The text to be translated.
    num_hypotheses: Number of hypothetical results expected

Returns:
    List of translation hypotheses

)NotImplementedError)r   r?   r=   s      r   r>   ITranslation.hypothesesB   s     "##r   c                $    U R                  S5      $ )zSplits input_text into paragraphs and returns a list of paragraphs.

Args:
    input_text: The text to be split.

Returns:
    A list of paragraphs.


)split)r?   s    r   split_into_paragraphs"ITranslation.split_into_paragraphsO   s     %%r   c                $    SR                  U 5      $ )zCombines a list of paragraphs together.

Args:
    paragraphs: A list of paragraphs.

Returns:
    list of n paragraphs combined into one string.

rF   )join)
paragraphss    r   combine_paragraphsITranslation.combine_paragraphs\   s     yy$$r   c                ^    [        U R                  5      S-   [        U R                  5      -   $ )Nz -> )r   r9   r:   r'   s    r   r(   ITranslation.__repr__i   s$    4>>"V+c$,,.???r   c                8    [        U 5      R                  SS5      $ )Nz->u   →)r&   replacer'   s    r   r+   ITranslation.__str__l   s    Dz!!$..r   r4   N)r?   r   returnr      r?   r   r=   intrT   list[Hypothesis])r?   r   rT   	list[str])rL   rZ   rT   r   )r-   r.   r/   r0   r1   r2   r@   r>   staticmethodrH   rM   r(   r+   r3   r4   r   r   r6   r6   *   sS     
F$ 
& 
& 
% 
%@/r   r6   c                  P    \ rS rSr% Sr/ rS\S'   / rS\S'   SS jrS r	SS jr
S	rg
)r8   p   aZ  Represents a language that can be translated from/to.

Attributes:
    code: The code representing the language.
    name: The human-readable name of the language.
    translations_from: A list of the translations
        that translate from this language.
    translations_to: A list of the translations
        that translate to this language

zlist[ITranslation]translations_fromtranslations_toc                8    Xl         X l        / U l        / U l        g r   codenamer^   r_   )r   rb   rc   s      r   r   Language.__init__   s    		!#!r   c                    U R                   $ r   rc   r'   s    r   r+   Language.__str__   s    yyr   c                t   ^ [        [        U4S jU R                  5      5      n[        U5      S:  a  US   $ g)zGets a translation from this Language to another Language.

Args:
    to: The Language to look for a Translation to.

Returns:
    A valid Translation if there is one in translations_from
        else None.

c                J   > U R                   R                  TR                  :H  $ r   )r:   rb   )xtos    r   <lambda>*Language.get_translation.<locals>.<lambda>   s    QYY^^rww6r   r   N)listfilterr^   len)r   rk   valid_translationss    ` r   get_translationLanguage.get_translation   s?     "68N8NO
 !"Q&%a((r   ra   N)rb   r   rc   r   )rk   r8   rT   zITranslation | None)r-   r.   r/   r0   r1   r^   r2   r_   r   r+   rr   r3   r4   r   r   r8   r8   p   s/    
 -/).*,O',"r   r8   c                  0    \ rS rSrSrSS jrSS	S jjrSrg)
PackageTranslation   z.A Translation that is installed with a packagec                8   Xl         X l        X0l        S U l        S n[        R
                  [        R                  R                  [        R                  R                  4;   a  [        nO[        R
                  [        R                  R                  :X  a  [        nOe[        R
                  [        R                  R                  :X  a  [        nO2[        R
                  [        R                  R                  :X  a  [        nUb  U" U5      U l        g [!        5       er   )r9   r:   pkg
translatorr
   
chunk_type	ChunkTypeARGOSTRANSLATEDEFAULTr   STANZAr   MINISBDSPACYr   sentencizerrC   )r   r9   r:   rx   Sentencizers        r   r   PackageTranslation.__init__   s    "8#5#5#D#DhFXFXF`F`"aa,K  H$6$6$=$==+K  H$6$6$>$>>,K  H$6$6$<$<</K"*3/D &''r   c           
        U R                   c  [        U R                  R                  S-  5      nU[        R
                  [        R                  [        R                  S.n[        R                  S:w  a  [        R                  US'   [        R                  " S0 UD6U l         [        R                  U5      n[        SU5        / nU H?  nUR                  [        U R                  UU R                   U R                   U5      5        MA     [        SU5        [#        U5       Vs/ s H  n[%        SS5      PM     n	n[#        U5       H  nU H\  n
[        R'                  X   R(                  X   R(                  /5      nX   R*                  X   R*                  -   n[%        X5      X'   M^     X   R(                  R-                  S	5      X   l        M     [        S
U	5        U	$ s  snf )Nmodel)
model_pathdeviceinter_threadsintra_threadsautocompute_typezparagraphs:ztranslated_paragraphs: r   rF   zhypotheses_to_return:r4   )ry   r   rx   package_pathr
   r   r   r   r   ctranslate2r   r6   rH   r   appendapply_packaged_translationr   ranger   rM   r   r   lstrip)r   r?   r=   r   paramsrL   translated_paragraphs	paragraphihypotheses_to_returntranslated_paragraphr   r   s                r   r>   PackageTranslation.hypotheses   s   ??"TXX22W<=J("//!)!7!7!)!7!7	F $$.)1)>)>~&)44>v>DO!77
C
]J' "#I!((*HHOO$$" $ 	%'<= <A;PQ;Pa
2q 1;PQ~&A(=$$77),224H4K4Q4QR -/558L8O8U8UU*4U*B$' )> -A,C,I,I,P,PQU,V #) ' 	$&:;##  Rs   $G")r9   rx   r   r:   ry   N)r9   r8   r:   r8   rx   r   rU   rW   r-   r.   r/   r0   r1   r   r>   r3   r4   r   r   ru   ru      s    8(,&$ &$r   ru   c                  0    \ rS rSrSrSS jrSS	S jjrSrg)
IdentityTranslation   z-A Translation that doesn't modify input_text.c                    Xl         Xl        g)zpCreates an IdentityTranslation.

Args:
    lang: The Language this Translation translates
        from and to.

Nr9   r:   )r   langs     r   r   IdentityTranslation.__init__   s     r   c                X    [        U5       Vs/ s H  n[        US5      PM     sn$ s  snf )Nr   )r   r   )r   r?   r=   r   s       r   r>   IdentityTranslation.hypotheses   s(    383HI3Ha
:q)3HIIIs   'r   N)r   r8   rU   )r?   r   r=   rX   r   r4   r   r   r   r      s    7	J Jr   r   c                  Z    \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   SS	 jrSSS
 jjrSrg)CompositeTranslation   zA ITranslation that is performed by chaining two Translations

Attributes:
    t1: The first Translation to apply.
    t2: The second Translation to apply.

r6   t1t2r8   r9   r:   c                `    Xl         X l        UR                  U l        UR                  U l        g)zxCreates a CompositeTranslation.

Args:
    t1: The first Translation to apply.
    t2: The second Translation to apply.
N)r   r   r9   r:   )r   r   r   s      r   r   CompositeTranslation.__init__   s$     zzr   c           	     L   U R                   R                  X5      n/ nU Hn  nU R                  R                  UR                  U5      nU H?  nUR	                  [        UR                  UR                  UR                  -   5      5        MA     Mp     UR                  SS9  USU $ )NT)reverser   )r   r>   r   r   r   r   r   sort)r   r?   r=   t1_hypotheses	to_returnt1_hypothesist2_hypothesest2_hypothesiss           r   r>   CompositeTranslation.hypotheses
  s    **:F 	*M GG..}/B/BNSM!.  %++]-@-@=CVCV-V "/ + 	t$>**r   )r9   r   r   r:   N)r   r6   r   r6   rU   rW   	r-   r.   r/   r0   r1   r2   r   r>   r3   r4   r   r   r   r      s0     	
"+ +r   r   c                  Z    \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   SS
 jrSSS jjrSrg)CachedTranslationi  a  Caches a translation to improve performance.

This is done by splitting up the text passed for translation
into paragraphs and translating each paragraph individually.
A hash of the paragraphs and their corresponding translations
are saved from the previous translation and used to improve
performance on the next one. This is especially useful if you
are repeatedly translating nearly identical text with a small
change at the end of it.

r6   
underlyingr8   r9   r:   dictcachec                r    Xl         UR                  U l        UR                  U l        [        5       U l        g)zYCreates a CachedTranslation.

Args:
    underlying: The underlying translation to cache.
N)r   r9   r:   r   r   )r   r   s     r   r   CachedTranslation.__init__.  s,     %#--!))V
r   c                   [        5       n[        R                  U5      n/ nU H`  nU R                  R	                  U5      nUb  [        U5      U:w  a  U R                  R                  Xb5      nXsU'   UR                  U5        Mb     X0l        [        U5       Vs/ s H  n[        SS5      PM     n	n[        U5       H  n[        [        U5      5       Hb  n
[        R                  X   R                  XZ   U   R                  /5      nX   R                  XZ   U   R                  -   n[        X5      X'   Md     X   R                  R                  S5      X   l        M     U	$ s  snf )Nr   r   rF   )r   r6   rH   r   getrp   r   r>   r   r   r   rM   r   r   r   )r   r?   r=   	new_cacherL   r   r   r   r   r   jr   r   s                r   r>   CachedTranslation.hypotheses9  sm   F	!77
C
 "#I#'::>>)#<  %,+,>'+'A'A($ $8i !(()=> $ 
 <A;PQ;Pa
2q 1;PQ~&A3456$77),224I4LQ4O4U4UV )+114I4LQ4O4U4UU  +5U*B$' 7 -A,C,I,I,P,PQU,V #) ' $#  Rs   E%)r   r9   r:   r   N)r   r6   rU   rW   r   r4   r   r   r   r     s/    
 K	$ $r   r   c                  F    \ rS rSr% SrS\S'   S\S'   S
S jrSSS jjrSrg	)RemoteTranslationi[  z8A translation provided by a remote LibreTranslate serverr8   r9   r:   c                (    Xl         X l        X0l        g r   )r9   r:   api)r   r9   r:   r   s       r   r   RemoteTranslation.__init__a  s    "r   c                    U R                   R                  XR                  R                  U R                  R                  5      n[        US5      /U-  $ )z}LibreTranslate only supports single hypotheses.

A list of length num_hypotheses will be returned with identical hypotheses.
r   )r   r@   r9   rb   r:   r   )r   r?   r=   results       r   r>   RemoteTranslation.hypothesesf  sC    
 ##J0C0CT\\EVEVW61%&77r   )r   r9   r:   N)r9   r8   r:   r8   r<   rW   r   r4   r   r   r   r   [  s     B
8 8r   r   c                  \    \ rS rSr% SrS\S'   S\S'   S\S'         SS jrSSS	 jjrS
rg)FewShotTranslationis  z6A translation performed with a few shot language modelr8   r9   r:   r   language_modelc                (    Xl         X l        X0l        g r   )r9   r:   r   )r   r9   r:   r   s       r   r   FewShotTranslation.__init__z  s     #,r   c                h   Sn/ nSnU[        U5      S-
  :  a  [        R                  " XS  5      n[        R                  " U R                  R                  U5      5      n[        R                  " XS  U5      nUS:X  a  XS-   n	OXX-   n	UR                  XU	 5        [        SU5        [        SU	5        [        XU	 5        U	nU[        U5      S-
  :  a  M  Sn
U H  n[        R                  " UU R                  R                  U R                  R                  U R                  R                  U R                  R                  5      n[        SU5        U R                  R                  U5      n[        S	U5        [        R                  " U5      n[        S
U5        X-  n
M     [!        U
S5      /U-  $ )N   r   r<   start_index	sbd_indexr   zfewshot promptzfewshot responsezfewshot result)rp   r	   generate_fewshot_sbd_promptparse_fewshot_responser   inferprocess_seq2seq_sbdr   r   r   generate_promptr9   rc   rb   r:   parse_inferencer   )r   r?   r=   DEFAULT_SENTENCE_LENGTH	sentencesr   promptresponsedetected_sentence_indexr   r   sentencer   s                r   r>   FewShotTranslation.hypotheses  s   "%	C
Oa//44Z5MNF11$2E2E2K2KF2STH&)&=&=<(('# '",'A	'A	ZI>?,i(	23#K C
Oa//" 	!H,,####!!!!F !6***008H#X.,,X6F!6*I " 9a()N::r   )r9   r   r:   N)r9   r8   r:   r8   r   r   r   rW   r   r4   r   r   r   r   s  s>    @""-!-,4-FT-%; %;r   r   c                   [        SU5        UR                  U5      n[        SU5        U Vs/ s H  o`R                  R                  U5      PM     nn[        SU5        SnU R                  S:w  a  U R                  //[        U5      -  nUR                  UUS[        R                  S[        U[        R                  5      USSS	9	n	[        S
U	5        / n
[        U5       H  n/ nSnU	 H2  nUR                  UR                  U   5        XR                  U   -  nM4     U R                  R                  U5      nU R                  S:w  a8  UR!                  U R                  5      (       a  U[        U R                  5      S n[        U5      S:  a  US   S:X  a  USS n[#        X5      nU
R%                  U5        M     [        SU
5        U
$ s  snf )a  Applies the translation in pkg to translate input_text.

Args:
    pkg: The package that provides the translation.
    input_text: The text to be translated.
    translator: The CTranslate2 Translator
    sentencizer: The Sentence Boundary Detection package
    num_hypotheses: The number of hypotheses to generate

Returns:
    A list of Hypotheses objects for translated input_text.
r   r   	tokenizedNr   Ttokensg?)target_prefixreplace_unknownsmax_batch_size
batch_type	beam_sizer=   length_penaltyreturn_scorestranslated_batchesr    r<   zvalue_hypotheses:)r   split_sentences	tokenizerencoder   rp   translate_batchr
   
batch_sizemaxr   r   extendr>   scoresdecode
startswithr   r   )rx   r?   ry   r   r=   r   r   r   r   r   value_hypothesesr   translated_tokenscumulative_scoretranslated_batchr   
hypothesiss                    r   r   r     s   ( 		%z2 ++J7Ii  AJJ	H%%h/	IJi  M
B++,-I>#33#**nh&8&89% 4 
 		12 >" 2$$%5%@%@%CD 7 7 :: !3 $$%67"u'7'78I8I'J'J#c//023Eu:>eAh#o !"IE8

+' #( 		./[ Ks   $Gc                  .    \ rS rSr% SrS\S'   S\S'   Srg)	InstalledTranslatei  z}
Global storage of instances of the CachedTranslation class by unique keys.
To avoid creating unnecessary objects in memory.
r   package_keyr   cached_translationr4   N)r-   r.   r/   r0   r1   r2   r3   r4   r   r   r   r     s    
 ))r   r   zList[InstalledTranslate]installed_translatesc                 
  ^ [        S5        [        R                  [        R                  R                  :X  Ga  [
        R                  " 5       n [        [        S U 5      5      n [        5       nU  GHg  nUR                  U;  a,  [        UR                  UR                  5      XR                  '   UR                  U;  a,  [        UR                  UR                  5      XR                  '   XR                     nXR                     nUR                   SUR                   3m[        [        U4S j[        5      5      n[!        U5      S:X  aC   [#        [%        X4U5      5      n['        5       nTUl        Xgl        [        R-                  U5        OUS   R*                  nUR0                  R-                  U5        UR2                  R-                  U5        GMj     [        UR5                  5       5      n	U	 HD  n
[7        U
5      nU
R0                  R-                  U5        U
R2                  R-                  U5        MF     U	 H  n
S	nU(       d  M  S
nU
R0                   H  nUR8                  R0                   Hn  nU
R;                  UR8                  5      b  M!  S	n[=        X5      nU
R0                  R-                  U5        UR8                  R2                  R-                  U5        Mp     M     U(       a  M  M     GO[        R                  [        R                  R>                  :X  a  [@        RB                  " 5       nURE                  5       nU Vs/ s H  n[        US   US   5      PM     n	nU	 HN  nU	 HE  n[G        X4U5      nUR0                  R-                  U5        UR2                  R-                  U5        MG     MP     O[        R                  [        R                  RH                  :X  a  [@        RJ                  " [        RL                  5      n[        SS5      [        SS5      /n	U	 HN  nU	 HE  n[O        X4U5      nUR0                  R-                  U5        UR2                  R-                  U5        MG     MP     Sn[Q        W	5       H  u  nn
U
RR                  S:X  d  M  Un  O   SnUb  U	RU                  U5      nU	RW                  S S9  Ub  U/U	-   n	U	$ ! [.         a  n[        ST SU 35         SnAGM  SnAff = fs  snf )z3Returns a list of Languages installed from packagesget_installed_languagesc                     U R                   S:H  $ )Nr@   )typerj   s    r   rl   )get_installed_languages.<locals>.<lambda>  s    ;)>r   -c                "   > U R                   T:H  $ r   )r   )rj   r   s    r   rl   r    s    +!=r   r   zSkipping package z: NTFrb   rc   enEnglishesSpanishc                    U R                   $ r   rf   r  s    r   rl   r  l  s    r   )key),r   r
   model_providerModelProviderOPENNMTr   get_installed_packagesrn   ro   r   	from_coder8   	from_nameto_codeto_namer   rp   r   ru   r   r   r   r   rC   r^   r_   valuesr   r:   rr   r   LIBRETRANSLATEr   LibreTranslateAPI	languagesLibreTranslateTranslationOPENAI	OpenAIAPIopenai_api_keyr   	enumeraterb   popr   )packageslanguage_of_coderx   r9   r:   containtranslation_to_addsaved_cacheer  languageidentity_translationkeep_adding_translationstranslationtranslation_2composite_translationlibretranslate_apisupported_languageslr   en_indexr   englishr   s                          @r   r  r    si    		"#("8"8"@"@@113 >IJ  6C}}$442:3==#--2X /{{"2208ckk0R -(7I&{{3G ]]O1S[[M:K=?STG 7|q 
):*9sC*& #5"6K.9K+5G2(//<
 &-QZ%B%B"''../AB##**+=>; > )0023	 "H#6x#@ &&--.BC$$++,@A " "H'+$**+0(#+#=#=K)4)<)<)N)N#33M4I4IJR 8<44H +51 %66==>ST)11AAHH 5 *O $> +* "$ 
	 	 H$:$:$I$I	I!335((* 	 >QQ=PXai63=P	Q"I$7(: ++22;?''..{; % # 
	 	 H$:$:$A$A	A(?(?@dI.y0IJ	"I$0^T++22;?''..{; % # H +8==D H , G--)NN'N(I	)	W + ,[MA3?@V Rs   AST
T&S>>Tc                     [        5       $ )z+Deprecated 1.2, use get_installed_languages)r  r4   r   r   load_installed_languagesr3  s  s    "$$r   c                H   ^  [        [        U 4S j[        5       5      S5      $ )zGets a language object from a code

An exception will be thrown if an installed language with this
code can not be found.

Args:
    code: The ISO 639 code of the language

Returns:
    The language object
c                "   > U R                   T:H  $ r   rb   )rj   rb   s    r   rl   (get_language_from_code.<locals>.<lambda>  s    4r   N)nextro   r  r6  s   `r   get_language_from_coder9  x  s     /1H1JKTRRr   c                P    [        U 5      n[        U5      nUR                  U5      $ )aD  Gets a translation object from codes for from and to languages

An exception will be thrown if an installed translation between the from lang
and to lang can not be found.

Args:
    from_code: The ISO 639 code of the source language
    to_code: The ISO 639 code of the target language

Returns:
    The translation object
)r9  rr   )r  r  r9   r:   s       r   get_translation_from_codesr;    s)     'y1I$W-G$$W--r   c                :    [        X5      nUR                  U 5      $ )zTranslate a string of text

Args:
    q: The text to translate
    from_code: The ISO 639 code of the source language
    to_code: The ISO 639 code of the target language

Returns:
    The translated text
)r;  r@   )qr  r  r*  s       r   r@   r@     s     -Y@K  ##r   rU   )rx   r   r?   r   ry   r   r   z#sbd.ISentenceBoundaryDetectionModelr=   rX   rT   rY   )rT   zlist[Language])rb   r   rT   zLanguage | None)r  r   r  r   rT   r6   )r=  r   r  r   r  r   rT   r   )+
__future__r   	functoolstypingr   r   r   argostranslater   r   r   r	   r
   argostranslate.modelsr   argostranslate.packager   argostranslate.sbdr   r   r   argostranslate.utilsr   r   r6   r8   ru   r   r   r   r   r  r   r   r   r   r2   	lru_cacher  r3  r9  r;  r@   r4   r   r   <module>rG     s8   "    " @ @ 0 * [ [ % 2C/ C/L) )X?$ ?$DJ, J$)+< )+X<$ <$~8 8* . 3; 3;v H	HH H 5	H
 H HV* * 24 . 3 n nb%
S.$$r   