ShowGraph / app.py
histlearn's picture
Update app.py
17c9459 verified
raw
history blame
3.37 kB
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()