Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,18 @@
|
|
1 |
import subprocess
|
2 |
import pkg_resources
|
3 |
import sys
|
4 |
-
import time
|
5 |
|
6 |
-
def
|
7 |
-
"""
|
8 |
try:
|
9 |
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'gradio'])
|
10 |
print("Gradio atualizado com sucesso!")
|
11 |
-
print("Reiniciando aplicação...")
|
12 |
-
time.sleep(2)
|
13 |
-
python = sys.executable
|
14 |
-
subprocess.Popen([python] + sys.argv)
|
15 |
-
sys.exit(0)
|
16 |
except Exception as e:
|
17 |
print(f"Erro ao atualizar Gradio: {e}")
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
latest_version = subprocess.check_output([sys.executable, '-m', 'pip', 'index', 'versions', 'gradio']).decode().split('\n')[0].split(' ')[-1]
|
23 |
-
|
24 |
-
if current_version != latest_version:
|
25 |
-
print(f"Atualizando Gradio: {current_version} -> {latest_version}")
|
26 |
-
update_gradio()
|
27 |
-
else:
|
28 |
-
print(f"Gradio já está na versão mais recente: {current_version}")
|
29 |
-
except:
|
30 |
-
print("Instalando Gradio...")
|
31 |
-
update_gradio()
|
32 |
-
|
33 |
-
# Importações necessárias
|
34 |
import gradio as gr
|
35 |
import rdflib
|
36 |
import requests
|
@@ -40,17 +22,15 @@ from io import BytesIO
|
|
40 |
import base64
|
41 |
|
42 |
def load_names_from_url(jsonld_url):
|
43 |
-
"""Carrega e extrai os nomes do arquivo JSON-LD
|
44 |
try:
|
45 |
response = requests.get(jsonld_url)
|
46 |
data = response.json()
|
47 |
-
|
48 |
names = []
|
49 |
for item in data:
|
50 |
if 'name' in item:
|
51 |
names.append(item['name'])
|
52 |
-
|
53 |
-
return sorted(names) # Retorna nomes ordenados alfabeticamente
|
54 |
except Exception as e:
|
55 |
print(f"Erro ao carregar dados: {e}")
|
56 |
return []
|
@@ -103,12 +83,12 @@ def run_query_and_visualize(selected_location, jsonld_url):
|
|
103 |
return f'<div style="color: red; text-align: center; padding: 20px;">{G}</div>'
|
104 |
|
105 |
plt.figure(figsize=(15, 10))
|
106 |
-
pos = nx.spring_layout(G)
|
107 |
|
108 |
nx.draw_networkx_nodes(G, pos, node_size=3000, node_color="skyblue", alpha=0.9)
|
109 |
nx.draw_networkx_edges(G, pos, width=2, alpha=0.5, edge_color='gray')
|
110 |
-
nx.draw_networkx_labels(G, pos, labels=nx.get_node_attributes(G, 'label'), font_size=9
|
111 |
-
nx.draw_networkx_edge_labels(G, pos, edge_labels=nx.get_edge_attributes(G, 'label'), font_size=9
|
112 |
|
113 |
plt.title(f"Grafo para: {selected_location}", size=15)
|
114 |
plt.axis('off')
|
@@ -130,31 +110,26 @@ jsonld_url = 'https://huggingface.co/spaces/histlearn/ShowGraph/raw/main/datafil
|
|
130 |
names = load_names_from_url(jsonld_url)
|
131 |
|
132 |
# Interface Gradio
|
133 |
-
with gr.Blocks() as demo:
|
134 |
-
gr.Markdown(""
|
135 |
-
|
136 |
-
Selecione um local para visualizar seu grafo de relacionamentos.
|
137 |
-
""")
|
138 |
|
139 |
with gr.Column():
|
140 |
selected_location = gr.Dropdown(
|
141 |
choices=names,
|
142 |
label="Selecione o Local",
|
143 |
-
|
144 |
)
|
145 |
-
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
def on_run_button_click(selected_location):
|
152 |
-
return run_query_and_visualize(selected_location, jsonld_url)
|
153 |
|
154 |
-
|
155 |
-
fn=
|
156 |
-
inputs=[selected_location],
|
157 |
-
outputs=
|
158 |
)
|
159 |
|
160 |
if __name__ == "__main__":
|
|
|
1 |
import subprocess
|
2 |
import pkg_resources
|
3 |
import sys
|
|
|
4 |
|
5 |
+
def ensure_latest_gradio():
|
6 |
+
"""Verifica e atualiza o Gradio silenciosamente."""
|
7 |
try:
|
8 |
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'gradio'])
|
9 |
print("Gradio atualizado com sucesso!")
|
|
|
|
|
|
|
|
|
|
|
10 |
except Exception as e:
|
11 |
print(f"Erro ao atualizar Gradio: {e}")
|
12 |
|
13 |
+
# Atualiza o Gradio uma única vez no início
|
14 |
+
ensure_latest_gradio()
|
15 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
import gradio as gr
|
17 |
import rdflib
|
18 |
import requests
|
|
|
22 |
import base64
|
23 |
|
24 |
def load_names_from_url(jsonld_url):
|
25 |
+
"""Carrega e extrai os nomes do arquivo JSON-LD."""
|
26 |
try:
|
27 |
response = requests.get(jsonld_url)
|
28 |
data = response.json()
|
|
|
29 |
names = []
|
30 |
for item in data:
|
31 |
if 'name' in item:
|
32 |
names.append(item['name'])
|
33 |
+
return sorted(names) # Retorna nomes ordenados
|
|
|
34 |
except Exception as e:
|
35 |
print(f"Erro ao carregar dados: {e}")
|
36 |
return []
|
|
|
83 |
return f'<div style="color: red; text-align: center; padding: 20px;">{G}</div>'
|
84 |
|
85 |
plt.figure(figsize=(15, 10))
|
86 |
+
pos = nx.spring_layout(G, k=1, iterations=50)
|
87 |
|
88 |
nx.draw_networkx_nodes(G, pos, node_size=3000, node_color="skyblue", alpha=0.9)
|
89 |
nx.draw_networkx_edges(G, pos, width=2, alpha=0.5, edge_color='gray')
|
90 |
+
nx.draw_networkx_labels(G, pos, labels=nx.get_node_attributes(G, 'label'), font_size=9)
|
91 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=nx.get_edge_attributes(G, 'label'), font_size=9)
|
92 |
|
93 |
plt.title(f"Grafo para: {selected_location}", size=15)
|
94 |
plt.axis('off')
|
|
|
110 |
names = load_names_from_url(jsonld_url)
|
111 |
|
112 |
# Interface Gradio
|
113 |
+
with gr.Blocks(css="footer {display: none}") as demo:
|
114 |
+
gr.Markdown("# Visualização de Grafos de Localidades")
|
115 |
+
gr.Markdown("Selecione um local para visualizar seu grafo de relacionamentos e citações.")
|
|
|
|
|
116 |
|
117 |
with gr.Column():
|
118 |
selected_location = gr.Dropdown(
|
119 |
choices=names,
|
120 |
label="Selecione o Local",
|
121 |
+
value=names[0] if names else None
|
122 |
)
|
123 |
+
view_button = gr.Button("Visualizar Grafo", variant="primary")
|
124 |
|
125 |
+
result_html = gr.HTML(
|
126 |
+
value='<div style="text-align: center; padding: 20px;">Selecione um local e clique em "Visualizar Grafo"</div>'
|
127 |
+
)
|
|
|
|
|
|
|
128 |
|
129 |
+
view_button.click(
|
130 |
+
fn=run_query_and_visualize,
|
131 |
+
inputs=[selected_location, gr.Textbox(value=jsonld_url, visible=False)],
|
132 |
+
outputs=result_html
|
133 |
)
|
134 |
|
135 |
if __name__ == "__main__":
|