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,13 +45,20 @@ def summarize(file):
|
|
| 45 |
else:
|
| 46 |
with open(file_path, 'r', encoding='utf-8') as f:
|
| 47 |
text = f.read()
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 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,9 +109,9 @@ Aseg煤rese de que la traducci贸n sea precisa y conserve el significado original
|
|
| 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'):
|
|
@@ -126,21 +133,37 @@ with gr.Blocks() as demo:
|
|
| 126 |
with gr.Column():
|
| 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
|
| 135 |
if action == "Traducir":
|
| 136 |
-
return gr.update(visible=True)
|
|
|
|
|
|
|
| 137 |
else:
|
| 138 |
-
return gr.update(visible
|
| 139 |
|
| 140 |
-
action.change(
|
| 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)
|
|
|
|
| 37 |
text += pdf_reader.pages[page].extract_text()
|
| 38 |
return text
|
| 39 |
|
| 40 |
+
def summarize(file, summary_length):
|
| 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 |
+
if summary_length == 'Corto':
|
| 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 |
+
{{TEXT}}
|
| 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. {length_instruction}
|
| 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 |
|
| 110 |
return f"Prompt:\n{formatted_prompt}\n\nTraducci贸n:\n{translated_text.content}"
|
| 111 |
|
| 112 |
+
def process_file(file, action, target_language=None, summary_length=None):
|
| 113 |
if action == "Resumen":
|
| 114 |
+
return summarize(file, summary_length)
|
| 115 |
elif action == "Clasificar":
|
| 116 |
file_path = file.name
|
| 117 |
if file_path.endswith('.pdf'):
|
|
|
|
| 133 |
with gr.Column():
|
| 134 |
file = gr.File(label="Subir un archivo")
|
| 135 |
action = gr.Radio(label="Seleccione una acci贸n", choices=["Resumen", "Clasificar", "Traducir"])
|
| 136 |
+
summary_length = gr.Radio(label="Seleccione la longitud del resumen", choices=["Corto", "Medio", "Largo"], visible=False)
|
| 137 |
target_language = gr.Dropdown(label="Seleccionar idioma de traducci贸n", choices=["en", "fr", "de"], visible=False)
|
| 138 |
|
| 139 |
with gr.Column():
|
| 140 |
output_text = gr.Textbox(label="Resultado", lines=20)
|
| 141 |
|
| 142 |
+
def update_ui(action):
|
| 143 |
if action == "Traducir":
|
| 144 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 145 |
+
elif action == "Resumen":
|
| 146 |
+
return gr.update(visible=True), gr.update(visible=False)
|
| 147 |
else:
|
| 148 |
+
return gr.update(visible(False)), gr.update(visible(False))
|
| 149 |
|
| 150 |
+
action.change(update_ui, inputs=action, outputs=[summary_length, target_language])
|
| 151 |
|
| 152 |
submit_button = gr.Button("Procesar")
|
| 153 |
+
submit_button.click(process_file, inputs=[file, action, target_language, summary_length], outputs=output_text)
|
| 154 |
+
|
| 155 |
+
def generate_file():
|
| 156 |
+
summary_text = output_text.value
|
| 157 |
+
filename = 'translation.txt' if action.value == 'Traducir' else 'summary.txt'
|
| 158 |
+
file_path = download_text(summary_text, filename)
|
| 159 |
+
return file_path
|
| 160 |
+
|
| 161 |
+
download_button = gr.Button("Descargar Resultado")
|
| 162 |
+
download_button.click(
|
| 163 |
+
fn=generate_file,
|
| 164 |
+
inputs=[],
|
| 165 |
+
outputs=gr.File()
|
| 166 |
+
)
|
| 167 |
|
| 168 |
# Ejecutar la aplicaci贸n Gradio
|
| 169 |
demo.launch(share=True)
|