jcmachicao commited on
Commit
dca1733
verified
1 Parent(s): de8e2ea

Create app_new.py

Browse files
Files changed (1) hide show
  1. app_new.py +75 -0
app_new.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import networkx as nx
3
+ import matplotlib.pyplot as plt
4
+ import uuid
5
+ import requests
6
+ import json
7
+
8
+ # Configuraci贸n de AirTable
9
+ BASE_ID = "TU_BASE_ID"
10
+ TABLE_NAME = "TU_TABLA"
11
+ AIRTABLE_API_KEY = "TU_API_KEY"
12
+
13
+ # Diccionario para almacenar los aportes y sus conexiones
14
+ aportes = {}
15
+ graph = nx.Graph()
16
+
17
+ def cargar_desde_airtable():
18
+ """Recupera los datos almacenados en AirTable y los grafica."""
19
+ global aportes, graph
20
+ url = f"https://api.airtable.com/v0/{BASE_ID}/{TABLE_NAME}"
21
+ headers = {"Authorization": f"Bearer {AIRTABLE_API_KEY}"}
22
+ response = requests.get(url, headers=headers)
23
+
24
+ if response.status_code == 200:
25
+ records = response.json().get("records", [])
26
+ for record in records:
27
+ node_id = record["id"]
28
+ texto = record["fields"].get("json_HF", "")
29
+ aportes[node_id] = texto
30
+ graph.add_node(node_id, label=texto)
31
+
32
+ # Conectar nodos de alguna manera (ejemplo: secuencialmente)
33
+ nodes = list(aportes.keys())
34
+ for i in range(1, len(nodes)):
35
+ graph.add_edge(nodes[i - 1], nodes[i])
36
+
37
+ return visualizar_grafo()
38
+
39
+ def guardar_en_airtable(node_id, texto):
40
+ url = f"https://api.airtable.com/v0/{BASE_ID}/{TABLE_NAME}"
41
+ headers = {
42
+ "Authorization": f"Bearer {AIRTABLE_API_KEY}",
43
+ "Content-Type": "application/json"
44
+ }
45
+ data = {"fields": {"json_HF": texto}}
46
+ requests.post(url, headers=headers, json=data)
47
+
48
+ def agregar_aporte(texto):
49
+ global aportes, graph
50
+ node_id = str(uuid.uuid4())[:8]
51
+ aportes[node_id] = texto
52
+ graph.add_node(node_id, label=texto)
53
+
54
+ if len(aportes) > 1:
55
+ prev_node = list(aportes.keys())[-2]
56
+ graph.add_edge(prev_node, node_id)
57
+
58
+ guardar_en_airtable(node_id, texto)
59
+ return visualizar_grafo()
60
+
61
+ def visualizar_grafo():
62
+ plt.figure(figsize=(8, 6))
63
+ pos = nx.spring_layout(graph)
64
+ labels = nx.get_node_attributes(graph, 'label')
65
+ nx.draw(graph, pos, with_labels=True, labels=labels, node_color='lightblue', edge_color='gray', node_size=2000, font_size=10)
66
+ plt.title("Red de Aportes")
67
+ plt.savefig("graph.png")
68
+ plt.close()
69
+ return "graph.png"
70
+
71
+ # Cargar datos existentes al inicio
72
+ graph_image = cargar_desde_airtable()
73
+
74
+ iface = gr.Interface(fn=agregar_aporte, inputs="text", outputs="image", title="Foro Din谩mico con Visualizaci贸n de Red")
75
+ iface.launch()