File size: 3,370 Bytes
695363c f3dbd83 64c5ee5 17c9459 695363c 17c9459 2e7c89d f3dbd83 17c9459 695363c 17c9459 9d8c9ea 695363c 176d12a 7b61abf 176d12a 7b61abf 17c9459 176d12a 17c9459 176d12a 17c9459 176d12a 17c9459 176d12a 17c9459 176d12a 17c9459 176d12a 17c9459 176d12a 17c9459 dec020e 17c9459 176d12a 17c9459 64c5ee5 17c9459 64c5ee5 a12b796 695363c 17c9459 695363c 17c9459 5291db6 17c9459 695363c 5291db6 695363c 17c9459 |
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
import gradio as gr
import rdflib
import requests
import networkx as nx
import plotly.graph_objs as go
from pyvis.network import Network
# Function to load and extract names from the JSON-LD file
def load_names_from_url(jsonld_url):
response = requests.get(jsonld_url)
data = response.json()
return [item['name'] for item in data if 'name' in item]
# Load names from the JSON-LD file
jsonld_url = 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafile.jsonld'
names = load_names_from_url(jsonld_url)
def build_graph_from_jsonld(jsonld_url, selected_name):
response = requests.get(jsonld_url)
data = response.json()
selected_data = next((item for item in data if item['name'] == selected_name), None)
if not selected_data:
return "Local não encontrado."
net = Network(height="600px", width="100%", bgcolor="#222222", font_color="white")
# Add Place node
place_id = selected_data['@id']
place_label = f"Name: {selected_data['name']}\nDescription: {selected_data['description'][:100]}..."
net.add_node(place_id, label=selected_data['name'], title=place_label, color="#00ffff")
# Add GeoCoordinates node
geo_data = selected_data['geo']
geo_id = geo_data['@id']
geo_label = f"Lat: {geo_data['lat']}\nLong: {geo_data['long']}\nFeatureCode: {geo_data['gn:featureCode']}\nName: {geo_data['gn:name']}"
net.add_node(geo_id, label="Geo", title=geo_label, color="#ff9999")
net.add_edge(place_id, geo_id, title="schema:geo")
# Add CreativeWork nodes
for work in selected_data.get('subjectOf', []):
work_id = work['@id']
work_label = f"Headline: {work['headline']}\nGenre: {work['genre']}\nDatePublished: {work['datePublished']}\nText: {work['text'][:100]}..."
net.add_node(work_id, label=work['headline'], title=work_label, color="#99ff99")
net.add_edge(place_id, work_id, title="schema:subjectOf")
net.toggle_physics(True)
net.show_buttons(filter_=['physics'])
return net
def run_query_and_visualize(selected_location):
net = build_graph_from_jsonld(jsonld_url, selected_location)
if isinstance(net, str): # Error case
return net
# Save the graph as HTML
net.save_graph("temp_graph.html")
with open("temp_graph.html", "r", encoding="utf-8") as f:
graph_html = f.read()
return graph_html
css = """
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #333;
text-align: center;
}
.gr-form {
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
"""
with gr.Blocks(css=css) as demo:
gr.Markdown("# Visualização de Grafos Literários")
with gr.Row():
with gr.Column(scale=1):
selected_location = gr.Dropdown(choices=names, label="Selecione o Local")
run_button = gr.Button("Visualizar Grafo", variant="primary")
with gr.Column(scale=3):
graph_output = gr.HTML()
def on_run_button_click(selected_location):
return run_query_and_visualize(selected_location)
run_button.click(fn=on_run_button_click, inputs=[selected_location], outputs=graph_output)
demo.launch() |