Update app.py
Browse files
app.py
CHANGED
@@ -37,7 +37,7 @@ def read_pdf(file_path):
|
|
37 |
text += pdf_reader.pages[page].extract_text()
|
38 |
return text
|
39 |
|
40 |
-
def summarize(file
|
41 |
# Leer el contenido del archivo subido
|
42 |
file_path = file.name
|
43 |
if file_path.endswith('.pdf'):
|
@@ -45,20 +45,13 @@ def summarize(file, summary_length):
|
|
45 |
else:
|
46 |
with open(file_path, 'r', encoding='utf-8') as f:
|
47 |
text = f.read()
|
48 |
-
|
49 |
-
|
50 |
-
length_instruction = "El resumen debe tener un m谩ximo de 100 palabras."
|
51 |
-
elif summary_length == 'Medio':
|
52 |
-
length_instruction = "El resumen debe tener un m谩ximo de 500 palabras."
|
53 |
-
else:
|
54 |
-
length_instruction = "El resumen debe tener un m谩ximo de 1000 palabras."
|
55 |
-
|
56 |
-
template = f'''
|
57 |
Por favor, lea detenidamente el siguiente documento:
|
58 |
<document>
|
59 |
-
{
|
60 |
</document>
|
61 |
-
Despu茅s de leer el documento, identifique los puntos clave y las ideas principales cubiertas en el texto. Organice estos puntos clave en una lista con vi帽etas concisa que resuma la informaci贸n esencial del documento.
|
62 |
Su objetivo es ser exhaustivo en la captura del contenido central del documento, mientras que tambi茅n es conciso en la expresi贸n de cada punto del resumen. Omita los detalles menores y conc茅ntrese en los temas centrales y hechos importantes.
|
63 |
'''
|
64 |
|
@@ -109,9 +102,9 @@ Aseg煤rese de que la traducci贸n sea precisa y conserve el significado original
|
|
109 |
|
110 |
return f"Prompt:\n{formatted_prompt}\n\nTraducci贸n:\n{translated_text.content}"
|
111 |
|
112 |
-
def process_file(file, action, target_language=None
|
113 |
if action == "Resumen":
|
114 |
-
return summarize(file
|
115 |
elif action == "Clasificar":
|
116 |
file_path = file.name
|
117 |
if file_path.endswith('.pdf'):
|
@@ -125,19 +118,6 @@ def process_file(file, action, target_language=None, summary_length=None):
|
|
125 |
else:
|
126 |
return "Acci贸n no v谩lida"
|
127 |
|
128 |
-
def download_text(output_text, filename='output.txt'):
|
129 |
-
if output_text:
|
130 |
-
file_path = Path(filename)
|
131 |
-
with open(file_path, 'w', encoding='utf-8') as f:
|
132 |
-
f.write(output_text)
|
133 |
-
return file_path
|
134 |
-
else:
|
135 |
-
return None
|
136 |
-
|
137 |
-
def create_download_file(output_text, filename='output.txt'):
|
138 |
-
file_path = download_text(output_text, filename)
|
139 |
-
return str(file_path) if file_path else None
|
140 |
-
|
141 |
# Crear la interfaz de Gradio
|
142 |
with gr.Blocks() as demo:
|
143 |
gr.Markdown("## Procesador de Documentos")
|
@@ -147,38 +127,20 @@ with gr.Blocks() as demo:
|
|
147 |
file = gr.File(label="Subir un archivo")
|
148 |
action = gr.Radio(label="Seleccione una acci贸n", choices=["Resumen", "Clasificar", "Traducir"])
|
149 |
target_language = gr.Dropdown(label="Seleccionar idioma de traducci贸n", choices=["en", "fr", "de"], visible=False)
|
150 |
-
summary_length = gr.Radio(label="Seleccione la longitud del resumen", choices=["Corto", "Medio", "Largo"], visible=False)
|
151 |
|
152 |
with gr.Column():
|
153 |
output_text = gr.Textbox(label="Resultado", lines=20)
|
154 |
|
155 |
-
def
|
156 |
if action == "Traducir":
|
157 |
-
return gr.update(visible=True)
|
158 |
-
elif action == "Resumen":
|
159 |
-
return gr.update(visible=False), gr.update(visible=True), gr.update(visible(False))
|
160 |
-
elif action == "Clasificar":
|
161 |
-
return gr.update(visible(False)), gr.update(visible(False)), gr.update(visible(True))
|
162 |
else:
|
163 |
-
return gr.update(visible
|
164 |
|
165 |
-
action.change(
|
166 |
|
167 |
submit_button = gr.Button("Procesar")
|
168 |
-
submit_button.click(process_file, inputs=[file, action, target_language
|
169 |
-
|
170 |
-
def generate_file():
|
171 |
-
summary_text = output_text.value
|
172 |
-
filename = 'translation.txt' if action.value == 'Traducir' else 'summary.txt'
|
173 |
-
file_path = download_text(summary_text, filename)
|
174 |
-
return file_path
|
175 |
-
|
176 |
-
download_button = gr.Button("Descargar Resultado")
|
177 |
-
download_button.click(
|
178 |
-
fn=generate_file,
|
179 |
-
inputs=[],
|
180 |
-
outputs=gr.File()
|
181 |
-
)
|
182 |
|
183 |
# Ejecutar la aplicaci贸n Gradio
|
184 |
demo.launch(share=True)
|
|
|
37 |
text += pdf_reader.pages[page].extract_text()
|
38 |
return text
|
39 |
|
40 |
+
def summarize(file):
|
41 |
# Leer el contenido del archivo subido
|
42 |
file_path = file.name
|
43 |
if file_path.endswith('.pdf'):
|
|
|
45 |
else:
|
46 |
with open(file_path, 'r', encoding='utf-8') as f:
|
47 |
text = f.read()
|
48 |
+
|
49 |
+
template = '''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
Por favor, lea detenidamente el siguiente documento:
|
51 |
<document>
|
52 |
+
{TEXT}
|
53 |
</document>
|
54 |
+
Despu茅s de leer el documento, identifique los puntos clave y las ideas principales cubiertas en el texto. Organice estos puntos clave en una lista con vi帽etas concisa que resuma la informaci贸n esencial del documento. El resumen debe tener un m谩ximo de 10 puntos.
|
55 |
Su objetivo es ser exhaustivo en la captura del contenido central del documento, mientras que tambi茅n es conciso en la expresi贸n de cada punto del resumen. Omita los detalles menores y conc茅ntrese en los temas centrales y hechos importantes.
|
56 |
'''
|
57 |
|
|
|
102 |
|
103 |
return f"Prompt:\n{formatted_prompt}\n\nTraducci贸n:\n{translated_text.content}"
|
104 |
|
105 |
+
def process_file(file, action, target_language=None):
|
106 |
if action == "Resumen":
|
107 |
+
return summarize(file)
|
108 |
elif action == "Clasificar":
|
109 |
file_path = file.name
|
110 |
if file_path.endswith('.pdf'):
|
|
|
118 |
else:
|
119 |
return "Acci贸n no v谩lida"
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
# Crear la interfaz de Gradio
|
122 |
with gr.Blocks() as demo:
|
123 |
gr.Markdown("## Procesador de Documentos")
|
|
|
127 |
file = gr.File(label="Subir un archivo")
|
128 |
action = gr.Radio(label="Seleccione una acci贸n", choices=["Resumen", "Clasificar", "Traducir"])
|
129 |
target_language = gr.Dropdown(label="Seleccionar idioma de traducci贸n", choices=["en", "fr", "de"], visible=False)
|
|
|
130 |
|
131 |
with gr.Column():
|
132 |
output_text = gr.Textbox(label="Resultado", lines=20)
|
133 |
|
134 |
+
def update_language_dropdown(action):
|
135 |
if action == "Traducir":
|
136 |
+
return gr.update(visible=True)
|
|
|
|
|
|
|
|
|
137 |
else:
|
138 |
+
return gr.update(visible=False)
|
139 |
|
140 |
+
action.change(update_language_dropdown, inputs=action, outputs=target_language)
|
141 |
|
142 |
submit_button = gr.Button("Procesar")
|
143 |
+
submit_button.click(process_file, inputs=[file, action, target_language], outputs=output_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
# Ejecutar la aplicaci贸n Gradio
|
146 |
demo.launch(share=True)
|