Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,12 @@ from modules.utils import handle_long_text
|
|
7 |
# Cargar modelos abstractivos finetuneados
|
8 |
summarizers = load_summarizers()
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Funci贸n para procesar el archivo cargado
|
11 |
def process_file(file):
|
12 |
"""
|
@@ -26,83 +32,32 @@ def process_file(file):
|
|
26 |
return "El archivo no contiene texto procesable."
|
27 |
return "Por favor, cargue un archivo v谩lido."
|
28 |
|
29 |
-
# Funci贸n
|
30 |
-
def
|
|
|
|
|
|
|
|
|
|
|
31 |
"""
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
Args:
|
35 |
-
|
36 |
-
file (UploadedFile): Archivo subido por el usuario.
|
37 |
-
summary_type (str): Tipo de resumen: Extractivo, Abstractivo o Combinado.
|
38 |
-
method (str): M茅todo de resumen extractivo.
|
39 |
-
num_sentences (int): N煤mero de oraciones para el resumen extractivo.
|
40 |
-
model_name (str): Nombre del modelo para resumen abstractivo.
|
41 |
-
max_length (int): Longitud m谩xima del resumen generado.
|
42 |
-
num_beams (int): N煤mero de haces para b煤squeda en el modelo.
|
43 |
|
44 |
Returns:
|
45 |
-
str:
|
46 |
"""
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
# Validar que haya texto para resumir
|
54 |
-
if not input_text.strip():
|
55 |
-
return "Por favor, ingrese texto o cargue un archivo v谩lido."
|
56 |
-
|
57 |
-
cleaned_text = preprocessor.clean_text(input_text)
|
58 |
-
|
59 |
-
# Procesar seg煤n el tipo de resumen seleccionado
|
60 |
-
if summary_type == "Extractivo":
|
61 |
-
if method == "TF-IDF":
|
62 |
-
summarizer = TFIDFSummarizer()
|
63 |
-
elif method == "TextRank":
|
64 |
-
summarizer = TextRankSummarizer()
|
65 |
-
elif method == "BERT":
|
66 |
-
summarizer = BERTSummarizer()
|
67 |
-
elif method == "TF-IDF + TextRank":
|
68 |
-
summarizer = CombinedSummarizer()
|
69 |
-
else:
|
70 |
-
return "M茅todo no v谩lido para resumen extractivo."
|
71 |
-
|
72 |
-
return summarizer.summarize(
|
73 |
-
preprocessor.split_into_sentences(cleaned_text),
|
74 |
-
preprocessor.clean_sentences(preprocessor.split_into_sentences(cleaned_text)),
|
75 |
-
num_sentences,
|
76 |
-
)
|
77 |
-
|
78 |
-
elif summary_type == "Abstractivo":
|
79 |
-
if model_name not in summarizers:
|
80 |
-
return "Modelo no disponible para resumen abstractivo."
|
81 |
-
return handle_long_text(
|
82 |
-
cleaned_text,
|
83 |
-
summarizers[model_name][0],
|
84 |
-
summarizers[model_name][1],
|
85 |
-
max_length=max_length,
|
86 |
-
stride=128,
|
87 |
-
)
|
88 |
-
|
89 |
-
elif summary_type == "Combinado":
|
90 |
-
if model_name not in summarizers:
|
91 |
-
return "Modelo no disponible para resumen abstractivo."
|
92 |
-
extractive_summary = TFIDFSummarizer().summarize(
|
93 |
-
preprocessor.split_into_sentences(cleaned_text),
|
94 |
-
preprocessor.clean_sentences(preprocessor.split_into_sentences(cleaned_text)),
|
95 |
-
num_sentences,
|
96 |
-
)
|
97 |
-
return handle_long_text(
|
98 |
-
extractive_summary,
|
99 |
-
summarizers[model_name][0],
|
100 |
-
summarizers[model_name][1],
|
101 |
-
max_length=max_length,
|
102 |
-
stride=128,
|
103 |
-
)
|
104 |
-
|
105 |
-
return "Seleccione un tipo de resumen v谩lido."
|
106 |
|
107 |
# Interfaz din谩mica
|
108 |
with gr.Blocks() as interface:
|
@@ -112,26 +67,23 @@ with gr.Blocks() as interface:
|
|
112 |
with gr.Row():
|
113 |
with gr.Column(scale=2):
|
114 |
input_text = gr.Textbox(lines=9, label="Ingrese texto", interactive=True)
|
115 |
-
|
116 |
-
|
|
|
117 |
with gr.Column(scale=1):
|
118 |
file = gr.File(label="Subir archivo (PDF, TXT)")
|
119 |
|
120 |
-
#
|
121 |
-
|
122 |
-
|
123 |
-
inputs=[
|
124 |
outputs=[input_text],
|
125 |
)
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
return gr.update(visible=file is not None)
|
130 |
-
|
131 |
-
file.change(
|
132 |
-
toggle_load_button,
|
133 |
inputs=[file],
|
134 |
-
outputs=[
|
135 |
)
|
136 |
|
137 |
# Selecci贸n de tipo de resumen y opciones din谩micas
|
|
|
7 |
# Cargar modelos abstractivos finetuneados
|
8 |
summarizers = load_summarizers()
|
9 |
|
10 |
+
# Ejemplo de texto
|
11 |
+
EXAMPLE_TEXT = """
|
12 |
+
La inteligencia artificial (IA) ha revolucionado m煤ltiples industrias, desde la medicina hasta el transporte.
|
13 |
+
Los avances en modelos de lenguaje, como T5, BART y PEGASUS, permiten aplicaciones innovadoras como la generaci贸n autom谩tica de res煤menes, lo cual facilita el acceso a informaci贸n clave en documentos extensos.
|
14 |
+
"""
|
15 |
+
|
16 |
# Funci贸n para procesar el archivo cargado
|
17 |
def process_file(file):
|
18 |
"""
|
|
|
32 |
return "El archivo no contiene texto procesable."
|
33 |
return "Por favor, cargue un archivo v谩lido."
|
34 |
|
35 |
+
# Funci贸n para cargar un ejemplo de texto
|
36 |
+
def load_example_text():
|
37 |
+
"""
|
38 |
+
Devuelve un ejemplo de texto predefinido.
|
39 |
+
|
40 |
+
Returns:
|
41 |
+
str: Texto de ejemplo.
|
42 |
"""
|
43 |
+
return EXAMPLE_TEXT
|
44 |
+
|
45 |
+
# Funci贸n para cargar y limpiar el contenido de un archivo
|
46 |
+
def process_uploaded_file(file):
|
47 |
+
"""
|
48 |
+
Procesa un archivo, lo limpia y devuelve su contenido en texto.
|
49 |
|
50 |
Args:
|
51 |
+
file (UploadedFile): Archivo subido.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
Returns:
|
54 |
+
str: Texto limpio extra铆do del archivo.
|
55 |
"""
|
56 |
+
raw_text = process_file(file)
|
57 |
+
if "El archivo no contiene" not in raw_text and "Por favor" not in raw_text:
|
58 |
+
preprocessor = Preprocessor()
|
59 |
+
return preprocessor.clean_text(raw_text)
|
60 |
+
return raw_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
# Interfaz din谩mica
|
63 |
with gr.Blocks() as interface:
|
|
|
67 |
with gr.Row():
|
68 |
with gr.Column(scale=2):
|
69 |
input_text = gr.Textbox(lines=9, label="Ingrese texto", interactive=True)
|
70 |
+
with gr.Row():
|
71 |
+
load_example_button = gr.Button("Load Example")
|
72 |
+
upload_file_button = gr.Button("Upload File")
|
73 |
with gr.Column(scale=1):
|
74 |
file = gr.File(label="Subir archivo (PDF, TXT)")
|
75 |
|
76 |
+
# Acciones de botones
|
77 |
+
load_example_button.click(
|
78 |
+
load_example_text,
|
79 |
+
inputs=[],
|
80 |
outputs=[input_text],
|
81 |
)
|
82 |
|
83 |
+
upload_file_button.click(
|
84 |
+
process_uploaded_file,
|
|
|
|
|
|
|
|
|
85 |
inputs=[file],
|
86 |
+
outputs=[input_text],
|
87 |
)
|
88 |
|
89 |
# Selecci贸n de tipo de resumen y opciones din谩micas
|