
    Fgz;                     (   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r	S SK
rS SKrSSSSSS	S
.rSSSSSSSSSS.	rS r\	R                   " 5       4S jr\	R                   " 5       4S jr\	R&                  R)                  SSSSSSSSS S!S"S#9r\	R,                  " S$\	R&                  R/                  \S%S&S'9S(S)9 r\	R2                  " S*5        \	R4                  " 5          \	R6                  " S+S,9   \	R2                  " S-5        \	R8                  " S.S/S0/S1S2/S39r\	R2                  " S45        \	R<                  " S5S6\R?                  5       S7S89r \	R<                  " S9S:\R?                  5       S;S89r!\	RD                  " / S<QS=S>S?S@9r#\	RH                  " SASBSCSD9r%\	RH                  " SESFSGS@9r&SHr'SI r(SJ\)SK\)4SL jr*SM\	RV                  4SN jr,\	R2                  " SOSCSP9r-\	R8                  " SQSCSR9r.\	R8                  " SSSCSR9r/\	R`                  " STSUSCSV9r1\	R2                  " \(" \'5      SW/SX9r2\ Rg                  \,\ \2\%/5        SSS5        \	R6                  " SYS,9   \	R2                  " SZ5        \	Rh                  " S[S\SR9r5SSS5        SSS5        \Rm                  \\\ /\\5\1/S]9  \1Ro                  \\\ \%\!\#\&/\.\5\/\.\/\-/S]9  SSS5        S`S^ jr8\9S_:X  a  \8" 5         gg! , (       d  f       N= f! , (       d  f       Nv= f! , (       d  f       N= f! , (       d  f       NU= f)a    N)Path__version__googledeepldeeplxollamaopenaiazure)GoogleDeepLDeepLXOllamaOpenAIAzurezhenfrdejakoruesit)	ChineseEnglishFrenchGermanJapaneseKoreanRussianSpanishItalianc                    [         R                  " U 5      nUS   nUR                  5       n[        R                  " UR
                  [        R                  5      R                  UR                  UR                  S5      nU$ )Nr      )
pymupdfopen
get_pixmapnp
frombuffersamplesuint8reshapeheightwidth)filedocpagepiximages        [/Users/admin/workspace/ai/PDFMathTranslate/myenv/lib/python3.13/site-packages/pdf2zh/gui.pypdf_previewr6   "   sZ    
,,t
Cq6D
//
CMM#++rxx088SYYPQREL    c                 P   U (       a$  [         R                  R                  U 5      (       d  SS[        R                  " SS94$ U" SSS9   [        U 5      nX[        R                  " SS94$ ! [         a/  n[        SU 35        SS[        R                  " SS94s SnA$ SnAff = f)	z4Handle file upload, validation, and initial preview.NFvisible333333?zConverting PDF for preview...descTzError converting PDF: )ospathexistsgrupdater6   	Exceptionprint)r0   serviceprogresspreview_imagees        r5   upload_filerI   *   s    rww~~d++T299U333S674#D)BIId$;;; 4&qc*+T299U3334s    A, ,
B%6$B B% B%c                    U (       d>  SSS[         R                  " SS9[         R                  " SS9[         R                  " SS94$ U" SSS9  [        R                  " 5        n[	        U5      nUS-  n	U" SS	S9  [        U S
5       n
[        U	S5       nUR                  U
R                  5       5        SSS5        SSS5        [        R                  US5      n[        R                  US5      nU" SSU S3S9  [	        S5      S-  nUR                  SSS9  US[        R                  R                  U 5       3-  nUS[        R                  R                  U 5       3-  nUR                  5       nUS:X  a  US-  nOUS:X  a  US-  nOUS:X  a  US-  nUS:X  a
  US:X  a  SOUnUS;   a  SU S U	 S!U S"U S#U S$U 3nOSU S U	 S!U S"U S$U 3
n[!        S%U 35        [!        S&[        R"                  " U5       35        [$        R&                  " US[$        R(                  [$        R*                  SS'9n UR,                  R/                  5       nUS:X  a  UR1                  5       b  OU(       ax  [!        S(UR                  5        35        [2        R4                  " S)UR                  5       5      nU(       a0  [7        UR9                  S*5      5      nSUS+-  S,-  -   nU" US-U S.3S9  M  UR1                  5       n[!        S/U 35        US0-  nUS1-  n[!        S2[        R"                  " U5       35        UR;                  5       (       dg  UR;                  5       (       dR  [!        S35        SSS[         R                  " SS9[         R                  " SS9[         R                  " SS94sSSS5        $ U" S4S5S9  UR;                  5       (       aI  [        US
5       n
[        US5       nUR                  U
R                  5       5        SSS5        SSS5        UR;                  5       (       aI  [        US
5       n
[        US5       nUR                  U
R                  5       5        SSS5        SSS5        U" S6S7S9   [=        [?        U5      5      nSSS5        U" S9S:S9  [?        W5      W[?        W5      [         R                  " SS9[         R                  " SS9[         R                  " SS94$ ! , (       d  f       GNA= f! , (       d  f       GNK= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       N= f! [@         a  n[!        S8U 35        Sn SnANSnAff = f! , (       d  f       N= f);z-Translate PDF content using selected service.NFr9   r   zStarting translation...r<   z	input.pdfg?zPreparing files...rbwbr   r   r;   zStarting translation with z...gradio_filesoutputsT)parentsexist_oktranslated_dual_All Firstz -p 1First 5 pagesz -p 1-5zzh-CN)r	   r
   zcd "z" && pdf2zh "z" -lo z -s : zExecuting command: zFiles in temp directory: )shellstdoutstderruniversal_newlineszCommand output: z(\d+)%   g      ?d   zTranslating content: %z$Command completed with return code: zinput-zh.pdfzinput-dual.pdfzFiles after translation: z)Translation failed: No output files foundg?zSaving translated files...g?zGenerating preview...zError generating preview: g      ?zTranslation complete!)!rA   rB   tempfileTemporaryDirectoryr   r'   writereadservice_mapgetlang_mapmkdirr>   r?   basenamestriprD   listdir
subprocessPopenPIPESTDOUTrZ   readlinepollresearchintgroupr@   r6   strrC   )	file_pathrE   model_idlang
page_range
extra_argsrF   temp_dir	temp_path	input_pdfsrcdstselected_servicelang_to
output_dirfinal_outputfinal_output_dualcommandprocessoutputmatchpercentprogress_valreturn_codetranslated_file	dual_filetranslated_previewrH   s                               r5   	translater   :   s#    IIe$IIe$IIe$
 	
 Q./ 
	$	$	&(N	+	 	/0)T"c4	4+@CIIchhj! ,A" '??7H=,,tT* 	78H7IMN .)I5
5!k"''2B2B92M1N$OO&51A1A)1L0M)NN  %%'
"J7"'!J?*)#J x'!(DggG33YK}YKvgYdScRddefneoopq{p|}GYK}YKvgYdScRddefpeqrG#G9-.)"**Y*?)@AB""??$$#
 ^^,,.F| :((89:		)V\\^<!%++a.1G#&'C-#*=#>L\2GyPQ0RS  lln4[MBC $n4 00	)"**Y*?)@AB%%''	0@0@0B0B=>		%(		%(		%([ 
'	&n 	78!!##ot,T,5MQT		#((*% 6N, i&#t4Et/LPS		#((*% 0M& 	23	&!,S->!?I 
'R S./L
		$
		$
		$ G ,A+@""f 6N5M,, 0M/L&&  	&.qc23!%	&K 
'	&s   "$U>S? S-3S?;K*U>/(U>T#$ TT#)U>5U T5"U*U>:U-
S<7S??
T		U>
T T##
T2	-U>5
U?U
U	U>
U;!U61U>6U;;U>>
Vz#E8F3FFz#BEDAFFz#94BFFFz#6AA1FFz#4080FFz#165DFFz#0E42D2z#0A2BA6z#061D79z#03114Dz#020B33)c50c100c200c300c400c500c600c700c800c900c950z9PDFMathTranslate - PDF Translation with preserved formatsmdlg)primary_huespacing_sizeradius_sizea  
    .secondary-text {color: #999 !important;}
    footer {visibility: hidden}
    .env-warning {color: #dd5500 !important;}
    .env-success {color: #559900 !important;}
    
    @keyframes pulse-background {
        0% { background-color: #FFFFFF; }
        25% { background-color: #FFFFFF; }
        50% { background-color: #E8F3FF; }
        75% { background-color: #FFFFFF; }
        100% { background-color: #FFFFFF; }
    }
    
    /* Add dashed border to input-file class */
    .input-file {
        border: 1.2px dashed #165DFF !important;
        border-radius: 6px !important;
        # background-color: #ffffff !important;
        animation: pulse-background 2s ease-in-out;
        transition: background-color 0.4s ease-out;
    }

    .input-file:hover {
        border: 1.2px dashed #165DFF !important;
        border-radius: 6px !important;
        color: #165DFF !important;
        background-color: #E8F3FF !important;
        transition: background-color 0.2s ease-in;
    }

.progress-bar-wrap {
  border-radius: 8px !important;
}
.progress-bar {
  border-radius: 8px !important;
}

    # .input-file label {
    #     color: #165DFF !important;
    #     border: 1.2px dashed #165DFF !important;
    #     border-left: none !important;
    #     border-top: none !important;
    # }
    # .input-file .wrap {
    #     color: #165DFF !important;
    # }
    # .input-file .or {
    #     color: #165DFF !important;
    # }
    )titlethemecssz# PDFMathTranslater]   )scalez## FileDocumentsinglez.pdffilepathz
input-file)label
file_count
file_typestypeelem_classesz	## OptionServicez3Which translation service to use. Some require keysr   )r   infochoicesvaluezTranslate toz)Which language to translate to (optional)r   )rS   rU   rV   Pagesz8Translate the full document or just few pages (optional)rS   )r   r   r   zModel IDzPlease enter the identifier of the model you wish to use (e.g., gemma2). This identifier will be used to specify the particular model for translation.F)r   r   r:   zAdvanced Argumentsz3Extra arguments supported in commandline (optional)rT   ;<span class='env-success'>- Properly configured.</span><br>c                      SU  S[          S3nU$ )Nzi 
                <details>
                    <summary>Technical details</summary>
                    z
                    - GitHub: <a href="https://github.com/Byaidu/PDFMathTranslate">Byaidu/PDFMathTranslate</a><br>
                    - GUI by: <a href="https://github.com/reycn">Rongxin</a><br>
                    - Version: z
                </details>r   )text_markdowntexts     r5   details_wrapperr   9  s-     #O $  !,} - r7   env_var_namereturnc                    [         R                  R                  U 5      (       a#  [         R                  R                  U 5      S:X  a  SU  S3nOl[        [         R                  R                  U 5      5      nSn[	        U5      S:  a'  US[         R                  R                  U 5       S3-  nOUSUS S  S3-  n[        U5      $ )	NrT   zG<span class='env-warning'>- Warning: environmental not found or error (z).</span><br>- Please make sure that the environment variables are properly configured (<a href='https://github.com/Byaidu/PDFMathTranslate'>guide</a>).<br>r      z- Env: <code>z</code><br>z***</code><br>)r>   environre   ru   lenr   )r   envs_statusr   s      r5   env_var_checkerr   D  s    

|44zz~~l3r9$klxky  zV  #WK

| <=EU   5zB#+BJJNN<,H+IU $uSbzl.'QQ&{33r7   evtc                 x   [         R                  " SS9nU S:X  a  [        S5      nX24$ U S:X  a  [        S5      nX24$ U S:X  a  [        S5      nX24$ U S	:X  a  [        S
5      nX24$ U S:X  a#  [         R                  " SSS9n[        S5      nX24$ U S:X  a#  [         R                  " SSS9n[        S5      nX24$ SnX24$ )NFr9   r   r   r   DEEPL_AUTH_KEYr   DEEPLX_AUTH_KEYr   AZURE_APIKEYr   Tzgpt-4o)r:   r   OPENAI_API_KEYr   gemma2OLLAMA_HOSTz<span class='env-warning'>- Warning: model not in the list.</span><br>- Please report via (<a href='https://github.com/Byaidu/PDFMathTranslate'>guide</a>).<br>)rA   rB   r   r   )r   r   model_visibilityr   s       r5   on_select_servicer   W  s   #%99U#; H$"1U#K, #44% g%"12B"CK" #44! h&"12C"DK #44 g%"1."AK #44 h&')yy $H($ #22B"CK #44 h&')yy $H($ #2-"@K #44 #DK"44r7   z## Translatedr9   zDownload Translation)r   r:   zDownload Translation (Dual)	Translateprimary)variantr:   zsecondary-text)r      z
## PreviewzDocument PreviewT)inputsrN   c                      [         R                  SSSU S9  g ! [         aV    [        S5         [         R                  SSSU S9   g ! [         a$    [        S5        [         R                  SSSS9    g f = ff = f)Nz0.0.0.0T)server_namedebug	inbrowsersharezWError launching GUI using 0.0.0.0.
This may be caused by global mode of proxy software.z	127.0.0.1zYError launching GUI using 127.0.0.1.
This may be caused by global mode of proxy software.)r   r   r   )demolaunchrC   rD   )r   s    r5   	setup_guir     s}    @	US @hi	@KKKttSXKY 	@noKKdd$K?	@	@s&    A9A*A51A94A55A9__main__)F):r>   rq   rk   r`   pathlibr   pdf2zhr   gradiorA   numpyr)   r&   rd   rf   r6   ProgressrI   r   themesColorcustom_blueBlocksDefaultr   MarkdownRowColumnFile
file_inputDropdownkeysrE   r   Radiory   Textboxrw   rz   r   r   ru   r   	EventDatar   output_titleoutput_fileoutput_file_dualButtontranslate_btntech_details_togselectImagepreviewuploadclickr   __name__ r7   r5   <module>r      s   	 	         
 )+ 4" JLBL iioo										   YY
E
))

d  2	8p 
KK$%	YYQKK	" #"8*^J KK$kkJ#((*	G kk$@ 	G 1O	J zz  n	H *JJ
 XK	4c 4c 4&5bll 5: ;;FL''(>NK!ww3U  IIk9eTM!{{,./  NN,g8H(7STi  l YYQKK%hh%7FG  o 
x G$Wm4   GXw
JO
  {8V	@ zK w  l  o 
w8 8sU   9$LK2.EK5K2$K!1K29;L
KK2!
K/+K22
L 	<L
L