File size: 2,675 Bytes
695363c f3dbd83 695363c 2e7c89d f3dbd83 695363c 2d000f8 2e7c89d 695363c 2e7c89d 2d000f8 695363c 2e7c89d 7b61abf d1cb3da 695363c 2e7c89d 7b61abf 695363c a12b796 695363c 7b61abf 695363c a12b796 695363c a12b796 3a02f13 a12b796 7b61abf a12b796 695363c 2e7c89d 695363c f3dbd83 695363c d1cb3da 695363c d1cb3da 695363c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import gradio as gr
import rdflib
import requests
from pyvis.network import Network
# Função para carregar e extrair os nomes do arquivo JSON-LD a partir de uma URL
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
# Carregar nomes do arquivo JSON-LD
names = load_names_from_url('https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld')
def run_query_and_visualize(qtext, jsonld_url):
print("Executando consulta SPARQL...")
print(f"Consulta SPARQL: {qtext}")
# Carrega o arquivo JSON-LD
g = rdflib.Graph()
g.parse(jsonld_url, format="json-ld")
print("Consulta SPARQL carregada...")
# Executa a consulta SPARQL
qres = g.query(qtext)
# Prepara o gráfico com recursos remotos
net = Network(notebook=False, height="400px", width="100%", cdn_resources='remote')
nodes = set()
print("Processando resultados da consulta...")
# Processa os resultados da consulta
for row in qres:
s, p, o = row
if str(s) not in nodes:
net.add_node(str(s), label=str(s))
nodes.add(str(s))
if str(o) not in nodes:
net.add_node(str(o), label=str(o))
nodes.add(str(o))
net.add_edge(str(s), str(o), title=str(p))
# Gera o gráfico e salva em um arquivo HTML
net.show("graph.html")
with open("graph.html", "r") as file:
graph_html = file.read()
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, 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld')
run_button.click(fn=on_run_button_click, inputs=[query_input], outputs=graph_output)
demo.launch()
|