Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,28 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import rdflib
|
| 3 |
-
import json
|
| 4 |
import requests
|
| 5 |
from pyvis.network import Network
|
| 6 |
from IPython.display import HTML
|
| 7 |
|
| 8 |
-
# Função para carregar e extrair os nomes do arquivo JSON-LD a partir de
|
| 9 |
-
def load_names_from_url(
|
| 10 |
-
response = requests.get(
|
| 11 |
data = response.json()
|
| 12 |
|
| 13 |
-
print("Data received from URL:", data) # Adicionando impressão para depuração
|
| 14 |
-
|
| 15 |
names = []
|
| 16 |
for item in data:
|
| 17 |
-
if
|
| 18 |
-
|
| 19 |
-
if 'name' in entry:
|
| 20 |
-
names.append(entry['name'])
|
| 21 |
|
| 22 |
return names
|
| 23 |
|
| 24 |
-
# Carregar nomes do arquivo JSON-LD
|
| 25 |
names = load_names_from_url('https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld')
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def run_query_and_visualize(qtext, jsonld_file):
|
| 30 |
# Carrega o arquivo JSON-LD
|
| 31 |
g = rdflib.Graph()
|
| 32 |
-
g.parse(
|
| 33 |
|
| 34 |
# Executa a consulta SPARQL
|
| 35 |
qres = g.query(qtext)
|
|
@@ -45,15 +38,13 @@ def run_query_and_visualize(qtext, jsonld_file):
|
|
| 45 |
net.add_node(str(s), label=str(s))
|
| 46 |
nodes.add(str(s))
|
| 47 |
if str(o) not in nodes:
|
| 48 |
-
net.add_node(str(o), label
|
| 49 |
nodes.add(str(o))
|
| 50 |
net.add_edge(str(s), str(o), title=str(p))
|
| 51 |
|
| 52 |
# Gera o gráfico e o exibe diretamente na célula do notebook
|
| 53 |
net.show("graph.html")
|
| 54 |
-
|
| 55 |
-
graph_html = file.read()
|
| 56 |
-
return graph_html
|
| 57 |
|
| 58 |
def update_query(selected_location):
|
| 59 |
return f"""
|
|
@@ -69,10 +60,7 @@ with gr.Blocks() as demo:
|
|
| 69 |
|
| 70 |
with gr.Column():
|
| 71 |
selected_location = gr.Dropdown(choices=names, label="Selecione o Local")
|
| 72 |
-
|
| 73 |
-
query_input = gr.Textbox(label="Consulta SPARQL", value=update_query(names[0]), lines=10)
|
| 74 |
-
else:
|
| 75 |
-
query_input = gr.Textbox(label="Consulta SPARQL", value="", lines=10)
|
| 76 |
run_button = gr.Button("Executar Consulta")
|
| 77 |
|
| 78 |
graph_output = gr.HTML()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import rdflib
|
|
|
|
| 3 |
import requests
|
| 4 |
from pyvis.network import Network
|
| 5 |
from IPython.display import HTML
|
| 6 |
|
| 7 |
+
# Função para carregar e extrair os nomes do arquivo JSON-LD a partir de uma URL
|
| 8 |
+
def load_names_from_url(jsonld_url):
|
| 9 |
+
response = requests.get(jsonld_url)
|
| 10 |
data = response.json()
|
| 11 |
|
|
|
|
|
|
|
| 12 |
names = []
|
| 13 |
for item in data:
|
| 14 |
+
if 'name' in item:
|
| 15 |
+
names.append(item['name'])
|
|
|
|
|
|
|
| 16 |
|
| 17 |
return names
|
| 18 |
|
| 19 |
+
# Carregar nomes do arquivo JSON-LD
|
| 20 |
names = load_names_from_url('https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld')
|
| 21 |
|
| 22 |
+
def run_query_and_visualize(qtext, jsonld_url):
|
|
|
|
|
|
|
| 23 |
# Carrega o arquivo JSON-LD
|
| 24 |
g = rdflib.Graph()
|
| 25 |
+
g.parse(jsonld_url, format="json-ld")
|
| 26 |
|
| 27 |
# Executa a consulta SPARQL
|
| 28 |
qres = g.query(qtext)
|
|
|
|
| 38 |
net.add_node(str(s), label=str(s))
|
| 39 |
nodes.add(str(s))
|
| 40 |
if str(o) not in nodes:
|
| 41 |
+
net.add_node(str(o), label(str(o)))
|
| 42 |
nodes.add(str(o))
|
| 43 |
net.add_edge(str(s), str(o), title=str(p))
|
| 44 |
|
| 45 |
# Gera o gráfico e o exibe diretamente na célula do notebook
|
| 46 |
net.show("graph.html")
|
| 47 |
+
return HTML("graph.html")
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def update_query(selected_location):
|
| 50 |
return f"""
|
|
|
|
| 60 |
|
| 61 |
with gr.Column():
|
| 62 |
selected_location = gr.Dropdown(choices=names, label="Selecione o Local")
|
| 63 |
+
query_input = gr.Textbox(label="Consulta SPARQL", value=update_query(names[0]) if names else "", lines=10)
|
|
|
|
|
|
|
|
|
|
| 64 |
run_button = gr.Button("Executar Consulta")
|
| 65 |
|
| 66 |
graph_output = gr.HTML()
|