|
import gradio as gr |
|
import rdflib |
|
import requests |
|
import matplotlib.pyplot as plt |
|
import networkx as nx |
|
from io import BytesIO |
|
import base64 |
|
|
|
|
|
def load_names_from_url(jsonld_url): |
|
response = requests.get(jsonld_url) |
|
data = response.json() |
|
|
|
names = [] |
|
for item in data: |
|
if 'name' in item: |
|
names.append(item['name']) |
|
|
|
return names |
|
|
|
|
|
jsonld_url = 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld' |
|
names = load_names_from_url(jsonld_url) |
|
|
|
def run_query_and_visualize(qtext, jsonld_url): |
|
print("Executando consulta SPARQL...") |
|
print(f"Consulta SPARQL: {qtext}") |
|
|
|
|
|
g = rdflib.Graph() |
|
g.parse(jsonld_url, format="json-ld") |
|
|
|
print("Consulta SPARQL carregada...") |
|
|
|
|
|
qres = g.query(qtext) |
|
|
|
|
|
G = nx.DiGraph() |
|
|
|
print("Processando resultados da consulta...") |
|
|
|
|
|
for row in qres: |
|
s, p, o = row |
|
G.add_node(str(s), label=str(s).split('/')[-1]) |
|
G.add_node(str(o), label=str(o).split('/')[-1]) |
|
G.add_edge(str(s), str(o), label=str(p).split('/')[-1]) |
|
|
|
|
|
pos = { |
|
"Adem": (0, 0.6), |
|
"Adem-geo": (-0.4, -0.3), |
|
"Adem-obra": (0.4, -0.1) |
|
} |
|
|
|
plt.figure(figsize=(10, 8)) |
|
nx.draw_networkx_nodes(G, pos, node_size=3000, node_color="skyblue", alpha=0.9) |
|
nx.draw_networkx_edges(G, pos, width=2, alpha=0.5, edge_color='gray') |
|
nx.draw_networkx_labels(G, pos, labels=nx.get_node_attributes(G, 'label'), font_size=9, font_color="black") |
|
nx.draw_networkx_edge_labels(G, pos, edge_labels=nx.get_edge_attributes(G, 'label'), font_size=9, font_color="red") |
|
|
|
plt.xlim(-1, 1) |
|
plt.ylim(-1, 1) |
|
|
|
plt.title("Resultado da Consulta SPARQL", size=15) |
|
plt.axis('off') |
|
|
|
|
|
buf = BytesIO() |
|
plt.savefig(buf, format='png') |
|
buf.seek(0) |
|
img_str = base64.b64encode(buf.read()).decode() |
|
graph_html = f'<img src="data:image/png;base64,{img_str}"/>' |
|
|
|
plt.close() |
|
|
|
print("Gráfico gerado com sucesso.") |
|
return graph_html |
|
|
|
def update_query(selected_location): |
|
return f""" |
|
PREFIX schema: <http://schema.org/> |
|
SELECT * WHERE {{ |
|
?s schema:name "{selected_location}" . |
|
?s ?p ?o . |
|
}} |
|
""" |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Visualização de Query SPARQL") |
|
|
|
with gr.Column(): |
|
selected_location = gr.Dropdown(choices=names, label="Selecione o Local") |
|
query_input = gr.Textbox(label="Consulta SPARQL", value=update_query(names[0]) if names else "", lines=10) |
|
run_button = gr.Button("Executar Consulta") |
|
|
|
graph_output = gr.HTML() |
|
|
|
def on_location_change(loc): |
|
return update_query(loc) |
|
|
|
selected_location.change(fn=on_location_change, inputs=selected_location, outputs=query_input) |
|
|
|
def on_run_button_click(query): |
|
return run_query_and_visualize(query, jsonld_url) |
|
|
|
run_button.click(fn=on_run_button_click, inputs=[query_input], outputs=graph_output) |
|
|
|
demo.launch() |
|
|