histlearn commited on
Commit
5291db6
·
verified ·
1 Parent(s): 048e9c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -43
app.py CHANGED
@@ -55,30 +55,11 @@ def build_graph_from_jsonld(jsonld_url, selected_name):
55
 
56
  return G
57
 
58
- def run_query_and_visualize(qtext, jsonld_url):
59
- print("Executando consulta SPARQL...")
60
- print(f"Consulta SPARQL: {qtext}")
61
-
62
- # Carrega o arquivo JSON-LD
63
- g = rdflib.Graph()
64
- g.parse(jsonld_url, format="json-ld")
65
-
66
- print("Consulta SPARQL carregada...")
67
-
68
- # Executa a consulta SPARQL
69
- qres = g.query(qtext)
70
-
71
- # Cria o gráfico de rede
72
- G = nx.DiGraph()
73
 
74
- print("Processando resultados da consulta...")
75
-
76
- # Processa os resultados da consulta
77
- for row in qres:
78
- s, p, o = row
79
- G.add_node(str(s), label=str(s).split('/')[-1]) # Usar o último segmento da URI como rótulo
80
- G.add_node(str(o), label=str(o).split('/')[-1])
81
- G.add_edge(str(s), str(o), label=str(p).split('/')[-1])
82
 
83
  # Define posições específicas para os nós importantes
84
  pos = nx.spring_layout(G)
@@ -90,7 +71,7 @@ def run_query_and_visualize(qtext, jsonld_url):
90
  nx.draw_networkx_labels(G, pos, labels=nx.get_node_attributes(G, 'label'), font_size=9, font_color="black")
91
  nx.draw_networkx_edge_labels(G, pos, edge_labels=nx.get_edge_attributes(G, 'label'), font_size=9, font_color="red")
92
 
93
- plt.title("Resultado da Consulta SPARQL", size=15)
94
  plt.axis('off')
95
 
96
  # Salva o gráfico em um arquivo
@@ -105,33 +86,18 @@ def run_query_and_visualize(qtext, jsonld_url):
105
  print("Gráfico gerado com sucesso.")
106
  return graph_html
107
 
108
- def update_query(selected_location):
109
- return f"""
110
- PREFIX schema: <http://schema.org/>
111
- SELECT * WHERE {{
112
- ?s schema:name "{selected_location}" .
113
- ?s ?p ?o .
114
- }}
115
- """
116
-
117
  with gr.Blocks() as demo:
118
  gr.Markdown("# Visualização de Query SPARQL")
119
 
120
  with gr.Column():
121
  selected_location = gr.Dropdown(choices=names, label="Selecione o Local")
122
- query_input = gr.Textbox(label="Consulta SPARQL", value=update_query(names[0]) if names else "", lines=10)
123
- run_button = gr.Button("Executar Consulta")
124
 
125
  graph_output = gr.HTML()
126
 
127
- def on_location_change(loc):
128
- return update_query(loc)
129
-
130
- selected_location.change(fn=on_location_change, inputs=selected_location, outputs=query_input)
131
-
132
- def on_run_button_click(query):
133
- return run_query_and_visualize(query, jsonld_url)
134
 
135
- run_button.click(fn=on_run_button_click, inputs=[query_input], outputs=graph_output)
136
 
137
  demo.launch()
 
55
 
56
  return G
57
 
58
+ def run_query_and_visualize(selected_location, jsonld_url):
59
+ G = build_graph_from_jsonld(jsonld_url, selected_location)
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ if isinstance(G, str): # Caso de erro
62
+ return G
 
 
 
 
 
 
63
 
64
  # Define posições específicas para os nós importantes
65
  pos = nx.spring_layout(G)
 
71
  nx.draw_networkx_labels(G, pos, labels=nx.get_node_attributes(G, 'label'), font_size=9, font_color="black")
72
  nx.draw_networkx_edge_labels(G, pos, edge_labels=nx.get_edge_attributes(G, 'label'), font_size=9, font_color="red")
73
 
74
+ plt.title("Resultado da Consulta", size=15)
75
  plt.axis('off')
76
 
77
  # Salva o gráfico em um arquivo
 
86
  print("Gráfico gerado com sucesso.")
87
  return graph_html
88
 
 
 
 
 
 
 
 
 
 
89
  with gr.Blocks() as demo:
90
  gr.Markdown("# Visualização de Query SPARQL")
91
 
92
  with gr.Column():
93
  selected_location = gr.Dropdown(choices=names, label="Selecione o Local")
94
+ run_button = gr.Button("Visualizar Grafo")
 
95
 
96
  graph_output = gr.HTML()
97
 
98
+ def on_run_button_click(selected_location):
99
+ return run_query_and_visualize(selected_location, jsonld_url)
 
 
 
 
 
100
 
101
+ run_button.click(fn=on_run_button_click, inputs=[selected_location], outputs=graph_output)
102
 
103
  demo.launch()