Update app.py
Browse files
app.py
CHANGED
|
@@ -22,54 +22,56 @@ def load_names_from_url(jsonld_url):
|
|
| 22 |
jsonld_url = 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld'
|
| 23 |
names = load_names_from_url(jsonld_url)
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
print("Consulta SPARQL carregada...")
|
| 34 |
-
|
| 35 |
-
# Executa a consulta SPARQL
|
| 36 |
-
qres = g.query(qtext)
|
| 37 |
-
|
| 38 |
-
# Cria o gráfico de rede
|
| 39 |
G = nx.DiGraph()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# Processa os resultados da consulta
|
| 44 |
-
for row in qres:
|
| 45 |
-
s, p, o = row
|
| 46 |
-
G.add_node(str(s), label=str(s).split('/')[-1]) # Usar o último segmento da URI como rótulo
|
| 47 |
-
G.add_node(str(o), label=str(o).split('/')[-1])
|
| 48 |
-
G.add_edge(str(s), str(o), label=str(p).split('/')[-1])
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
# Define posições específicas para os nós importantes
|
| 51 |
-
pos =
|
| 52 |
-
|
| 53 |
-
"https://huggingface.co/spaces/histlearn/MachadodeAssis/Aden-geo": (-0.6, -0.5),
|
| 54 |
-
"http://dados.literaturabrasileira.ufsc.br/resource/O_imortal_(Machado_de_Assis)#o-imortal": (0.6, -0.5)
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
# Preencher posições para todos os nós se não definidos
|
| 58 |
-
for node in G.nodes():
|
| 59 |
-
if node not in pos:
|
| 60 |
-
pos[node] = (0, 0) # Posição padrão para nós não especificados
|
| 61 |
-
|
| 62 |
# Desenha o gráfico usando NetworkX e Matplotlib
|
| 63 |
-
plt.figure(figsize=(
|
| 64 |
nx.draw_networkx_nodes(G, pos, node_size=3000, node_color="skyblue", alpha=0.9)
|
| 65 |
nx.draw_networkx_edges(G, pos, width=2, alpha=0.5, edge_color='gray')
|
| 66 |
nx.draw_networkx_labels(G, pos, labels=nx.get_node_attributes(G, 'label'), font_size=9, font_color="black")
|
| 67 |
nx.draw_networkx_edge_labels(G, pos, edge_labels=nx.get_edge_attributes(G, 'label'), font_size=9, font_color="red")
|
| 68 |
|
| 69 |
-
plt.
|
| 70 |
-
plt.ylim(-1, 1)
|
| 71 |
-
|
| 72 |
-
plt.title("Resultado da Consulta SPARQL", size=15)
|
| 73 |
plt.axis('off')
|
| 74 |
|
| 75 |
# Salva o gráfico em um arquivo
|
|
@@ -84,33 +86,18 @@ def run_query_and_visualize(qtext, jsonld_url):
|
|
| 84 |
print("Gráfico gerado com sucesso.")
|
| 85 |
return graph_html
|
| 86 |
|
| 87 |
-
def update_query(selected_location):
|
| 88 |
-
return f"""
|
| 89 |
-
PREFIX schema: <http://schema.org/>
|
| 90 |
-
SELECT * WHERE {{
|
| 91 |
-
?s schema:name "{selected_location}" .
|
| 92 |
-
?s ?p ?o .
|
| 93 |
-
}}
|
| 94 |
-
"""
|
| 95 |
-
|
| 96 |
with gr.Blocks() as demo:
|
| 97 |
gr.Markdown("# Visualização de Query SPARQL")
|
| 98 |
|
| 99 |
with gr.Column():
|
| 100 |
selected_location = gr.Dropdown(choices=names, label="Selecione o Local")
|
| 101 |
-
|
| 102 |
-
run_button = gr.Button("Executar Consulta")
|
| 103 |
|
| 104 |
graph_output = gr.HTML()
|
| 105 |
|
| 106 |
-
def
|
| 107 |
-
return
|
| 108 |
-
|
| 109 |
-
selected_location.change(fn=on_location_change, inputs=selected_location, outputs=query_input)
|
| 110 |
-
|
| 111 |
-
def on_run_button_click(query):
|
| 112 |
-
return run_query_and_visualize(query, jsonld_url)
|
| 113 |
|
| 114 |
-
run_button.click(fn=on_run_button_click, inputs=[
|
| 115 |
|
| 116 |
demo.launch()
|
|
|
|
| 22 |
jsonld_url = 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld'
|
| 23 |
names = load_names_from_url(jsonld_url)
|
| 24 |
|
| 25 |
+
def build_graph_from_jsonld(jsonld_url, selected_name):
|
| 26 |
+
response = requests.get(jsonld_url)
|
| 27 |
+
data = response.json()
|
| 28 |
|
| 29 |
+
# Filtrar o local selecionado
|
| 30 |
+
selected_data = next((item for item in data if item['name'] == selected_name), None)
|
| 31 |
+
|
| 32 |
+
if not selected_data:
|
| 33 |
+
return "Local não encontrado."
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
G = nx.DiGraph()
|
| 36 |
+
|
| 37 |
+
# Adicionar nó do Place
|
| 38 |
+
place_id = selected_data['@id']
|
| 39 |
+
place_label = f"schema:Place\nName: {selected_data['name']}\nDescription: {selected_data['description'][:30]}..."
|
| 40 |
+
G.add_node(place_id, label=place_label)
|
| 41 |
+
|
| 42 |
+
# Adicionar nó de GeoCoordinates
|
| 43 |
+
geo_data = selected_data['geo']
|
| 44 |
+
geo_id = geo_data['@id']
|
| 45 |
+
geo_label = f"geo:SpatialThing\nLat: {geo_data['lat']}\nLong: {geo_data['long']}\nFeatureCode: {geo_data['gn:featureCode']}\nFeatureCodeName: {geo_data['gn:featureCodeName']}\nName: {geo_data['gn:name']}"
|
| 46 |
+
G.add_node(geo_id, label=geo_label)
|
| 47 |
+
G.add_edge(place_id, geo_id, label="schema:geo")
|
| 48 |
+
|
| 49 |
+
# Adicionar nós de CreativeWork
|
| 50 |
+
for work in selected_data.get('subjectOf', []):
|
| 51 |
+
work_id = work['@id']
|
| 52 |
+
work_label = f"schema:CreativeWork\nHeadline: {work['headline']}\nGenre: {work['genre']}\nDatePublished: {work['datePublished']}\nText: {work['text'][:30]}...\nLanguage: {work['inLanguage']}"
|
| 53 |
+
G.add_node(work_id, label=work_label)
|
| 54 |
+
G.add_edge(place_id, work_id, label="schema:subjectOf")
|
| 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)
|
| 66 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# Desenha o gráfico usando NetworkX e Matplotlib
|
| 68 |
+
plt.figure(figsize=(15, 10))
|
| 69 |
nx.draw_networkx_nodes(G, pos, node_size=3000, node_color="skyblue", alpha=0.9)
|
| 70 |
nx.draw_networkx_edges(G, pos, width=2, alpha=0.5, edge_color='gray')
|
| 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()
|