Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import spacy
|
|
| 3 |
from graphviz import Digraph
|
| 4 |
import os
|
| 5 |
import uuid
|
|
|
|
| 6 |
|
| 7 |
# Cargar modelo de spaCy
|
| 8 |
nlp = spacy.load("es_dep_news_trf")
|
|
@@ -46,7 +47,28 @@ def generar_grafico_dependencia(texto):
|
|
| 46 |
dot.render(filename=nombre_archivo, format='png', cleanup=True)
|
| 47 |
rutas_imagenes.append(nombre_archivo + ".png") # Graphviz añade extensión
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Ejemplos
|
| 52 |
ejemplos = [
|
|
@@ -59,7 +81,7 @@ ejemplos = [
|
|
| 59 |
|
| 60 |
CSS = """
|
| 61 |
.contain { display: flex; flex-direction: column; }
|
| 62 |
-
.gallery-container { height: calc(100vh -
|
| 63 |
#component-0 { height: 100%; }
|
| 64 |
#gallery { flex-grow: 1; overflow: auto;}
|
| 65 |
"""
|
|
@@ -68,7 +90,7 @@ CSS = """
|
|
| 68 |
with gr.Blocks(title="Visualización de Dependencias Sintácticas", theme=gr.themes.Ocean(), css=CSS) as demo:
|
| 69 |
gr.Markdown("# 🌐 Visualización de Dependencias Sintácticas en Español")
|
| 70 |
gr.Markdown("Introduce un texto en español para ver el árbol de dependencias generado con spaCy y Graphviz.")
|
| 71 |
-
|
| 72 |
with gr.Row():
|
| 73 |
with gr.Column(scale=1):
|
| 74 |
texto_input = gr.Textbox(lines=4, label="Texto en español", placeholder="Escribe aquí tu frase...")
|
|
@@ -76,13 +98,29 @@ with gr.Blocks(title="Visualización de Dependencias Sintácticas", theme=gr.the
|
|
| 76 |
gr.Examples(
|
| 77 |
examples=ejemplos,
|
| 78 |
inputs=texto_input,
|
|
|
|
| 79 |
fn=generar_grafico_dependencia,
|
| 80 |
label="Ejemplos de texto"
|
| 81 |
)
|
|
|
|
| 82 |
with gr.Column(scale=2):
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
boton.click(fn=generar_grafico_dependencia, inputs=texto_input, outputs=galeria)
|
| 86 |
|
| 87 |
# Lanzar app
|
| 88 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from graphviz import Digraph
|
| 4 |
import os
|
| 5 |
import uuid
|
| 6 |
+
import pandas as pd
|
| 7 |
|
| 8 |
# Cargar modelo de spaCy
|
| 9 |
nlp = spacy.load("es_dep_news_trf")
|
|
|
|
| 47 |
dot.render(filename=nombre_archivo, format='png', cleanup=True)
|
| 48 |
rutas_imagenes.append(nombre_archivo + ".png") # Graphviz añade extensión
|
| 49 |
|
| 50 |
+
# Crear tabla de atributos
|
| 51 |
+
df = pd.DataFrame([{
|
| 52 |
+
"idx": token.i,
|
| 53 |
+
"text": token.text,
|
| 54 |
+
"lemma_": token.lemma_,
|
| 55 |
+
"pos_": token.pos_,
|
| 56 |
+
"tag_": token.tag_,
|
| 57 |
+
"dep_": token.dep_,
|
| 58 |
+
"head": token.head.text,
|
| 59 |
+
"morph": str(token.morph),
|
| 60 |
+
"ent_type_": token.ent_type_,
|
| 61 |
+
"ent_iob_": token.ent_iob_,
|
| 62 |
+
"shape_": token.shape_,
|
| 63 |
+
"is_alpha": token.is_alpha,
|
| 64 |
+
"is_ascii": token.is_ascii,
|
| 65 |
+
"is_digit": token.is_digit,
|
| 66 |
+
"is_punct": token.is_punct,
|
| 67 |
+
"like_num": token.like_num,
|
| 68 |
+
"is_sent_start": str(token.is_sent_start) if token.is_sent_start is not None else "None"
|
| 69 |
+
} for token in doc])
|
| 70 |
+
|
| 71 |
+
return rutas_imagenes, df
|
| 72 |
|
| 73 |
# Ejemplos
|
| 74 |
ejemplos = [
|
|
|
|
| 81 |
|
| 82 |
CSS = """
|
| 83 |
.contain { display: flex; flex-direction: column; }
|
| 84 |
+
.gallery-container { height: calc(100vh - 250px) !important; }
|
| 85 |
#component-0 { height: 100%; }
|
| 86 |
#gallery { flex-grow: 1; overflow: auto;}
|
| 87 |
"""
|
|
|
|
| 90 |
with gr.Blocks(title="Visualización de Dependencias Sintácticas", theme=gr.themes.Ocean(), css=CSS) as demo:
|
| 91 |
gr.Markdown("# 🌐 Visualización de Dependencias Sintácticas en Español")
|
| 92 |
gr.Markdown("Introduce un texto en español para ver el árbol de dependencias generado con spaCy y Graphviz.")
|
| 93 |
+
|
| 94 |
with gr.Row():
|
| 95 |
with gr.Column(scale=1):
|
| 96 |
texto_input = gr.Textbox(lines=4, label="Texto en español", placeholder="Escribe aquí tu frase...")
|
|
|
|
| 98 |
gr.Examples(
|
| 99 |
examples=ejemplos,
|
| 100 |
inputs=texto_input,
|
| 101 |
+
# outputs=galeria,
|
| 102 |
fn=generar_grafico_dependencia,
|
| 103 |
label="Ejemplos de texto"
|
| 104 |
)
|
| 105 |
+
|
| 106 |
with gr.Column(scale=2):
|
| 107 |
+
with gr.Tab("visualización"):
|
| 108 |
+
galeria = gr.Gallery(label="Gráfico(s) generado(s)", show_label=True, columns=4, height="auto", elem_id="gallery")
|
| 109 |
+
with gr.Tab("tabla"):
|
| 110 |
+
tabla = gr.Dataframe(
|
| 111 |
+
headers=[
|
| 112 |
+
"idx", "text", "lemma_", "pos_", "tag_", "dep_", "head", "morph",
|
| 113 |
+
"ent_type_", "ent_iob_", "shape_", "is_alpha", "is_ascii",
|
| 114 |
+
"is_digit", "is_punct", "like_num", "is_sent_start"
|
| 115 |
+
],
|
| 116 |
+
label="Atributos de tokens",
|
| 117 |
+
interactive=False,
|
| 118 |
+
type="pandas",
|
| 119 |
+
row_count=5,
|
| 120 |
+
max_height="600"
|
| 121 |
+
)
|
| 122 |
|
| 123 |
+
boton.click(fn=generar_grafico_dependencia, inputs=texto_input, outputs=[galeria, tabla])
|
| 124 |
|
| 125 |
# Lanzar app
|
| 126 |
if __name__ == "__main__":
|