Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import rdflib
|
| 3 |
import requests
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
import networkx as nx
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
|
| 9 |
-
#
|
| 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 |
-
#
|
| 22 |
jsonld_url = 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld'
|
| 23 |
names = load_names_from_url(jsonld_url)
|
| 24 |
|
|
@@ -26,78 +19,85 @@ 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 |
-
|
| 36 |
|
| 37 |
-
#
|
| 38 |
place_id = selected_data['@id']
|
| 39 |
-
place_label = f"
|
| 40 |
-
|
| 41 |
|
| 42 |
-
#
|
| 43 |
geo_data = selected_data['geo']
|
| 44 |
geo_id = geo_data['@id']
|
| 45 |
-
geo_label = f"
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
#
|
| 50 |
for work in selected_data.get('subjectOf', []):
|
| 51 |
work_id = work['@id']
|
| 52 |
-
work_label = f"
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
G = build_graph_from_jsonld(jsonld_url, selected_location)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 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 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 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 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
| 103 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import rdflib
|
| 3 |
import requests
|
|
|
|
| 4 |
import networkx as nx
|
| 5 |
+
import plotly.graph_objs as go
|
| 6 |
+
from pyvis.network import Network
|
| 7 |
|
| 8 |
+
# Function to load and extract names from the JSON-LD file
|
| 9 |
def load_names_from_url(jsonld_url):
|
| 10 |
response = requests.get(jsonld_url)
|
| 11 |
data = response.json()
|
| 12 |
+
return [item['name'] for item in data if 'name' in item]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Load names from the JSON-LD file
|
| 15 |
jsonld_url = 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld'
|
| 16 |
names = load_names_from_url(jsonld_url)
|
| 17 |
|
|
|
|
| 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 |
+
net = Network(height="600px", width="100%", bgcolor="#222222", font_color="white")
|
| 28 |
|
| 29 |
+
# Add Place node
|
| 30 |
place_id = selected_data['@id']
|
| 31 |
+
place_label = f"Name: {selected_data['name']}\nDescription: {selected_data['description'][:100]}..."
|
| 32 |
+
net.add_node(place_id, label=selected_data['name'], title=place_label, color="#00ffff")
|
| 33 |
|
| 34 |
+
# Add GeoCoordinates node
|
| 35 |
geo_data = selected_data['geo']
|
| 36 |
geo_id = geo_data['@id']
|
| 37 |
+
geo_label = f"Lat: {geo_data['lat']}\nLong: {geo_data['long']}\nFeatureCode: {geo_data['gn:featureCode']}\nName: {geo_data['gn:name']}"
|
| 38 |
+
net.add_node(geo_id, label="Geo", title=geo_label, color="#ff9999")
|
| 39 |
+
net.add_edge(place_id, geo_id, title="schema:geo")
|
| 40 |
|
| 41 |
+
# Add CreativeWork nodes
|
| 42 |
for work in selected_data.get('subjectOf', []):
|
| 43 |
work_id = work['@id']
|
| 44 |
+
work_label = f"Headline: {work['headline']}\nGenre: {work['genre']}\nDatePublished: {work['datePublished']}\nText: {work['text'][:100]}..."
|
| 45 |
+
net.add_node(work_id, label=work['headline'], title=work_label, color="#99ff99")
|
| 46 |
+
net.add_edge(place_id, work_id, title="schema:subjectOf")
|
| 47 |
|
| 48 |
+
net.toggle_physics(True)
|
| 49 |
+
net.show_buttons(filter_=['physics'])
|
| 50 |
+
return net
|
|
|
|
| 51 |
|
| 52 |
+
def run_query_and_visualize(selected_location):
|
| 53 |
+
net = build_graph_from_jsonld(jsonld_url, selected_location)
|
| 54 |
|
| 55 |
+
if isinstance(net, str): # Error case
|
| 56 |
+
return net
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Save the graph as HTML
|
| 59 |
+
net.save_graph("temp_graph.html")
|
| 60 |
+
with open("temp_graph.html", "r", encoding="utf-8") as f:
|
| 61 |
+
graph_html = f.read()
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
return graph_html
|
| 64 |
|
| 65 |
+
css = """
|
| 66 |
+
body {
|
| 67 |
+
background-color: #f0f0f0;
|
| 68 |
+
font-family: Arial, sans-serif;
|
| 69 |
+
}
|
| 70 |
+
.container {
|
| 71 |
+
max-width: 1200px;
|
| 72 |
+
margin: 0 auto;
|
| 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 |
|
| 103 |
+
demo.launch()
|