Spaces:
Sleeping
Sleeping
tomaseo2022
commited on
Commit
路
fb0ce36
1
Parent(s):
866e746
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
import fitz
|
3 |
from zipfile import ZipFile
|
4 |
-
|
|
|
5 |
|
6 |
def pdf_to_cbz(pdf_file):
|
7 |
if not pdf_file:
|
8 |
-
|
9 |
-
return create_error_file(error_message)
|
10 |
|
11 |
try:
|
12 |
# Desempaqueta el archivo PDF
|
13 |
pdf_filename, file_type, pdf_bytes = pdf_file
|
14 |
except ValueError:
|
15 |
-
|
16 |
-
return create_error_file(error_message)
|
17 |
|
18 |
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
19 |
-
cbz_temp_file = BytesIO()
|
20 |
|
21 |
-
with
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
doc.close()
|
29 |
|
30 |
-
|
31 |
-
return cbz_temp_file, "comic.cbz"
|
32 |
|
33 |
def create_error_file(error_message):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
return error_file, "error.txt"
|
38 |
|
39 |
iface = gr.Interface(
|
40 |
fn=pdf_to_cbz,
|
41 |
inputs=gr.File(type="binary", label="Cargar archivo PDF"),
|
42 |
outputs=gr.File(label="Descargar archivo CBZ"),
|
43 |
title="Conversor de PDF a CBZ",
|
44 |
-
|
45 |
-
)
|
46 |
-
|
47 |
-
if __name__ == "__main__":
|
48 |
-
iface.launch()
|
49 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import fitz
|
3 |
from zipfile import ZipFile
|
4 |
+
import os
|
5 |
+
from tempfile import NamedTemporaryFile
|
6 |
|
7 |
def pdf_to_cbz(pdf_file):
|
8 |
if not pdf_file:
|
9 |
+
return create_error_file("No se carg贸 ning煤n archivo. Por favor, carga un archivo PDF.")
|
|
|
10 |
|
11 |
try:
|
12 |
# Desempaqueta el archivo PDF
|
13 |
pdf_filename, file_type, pdf_bytes = pdf_file
|
14 |
except ValueError:
|
15 |
+
return create_error_file("Error al procesar el archivo. Aseg煤rate de que sea un archivo PDF v谩lido.")
|
|
|
16 |
|
17 |
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
|
|
18 |
|
19 |
+
with NamedTemporaryFile(delete=False, suffix=".cbz") as temp_cbz:
|
20 |
+
with ZipFile(temp_cbz.name, 'w') as zipf:
|
21 |
+
for page_num in range(len(doc)):
|
22 |
+
page = doc.load_page(page_num)
|
23 |
+
pix = page.get_pixmap()
|
24 |
+
img_data = pix.tobytes("png")
|
25 |
+
img_filename = f"page_{page_num + 1}.png"
|
26 |
+
zipf.writestr(img_filename, img_data)
|
27 |
+
|
28 |
doc.close()
|
29 |
|
30 |
+
return temp_cbz.name
|
|
|
31 |
|
32 |
def create_error_file(error_message):
|
33 |
+
with NamedTemporaryFile(delete=False, suffix=".txt") as temp_txt:
|
34 |
+
temp_txt.write(error_message.encode())
|
35 |
+
return temp_txt.name
|
|
|
36 |
|
37 |
iface = gr.Interface(
|
38 |
fn=pdf_to_cbz,
|
39 |
inputs=gr.File(type="binary", label="Cargar archivo PDF"),
|
40 |
outputs=gr.File(label="Descargar archivo CBZ"),
|
41 |
title="Conversor de PDF a CBZ",
|
42 |
+
de
|
|
|
|
|
|
|
|
|
|