File size: 3,079 Bytes
d61bee5 37944ec d61bee5 bfa2e5d 29934b7 d61bee5 09a8de9 5f302e9 63eb0e1 5f302e9 63eb0e1 5f302e9 63eb0e1 5f302e9 63eb0e1 5f302e9 63eb0e1 09a8de9 5f302e9 09a8de9 d61bee5 09a8de9 d61bee5 09a8de9 3a5f37b 09a8de9 d61bee5 09a8de9 d61bee5 09a8de9 d61bee5 37944ec d61bee5 37944ec d61bee5 37944ec d61bee5 |
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
import spacy
import pandas as pd
from docx import Document
from io import BytesIO
import tempfile
import os
# Cargar el modelo de SpaCy en espa帽ol
nlp = spacy.load('zh_core_web_trf')
#nlp.max_length = 15000000 # Aumenta el l铆mite a 3 millones de caracteres
# Funci贸n para procesar bloques de texto y extraer nombres de personas
def extract_names_from_text(text):
doc = nlp(text)
# Extraer las entidades de tipo PERSON
persons = [ent.text for ent in doc.ents if ent.label_ == 'PERSON']
return persons
# Funci贸n para dividir el texto en fragmentos m谩s peque帽os
def split_text(file_path, max_length=100000):
result = []
current_chunk = []
current_length = 0
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
if line.strip() == '':
# Maneja el separador de p谩rrafos
paragraph_length = 2 # '\n\n'
else:
paragraph_length = len(line)
if current_length + paragraph_length <= max_length:
current_chunk.append(line)
current_length += paragraph_length
else:
# Almacena el chunk actual
result.append(''.join(current_chunk))
current_chunk = [line]
current_length = paragraph_length
# A帽ade el 煤ltimo fragmento
if current_chunk:
result.append(''.join(current_chunk))
return result
# Funci贸n principal para extraer nombres de personas desde un archivo DOCX
def extract_names_from_docx(docx_file):
# Cargar el archivo DOCX
document = Document(docx_file)
full_text = []
for para in document.paragraphs:
full_text.append(para.text)
# Unir todo el texto
text = ' '.join(full_text)
# Dividir el texto en fragmentos si es necesario
text_fragments = split_text(text)
# Extraer los nombres de cada fragmento y combinarlos
all_persons = []
for fragment in text_fragments:
print(f'{len(fragment)}\n\n{fragment}')
persons = extract_names_from_text(fragment)
all_persons.extend(persons)
# Eliminar duplicados
all_persons = list(set(all_persons))
# Crear un DataFrame
df = pd.DataFrame(all_persons, columns=['Nombres'])
# Crear un archivo temporal para guardar el Excel
temp_dir = tempfile.mkdtemp()
temp_file_path = os.path.join(temp_dir, "nombres_personas.xlsx")
# Guardar el DataFrame en un archivo Excel
with pd.ExcelWriter(temp_file_path, engine='openpyxl') as writer:
df.to_excel(writer, index=False)
return temp_file_path # Devolver la ruta del archivo temporal
# Interfaz de Gradio
iface = gr.Interface(
fn=extract_names_from_docx,
inputs=gr.File(file_types=[".docx"]),
outputs=gr.File(),
title="Extractor de Nombres",
description="Sube un archivo .docx y extrae los nombres de las personas usando NLP con SpaCy. Descarga el resultado en un archivo Excel."
)
# Iniciar la aplicaci贸n
iface.launch() |