
    Tiκ                     @   S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
  S SKJ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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!  S SK"J#r#  S SK$J%r%  S SK&J'r'  S SK(J)r)  S SK*J+r+J,r,J-r-J.r.J/r/J0r0J1r1  S SK2J3r3J4r4J5r5J6r6J7r7  S SK8J9r9J:r:J;r;J<r<J=r=J>r>J?r?J@r@  SSKAJBrBJCrC  SSKDJBrE  \F" S5      /\G" \H" SS5      5      -   \G" \H" SS5      5      -   \G" \H" SS5      5      -   \G" \H" SS5      5      -   \G" \H" SS5      5      -   \G" \H" S S!5      5      -   \G" \H" S"S#5      5      -   \G" \H" S$S%5      5      -   \G" \H" S&S'5      5      -    V s0 s H  o S(_M     sn rIS) rJS* rKS+ rLS, rMS- rNS. rOS/ rPS6S0 jrQS1 rRS2 rSS3 rTS4 rUS5 rVgs  sn f )7    N)datetime)wraps)unescape)default_timer)get_supported_formats)
	BlueprintFlaskResponseabortjsonifyrender_templaterequest	send_fileurl_formake_response)Babel)swagger)get_swaggerui_blueprint)translate_html)HTTPException)	http_date)secure_filename)floodremove_translated_files	schedulersecretsecuritystoragecache)	model2iso	iso2modeldetect_languagesimprove_translation_formattingget_language_with_fallback)__lazyget_alternate_locale_linksget_available_locale_codesget_available_localesgettext_escapedgettext_html	lazy_swag   )DatabaseRemoteDatabase)r.    i  iO i  i i i i &  i&  i '  i'  i   i  i  i i i i   i   Tc                  j    [        [        R                  R                  [        R                  R	                  [
        5      SS5      5       n U R                  5       R                  5       sS S S 5        $ ! , (       d  f       O= f! [         a     Of = f SSK	J
n  U" S5      $ ! [         a     gf = f)Nz..VERSIONr   )versionlibretranslate?)openospathjoindirname__file__readstripOSErrorimportlib.metadatar3   	Exception)fr3   s     d/var/www/html/backend/Backoffice_Marketplace/venv/lib/python3.13/site-packages/libretranslate/app.pyget_versionrC   7   s    "'',,rwwx8$	JKq668>># LKK .'(( sB   AB A5+	B 5
B?B B 
BBB% %
B21B2c                      [         R                  R                  [        R                  " 5       S5      n [         R                  R                  U 5      (       d  [         R                  " U 5        U $ )Nzlibretranslate-files-translate)r7   r8   r9   tempfile
gettempdirisdirmkdir)
upload_dirs    rB   get_upload_dirrJ   G   sE    h1135UVJ77==$$
    c                      [         R                  (       a"  [        [         5      n U R                  S5      nU$ [         R                  R                  S5      nU$ )Napi_keyr   is_jsonget_json_dictgetvaluesjsonaks     rB   get_req_api_keyrV   P   sC    W%XXi  I ^^	*IrK   c                      [         R                  (       a"  [        [         5      n U R                  S5      nU$ [         R                  R                  S5      nU$ )Nr   rN   rS   s     rB   get_req_secretrX   Y   sC    W%XXh I ^^)IrK   c                 x    U R                   " 5       n[        U[        5      (       d  [        S[	        S5      S9  U$ )N  zInvalid JSON formatdescription)get_json
isinstancedictr   r%   )r   ds     rB   rP   rP   c   s2    Aacq!678HrK   c                      [         R                  R                  S5      (       a6  [         R                  R                  S5      S   R                  S5      S   n U $ [         R                  =(       d    Sn U $ )NzX-Forwarded-Forr   ,	127.0.0.1)r   headersgetlistsplitremote_addr)ips    rB   get_remote_addressri   j   s`    011__$$%67:@@EaH I   /KIrK   c                      [         R                  R                  SS5      [         R                  R                  SS5      -   $ )N
User-Agent Cookie)r   rd   rQ    rK   rB   get_fingerprintro   r   s0    ??|R07??3F3FxQS3TTTrK   c                     U nU(       a-  [        5       nU(       a  UR                  U5      nUb  US   U-  n[        XC-  5      $ Nr   )rV   lookupint)default_limitapi_keys_dbdb_multiplier
multiplier	req_limitrM   api_key_limitss          rB   get_req_limitsrz   v   sH    I!#(//8N)*1-=	y%&&rK   c                 x    U nU(       a0  [        5       nU(       a  UR                  U5      nUb  US   b  US   nU$ )Nr-   )rV   rr   )rt   ru   
char_limitrM   ry   s        rB   get_char_limitr}      sF    J!#(//8N)!!$0!/!2JrK   c                 4  ^ ^^ T R                   mTS:X  a  SmUU4S jnUU 4S jnUU 4S jnU/nT R                  S:  a7  [        ST R                  S-   5       H  nUR	                  U" U5      5        M     T R
                  S:  a  UR	                  U5        U$ )	Nl   a$c                  "   > S[        TT 5      -  $ )Nz%s per minute)rz   )ru   default_req_limits   rB   minute_limits(get_routes_limits.<locals>.minute_limits   s    0A;!OOOrK   c                    >^  UUU 4S jnU$ )Nc                     > STS-
  -  n SR                  [        TR                  T-  T[        [        R
                  R                  SS5      T-  5      U 5      T5      $ )Ng      ?r-   z{} per {} hourLT_HOURLY_REQ_LIMIT_MULTIPLIER<   )formatrz   hourly_req_limitrs   r7   environrQ   )decayru   argsns    rB   func6get_routes_limits.<locals>.hourly_limits.<locals>.func   s    AE?%!((8M8MPQ8QS^`cdfdndndrdr  tT  VX  eY  \]  e]  a^  `e  *f  hi  j  jrK   rn   )r   r   ru   r   s   ` rB   hourly_limits(get_routes_limits.<locals>.hourly_limits   s    	j rK   c                     > S[        TR                  T [        [        R                  R                  SS5      5      5      -  $ )Nz
%s per dayLT_DAILY_REQ_LIMIT_MULTIPLIERi  )rz   daily_req_limitrs   r7   r   rQ   )ru   r   s   rB   daily_limits'get_routes_limits.<locals>.daily_limits   sR    nT-A-A;PSTVT^T^TbTb  dC  EI  UJ  QK  L  L  	LrK   r   r-      )rx   r   rangehourly_req_limit_decayappendr   )r   ru   r   r   r   resr   r   s   ``     @rB   get_routes_limitsr      s    B)PL /Cq Q33a78!

=#$ 9 a

< JrK   c                     [        US15      nUR                  nU  Vs/ s H  oDU;   a  M
  U" U5      (       a  M  UPM     sn$ s  snf )Nrl   )setadd)seqextraseenseen_addxs        rB   filter_uniquer      s=    r{DxxH=s!9AAs===s   	AAAc                     [        U [        5      (       a  [        S U  5       5      $ U  H  n[        U5      [        ;  d  M    g   g)Nc              3   8   #    U  H  n[        U5      v   M     g 7fN)detect_translatable).0ts     rB   	<genexpr>&detect_translatable.<locals>.<genexpr>   s     9y!"1%%ys   TF)r^   listanyordemojis)	src_textschs     rB   r   r      s>    	4  9y999bGv 
 
rK   c           	        ^ ^%^&^'^(^)^*^+^,^-^.^/^0^1^2^3^4^5^6 SSK Jn  U" T R                  T R                  T R                  5        SSKJn  T R                  S-   m5Sn[        S[        5      n[        R                  " T R                  5        [        R                  " T R                  5      m6T R                  (       d  [         R                  " [#        5       5        U" 5       m10 m.T1 HI  n[%        UR&                   Vs/ s H  ofR(                  R*                  PM     sn5      T.UR*                  '   MK     T R,                  S:X  a  [/        S[0        4S[3        S	5      S
.5      m(OA[5        [7        T1 Vs/ s H   ofR*                  T R,                  :X  d  M  UPM"     sn5      S 5      m(T(c  T1S   m([9        T15      S:  a  T1S   OT1S   m0T R:                  S:X  a  U-U0U14S jnUm)OL[5        [7        T1 Vs/ s H   ofR*                  T R:                  :X  d  M  UPM"     sn5      S 5      m/T/c  T0m/U/4S jm)/ m*[=        5        H'  nUR>                   H  n	T*RA                  U	5        M     M)     S m'T RB                  S:  d1  T RD                  (       d   T RF                  S:  d  T RH                  S:  a  S m'T RD                  (       a;  T RJ                  (       a  [M        T RJ                  5      O[O        T RP                  5      m'SSK)J*n
  U 4S jnU 4S jnU
" U" 5       [W        T T'5      T RX                  S USS9nOSSK-J*n
  U
" 5       nS[\        R^                  Ra                  SS5      ;  a  [b        R                  " T 5        [d        R                  " T 5        [f        R                  " T 5        S m2S m+T Rh                  (       Ga%  [\        R^                  Ra                  S5      c  [\        Rj                  Rm                  [\        Rj                  Ro                  SS5      5      n[\        Rj                  Rq                  U5      (       d  [\        Rr                  " U5        U[\        R^                  S'   SSK:J;m%J<m&J=nJ>nJ?m,J@m3  UR                  S5      UR                  U%U&U U,U34S j5       5       nU" SS / S!Q5      m2T2R                  S"S#S$S5        U" S%S&/ S'QS(S)9m+T+R                  S"S$S5        U'U U+U24S* jnUR                  S+5      S, 5       nUR                  S-5      S. 5       nUR                  S/5      S0 5       nUR                  S15      S2 5       nUR                  S35      UR                  U U-U54S4 j5       5       nUR                  S55      UR                  U 4S6 j5       5       nURa                  S75      UR                  U.U14S8 j5       5       nURa                  S95      UR                  S: 5       5       nUR                  S; 5       nUR                  S"5      UU'U U1U64S< j5       5       nUR                  S=5      UU'U U*U14S> j5       5       nURa                  S?5      S@[        4U 4SA jj5       nUR                  SB5      USC 5       5       nUR                  SD5      UR                  U U(U)U*4SE j5       5       n UR                  SF5      U 4SG j5       n![        [        5      n"SHU"R                  SI'   T R                  (       a  SJU"R                  SK'   U"R                  U5        UR                  U"5        [        U"5      m4T R                  S:w  a  T R                  OS3T4SL'   [        5       T4SM   SN'   SOT4SM   SP'   SQT4SM   SR'   SSST0T4SM   SU'   U"R                  U5      UR                  U44SV j5       5       n#SWU"R                  SX'   S^SY jm-[        U"T-SZ9  U"R                  R                  R                  [        [        S[9  [        T5T R                  U-   5      n$SU$l        U"R                  U$5        [\        R^                  Ra                  S\5      b!  [        S]5        [        R                  " S5        U"$ s  snf s  snf s  snf )_Nr   )boot)load_languagesz/docsz/speczMain appautoobjzAuto Detectcodenamer   r-   localec            	         > T" 5       n [        [        T Vs/ s H  oR                  U :X  d  M  UPM     sn5      S 5      nUc  TnU$ s  snf r   )nextiterr   )locllanguage_target
get_localelanguage_target_fallback	languagess      rB   resolve_language_locale+create_app.<locals>.resolve_language_locale   sM    # y:y!FFcMAy:;T/ $6O 
 	 ;s
   AAc                     > T $ r   rn   )r   s   rB   <lambda>create_app.<locals>.<lambda>   s    rK   )Limiterc                     > [        [        SS5      n TR                  S:  aE  [        U [	        [
        R                  " [        [        SS5      TR                  -  5      5      5      $ U $ )Nreq_costr-   r   duration)getattrr   req_time_costmaxrs   mathceil)r   r   s    rB   limits_costcreate_app.<locals>.limits_cost
  sT    Wj!4(!#xTYYww
A/NQUQcQc/c%d!effOrK   c                  <   > TR                   (       a  S n U $ [        $ )Nc                  <    [        5       n U (       a  U $ [        5       $ r   )rV   ri   )rU   s    rB   r   5create_app.<locals>.get_limits_key_func.<locals>.func  s    "$bR7#5#77rK   )api_keysri   )r   r   s    rB   get_limits_key_func'create_app.<locals>.get_limits_key_func  s    ]]8 K%%rK   c                     g)NTrn   )reqs    rB   r   r      s    4rK   zmoving-window)key_funcdefault_limitsstorage_uridefault_limits_deduct_whendefault_limits_coststrategygunicornSERVER_SOFTWARErl   PROMETHEUS_MULTIPROC_DIRdb
prometheus)CONTENT_TYPE_LATESTCollectorRegistryGaugeSummarygenerate_latestmultiprocessz/metricsc                     > TR                   (       aE  [        R                  R                  S5      n U STR                   -   :w  a  [	        S[        S5      S9  T" 5       nTR                  U5        [        T" U5      TS9$ )NAuthorizationzBearer i  Unauthorizedr[   )mimetype)metrics_auth_tokenr   rd   rQ   r   r%   MultiProcessCollectorr
   )authorizationregistryr   r   r   r   r   s     rB   prometheus_metrics&create_app.<locals>.prometheus_metrics;  sm     ""!//--o>-i$*A*AAA#1^#45$&**841<OPPrK   ,libretranslate_http_request_duration_secondszTime spent on request)endpointstatus
request_iprM   z
/translate   rc   &libretranslate_http_requests_in_flightzActive requests)r   r   rM   livesum)multiprocess_modec                    >^ ^ [        T 5      UUU 4S j5       mTR                  (       a  [        T5      UUU4S j5       nU$ [        T5      U4S j5       nU$ )Nc                    > [        5       n[        R                  " U5      (       a  [        S[	        S5      S9  T	R
                  (       Ga  [        5       nU(       a'  TR                  U5      c  [        S[	        S5      S9  GOSnTR                  U5      S L nT	R                  (       aM  U(       aF  [        R                  " T	R                  [        R                  R                  SS5      5      (       d  Sn[        5       nT	R                  (       aw  U(       ap  [         R"                  " U5      (       dU  Sn[         R$                  " U5      (       a8  [        ['        [)        [         R*                  " 5       / S	S
S.S.5      S5      5        T	R,                  (       a-  U(       a&  [        R.                  " U[1        5       5      (       a  SnT	R2                  (       a	  U(       a  SnU(       aX  [	        S5      nT	R4                  (       a  [	        ST	R4                  S9n[        SUS9  [        R6                  " [        5       5        T
" U 0 UD6$ )N  z"Too many request limits violationsr[   zInvalid API keyFOriginrl   Td   en
confidencelanguage)translatedTextalternativesdetectedLanguager   z4Please contact the server operator to get an API keyzVisit %(url)s to get an API key)urlrZ   )ri   r   	is_bannedr   r%   r   rV   rr   require_api_key_originrematchr   rd   rQ   rX   require_api_key_secretr   secret_matchsecret_bogus_matchr   r   	get_emojirequire_api_key_fingerprintfingerprint_mismatchro   under_attackget_api_key_linkreport)akwrh   rU   need_keykey_missing
req_secretr\   ru   r   rA   s           rB   r   .create_app.<locals>.access_check.<locals>.funcN  s   #%Br""cq)M'NO}}}$&+,,R08$%&7$8
 #( + 2 22 6$ >+11% hht'B'BGOODWDWX`bdDeff#H-/*11#"//
;;#H00<<M'*0*:*:*<(*<?T,S3 + 	   66#11"o6GHH!%h&&;#H"#$Z"[K,,&'(ItOdOd&e$/ LL!3!56a;2;rK   c                    > [        5       nSn[        5       n[        5       =(       d    SnT	R                  [        R
                  XE5      n UR                  5         T" U 0 UD6[        [        5       U-
  S5      [        l        T
R                  [        R
                  X4U5      R                  [        R                  5        UR                  5         $ ! [         a  nUR                  nUS:X  a  SnUeS nAff = f! [        [        5       U-
  S5      [        l        T
R                  [        R
                  X4U5      R                  [        R                  5        UR                  5         f = f)Nr   rl   r   r  )r   ri   rV   labelsr   r8   incr   r   observedecr   r   )r  r  start_tr   rh   rU   ger   gauge_requestmeasure_requests           rB   measure_func6create_app.<locals>.access_check.<locals>.measure_func  s   %gf%'b"$*b&&w||R<aQ~"~ $'}'@!#D &&w||VDLLWM]M]^ # S="	 $'}'@!#D &&w||VDLLWM]M]^s%   C 
C7C22C77C: :A0E*c                     > [        5       n T" U 0 UD6[        [        5       U-
  S5      [        l        $ ! [        [        5       U-
  S5      [        l        f = frq   )r   r   r   r   )r  r  r&  r   s      rB   	time_func3create_app.<locals>.access_check.<locals>.time_func  sH    #oGC1^^!$]_w%>!Bg]_w%>!Bgs	   5 "A)r   metrics)rA   r+  r.  r   ru   r   r)  r*  s   `  @rB   access_check create_app.<locals>.access_checkM  sa    	q6	 
6	p <<; $ 
;C C 
rK   rZ   c                 F    [        S[        U R                  5      05      S4$ )NerrorrZ   r   strr\   r(  s    rB   invalid_apicreate_app.<locals>.invalid_api       Q]]!345s::rK     c                 F    [        S[        U R                  5      05      S4$ )Nr4  r;  r5  r7  s    rB   server_error create_app.<locals>.server_error  r:  rK     c                     [         R                  " [        5       5        [        S[	        S5      S-   [        U R                  5      -   05      S4$ )Nr4  z	Slowdown:r0   r?  )r   r  ri   r   r%   r6  r\   r7  s    rB   slow_down_error#create_app.<locals>.slow_down_error  s>    ')*;#!5AMM8J!JKLcQQrK   r  c                 F    [        S[        U R                  5      05      S4$ )Nr4  r  r5  r7  s    rB   deniedcreate_app.<locals>.denied  r:  rK   /c                  8  > TR                   (       a  [        S5        [        TR                  5      S:  a  TR                  O[        R
                  R                  S5      n S nU (       a   U [        TR                  (       + 5      ;   a  U n[        [        STR                  TR                  TR                  [        R                  R                  S5      S L[!        5       T[#        [%        TR                  (       + 5       Vs/ s H  o"S   ['        US   5      S.PM     snS	 S
9T" U5      [)        5       TR*                  TR,                  TR.                  S95      nTR0                  (       a  UR3                  SS5        Ub  UR3                  SU 5        U$ s  snf )N  r   langz
index.htmlLT_WEBr   r   r   c                     U S   $ )Nr   rn   )ss    rB   r   +create_app.<locals>.index.<locals>.<lambda>  s      TU  V\  T]rK   )key)frontendTimeoutr   r  web_versionr3   swagger_urlavailable_localescurrent_localealternate_localesr  hide_apifrontend_titler1preferred_lang)disable_web_uir   lenfrontend_languager   r   rQ   r(   debugr   r   frontend_timeoutr   r  r7   r   rC   sortedr)   r&   r'   r  rU  rV  r  
set_cookie)langcodelang_cookier   respr   r   rQ  s       rB   indexcreate_app.<locals>.index  ss    #J-01G1G-H1-L4))RYR^R^RbRbciRj$>4::~$NN"K_ 11]]!22

x0<M#$]rw{  xB  xB  tB  ^C  &D  ^CXYy%&	BR&S  ^C  &D  J]  ^%k28:**]]..
   &&
//#s
#"
//*H
5 &Ds   Fz
/js/app.jsc                    > TR                   (       a  [        S5        Sn SnTR                  (       ax  [        R                  " 5       nS[
        R                  ;   a:  [
        R                  R                  S5      (       a  [        R                  " 5       n O[        R                  " 5       n [        [        STR                  TR                  U UTR                  S9SS9nTR                  (       aX  [!        ["        R$                  " 5       5      UR                  S	'   S
UR                  S'   SUR                  S'   SUR                  S'   U$ )NrH  rl   rk   rW  zapp.js.template)
url_prefixr  
api_secretbogus_api_secretr  z%application/javascript; charset=utf-8)content_typezLast-ModifiedzIno-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0zCache-Controlzno-cachePragmaz-1Expires)rZ  r   r  r   get_bogus_secret_b64r   rd   cookiesrQ   get_current_secret_jsget_bogus_secret_jsr
   r   rg  r  r  r   r   now)rh  ri  responser   s      rB   appjscreate_app.<locals>.appjs  s	    
		c
j		$	$!6687??*w/B/B3/G/G335*113*/*;!22!-**,
 ;bch 
	$	$,5hlln,E),w)%/"&*#orK   z
/languagesc                     > [        T V s/ s HR  n [        U R                  5      [        U R                  5      [        TR                  U R                  / 5      5      S.PMT     sn 5      $ s  sn f )aN  
Get Supported Languages
---
tags:
  - translate
responses:
  200:
    description: List of supported languages
    schema:
      id: languages
      type: array
      items:
        type: object
        description: Supported language
        properties:
          code:
            type: string
            description: Language code
          name:
            type: string
            description: Human-readable language name (in English)
          targets:
            type: array
            items:
              type: string
            description: Supported target language codes
)r   r   targets)r   r    r   r&   r   rQ   )r   language_pairsr   s    rB   langscreate_app.<locals>.langs  sj    <  $-. $-a "+166!2!&qvv$-n.@.@.L$M $-. / 	/ .s   AA+z/healthc                      [        SS05      $ )a  
Health Check
---
tags:
  - misc
responses:
  200:
    description: Service is healthy
    schema:
      id: health-response
      type: object
      properties:
        status:
          type: string
          description: Health status
          example: ok
r   ok)r   rn   rK   rB   healthcreate_app.<locals>.health  s    ( $'((rK   c                 V   U R                   R                  SS5        U R                   R                  SS5        U R                   R                  SS5        U R                   R                  SS5        U R                   R                  S	S
5        U R                   R                  SS5        U $ )NzAccess-Control-Allow-Origin*zAccess-Control-Allow-HeaderszAuthorization, Content-TypezAccess-Control-Expose-Headersr   zAccess-Control-Allow-Methodsz	GET, POSTz Access-Control-Allow-CredentialstruezAccess-Control-Max-Agei ^ )rd   r   )rr  s    rB   after_request!create_app.<locals>.after_request3  s    :C@*,I	
 	<oN;[I?H57HIrK   c                  n  >^^ [         R                  (       a  [        [         5      n U R                  S5      n[	        U R                  S5      5      m[	        U R                  S5      5      mU R                  S5      n[        U R                  SS5      5      nO[         R                  R                  S5      n[	        [         R                  R                  S5      5      m[	        [         R                  R                  S5      5      m[         R                  R                  S5      n[         R                  R                  SS5      nU(       d  [        S[        SSS	9S
9  T(       d  [        S[        SSS	9S
9  T(       d  [        S[        SSS	9S
9   [        S[        U5      5      nTR                  S:w  a-  UTR                  :  a  [        S[        SSTR                  S9S
9  [         R                  (       d  SR                  UR                  5       5      n[        TR                  T5      n[!        U["        5      nU(       aH  TR$                  S:w  a8  ['        U5      nTR$                  U:  a  [        S[        SUTR$                  S9S
9  U(       a  UOU/n[)        5       nSn	TR+                  U5      (       a$  TR-                  UTTX#5      u  pU
b  [/        U
SSS9$ US:w  a6  U H0  n['        U5      U:  d  M  [        S[        S['        U5      US9S
9  M2     U(       a  [        S['        U5      5      [         l        [3        U5      nU(       aB  TS:X  a   [5        U5      nUS   n[7        US   T5      nO4STS.n[9        U4S jT 5       S5      nOSSS.n[9        S T 5       S5      nUc  [        S[        STS 9S
9  [9        U4S! jT 5       S5      nUc  [        S[        STS 9S
9  U(       d  S"nUS#;  a  [        S[        S$US%9S
9   U(       Ga  / n/ nU GHB  nUR;                  U5      nUcO  [        S[        S&[=        UR>                  5      UR@                  [=        UR>                  5      UR@                  S'9S
9  U(       a  US(:X  a!  [C        [E        [G        UU5      5      5      n/ nOURI                  XS-   5      n[C        [K        UUS   RL                  5      5      n[O        [Q        S['        U5      5       Vs/ s H%  n[C        [K        UUU   RL                  5      5      PM'     snU5      nOUn/ nURS                  U5        URS                  U5        GME     S)U0nTS:X  a  [U        U5      /['        U5      -  US*'   US:  a  UUS'   GO?UR;                  U5      nUcO  [        S[        S&[=        UR>                  5      UR@                  [=        UR>                  5      UR@                  S'9S
9  U(       a  US(:X  a!  [C        [E        [G        UU5      5      5      n/ nOURI                  XS-   5      n[C        [K        UUS   RL                  5      5      n[O        [Q        S['        U5      5       Vs/ s H%  n[C        [K        UUU   RL                  5      5      PM'     snU5      nOUn/ nS)U0nTS:X  a  [U        U5      US*'   US:  a  UUS'   U	b  TRW                  U	U5        [Y        U5      $ ! [         a    [        S[        SSS	9S
9   GNlf = fs  snf s  snf ! [Z         a  nUeSnAff = f)+aU  
Translate Text
---
tags:
  - translate
parameters:
  - in: formData
    name: q
    schema:
      oneOf:
        - type: string
          example: Hello world!
        - type: array
          example: ['Hello world!']
    required: true
    description: Text(s) to translate
  - in: formData
    name: source
    schema:
      type: string
      example: en
    required: true
    description: Source language code or "auto" for auto detection
  - in: formData
    name: target
    schema:
      type: string
      example: es
    required: true
    description: Target language code
  - in: formData
    name: format
    schema:
      type: string
      enum: [text, html]
      default: text
      example: text
    required: false
    description: >
      Format of source text:
       * `text` - Plain text
       * `html` - HTML markup
  - in: formData
    name: alternatives
    schema:
      type: integer
      default: 0
      example: 3
    required: false
    description: Preferred number of alternative translations
  - in: formData
    name: api_key
    schema:
      type: string
      example: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    required: false
    description: API key
responses:
  200:
    description: Translation
    schema:
      id: translate
      type: object
      properties:
        translatedText:
          oneOf:
            - type: string
            - type: array
          description: Translated text(s)
        detectedLanguage:
          oneOf:
            - type: object
              properties:
                confidence:
                  type: number
                  format: float
                  minimum: 0
                  maximum: 100
                  description: Confidence value
                  example: 100
                language:
                  type: string
                  description: Language code
            - type: array
              items:
                type: object
                properties:
                  confidence:
                    type: number
                    format: float
                    minimum: 0
                    maximum: 100
                    description: Confidence value
                    example: 100
                  language:
                    type: string
                    description: Language code
        alternatives:
          oneOf:
            - type: array
              items:
                type: string
            - type: array
              items:
                type: array
                items:
                  type: string
          description: Alternative translations
      required:
        - translatedText
  400:
    description: Invalid request
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
  500:
    description: Translation error
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
  429:
    description: Slow down
    schema:
      id: error-slow-down
      type: object
      properties:
        error:
          type: string
          description: Reason for slow down
  403:
    description: Banned
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
qsourcetargetr   r  r   rZ   +Invalid request: missing %(name)s parameterr   r[   z3Invalid request: %(name)s parameter is not a numberr   z8Invalid request: %(name)s parameter must be <= %(value)s)r   value
zBInvalid request: request (%(size)s) exceeds text limit (%(limit)s))sizelimitNr   zapplication/json)r   r   r-   r   r	  g      Y@r  c              3   J   >#    U  H  oR                   T:X  d  M  Uv   M     g 7fr   r   r   r   source_langs     rB   r   0create_app.<locals>.translate.<locals>.<genexpr>   s     M)Qvv7Lqq)   #	#g        r  c              3   H   #    U  H  oR                   S :X  d  M  Uv   M     g7f)r  Nr  )r   r   s     rB   r   r  #  s     Bi66T>11is   "	"%(lang)s is not supportedrI  c              3   J   >#    U  H  oR                   T:X  d  M  Uv   M     g 7fr   r  r   r   target_langs     rB   r   r  (       GIq;1FIr  text)r  htmlz"%(format)s format is not supported)r   zV%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s))tnametcodesnamescoder  r
  r  ).r   rO   rP   rQ   r!   rs   rR   r   r%   r   
ValueErroralternatives_limitr9   
splitlinesr}   r|   r^   r   batch_limitr[  rV   should_checkhitr
   r   r   r"   r$   r   get_translationr&   r   r   r   r6  r   
hypothesesr#   r  r   r   r   r    r   r   r@   ) rT   r  text_formatnum_alternativesr|   batch
batch_sizer   rU   	cache_keyr  r  translatablecandidate_langsdetected_src_langsrc_langtgt_langbatch_resultsbatch_alternatives
translatortranslated_textr  r  iresultr(  r  r  ru   r   r   trans_caches                              @@rB   	translatecreate_app.<locals>.translate?  s   l ?? )DA#DHHX$67K#DHHX$67K((8,K"488NA#>?""3'A#GNN$6$6x$@AK#GNN$6$6x$@AK!..,,X6K&~~11.!D#1%RY\#]^#1%RYa#bc#1%RYa#bc	r"1c*:&;< ""b(-=@W@W-W#1%_ft  }A  }T  }T  $U  V 		!,,.)A#DOO[A
1d#T%%+QJ*, !"fmw  @D  @P  @P  !Q
 AQC		##B''&??9k;P[n.)_C6HII!t9z)$%&jqtuyqz  CM  %N " "1c!f~G*95F" 0 ;o"1!"434Ej4QS\]h16K"PM)MtTh-0dC
BiBDI(#1%@{#STGIGN#1%@k#RS K..#1%IR]#^_<	V "%'"D!)!9!9(!CJ!)cq  2J  RW  X`  Xe  Xe  Rf  nv  n{  n{  CH  IQ  IV  IV  CW  _g  _l  _l  0m  n#$.,4S
TX9Y5Z,[/)+,'1'<'<TVWCW'X*,45STXZdefZgZmZm5n,o/)6  FK  LM  OR  S]  O^  F_  8`  F_  ABA_`dfpqrfsfyfyAz8{  F_  8`  bq  *r,(,o%'l!((9&--l;% ( +M:&(2;<M2N1ORUVWRX1XF-.#a'-?F>*%55h?
%#1  .F  NS  T\  Ta  Ta  Nb  jr  jw  jw  D  EM  ER  ER  S  [c  [h  [h  ,i  j F*(0^JPQ5R1S(To%'l#-#8#8q<P#Qj(01OPQS]^_S`SfSf1g(ho%2  D  EF  HK  LV  HW  X  4Y  XyzH=[\]_ijk_l_r_r=s4t  X  4Y  [j  &kl$%/!#,*O<&(1:;L1MF-.#a'-9F>*$	626?"A  	r#1%Zao#pq	rn 8`4 4Y   	VG	VsK   [6 C5\# ,\
1E	\# :,\&A\# 6\\
\# #
\4-\//\4z/translate_filec            	      d  >^^ TR                   (       a  [        S[        S5      S9  [        [        R
                  R                  S5      5      m[        [        R
                  R                  S5      5      m[        R                  S   n [        TR                  T5      nU (       d  [        S[        SSS	9S9  T(       d  [        S[        SSS	9S9  T(       d  [        S[        SSS	9S9  U R                  S
:X  a  [        S[        S5      S9  [        R                  R                  U R                  5      S   T;  a  [        S[        S5      S9  [        U4S jT 5       S5      nUc  TS:w  a  [        S[        STS9S9  [        U4S jT 5       S5      nUc  [        S[        STS9S9   [        [         R"                  " 5       5      S-   [%        U R                  5      -   n[        R                  R'                  [)        5       U5      nU R+                  U5        US:  a>  [-        S[/        [        R                  R1                  U5      U-  5      5      [        l        TS:X  aM  [4        R6                  " U5      n[9        U5      nUS   n[;        US   T5      nUc  [        S[        SUS   S9S9  [4        R<                  " UR?                  U5      U5      n	[        R                  RA                  U	5      n
[C        S[E        SU
SS905      $ ! [F         a  n[        SUS9   SnAgSnAff = f)a^  
Translate a File
---
tags:
  - translate
consumes:
 - multipart/form-data
parameters:
  - in: formData
    name: file
    type: file
    required: true
    description: File to translate
  - in: formData
    name: source
    schema:
      type: string
      example: en
    required: true
    description: Source language code  or "auto" for auto detection
  - in: formData
    name: target
    schema:
      type: string
      example: es
    required: true
    description: Target language code
  - in: formData
    name: api_key
    schema:
      type: string
      example: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    required: false
    description: API key
responses:
  200:
    description: Translated file
    schema:
      id: translate-file
      type: object
      properties:
        translatedFileUrl:
          type: string
          description: Translated file url
  400:
    description: Invalid request
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
  500:
    description: Translation error
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
  429:
    description: Slow down
    schema:
      id: error-slow-down
      type: object
      properties:
        error:
          type: string
          description: Reason for slow down
  403:
    description: Banned
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
r  .Files translation are disabled on this server.r[   r  r  filerZ   r  r  rl   zInvalid request: empty filer-   z*Invalid request: file format not supportedc              3   J   >#    U  H  oR                   T:X  d  M  Uv   M     g 7fr   r  r  s     rB   r   5create_app.<locals>.translate_file.<locals>.<genexpr>  r  r  Nr   r  r  c              3   J   >#    U  H  oR                   T:X  d  M  Uv   M     g 7fr   r  r  s     rB   r   r    r  r  .r   r	  translatedFileUrlzMain app.download_fileT)filename	_externalr;  )$disable_files_translationr   r%   r!   r   formrQ   filesr}   r|   r  r7   r8   splitextr   r6  uuiduuid4r   r9   rJ   saver   rs   getsizer   argostranslatefiles	get_textsr"   r$   translate_filer  basenamer   r   r@   )r  r|   r  r  r  filepathr   r  r  translated_file_pathtranslated_filenamer(  r  r  ru   r   %frontend_argos_supported_files_formatr   s               @@rB   r  "create_app.<locals>.translate_fileq  s   h ))#1%U#VW 0 0 :; 0 0 :;}}V$#DOO[A
#1%RY_#`a#1%RYa#bc#1%RYa#bc==B#1%B#CD77DMM*1-5ZZ#1%Q#RSGIGNv 5#1%@{#STGIGN#1%@{#ST	&4::<(3.1OOHww||N$4h?HIIh A~#&q#bggooh.G*.T*U#V f$/99(C	"29"=$3A$6!56G
6SU^_##1-HO`akOl+mn#6#E#EhF^F^_gFhjr#s "$''"2"23G"H'1ITgsw)x 
  	&#1%	&s   /E!L 
L/
L**L/z /download_file/<string:filename>r  c                   > TR                   (       a  [        S[        S5      S9  [        R                  R                  [        5       U 5      n [        R                  " U[        5       5      n[        R                  R                  U5      (       a  Un[        R                  " 5       n[        US5       nUR                  UR                  5       5        SSS5        UR!                  S5        U R#                  S5      nUR%                  S5        SR                  U5      n['        US	US
9$ ! [        R                   a    [        S[        S5      S9   Nf = f! , (       d  f       N= f)z
Download a translated file
rZ   r  r[   zInvalid filenamerbNr   r  T)as_attachmentdownload_name)r  r   r%   r7   r8   r9   rJ   r   path_traversal_checkisfileSuspiciousFileOperationErrorioBytesIOr6   writer<   seekrf   popr   )r  r  checked_filepathreturn_datafodownload_filenamer   s         rB   download_file!create_app.<locals>.download_file  s   
 ))#1%U#VW77<< 0(;	:'<<X~GWXww~~.//+ jjl(D!Rbggi( "$NN3/a HH%67DHYZZ 44 	:#1%7#89	: "!s   AD, 5 E,'EE
E'z/detectc                     [         R                  (       a!  [        [         5      n U R                  S5      nO[         R                  R                  S5      nU(       d  [        S[        SSS9S9  [        [        [        U5      5      5      $ )a!  
Detect Language of Text
---
tags:
  - translate
parameters:
  - in: formData
    name: q
    schema:
      type: string
      example: What language is this?
    required: true
    description: Text to detect
  - in: formData
    name: api_key
    schema:
      type: string
      example: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    required: false
    description: API key
responses:
  200:
    description: Detections
    schema:
      id: detections
      type: array
      items:
        type: object
        properties:
          confidence:
            type: number
            format: float
            minimum: 0
            maximum: 100
            description: Confidence value
            example: 100
          language:
            type: string
            description: Language code
            example: en
  400:
    description: Invalid request
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
  500:
    description: Detection error
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
  429:
    description: Slow down
    schema:
      id: error-slow-down
      type: object
      properties:
        error:
          type: string
          description: Reason for slow down
  403:
    description: Banned
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
r  rZ   r  r  r[   )
r   rO   rP   rQ   rR   r   r%   r   r    r"   )rT   r  s     rB   detectcreate_app.<locals>.detect   sc    ` ?? )DA""3'A#1%RY\#]^y!1!!4566rK   z/frontend/settingsc                    > T" 5       n [        TR                  TR                  TR                  [	        TR                  =(       a    TR
                  5      TR                  TR                  (       + TR                  (       a  / OTTR                  [        TR                  5      S.U R                  [        U R                  5      S.S.S.5      $ )a  
Retrieve Frontend Settings
---
tags:
  - misc
responses:
  200:
    description: frontend settings
    schema:
      id: frontend-settings
      type: object
      properties:
        charLimit:
          type: integer
          description: Character input limit for this language (-1 indicates no limit)
        frontendTimeout:
          type: integer
          description: Frontend translation timeout
        apiKeys:
          type: boolean
          description: Whether the API key database is enabled.
        keyRequired:
          type: boolean
          description: Whether an API key is required.
        suggestions:
          type: boolean
          description: Whether submitting suggestions is enabled.
        supportedFilesFormat:
          type: array
          items:
            type: string
          description: Supported files format
        language:
          type: object
          properties:
            source:
              type: object
              properties:
                code:
                  type: string
                  description: Language code
                name:
                  type: string
                  description: Human-readable language name (in English)
            target:
              type: object
              properties:
                code:
                  type: string
                  description: Language code
                name:
                  type: string
                  description: Human-readable language name (in English)
r   )r  r  )	charLimitrO  apiKeyskeyRequiredsuggestionsfilesTranslationsupportedFilesFormatr	  )r   r|   r^  r   boolr  r  r  r   r&   r   )r  r   frontend_argos_language_sourcefrontend_argos_language_targetr  s    rB   frontend_settings%create_app.<locals>.frontend_settings{  s    r 56!__#'#8#8==#DMM$Qd6Q6QR#//(,(F(F$F.2.L.LRw !? C C %&D&I&I J
 !, 0 0 %k&6&6 7	
 	
rK   z/suggestc                  t  > TR                   (       d  [        S[        S5      S9  [        R                  (       af  [        [        5      n U R                  S5      nU R                  S5      n[        U R                  S5      5      n[        U R                  S5      5      nO[        R                  R                  S5      n[        R                  R                  S5      n[        [        R                  R                  S5      5      n[        [        R                  R                  S5      5      nU(       d  [        S[        S	SS
9S9  U(       d  [        S[        S	SS
9S9  U(       d  [        S[        S	SS
9S9  U(       d  [        S[        S	SS
9S9  [        5       R                  XX45        [        SS05      $ )u)  
Submit a Suggestion to Improve a Translation
---
tags:
  - misc
parameters:
  - in: formData
    name: q
    schema:
      type: string
      example: Hello world!
    required: true
    description: Original text
  - in: formData
    name: s
    schema:
      type: string
      example: ¡Hola mundo!
    required: true
    description: Suggested translation
  - in: formData
    name: source
    schema:
      type: string
      example: en
    required: true
    description: Language of original text
  - in: formData
    name: target
    schema:
      type: string
      example: es
    required: true
    description: Language of suggested translation
responses:
  200:
    description: Success
    schema:
      id: suggest-response
      type: object
      properties:
        success:
          type: boolean
          description: Whether submission was successful
  403:
    description: Not authorized
    schema:
      id: error-response
      type: object
      properties:
        error:
          type: string
          description: Error message
r  z(Suggestions are disabled on this server.r[   r  rL  r  r  rZ   r  r  successT)r  r   r%   r   rO   rP   rQ   r!   rR   SuggestionsDatabaser   r   )rT   r  rL  r  r  r   s        rB   suggestcreate_app.<locals>.suggest  sE   p #1%O#PQ?? )DAA#DHHX$67K#DHHX$67K""3'A""3'A#GNN$6$6x$@AK#GNN$6$6x$@AK#1%RY\#]^#1%RY\#]^#1%RYa#bc#1%RYa#bc!!!A	4())rK   FJSON_AS_ASCIITTEMPLATES_AUTO_RELOADbasePathinfor3   LibreTranslatetitlez-Free and Open Source Machine Translation API.r\   r   zAGPL-3.0licensec                  ,   > [        [        T 5      5      $ r   )r   r,   )swags   rB   speccreate_app.<locals>.spec0  s     y''rK   localesBABEL_TRANSLATION_DIRECTORIESc                 x   [        5       n[        R                  R                  S5      nU(       a  X!;   a  U$ [        R                  R                  S5      nU(       a  X1;   a  U$ U c  [        R
                  R                  S5      n U (       a  X;   a  U $ [        R                  R                  [        5       5      $ )NzX-Override-Accept-LanguagerI  rY  )r(   r   rd   rQ   r   rn  accept_languages
best_match)cookieavailable_codesoverride_langqlangs       rB   r   create_app.<locals>.get_locale7  s    46++,HI]=    (U-,>??&&'78&f/-))445O5QR
RrK   )locale_selector)_e_hLT_POWERCYCLEzPower cycling...r   )Ylibretranslate.initr   	load_onlyupdate_modelsforce_update_modelslibretranslate.languager   rg  r   __name__r   setupshared_storager   translation_cacher  r   rJ   r_  translations_fromto_langr   frontend_language_sourcetypeobjectr%   r   r   r[  frontend_language_targetr   supported_file_extensionsr   rx   r   r   r   api_keys_remoter/   r.   api_keys_db_pathflask_limiterr   r   req_limit_storage
no_limiterr7   r   rQ   r   r   r   r0  r8   abspathr9   rG   rH   prometheus_clientr   r   r   r   r   r   routeexemptr"  errorhandlerr  postr6  r	   configr]  register_blueprintinit_appr   rC   r   	jinja_envglobalsupdater*   r+   r   printsysexit)7r   r   r   api_urlbprI  r   r   file_formatffr   r   r   limiterdefault_mp_dirr   r   r   r1  r8  r=  rA  rD  rd  rs  rx  r|  r  r  r  r  r  r  r  appr  swaggerui_blueprintr   r   ru   r  r  r  r)  r   r   rw  r   r   r   r*  r   r  rQ  r  s7   `                                    @@@@@@@@@@@@@@@@@@rB   
create_apprA     s   (++T-E-EF6//G+KG	:x	(BMM$%%&++d445K))%%n&67 IN$*DDZDZ+[DZqIINNDZ+[$\tyy!  $$.)-F9vq7GH*
& *.YRY&&D4Q4Q*Q!YRS*
& &-)21& 039~/By|	RS$$0! (?$
9P9a$2O2O(O9P
QSWo 
	 2'>$,.),.77B188< 8 / K~~T]]d.B.BQ.F$J_J_bcJc==BFBVBV.)=)=>\deiezez\{K)		& (*,k ..'7 +$	
 	()(92>>ood	KK
LLOM|||	2	3	;77??277<<l+KL.~..HH^$3A"**/
0yy	xx
~~Q Q  Q   NPg  jY  Zo\3R@DFWY~  S\  ]m<b9W Wr __S; ; __S; ; __SR R __S; ; XXc]^^  B XXl^^  : VVL^^/  /B VVI^^)  )* 	 	 WW\nV  nV`	 WWP&   P&d VV./[ [ 0[4 WWYW7  W7r XX"#^^M
  $M
^ WWZP* P*d /C"'CJJzz.2

*+2S3<D*.//R*?tSD)mDL,DL"QDL%z2DLYYw^^(  ( 3<CJJ./S 
#z*MM  O E 2+tQX?XY%,"./	zz~~o&2	hhqkJ}# ,\ S, Qs   9a
-aa*a a )r-   r-   )Wr  r   r7   r  rE   r  r7  warningsr   	functoolsr   r  r   timeitr   r  r   flaskr   r	   r
   r   r   r   r   r   r   r   flask_babelr   flask_swaggerr   flask_swagger_uir   !argostranslatefiles.translatehtmlr   werkzeug.exceptionsr   werkzeug.httpr   werkzeug.utilsr   r4   r   r   r   r   r   r   r   r  r    r!   r"   r#   r$   libretranslate.localesr%   r&   r'   r(   r)   r*   r+   r,   r   r.   r/   r  r  r   r   r   r   rC   rJ   rV   rX   rP   ri   ro   rz   r}   r   r   r   rA  r7  s   0rB   <module>rN     s   	  	 	   
        5 y y y  ! 4 < - # * f f f G  G	 	 	 / 8 s8*uWWuWW uWW uVF	
 uVF uVF uWW uWW uVF	
	 T'	
 U':>	Rq
s   "F