Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import rdflib
|
| 3 |
import requests
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Função para carregar e extrair os nomes do arquivo JSON-LD a partir de uma URL
|
| 7 |
def load_names_from_url(jsonld_url):
|
|
@@ -32,28 +35,38 @@ def run_query_and_visualize(qtext, jsonld_url):
|
|
| 32 |
# Executa a consulta SPARQL
|
| 33 |
qres = g.query(qtext)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
nodes = set()
|
| 38 |
|
| 39 |
print("Processando resultados da consulta...")
|
| 40 |
|
| 41 |
# Processa os resultados da consulta
|
| 42 |
for row in qres:
|
| 43 |
s, p, o = row
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
if str(o) not in nodes:
|
| 48 |
-
net.add_node(str(o), label=str(o))
|
| 49 |
-
nodes.add(str(o))
|
| 50 |
-
net.add_edge(str(s), str(o), title=str(p))
|
| 51 |
-
|
| 52 |
-
# Gera o gráfico e salva em um arquivo HTML
|
| 53 |
-
net.show("graph.html")
|
| 54 |
-
with open("graph.html", "r") as file:
|
| 55 |
-
graph_html = file.read()
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
print("Gráfico gerado com sucesso.")
|
| 58 |
return graph_html
|
| 59 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import rdflib
|
| 3 |
import requests
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import networkx as nx
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import base64
|
| 8 |
|
| 9 |
# Função para carregar e extrair os nomes do arquivo JSON-LD a partir de uma URL
|
| 10 |
def load_names_from_url(jsonld_url):
|
|
|
|
| 35 |
# Executa a consulta SPARQL
|
| 36 |
qres = g.query(qtext)
|
| 37 |
|
| 38 |
+
# Cria o gráfico de rede
|
| 39 |
+
G = nx.DiGraph()
|
|
|
|
| 40 |
|
| 41 |
print("Processando resultados da consulta...")
|
| 42 |
|
| 43 |
# Processa os resultados da consulta
|
| 44 |
for row in qres:
|
| 45 |
s, p, o = row
|
| 46 |
+
G.add_node(str(s))
|
| 47 |
+
G.add_node(str(o))
|
| 48 |
+
G.add_edge(str(s), str(o), label=str(p))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Desenha o gráfico usando NetworkX e Matplotlib
|
| 51 |
+
pos = nx.spring_layout(G, k=0.5)
|
| 52 |
+
plt.figure(figsize=(15, 10))
|
| 53 |
+
|
| 54 |
+
nx.draw(G, pos, with_labels=True, node_size=5000, node_color="skyblue", font_size=10, font_color="black", font_weight="bold", edge_color="gray", width=2, alpha=0.7)
|
| 55 |
+
edge_labels = nx.get_edge_attributes(G, 'label')
|
| 56 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color="red", font_size=8)
|
| 57 |
+
|
| 58 |
+
plt.title("Resultado da Consulta SPARQL", size=15)
|
| 59 |
+
plt.box(False)
|
| 60 |
+
|
| 61 |
+
# Salva o gráfico em um arquivo
|
| 62 |
+
buf = BytesIO()
|
| 63 |
+
plt.savefig(buf, format='png')
|
| 64 |
+
buf.seek(0)
|
| 65 |
+
img_str = base64.b64encode(buf.read()).decode()
|
| 66 |
+
graph_html = f'<img src="data:image/png;base64,{img_str}"/>'
|
| 67 |
+
|
| 68 |
+
plt.close()
|
| 69 |
+
|
| 70 |
print("Gráfico gerado com sucesso.")
|
| 71 |
return graph_html
|
| 72 |
|