Spaces:
Running
Running
leonsimon23
commited on
Update pdf2zh/gui.py
Browse files- pdf2zh/gui.py +19 -19
pdf2zh/gui.py
CHANGED
@@ -49,6 +49,25 @@ if os.environ.get("PDF2ZH_DEMO"):
|
|
49 |
"First 20 pages": list(range(0, 20)),
|
50 |
}
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
def pdf_preview(file):
|
53 |
doc = pymupdf.open(file)
|
54 |
page = doc[0]
|
@@ -70,25 +89,6 @@ def upload_file(file, service, progress=gr.Progress()):
|
|
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
|
76 |
-
with requests.get(url, stream=True, timeout=10) as response:
|
77 |
-
response.raise_for_status()
|
78 |
-
content = response.headers.get("Content-Disposition")
|
79 |
-
try:
|
80 |
-
_, params = cgi.parse_header(content)
|
81 |
-
filename = params["filename"]
|
82 |
-
except Exception:
|
83 |
-
filename = os.path.basename(url)
|
84 |
-
with open(save_path / filename, "wb") as file:
|
85 |
-
for chunk in response.iter_content(chunk_size=chunk_size):
|
86 |
-
total_size += len(chunk)
|
87 |
-
if size_limit and total_size > size_limit:
|
88 |
-
raise gr.Error("Exceeds file size limit")
|
89 |
-
file.write(chunk)
|
90 |
-
return save_path / filename
|
91 |
-
|
92 |
def translate(
|
93 |
file_type,
|
94 |
file_input,
|
|
|
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)
|
73 |
page = doc[0]
|
|
|
89 |
print(f"Error converting PDF: {e}")
|
90 |
return None, None
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
def translate(
|
93 |
file_type,
|
94 |
file_input,
|