File size: 2,312 Bytes
eceac6f
3d49109
 
93db579
 
 
387ef08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eceac6f
3d49109
 
 
eceac6f
3d49109
 
eceac6f
3d49109
4cb29ef
 
 
 
 
 
eceac6f
3d49109
eceac6f
4cb29ef
3d49109
 
 
 
 
eceac6f
3d49109
 
 
eceac6f
3d49109
 
 
 
eceac6f
3d49109
eceac6f
3d49109
 
 
 
eceac6f
3d49109
 
eceac6f
3d49109
 
 
 
 
eceac6f
3d49109
 
 
 
eceac6f
 
3d49109
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import gradio as gr
from PyPDF2 import PdfReader
import tempfile
import re
import unicodedata

def limpiar_texto(texto):
    # Normaliza caracteres Unicode (acentos y símbolos)
    texto = unicodedata.normalize("NFKD", texto)

    # Elimina caracteres no imprimibles
    texto = re.sub(
        r"[^\x09\x0A\x0D\x20-\x7EáéíóúÁÉÍÓÚñÑüÜ]",
        "",
        texto
    )

    # Reemplazos comunes de símbolos en PDFs
    reemplazos = {
        "�": "",
        "•": "-",
        "▪": "-",
        "–": "-",
        "—": "-",
        "“": '"',
        "”": '"',
        "‘": "'",
        "’": "'",
        "Â": "",
    }

    for simbolo, reemplazo in reemplazos.items():
        texto = texto.replace(simbolo, reemplazo)

    # Limpia espacios y saltos de línea excesivos
    texto = re.sub(r"\n{3,}", "\n\n", texto)
    texto = re.sub(r"[ \t]{2,}", " ", texto)

    return texto.strip()

def extraer_texto_pdf(archivo_pdf):
    if archivo_pdf is None:
        return ""

    reader = PdfReader(archivo_pdf.name)
    texto = ""

    for pagina in reader.pages:
        contenido = pagina.extract_text()
        if contenido:
            texto += contenido + "\n"

    # 🔹 Limpieza del texto extraído
    texto = limpiar_texto(texto)

    return texto


def guardar_texto_en_txt(texto):
    archivo_temp = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
    with open(archivo_temp.name, "w", encoding="utf-8") as f:
        f.write(texto)
    return archivo_temp.name

with gr.Blocks() as app:
    gr.Markdown("# 📄 Extractor de texto desde PDF - Alex")
    gr.Markdown("Sube un PDF, extrae su texto y descárgalo en formato .txt")

    entrada_pdf = gr.File(
        label="📂 Selecciona tu archivo PDF",
        file_types=[".pdf"]
    )

    boton_extraer = gr.Button("🔍 Extraer texto")

    salida_texto = gr.Textbox(
        label="Texto extraído",
        lines=15
    )

    boton_descargar = gr.Button("💾 Descargar texto (.txt)")
    salida_archivo = gr.File(label="Descargar archivo")

    boton_extraer.click(
        fn=extraer_texto_pdf,
        inputs=entrada_pdf,
        outputs=salida_texto
    )

    boton_descargar.click(
        fn=guardar_texto_en_txt,
        inputs=salida_texto,
        outputs=salida_archivo
    )

app.launch()