geronimo-pericoli commited on
Commit
e4e7d08
verified
1 Parent(s): 245c3b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -31
app.py CHANGED
@@ -19,9 +19,7 @@ from llama_index.llms.openai import OpenAI
19
  from llama_index.embeddings.openai import OpenAIEmbedding
20
  from llama_index.core import Settings
21
 
22
- import openai
23
  import os
24
- from github import Github
25
  from datetime import datetime
26
  import gradio as gr
27
  import pandas as pd
@@ -34,38 +32,49 @@ exec(os.environ.get('context'))
34
 
35
 
36
  ##### Graph start ##########
37
- import networkx as nx
38
- import matplotlib.pyplot as plt
39
- from PIL import Image
40
- from io import BytesIO
41
 
42
  def draw_graph():
43
  global kg_data
44
- G = nx.DiGraph()
 
 
 
 
 
 
 
 
 
 
 
45
  for source, relation, target in kg_data:
46
- G.add_edge(source, target, label=relation)
47
-
48
- # Utilizar spring_layout para mejorar la disposici贸n de los nodos
49
- pos = nx.spring_layout(G)
50
-
51
- plt.figure(figsize=(12, 8))
52
- # Ajustar el tama帽o de los nodos
53
- nx.draw(G, pos, with_labels=True, node_color='skyblue', node_size=400, edge_color='k', linewidths=1, font_size=8, font_weight='bold')
54
- # Ajustar el tama帽o de las flechas y el espaciado entre ellas
55
- edge_labels = {}
56
- for source, target, data in G.edges(data=True):
57
- if 'label' in data:
58
- edge_labels[(source, target)] = data['label']
59
- nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=7, font_weight='normal')
60
- plt.title("Graph")
61
- plt.axis('off')
62
-
63
- buf = BytesIO()
64
- plt.savefig(buf, format='png')
65
- buf.seek(0)
66
- plt.close()
67
-
68
- return Image.open(buf)
 
69
  ##### Graph end ##########
70
 
71
 
@@ -716,7 +725,7 @@ with gr.Blocks(theme='sudeepshouche/minimalist', css=css) as demo:
716
  """)
717
 
718
  with gr.Row():
719
- grafo = gr.Image(label="Grafo", show_share_button=False)
720
 
721
  with gr.Accordion(elem_classes="accordion", label="Audit trail", open=False):
722
  with gr.Row():
 
19
  from llama_index.embeddings.openai import OpenAIEmbedding
20
  from llama_index.core import Settings
21
 
 
22
  import os
 
23
  from datetime import datetime
24
  import gradio as gr
25
  import pandas as pd
 
32
 
33
 
34
  ##### Graph start ##########
35
+ from pyvis.network import Network
36
+ from io import StringIO
37
+ import html
 
38
 
39
  def draw_graph():
40
  global kg_data
41
+
42
+ # Crear un grafo con opciones visuales b谩sicas
43
+ net = Network(
44
+ notebook=True,
45
+ height="700px",
46
+ width="100%",
47
+ select_menu=True,
48
+ neighborhood_highlight=True,
49
+ cdn_resources="remote"
50
+ )
51
+
52
+ # A帽adir nodos y relaciones desde los datos del grafo
53
  for source, relation, target in kg_data:
54
+ # Agregar nodos
55
+ net.add_node(source, label=source, color="skyblue")
56
+ net.add_node(target, label=target, color="skyblue")
57
+ # Agregar aristas con la relaci贸n como etiqueta
58
+ net.add_edge(source, target, title=relation, label=relation, color="black")
59
+
60
+ # Generar el HTML como string
61
+ html_output = StringIO()
62
+ net.write_html(html_output)
63
+ html_output.seek(0)
64
+ html_contenido = html_output.getvalue()
65
+
66
+ # Generar el iframe embebido
67
+ return generar_iframe_embebido(html_contenido)
68
+
69
+ def generar_iframe_embebido(html_contenido):
70
+ # Escapar el contenido HTML para que sea seguro dentro del atributo srcdoc
71
+ html_escapado = html.escape(html_contenido)
72
+
73
+ # Generar el string del iframe con el contenido embebido
74
+ iframe = f"""
75
+ <iframe srcdoc="{html_escapado}" width="100%" height="700px" style="border:none;"></iframe>
76
+ """
77
+ return iframe
78
  ##### Graph end ##########
79
 
80
 
 
725
  """)
726
 
727
  with gr.Row():
728
+ grafo = gr.HTML(label="Grafo", show_share_button=False)
729
 
730
  with gr.Accordion(elem_classes="accordion", label="Audit trail", open=False):
731
  with gr.Row():