Spaces:
Running
Running
Updated app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
|
|
|
| 3 |
import requests
|
| 4 |
import gradio as gr
|
| 5 |
from urllib.parse import urlparse
|
| 6 |
-
from marker.pdf import process_pdf
|
| 7 |
|
| 8 |
def procesar_pdf(pdf_file=None, url_pdf=None):
|
| 9 |
output_base = "./marker_output"
|
| 10 |
os.makedirs(output_base, exist_ok=True)
|
| 11 |
|
| 12 |
-
# Determinar si se usa archivo o URL
|
| 13 |
if url_pdf:
|
| 14 |
parsed = urlparse(url_pdf)
|
| 15 |
file_name = os.path.basename(parsed.path) or "documento.pdf"
|
|
@@ -21,10 +20,8 @@ def procesar_pdf(pdf_file=None, url_pdf=None):
|
|
| 21 |
response = requests.get(url_pdf)
|
| 22 |
if response.status_code != 200 or b"%PDF" not in response.content[:1024]:
|
| 23 |
return "La URL proporcionada no contiene un archivo PDF v谩lido."
|
| 24 |
-
|
| 25 |
with open(file_path, "wb") as f:
|
| 26 |
f.write(response.content)
|
| 27 |
-
|
| 28 |
except Exception as e:
|
| 29 |
return f"Error al descargar el PDF desde la URL: {str(e)}"
|
| 30 |
|
|
@@ -39,20 +36,19 @@ def procesar_pdf(pdf_file=None, url_pdf=None):
|
|
| 39 |
else:
|
| 40 |
return "Por favor, proporciona una URL o sube un archivo PDF."
|
| 41 |
|
| 42 |
-
# Crear carpeta de salida y limpiar si ya existe
|
| 43 |
name_wo_ext = os.path.splitext(os.path.basename(file_path))[0]
|
| 44 |
output_dir = os.path.join(output_base, name_wo_ext)
|
| 45 |
if os.path.exists(output_dir):
|
| 46 |
shutil.rmtree(output_dir)
|
|
|
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
|
|
|
|
| 50 |
file_path,
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
extract_html=True
|
| 55 |
-
)
|
| 56 |
|
| 57 |
md_path = os.path.join(output_dir, f"{name_wo_ext}.md")
|
| 58 |
if os.path.exists(md_path):
|
|
@@ -61,10 +57,11 @@ def procesar_pdf(pdf_file=None, url_pdf=None):
|
|
| 61 |
else:
|
| 62 |
return "No se gener贸 el archivo Markdown. Es posible que el PDF est茅 da帽ado o sea incompatible."
|
| 63 |
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
-
return f"Error
|
| 66 |
|
| 67 |
-
# Interfaz Gradio
|
| 68 |
demo = gr.Interface(
|
| 69 |
fn=procesar_pdf,
|
| 70 |
inputs=[
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
+
import subprocess
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
from urllib.parse import urlparse
|
|
|
|
| 7 |
|
| 8 |
def procesar_pdf(pdf_file=None, url_pdf=None):
|
| 9 |
output_base = "./marker_output"
|
| 10 |
os.makedirs(output_base, exist_ok=True)
|
| 11 |
|
|
|
|
| 12 |
if url_pdf:
|
| 13 |
parsed = urlparse(url_pdf)
|
| 14 |
file_name = os.path.basename(parsed.path) or "documento.pdf"
|
|
|
|
| 20 |
response = requests.get(url_pdf)
|
| 21 |
if response.status_code != 200 or b"%PDF" not in response.content[:1024]:
|
| 22 |
return "La URL proporcionada no contiene un archivo PDF v谩lido."
|
|
|
|
| 23 |
with open(file_path, "wb") as f:
|
| 24 |
f.write(response.content)
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
return f"Error al descargar el PDF desde la URL: {str(e)}"
|
| 27 |
|
|
|
|
| 36 |
else:
|
| 37 |
return "Por favor, proporciona una URL o sube un archivo PDF."
|
| 38 |
|
|
|
|
| 39 |
name_wo_ext = os.path.splitext(os.path.basename(file_path))[0]
|
| 40 |
output_dir = os.path.join(output_base, name_wo_ext)
|
| 41 |
if os.path.exists(output_dir):
|
| 42 |
shutil.rmtree(output_dir)
|
| 43 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 44 |
|
| 45 |
try:
|
| 46 |
+
subprocess.run([
|
| 47 |
+
"marker_single",
|
| 48 |
file_path,
|
| 49 |
+
"--output_format", "markdown",
|
| 50 |
+
"--output_dir", output_dir
|
| 51 |
+
], check=True)
|
|
|
|
|
|
|
| 52 |
|
| 53 |
md_path = os.path.join(output_dir, f"{name_wo_ext}.md")
|
| 54 |
if os.path.exists(md_path):
|
|
|
|
| 57 |
else:
|
| 58 |
return "No se gener贸 el archivo Markdown. Es posible que el PDF est茅 da帽ado o sea incompatible."
|
| 59 |
|
| 60 |
+
except subprocess.CalledProcessError as e:
|
| 61 |
+
return f"Error ejecutando Marker: {str(e)}"
|
| 62 |
except Exception as e:
|
| 63 |
+
return f"Error inesperado: {str(e)}"
|
| 64 |
|
|
|
|
| 65 |
demo = gr.Interface(
|
| 66 |
fn=procesar_pdf,
|
| 67 |
inputs=[
|