Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from utils_gdmk import (
|
3 |
-
inicializar_grafo, visualizar_grafo, agregar_aporte
|
4 |
-
)
|
5 |
|
6 |
students = ["Alice", "Bob", "Charlie"]
|
7 |
|
8 |
# Initialize graph before launching UI
|
9 |
inicializar_grafo()
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def chat_interface(student, message):
|
12 |
"""Handles chat messages, processes them, and updates AirTable."""
|
13 |
graph_image = create_graph(student, message)
|
|
|
1 |
+
import networkx as nx
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import json
|
4 |
+
import textwrap
|
5 |
+
import requests
|
6 |
+
import pandas as pd
|
7 |
+
import os
|
8 |
import gradio as gr
|
|
|
|
|
|
|
9 |
|
10 |
students = ["Alice", "Bob", "Charlie"]
|
11 |
|
12 |
# Initialize graph before launching UI
|
13 |
inicializar_grafo()
|
14 |
|
15 |
+
api_key = os.getenv("AIRT_KEY")
|
16 |
+
AIRT_DBASEx = os.getenv("AIRT_DBASE")
|
17 |
+
AIRT_TABLEx = os.getenv("AIRT_TABLE")
|
18 |
+
|
19 |
+
G = nx.DiGraph()
|
20 |
+
|
21 |
+
url = f"https://api.airtable.com/v0/{AIRT_DBASEx}/{AIRT_TABLEx}"
|
22 |
+
headers = {
|
23 |
+
"Authorization": f"Bearer {api_key}",
|
24 |
+
"Content-Type": "application/json"
|
25 |
+
}
|
26 |
+
|
27 |
+
def cargar_desde_airtable():
|
28 |
+
response = requests.get(url, headers=headers)
|
29 |
+
|
30 |
+
if response.status_code != 200:
|
31 |
+
print(f"Error: {response.status_code} - {response.text}")
|
32 |
+
return pd.DataFrame(columns=["Nombre", "Conceptos"])
|
33 |
+
records = response.json().get("records", [])
|
34 |
+
aportes = [
|
35 |
+
[record["fields"].get("Nombre", ""),
|
36 |
+
record["fields"].get("Conceptos", "")] for record in records
|
37 |
+
]
|
38 |
+
return pd.DataFrame(aportes, columns=["Nombre", "Conceptos"])
|
39 |
+
|
40 |
+
def inicializar_grafo():
|
41 |
+
df = cargar_desde_airtable()
|
42 |
+
for _, row in df.iterrows():
|
43 |
+
nombre, conceptos = row["Nombre"], row["Conceptos"]
|
44 |
+
for termino in conceptos:
|
45 |
+
if not G.has_node(termino):
|
46 |
+
G.add_node(termino, color='gray')
|
47 |
+
if not G.has_edge(nombre, termino):
|
48 |
+
G.add_edge(norma, enfoque)
|
49 |
+
|
50 |
+
def guardar_en_airtable(nombre, conceptos):
|
51 |
+
data = {"fields": {"Nombre": nombre, "Conceptos": conceptos}}
|
52 |
+
requests.post(url, headers=headers, json=data)
|
53 |
+
|
54 |
+
def agregar_aporte(nombre, enfoque, norma, texto):
|
55 |
+
textox = wrap_text(f"{nombre}: {texto}")
|
56 |
+
|
57 |
+
if not G.has_node(norma):
|
58 |
+
G.add_node(norma, color='gray')
|
59 |
+
if not G.has_edge(norma, enfoque):
|
60 |
+
G.add_edge(norma, enfoque, label=textox)
|
61 |
+
|
62 |
+
guardar_en_airtable(nombre, enfoque, norma, texto)
|
63 |
+
return visualizar_grafo()
|
64 |
+
|
65 |
+
def visualizar_grafo():
|
66 |
+
plt.figure(figsize=(10, 6))
|
67 |
+
pos = nx.spring_layout(G)
|
68 |
+
|
69 |
+
edge_labels = nx.get_edge_attributes(G, 'label')
|
70 |
+
|
71 |
+
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=2000, font_size=10)
|
72 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
|
73 |
+
|
74 |
+
plt.title("Red de Aportes")
|
75 |
+
plt.savefig("graph.png")
|
76 |
+
plt.close()
|
77 |
+
return "graph.png"
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
def chat_interface(student, message):
|
84 |
"""Handles chat messages, processes them, and updates AirTable."""
|
85 |
graph_image = create_graph(student, message)
|