Delete app_old.py
Browse files- app_old.py +0 -170
app_old.py
DELETED
@@ -1,170 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import networkx as nx
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
-
import uuid
|
5 |
-
import os
|
6 |
-
import json
|
7 |
-
import textwrap
|
8 |
-
import requests
|
9 |
-
import pandas as pd
|
10 |
-
|
11 |
-
api_key = os.getenv("AIRT_KEY")
|
12 |
-
AIRT_DBASEx = os.getenv("AIRT_DBASE")
|
13 |
-
AIRT_TABLEx = os.getenv("AIRT_TABLE")
|
14 |
-
|
15 |
-
# Use Directed Graph to represent relationships
|
16 |
-
G = nx.DiGraph() # Fix: Use DiGraph instead of Graph
|
17 |
-
|
18 |
-
url = f"https://api.airtable.com/v0/{AIRT_DBASEx}/{AIRT_TABLEx}"
|
19 |
-
headers = {
|
20 |
-
"Authorization": f"Bearer {api_key}",
|
21 |
-
"Content-Type": "application/json"
|
22 |
-
}
|
23 |
-
|
24 |
-
# Cargar el JSON de normativas
|
25 |
-
def cargar_normativas():
|
26 |
-
with open('normativas.json', 'r', encoding='utf-8') as f:
|
27 |
-
return json.load(f)
|
28 |
-
|
29 |
-
# Cargar la lista de estudiantes
|
30 |
-
def cargar_estudiantes():
|
31 |
-
with open('estudiantes.json', 'r', encoding='utf-8') as f:
|
32 |
-
return json.load(f)
|
33 |
-
|
34 |
-
def crear_html(normativa):
|
35 |
-
htmlx = f"""
|
36 |
-
<div style="padding: 16px; border: 1px solid #ddd; border-radius: 8px;">
|
37 |
-
<style>
|
38 |
-
p {{ font-size: 20px; }}
|
39 |
-
</style>
|
40 |
-
<h2>{normativa["nombre"]}</h2>
|
41 |
-
<h2>{normativa["titulo"], normativa["a帽o"]}</h2>
|
42 |
-
<p><strong>ID:</strong> {normativa["id"]}</p>
|
43 |
-
<p><strong>Descripci贸n:</strong> {normativa["descripcion"]}</p>
|
44 |
-
<p><strong>Enfoque de Gesti贸n:</strong> {normativa["enfoque_riesgo"]}</p>
|
45 |
-
<p><strong>Opini贸n Docente:</strong> {normativa["opinion_docente"]}</p>
|
46 |
-
<p><strong>Visite <a href={normativa["url"]} target="_blank">este sitio web</a> para m谩s informaci贸n.</strong></p>
|
47 |
-
</div>
|
48 |
-
"""
|
49 |
-
return htmlx
|
50 |
-
|
51 |
-
def mostrar_detalles(normativa_seleccionada):
|
52 |
-
if not normativa_seleccionada:
|
53 |
-
return "<p>Por favor selecciona una normativa</p>", None
|
54 |
-
normativas = cargar_normativas()
|
55 |
-
partes = normativa_seleccionada.split()
|
56 |
-
id_normativa = normativa['nombre']
|
57 |
-
for normativa in normativas["normativa_peruana_gestion_riesgos"]:
|
58 |
-
if id_normativa == normativa['nombre']:
|
59 |
-
html = crear_html(normativa)
|
60 |
-
return html
|
61 |
-
|
62 |
-
def mostrar_detalles(normativa_seleccionada):
|
63 |
-
if not normativa_seleccionada:
|
64 |
-
return "<p>Por favor selecciona una normativa</p>", ""
|
65 |
-
normativas = cargar_normativas()
|
66 |
-
id_normativa = normativa_seleccionada # Extract the norm name
|
67 |
-
for normativa in normativas["normativa_peruana_gestion_riesgos"]:
|
68 |
-
if id_normativa == normativa['nombre']:
|
69 |
-
return crear_html(normativa), normativa["nombre"] # Return both HTML and name
|
70 |
-
return "<p>No se encontr贸 la normativa</p>", ""
|
71 |
-
|
72 |
-
def cargar_desde_airtable():
|
73 |
-
response = requests.get(url, headers=headers)
|
74 |
-
|
75 |
-
if response.status_code != 200:
|
76 |
-
print(f"Error: {response.status_code} - {response.text}") # Debugging info
|
77 |
-
return pd.DataFrame(columns=["Nombre", "Enfoque", "Norma", "Texto_HF"]) # Return empty DataFrame
|
78 |
-
|
79 |
-
records = response.json().get("records", [])
|
80 |
-
|
81 |
-
# Compact list comprehension to extract data
|
82 |
-
aportes = [
|
83 |
-
[record["fields"].get("Nombre", ""),
|
84 |
-
record["fields"].get("Enfoque", ""),
|
85 |
-
record["fields"].get("Norma", ""),
|
86 |
-
record["fields"].get("Texto_HF", "")] for record in records
|
87 |
-
]
|
88 |
-
|
89 |
-
return pd.DataFrame(aportes, columns=["Nombre", "Enfoque", "Norma", "Texto_HF"])
|
90 |
-
|
91 |
-
def wrap_text(text, width=10):
|
92 |
-
return "\n".join(textwrap.wrap(text, width=width))
|
93 |
-
|
94 |
-
def inicializar_grafo():
|
95 |
-
df = cargar_desde_airtable()
|
96 |
-
|
97 |
-
# Add base nodes for categories
|
98 |
-
G.add_node("Determinista", color='red')
|
99 |
-
G.add_node("Sist茅mico", color='blue')
|
100 |
-
|
101 |
-
# Process each row and add nodes/edges
|
102 |
-
for _, row in df.iterrows():
|
103 |
-
nombre, enfoque, norma, texto = row["Nombre"], row["Enfoque"], row["Norma"], row["Texto_HF"]
|
104 |
-
textox = wrap_text(f"{nombre}: {texto}")
|
105 |
-
|
106 |
-
if not G.has_node(norma):
|
107 |
-
G.add_node(norma, color='gray')
|
108 |
-
if not G.has_edge(norma, enfoque):
|
109 |
-
G.add_edge(norma, enfoque, label=textox)
|
110 |
-
|
111 |
-
def guardar_en_airtable(nombre, enfoque, norma, texto):
|
112 |
-
data = {"fields": {"Nombre": nombre, "Enfoque": enfoque, "Norma": norma, "Texto_HF": texto}}
|
113 |
-
requests.post(url, headers=headers, json=data)
|
114 |
-
|
115 |
-
def agregar_aporte(nombre, enfoque, norma, texto):
|
116 |
-
textox = wrap_text(f"{nombre}: {texto}")
|
117 |
-
|
118 |
-
if not G.has_node(norma):
|
119 |
-
G.add_node(norma, color='gray')
|
120 |
-
if not G.has_edge(norma, enfoque):
|
121 |
-
G.add_edge(norma, enfoque, label=textox)
|
122 |
-
|
123 |
-
guardar_en_airtable(nombre, enfoque, norma, texto)
|
124 |
-
return visualizar_grafo()
|
125 |
-
|
126 |
-
def visualizar_grafo():
|
127 |
-
plt.figure(figsize=(8, 8))
|
128 |
-
pos = nx.spring_layout(G)
|
129 |
-
|
130 |
-
edge_labels = nx.get_edge_attributes(G, 'label')
|
131 |
-
|
132 |
-
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=2000, font_size=10)
|
133 |
-
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
|
134 |
-
|
135 |
-
plt.title("Red de Aportes")
|
136 |
-
plt.savefig("graph.png")
|
137 |
-
plt.close()
|
138 |
-
return "graph.png"
|
139 |
-
|
140 |
-
inicializar_grafo()
|
141 |
-
normativas = cargar_normativas()
|
142 |
-
estudiantes = cargar_estudiantes()
|
143 |
-
norm_options = [f"{norm['nombre']} {norm['id']}" for norm in normativas["normativa_peruana_gestion_riesgos"]]
|
144 |
-
student_names = [student for student in estudiantes["estudiantes"]]
|
145 |
-
|
146 |
-
iface = gr.Blocks()
|
147 |
-
|
148 |
-
with iface:
|
149 |
-
gr.Markdown("# Foro Din谩mico con Visualizaci贸n de Red")
|
150 |
-
|
151 |
-
with gr.Row():
|
152 |
-
gr.Markdown('## Selecci贸n de Norma')
|
153 |
-
normativa_dropdown.change(fn=mostrar_detalles, inputs=normativa_dropdown, outputs=[normativa_html, norma_field])
|
154 |
-
|
155 |
-
with gr.Row():
|
156 |
-
gr.Markdown("## Red de Aportes:")
|
157 |
-
graph_output = gr.Image(visualizar_grafo(), label="Red de Aportes")
|
158 |
-
|
159 |
-
with gr.Row():
|
160 |
-
nombre = gr.Dropdown(choices=student_names, label="Nombre del Estudiante")
|
161 |
-
enfoque = gr.Radio(["Determinista", "Sist茅mico"], label="Enfoque")
|
162 |
-
|
163 |
-
with gr.Row():
|
164 |
-
norma_field = gr.Textbox(label="Norma", interactive=False) # Read-only
|
165 |
-
texto = gr.Textbox(label="Tu aporte")
|
166 |
-
|
167 |
-
submit_button = gr.Button("Agregar Aporte")
|
168 |
-
submit_button.click(fn=agregar_aporte, inputs=[nombre, enfoque, norma_field, texto], outputs=graph_output)
|
169 |
-
|
170 |
-
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|