Spaces:
Running
Running
leonsimon23
commited on
Update pdf2zh/gui.py
Browse files- pdf2zh/gui.py +109 -19
pdf2zh/gui.py
CHANGED
@@ -48,25 +48,22 @@ if os.environ.get("PDF2ZH_DEMO"):
|
|
48 |
"First": [0],
|
49 |
"First 20 pages": list(range(0, 20)),
|
50 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
def download_with_limit(url, save_path, size_limit):
|
53 |
-
chunk_size = 1024
|
54 |
-
total_size = 0
|
55 |
-
with requests.get(url, stream=True, timeout=10) as response:
|
56 |
-
response.raise_for_status()
|
57 |
-
content = response.headers.get("Content-Disposition")
|
58 |
-
try:
|
59 |
-
_, params = cgi.parse_header(content)
|
60 |
-
filename = params["filename"]
|
61 |
-
except Exception:
|
62 |
-
filename = os.path.basename(url)
|
63 |
-
with open(save_path / filename, "wb") as file:
|
64 |
-
for chunk in response.iter_content(chunk_size=chunk_size):
|
65 |
-
total_size += len(chunk)
|
66 |
-
if size_limit and total_size > size_limit:
|
67 |
-
raise gr.Error("Exceeds file size limit")
|
68 |
-
file.write(chunk)
|
69 |
-
return save_path / filename
|
70 |
|
71 |
def pdf_preview(file):
|
72 |
doc = pymupdf.open(file)
|
@@ -75,6 +72,7 @@ def pdf_preview(file):
|
|
75 |
image = np.frombuffer(pix.samples, np.uint8).reshape(pix.height, pix.width, 3)
|
76 |
return image
|
77 |
|
|
|
78 |
def upload_file(file, service, progress=gr.Progress()):
|
79 |
"""Handle file upload, validation, and initial preview."""
|
80 |
if not file or not os.path.exists(file):
|
@@ -89,6 +87,27 @@ def upload_file(file, service, progress=gr.Progress()):
|
|
89 |
print(f"Error converting PDF: {e}")
|
90 |
return None, None
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
def translate(
|
93 |
file_type,
|
94 |
file_input,
|
@@ -99,9 +118,13 @@ def translate(
|
|
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,6 +196,7 @@ def translate(
|
|
173 |
gr.update(visible=True),
|
174 |
)
|
175 |
|
|
|
176 |
# Global setup
|
177 |
custom_blue = gr.themes.Color(
|
178 |
c50="#E8F3FF",
|
@@ -198,9 +222,11 @@ with gr.Blocks(
|
|
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 {
|
@@ -216,9 +242,36 @@ with gr.Blocks(
|
|
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,6 +390,10 @@ with gr.Blocks(
|
|
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,6 +406,21 @@ with gr.Blocks(
|
|
349 |
on_select_filetype,
|
350 |
file_type,
|
351 |
[file_input, link_input],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
352 |
)
|
353 |
|
354 |
with gr.Column(scale=2):
|
@@ -360,6 +432,21 @@ with gr.Blocks(
|
|
360 |
upload_file,
|
361 |
inputs=[file_input, service],
|
362 |
outputs=[file_input, preview],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
363 |
)
|
364 |
|
365 |
translate_btn.click(
|
@@ -374,6 +461,7 @@ with gr.Blocks(
|
|
374 |
lang_from,
|
375 |
lang_to,
|
376 |
page_range,
|
|
|
377 |
],
|
378 |
outputs=[
|
379 |
output_file,
|
@@ -383,7 +471,8 @@ with gr.Blocks(
|
|
383 |
output_file_dual,
|
384 |
output_title,
|
385 |
],
|
386 |
-
)
|
|
|
387 |
|
388 |
def setup_gui(share=False):
|
389 |
if flag_demo:
|
@@ -405,6 +494,7 @@ def setup_gui(share=False):
|
|
405 |
)
|
406 |
demo.launch(debug=True, inbrowser=True, share=True)
|
407 |
|
|
|
408 |
# For auto-reloading while developing
|
409 |
if __name__ == "__main__":
|
410 |
setup_gui()
|
|
|
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 |
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 |
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
|
94 |
+
with requests.get(url, stream=True, timeout=10) as response:
|
95 |
+
response.raise_for_status()
|
96 |
+
content = response.headers.get("Content-Disposition")
|
97 |
+
try:
|
98 |
+
_, params = cgi.parse_header(content)
|
99 |
+
filename = params["filename"]
|
100 |
+
except Exception:
|
101 |
+
filename = os.path.basename(url)
|
102 |
+
with open(save_path / filename, "wb") as file:
|
103 |
+
for chunk in response.iter_content(chunk_size=chunk_size):
|
104 |
+
total_size += len(chunk)
|
105 |
+
if size_limit and total_size > size_limit:
|
106 |
+
raise gr.Error("Exceeds file size limit")
|
107 |
+
file.write(chunk)
|
108 |
+
return save_path / filename
|
109 |
+
|
110 |
+
|
111 |
def translate(
|
112 |
file_type,
|
113 |
file_input,
|
|
|
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 |
gr.update(visible=True),
|
197 |
)
|
198 |
|
199 |
+
|
200 |
# Global setup
|
201 |
custom_blue = gr.themes.Color(
|
202 |
c50="#E8F3FF",
|
|
|
222 |
footer {visibility: hidden}
|
223 |
.env-warning {color: #dd5500 !important;}
|
224 |
.env-success {color: #559900 !important;}
|
225 |
+
/* Add dashed border to input-file class */
|
226 |
.input-file {
|
227 |
border: 1.2px dashed #165DFF !important;
|
228 |
border-radius: 6px !important;
|
229 |
+
# background-color: #ffffff !important;
|
230 |
transition: background-color 0.4s ease-out;
|
231 |
}
|
232 |
.input-file:hover {
|
|
|
242 |
.progress-bar {
|
243 |
border-radius: 8px !important;
|
244 |
}
|
245 |
+
# .input-file label {
|
246 |
+
# color: #165DFF !important;
|
247 |
+
# border: 1.2px dashed #165DFF !important;
|
248 |
+
# border-left: none !important;
|
249 |
+
# border-top: none !important;
|
250 |
+
# }
|
251 |
+
# .input-file .wrap {
|
252 |
+
# color: #165DFF !important;
|
253 |
+
# }
|
254 |
+
# .input-file .or {
|
255 |
+
# color: #165DFF !important;
|
256 |
+
# }
|
257 |
""",
|
258 |
+
head=(
|
259 |
+
"""
|
260 |
+
<script src="https://www.google.com/recaptcha/api.js?render=explicit" async defer></script>
|
261 |
+
<script type="text/javascript">
|
262 |
+
var onVerify = function(token) {
|
263 |
+
el=document.getElementById('verify').getElementsByTagName('textarea')[0];
|
264 |
+
el.value=token;
|
265 |
+
el.dispatchEvent(new Event('input'));
|
266 |
+
};
|
267 |
+
</script>
|
268 |
+
"""
|
269 |
+
if flag_demo
|
270 |
+
else ""
|
271 |
+
),
|
272 |
) as demo:
|
273 |
gr.Markdown(
|
274 |
+
#"# [PDFMathTranslate @ GitHub](https://github.com/Byaidu/PDFMathTranslate)"
|
275 |
"# [PDFMathTranslate——科研之心免费提供(更多科研AI智能体请点击)](https://ai.linkagi.top)"
|
276 |
)
|
277 |
|
|
|
390 |
output_file_dual = gr.File(
|
391 |
label="Download Translation (Dual)", visible=False
|
392 |
)
|
393 |
+
recaptcha_response = gr.Textbox(
|
394 |
+
label="reCAPTCHA Response", elem_id="verify", visible=False
|
395 |
+
)
|
396 |
+
recaptcha_box = gr.HTML('<div id="recaptcha-box"></div>')
|
397 |
translate_btn = gr.Button("Translate", variant="primary")
|
398 |
tech_details_tog = gr.Markdown(
|
399 |
details_wrapper(envs_status),
|
|
|
406 |
on_select_filetype,
|
407 |
file_type,
|
408 |
[file_input, link_input],
|
409 |
+
js=(
|
410 |
+
f"""
|
411 |
+
(a,b)=>{{
|
412 |
+
try{{
|
413 |
+
grecaptcha.render('recaptcha-box',{{
|
414 |
+
'sitekey':'{client_key}',
|
415 |
+
'callback':'onVerify'
|
416 |
+
}});
|
417 |
+
}}catch(error){{}}
|
418 |
+
return [a];
|
419 |
+
}}
|
420 |
+
"""
|
421 |
+
if flag_demo
|
422 |
+
else ""
|
423 |
+
),
|
424 |
)
|
425 |
|
426 |
with gr.Column(scale=2):
|
|
|
432 |
upload_file,
|
433 |
inputs=[file_input, service],
|
434 |
outputs=[file_input, preview],
|
435 |
+
js=(
|
436 |
+
f"""
|
437 |
+
(a,b)=>{{
|
438 |
+
try{{
|
439 |
+
grecaptcha.render('recaptcha-box',{{
|
440 |
+
'sitekey':'{client_key}',
|
441 |
+
'callback':'onVerify'
|
442 |
+
}});
|
443 |
+
}}catch(error){{}}
|
444 |
+
return [a];
|
445 |
+
}}
|
446 |
+
"""
|
447 |
+
if flag_demo
|
448 |
+
else ""
|
449 |
+
),
|
450 |
)
|
451 |
|
452 |
translate_btn.click(
|
|
|
461 |
lang_from,
|
462 |
lang_to,
|
463 |
page_range,
|
464 |
+
recaptcha_response,
|
465 |
],
|
466 |
outputs=[
|
467 |
output_file,
|
|
|
471 |
output_file_dual,
|
472 |
output_title,
|
473 |
],
|
474 |
+
).then(lambda: None, js="()=>{grecaptcha.reset()}" if flag_demo else "")
|
475 |
+
|
476 |
|
477 |
def setup_gui(share=False):
|
478 |
if flag_demo:
|
|
|
494 |
)
|
495 |
demo.launch(debug=True, inbrowser=True, share=True)
|
496 |
|
497 |
+
|
498 |
# For auto-reloading while developing
|
499 |
if __name__ == "__main__":
|
500 |
setup_gui()
|