Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import rdflib
|
| 3 |
import requests
|
|
|
|
| 4 |
import networkx as nx
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
def load_names_from_url(jsonld_url):
|
| 10 |
response = requests.get(jsonld_url)
|
| 11 |
data = response.json()
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
jsonld_url = 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld'
|
| 16 |
names = load_names_from_url(jsonld_url)
|
| 17 |
|
|
@@ -19,84 +26,77 @@ def build_graph_from_jsonld(jsonld_url, selected_name):
|
|
| 19 |
response = requests.get(jsonld_url)
|
| 20 |
data = response.json()
|
| 21 |
|
|
|
|
| 22 |
selected_data = next((item for item in data if item['name'] == selected_name), None)
|
| 23 |
|
| 24 |
if not selected_data:
|
| 25 |
return "Local não encontrado."
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
place_id = selected_data['@id']
|
| 31 |
-
place_label = f"
|
| 32 |
-
|
| 33 |
|
| 34 |
-
#
|
| 35 |
geo_data = selected_data['geo']
|
| 36 |
geo_id = geo_data['@id']
|
| 37 |
-
geo_label = f"
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
#
|
| 42 |
for work in selected_data.get('subjectOf', []):
|
| 43 |
work_id = work['@id']
|
| 44 |
-
work_label = f"
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
return graph_html
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
.
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
padding: 20px;
|
| 74 |
-
}
|
| 75 |
-
h1 {
|
| 76 |
-
color: #333;
|
| 77 |
-
text-align: center;
|
| 78 |
-
}
|
| 79 |
-
.gr-form {
|
| 80 |
-
background-color: white;
|
| 81 |
-
padding: 20px;
|
| 82 |
-
border-radius: 10px;
|
| 83 |
-
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
| 84 |
-
}
|
| 85 |
-
"""
|
| 86 |
|
| 87 |
-
with gr.Blocks(css=css) as demo:
|
| 88 |
-
gr.Markdown("# Visualização de Grafos Literários")
|
| 89 |
-
|
| 90 |
-
with gr.Row():
|
| 91 |
-
with gr.Column(scale=1):
|
| 92 |
-
selected_location = gr.Dropdown(choices=names, label="Selecione o Local")
|
| 93 |
-
run_button = gr.Button("Visualizar Grafo", variant="primary")
|
| 94 |
-
|
| 95 |
-
with gr.Column(scale=3):
|
| 96 |
-
graph_output = gr.HTML()
|
| 97 |
-
|
| 98 |
def on_run_button_click(selected_location):
|
| 99 |
-
return run_query_and_visualize(selected_location)
|
| 100 |
|
| 101 |
run_button.click(fn=on_run_button_click, inputs=[selected_location], outputs=graph_output)
|
| 102 |
|
|
|
|
| 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):
|
| 11 |
response = requests.get(jsonld_url)
|
| 12 |
data = response.json()
|
| 13 |
+
|
| 14 |
+
names = []
|
| 15 |
+
for item in data:
|
| 16 |
+
if 'name' in item:
|
| 17 |
+
names.append(item['name'])
|
| 18 |
+
|
| 19 |
+
return names
|
| 20 |
|
| 21 |
+
# Carregar nomes do arquivo JSON-LD
|
| 22 |
jsonld_url = 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld'
|
| 23 |
names = load_names_from_url(jsonld_url)
|
| 24 |
|
|
|
|
| 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
|
| 78 |
+
buf = BytesIO()
|
| 79 |
+
plt.savefig(buf, format='png')
|
| 80 |
+
buf.seek(0)
|
| 81 |
+
img_str = base64.b64encode(buf.read()).decode()
|
| 82 |
+
graph_html = f'<img src="data:image/png;base64,{img_str}"/>'
|
| 83 |
|
| 84 |
+
plt.close()
|
| 85 |
+
|
| 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 |
|