Spaces:
Running
Running
leonsimon23
commited on
Update pdf2zh/gui.py
Browse files- pdf2zh/gui.py +2 -96
pdf2zh/gui.py
CHANGED
@@ -48,22 +48,6 @@ if os.environ.get("PDF2ZH_DEMO"):
|
|
48 |
"First": [0],
|
49 |
"First 20 pages": list(range(0, 20)),
|
50 |
}
|
51 |
-
client_key = os.environ.get("PDF2ZH_CLIENT_KEY")
|
52 |
-
server_key = os.environ.get("PDF2ZH_SERVER_KEY")
|
53 |
-
|
54 |
-
|
55 |
-
def verify_recaptcha(response):
|
56 |
-
recaptcha_url = "https://www.google.com/recaptcha/api/siteverify"
|
57 |
-
|
58 |
-
print("reCAPTCHA", server_key, response)
|
59 |
-
|
60 |
-
data = {"secret": server_key, "response": response}
|
61 |
-
result = requests.post(recaptcha_url, data=data).json()
|
62 |
-
|
63 |
-
print("reCAPTCHA", result.get("success"))
|
64 |
-
|
65 |
-
return result.get("success")
|
66 |
-
|
67 |
|
68 |
def pdf_preview(file):
|
69 |
doc = pymupdf.open(file)
|
@@ -72,7 +56,6 @@ def pdf_preview(file):
|
|
72 |
image = np.frombuffer(pix.samples, np.uint8).reshape(pix.height, pix.width, 3)
|
73 |
return image
|
74 |
|
75 |
-
|
76 |
def upload_file(file, service, progress=gr.Progress()):
|
77 |
"""Handle file upload, validation, and initial preview."""
|
78 |
if not file or not os.path.exists(file):
|
@@ -87,7 +70,6 @@ def upload_file(file, service, progress=gr.Progress()):
|
|
87 |
print(f"Error converting PDF: {e}")
|
88 |
return None, None
|
89 |
|
90 |
-
|
91 |
def download_with_limit(url, save_path, size_limit):
|
92 |
chunk_size = 1024
|
93 |
total_size = 0
|
@@ -107,7 +89,6 @@ def download_with_limit(url, save_path, size_limit):
|
|
107 |
file.write(chunk)
|
108 |
return save_path / filename
|
109 |
|
110 |
-
|
111 |
def translate(
|
112 |
file_type,
|
113 |
file_input,
|
@@ -118,13 +99,9 @@ def translate(
|
|
118 |
lang_from,
|
119 |
lang_to,
|
120 |
page_range,
|
121 |
-
recaptcha_response,
|
122 |
progress=gr.Progress(),
|
123 |
):
|
124 |
"""Translate PDF content using selected service."""
|
125 |
-
if flag_demo and not verify_recaptcha(recaptcha_response):
|
126 |
-
raise gr.Error("reCAPTCHA fail")
|
127 |
-
|
128 |
progress(0, desc="Starting translation...")
|
129 |
|
130 |
output = Path("pdf2zh_files")
|
@@ -196,7 +173,6 @@ def translate(
|
|
196 |
gr.update(visible=True),
|
197 |
)
|
198 |
|
199 |
-
|
200 |
# Global setup
|
201 |
custom_blue = gr.themes.Color(
|
202 |
c50="#E8F3FF",
|
@@ -222,15 +198,11 @@ with gr.Blocks(
|
|
222 |
footer {visibility: hidden}
|
223 |
.env-warning {color: #dd5500 !important;}
|
224 |
.env-success {color: #559900 !important;}
|
225 |
-
|
226 |
-
/* Add dashed border to input-file class */
|
227 |
.input-file {
|
228 |
border: 1.2px dashed #165DFF !important;
|
229 |
border-radius: 6px !important;
|
230 |
-
# background-color: #ffffff !important;
|
231 |
transition: background-color 0.4s ease-out;
|
232 |
}
|
233 |
-
|
234 |
.input-file:hover {
|
235 |
border: 1.2px dashed #165DFF !important;
|
236 |
border-radius: 6px !important;
|
@@ -238,44 +210,15 @@ with gr.Blocks(
|
|
238 |
background-color: #E8F3FF !important;
|
239 |
transition: background-color 0.2s ease-in;
|
240 |
}
|
241 |
-
|
242 |
.progress-bar-wrap {
|
243 |
border-radius: 8px !important;
|
244 |
}
|
245 |
.progress-bar {
|
246 |
border-radius: 8px !important;
|
247 |
}
|
248 |
-
|
249 |
-
# .input-file label {
|
250 |
-
# color: #165DFF !important;
|
251 |
-
# border: 1.2px dashed #165DFF !important;
|
252 |
-
# border-left: none !important;
|
253 |
-
# border-top: none !important;
|
254 |
-
# }
|
255 |
-
# .input-file .wrap {
|
256 |
-
# color: #165DFF !important;
|
257 |
-
# }
|
258 |
-
# .input-file .or {
|
259 |
-
# color: #165DFF !important;
|
260 |
-
# }
|
261 |
""",
|
262 |
-
head=(
|
263 |
-
"""
|
264 |
-
<script src="https://www.google.com/recaptcha/api.js?render=explicit" async defer></script>
|
265 |
-
<script type="text/javascript">
|
266 |
-
var onVerify = function(token) {
|
267 |
-
el=document.getElementById('verify').getElementsByTagName('textarea')[0];
|
268 |
-
el.value=token;
|
269 |
-
el.dispatchEvent(new Event('input'));
|
270 |
-
};
|
271 |
-
</script>
|
272 |
-
"""
|
273 |
-
if flag_demo
|
274 |
-
else ""
|
275 |
-
),
|
276 |
) as demo:
|
277 |
gr.Markdown(
|
278 |
-
#"# [PDFMathTranslate @ GitHub](https://github.com/Byaidu/PDFMathTranslate)"
|
279 |
"# [PDFMathTranslate——科研之心免费提供(更多科研AI智能体请点击)](https://ai.linkagi.top)"
|
280 |
)
|
281 |
|
@@ -394,10 +337,6 @@ with gr.Blocks(
|
|
394 |
output_file_dual = gr.File(
|
395 |
label="Download Translation (Dual)", visible=False
|
396 |
)
|
397 |
-
recaptcha_response = gr.Textbox(
|
398 |
-
label="reCAPTCHA Response", elem_id="verify", visible=False
|
399 |
-
)
|
400 |
-
recaptcha_box = gr.HTML('<div id="recaptcha-box"></div>')
|
401 |
translate_btn = gr.Button("Translate", variant="primary")
|
402 |
tech_details_tog = gr.Markdown(
|
403 |
details_wrapper(envs_status),
|
@@ -410,21 +349,6 @@ with gr.Blocks(
|
|
410 |
on_select_filetype,
|
411 |
file_type,
|
412 |
[file_input, link_input],
|
413 |
-
js=(
|
414 |
-
f"""
|
415 |
-
(a,b)=>{{
|
416 |
-
try{{
|
417 |
-
grecaptcha.render('recaptcha-box',{{
|
418 |
-
'sitekey':'{client_key}',
|
419 |
-
'callback':'onVerify'
|
420 |
-
}});
|
421 |
-
}}catch(error){{}}
|
422 |
-
return [a];
|
423 |
-
}}
|
424 |
-
"""
|
425 |
-
if flag_demo
|
426 |
-
else ""
|
427 |
-
),
|
428 |
)
|
429 |
|
430 |
with gr.Column(scale=2):
|
@@ -436,21 +360,6 @@ with gr.Blocks(
|
|
436 |
upload_file,
|
437 |
inputs=[file_input, service],
|
438 |
outputs=[file_input, preview],
|
439 |
-
js=(
|
440 |
-
f"""
|
441 |
-
(a,b)=>{{
|
442 |
-
try{{
|
443 |
-
grecaptcha.render('recaptcha-box',{{
|
444 |
-
'sitekey':'{client_key}',
|
445 |
-
'callback':'onVerify'
|
446 |
-
}});
|
447 |
-
}}catch(error){{}}
|
448 |
-
return [a];
|
449 |
-
}}
|
450 |
-
"""
|
451 |
-
if flag_demo
|
452 |
-
else ""
|
453 |
-
),
|
454 |
)
|
455 |
|
456 |
translate_btn.click(
|
@@ -465,7 +374,6 @@ with gr.Blocks(
|
|
465 |
lang_from,
|
466 |
lang_to,
|
467 |
page_range,
|
468 |
-
recaptcha_response,
|
469 |
],
|
470 |
outputs=[
|
471 |
output_file,
|
@@ -475,8 +383,7 @@ with gr.Blocks(
|
|
475 |
output_file_dual,
|
476 |
output_title,
|
477 |
],
|
478 |
-
)
|
479 |
-
|
480 |
|
481 |
def setup_gui(share=False):
|
482 |
if flag_demo:
|
@@ -498,7 +405,6 @@ def setup_gui(share=False):
|
|
498 |
)
|
499 |
demo.launch(debug=True, inbrowser=True, share=True)
|
500 |
|
501 |
-
|
502 |
# For auto-reloading while developing
|
503 |
if __name__ == "__main__":
|
504 |
-
setup_gui()
|
|
|
48 |
"First": [0],
|
49 |
"First 20 pages": list(range(0, 20)),
|
50 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def pdf_preview(file):
|
53 |
doc = pymupdf.open(file)
|
|
|
56 |
image = np.frombuffer(pix.samples, np.uint8).reshape(pix.height, pix.width, 3)
|
57 |
return image
|
58 |
|
|
|
59 |
def upload_file(file, service, progress=gr.Progress()):
|
60 |
"""Handle file upload, validation, and initial preview."""
|
61 |
if not file or not os.path.exists(file):
|
|
|
70 |
print(f"Error converting PDF: {e}")
|
71 |
return None, None
|
72 |
|
|
|
73 |
def download_with_limit(url, save_path, size_limit):
|
74 |
chunk_size = 1024
|
75 |
total_size = 0
|
|
|
89 |
file.write(chunk)
|
90 |
return save_path / filename
|
91 |
|
|
|
92 |
def translate(
|
93 |
file_type,
|
94 |
file_input,
|
|
|
99 |
lang_from,
|
100 |
lang_to,
|
101 |
page_range,
|
|
|
102 |
progress=gr.Progress(),
|
103 |
):
|
104 |
"""Translate PDF content using selected service."""
|
|
|
|
|
|
|
105 |
progress(0, desc="Starting translation...")
|
106 |
|
107 |
output = Path("pdf2zh_files")
|
|
|
173 |
gr.update(visible=True),
|
174 |
)
|
175 |
|
|
|
176 |
# Global setup
|
177 |
custom_blue = gr.themes.Color(
|
178 |
c50="#E8F3FF",
|
|
|
198 |
footer {visibility: hidden}
|
199 |
.env-warning {color: #dd5500 !important;}
|
200 |
.env-success {color: #559900 !important;}
|
|
|
|
|
201 |
.input-file {
|
202 |
border: 1.2px dashed #165DFF !important;
|
203 |
border-radius: 6px !important;
|
|
|
204 |
transition: background-color 0.4s ease-out;
|
205 |
}
|
|
|
206 |
.input-file:hover {
|
207 |
border: 1.2px dashed #165DFF !important;
|
208 |
border-radius: 6px !important;
|
|
|
210 |
background-color: #E8F3FF !important;
|
211 |
transition: background-color 0.2s ease-in;
|
212 |
}
|
|
|
213 |
.progress-bar-wrap {
|
214 |
border-radius: 8px !important;
|
215 |
}
|
216 |
.progress-bar {
|
217 |
border-radius: 8px !important;
|
218 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
) as demo:
|
221 |
gr.Markdown(
|
|
|
222 |
"# [PDFMathTranslate——科研之心免费提供(更多科研AI智能体请点击)](https://ai.linkagi.top)"
|
223 |
)
|
224 |
|
|
|
337 |
output_file_dual = gr.File(
|
338 |
label="Download Translation (Dual)", visible=False
|
339 |
)
|
|
|
|
|
|
|
|
|
340 |
translate_btn = gr.Button("Translate", variant="primary")
|
341 |
tech_details_tog = gr.Markdown(
|
342 |
details_wrapper(envs_status),
|
|
|
349 |
on_select_filetype,
|
350 |
file_type,
|
351 |
[file_input, link_input],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
352 |
)
|
353 |
|
354 |
with gr.Column(scale=2):
|
|
|
360 |
upload_file,
|
361 |
inputs=[file_input, service],
|
362 |
outputs=[file_input, preview],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
363 |
)
|
364 |
|
365 |
translate_btn.click(
|
|
|
374 |
lang_from,
|
375 |
lang_to,
|
376 |
page_range,
|
|
|
377 |
],
|
378 |
outputs=[
|
379 |
output_file,
|
|
|
383 |
output_file_dual,
|
384 |
output_title,
|
385 |
],
|
386 |
+
)
|
|
|
387 |
|
388 |
def setup_gui(share=False):
|
389 |
if flag_demo:
|
|
|
405 |
)
|
406 |
demo.launch(debug=True, inbrowser=True, share=True)
|
407 |
|
|
|
408 |
# For auto-reloading while developing
|
409 |
if __name__ == "__main__":
|
410 |
+
setup_gui()
|