ZahirJS commited on
Commit
27e273f
·
verified ·
1 Parent(s): 2122af1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -81
app.py CHANGED
@@ -1,10 +1,9 @@
1
- # app.py
2
  import gradio as gr
3
  import json
4
  from graphviz import Digraph
5
  import os
6
  from tempfile import NamedTemporaryFile
7
- from sample_data import COMPLEX_SAMPLE_JSON
8
 
9
  def generate_concept_map(json_input: str) -> str:
10
  """
@@ -51,100 +50,99 @@ def generate_concept_map(json_input: str) -> str:
51
  if 'central_node' not in data or 'nodes' not in data:
52
  raise ValueError("Missing required fields: central_node or nodes")
53
 
54
- # Create graph
55
  dot = Digraph(
56
  name='ConceptMap',
57
  format='png',
58
  graph_attr={
59
- 'rankdir': 'TB',
60
- 'splines': 'ortho',
61
- 'bgcolor': 'transparent'
62
  }
63
  )
64
 
65
- # Central node (ellipse)
 
 
 
66
  dot.node(
67
  'central',
68
  data['central_node'],
69
- shape='ellipse',
70
- style='filled',
71
- fillcolor='#2196F3',
72
  fontcolor='white',
73
- fontsize='14'
74
  )
75
 
76
- # Process nodes (rectangles)
77
- for node in data['nodes']:
78
- node_id = node.get('id')
79
- label = node.get('label')
80
- relationship = node.get('relationship')
 
81
 
82
- # Validate node
83
- if not all([node_id, label, relationship]):
84
- raise ValueError(f"Invalid node: {node}")
85
-
86
- # Create main node (rectangle)
87
- dot.node(
88
- node_id,
89
- label,
90
- shape='box',
91
- style='filled',
92
- fillcolor='#4CAF50',
93
- fontcolor='white',
94
- fontsize='12'
95
- )
96
 
97
- # Connect to central node
98
- dot.edge(
99
- 'central',
100
- node_id,
101
- label=relationship,
102
- color='#9C27B0',
103
- fontsize='10'
104
- )
 
 
 
 
 
 
 
105
 
106
- # Helper function to recursively add subnodes and edges
107
- def add_subnodes(parent_id, subnodes_list, fill_color, font_size, edge_color, edge_font_size):
108
- for subnode in subnodes_list:
109
- sub_id = subnode.get('id')
110
- sub_label = subnode.get('label')
111
- sub_rel = subnode.get('relationship')
112
-
113
- if not all([sub_id, sub_label, sub_rel]):
114
- raise ValueError(f"Invalid subnode: {subnode}")
115
-
116
- dot.node(
117
- sub_id,
118
- sub_label,
119
- shape='box',
120
- style='filled',
121
- fillcolor=fill_color,
122
- fontcolor='white',
123
- fontsize=str(font_size)
124
- )
125
-
126
- dot.edge(
127
- parent_id,
128
- sub_id,
129
- label=sub_rel,
130
- color=edge_color,
131
- fontsize=str(edge_font_size)
132
- )
133
-
134
- # Recursively call for deeper levels
135
- if 'subnodes' in subnode:
136
- # Slightly adjust colors/sizes for deeper levels if desired
137
- # For fixed 2 children per parent, you might keep colors consistent per level or vary them.
138
- # Here, I'll slightly adjust font size for consistency with depth.
139
- add_subnodes(sub_id, subnode['subnodes'],
140
- '#FFA726' if font_size > 8 else '#FFCC80', # Lighter orange/yellow for deeper levels
141
- font_size - 1 if font_size > 7 else font_size,
142
- '#E91E63' if edge_font_size > 7 else '#FF5252', # Reddish for deeper edges
143
- edge_font_size - 1 if edge_font_size > 7 else edge_font_size)
144
 
145
- # Start processing subnodes from the first level
146
- add_subnodes(node_id, node.get('subnodes', []), '#FFA726', 10, '#E91E63', 8)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
 
 
148
 
149
  # Save to temporary file
150
  with NamedTemporaryFile(delete=False, suffix='.png') as tmp:
@@ -160,7 +158,7 @@ if __name__ == "__main__":
160
  demo = gr.Interface(
161
  fn=generate_concept_map,
162
  inputs=gr.Textbox(
163
- value=COMPLEX_SAMPLE_JSON, # ¡Ahora usa el JSON AI simétrico y complejo!
164
  placeholder="Paste JSON following the documented format",
165
  label="Structured JSON Input",
166
  lines=25
@@ -170,8 +168,8 @@ if __name__ == "__main__":
170
  type="filepath",
171
  show_download_button=True
172
  ),
173
- title="Advanced Concept Map Generator (Symmetric AI)",
174
- description="Create symmetric, multi-level concept maps for AI from properly formatted JSON."
175
  )
176
 
177
  demo.launch(
 
 
1
  import gradio as gr
2
  import json
3
  from graphviz import Digraph
4
  import os
5
  from tempfile import NamedTemporaryFile
6
+ from sample_data import COMPLEX_SAMPLE_JSON # El JSON de ejemplo se mantiene sin cambios
7
 
8
  def generate_concept_map(json_input: str) -> str:
9
  """
 
50
  if 'central_node' not in data or 'nodes' not in data:
51
  raise ValueError("Missing required fields: central_node or nodes")
52
 
 
53
  dot = Digraph(
54
  name='ConceptMap',
55
  format='png',
56
  graph_attr={
57
+ 'rankdir': 'TB', # Top-to-Bottom
58
+ 'splines': 'ortho', # Straight lines
59
+ 'bgcolor': 'white' # Fondo blanco
60
  }
61
  )
62
 
63
+ # Base color for the central node
64
+ base_color = '#19191a' # Casi negro
65
+
66
+ # Central node (now a rounded box)
67
  dot.node(
68
  'central',
69
  data['central_node'],
70
+ shape='box', # Ahora es un rectángulo
71
+ style='filled,rounded', # Redondeado
72
+ fillcolor=base_color, # Color base
73
  fontcolor='white',
74
+ fontsize='16' # Un poco más grande para el título
75
  )
76
 
77
+ # Helper function to recursively add nodes and edges
78
+ def add_nodes_and_edges(parent_id, nodes_list, current_depth=0):
79
+ # Calculate color for current depth, making it lighter
80
+ # We'll blend towards white based on depth
81
+ # Max depth in your example is 5 (central + 4 levels of nodes)
82
+ # We want colors to get lighter as depth increases
83
 
84
+ # Simple linear interpolation for color lightening
85
+ # You can adjust the lightening factor (e.g., 0.15)
86
+ lightening_factor = 0.12 # How much lighter each level gets
 
 
 
 
 
 
 
 
 
 
 
87
 
88
+ # Convert base_color hex to RGB
89
+ base_r = int(base_color[1:3], 16)
90
+ base_g = int(base_color[3:5], 16)
91
+ base_b = int(base_color[5:7], 16)
92
+
93
+ # Calculate current node color
94
+ # Target is white (255, 255, 255)
95
+ current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
96
+ current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
97
+ current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
98
+
99
+ # Clamp values to 255
100
+ current_r = min(255, current_r)
101
+ current_g = min(255, current_g)
102
+ current_b = min(255, current_b)
103
 
104
+ node_fill_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
105
+
106
+ # Font color: white for dark nodes, black for very light nodes
107
+ font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
+ # Edge colors can remain constant or change. Let's make them slightly visible.
110
+ edge_color = '#4a4a4a' # Un gris oscuro para las líneas
111
+ font_size = max(9, 14 - (current_depth * 2)) # Adjust font size based on depth
112
+ edge_font_size = max(7, 10 - (current_depth * 1))
113
+
114
+ for node in nodes_list:
115
+ node_id = node.get('id')
116
+ label = node.get('label')
117
+ relationship = node.get('relationship')
118
+
119
+ if not all([node_id, label, relationship]):
120
+ raise ValueError(f"Invalid node: {node}")
121
+
122
+ dot.node(
123
+ node_id,
124
+ label,
125
+ shape='box', # Rectángulo
126
+ style='filled,rounded', # Redondeado
127
+ fillcolor=node_fill_color,
128
+ fontcolor=font_color,
129
+ fontsize=str(font_size)
130
+ )
131
+
132
+ dot.edge(
133
+ parent_id,
134
+ node_id,
135
+ label=relationship,
136
+ color=edge_color,
137
+ fontcolor=edge_color, # Color de la fuente de la arista también gris
138
+ fontsize=str(edge_font_size)
139
+ )
140
+
141
+ if 'subnodes' in node:
142
+ add_nodes_and_edges(node_id, node['subnodes'], current_depth + 1)
143
 
144
+ # Start processing from the top-level nodes connected to the central node
145
+ add_nodes_and_edges('central', data.get('nodes', []), current_depth=1) # Initial depth is 1 for nodes under central
146
 
147
  # Save to temporary file
148
  with NamedTemporaryFile(delete=False, suffix='.png') as tmp:
 
158
  demo = gr.Interface(
159
  fn=generate_concept_map,
160
  inputs=gr.Textbox(
161
+ value=COMPLEX_SAMPLE_JSON,
162
  placeholder="Paste JSON following the documented format",
163
  label="Structured JSON Input",
164
  lines=25
 
168
  type="filepath",
169
  show_download_button=True
170
  ),
171
+ title="AI Concept Map (Custom Style)",
172
+ description="Generates an AI concept map with custom rounded boxes, color gradient, and white background."
173
  )
174
 
175
  demo.launch(