File size: 7,926 Bytes
8e56712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import graphviz
import json
from tempfile import NamedTemporaryFile
import os
from graph_generator_utils import add_nodes_and_edges

def generate_concept_map(json_input: str, output_format: str) -> str:
    """
    Generates a concept map from JSON input.

    Args:
        json_input (str): A JSON string describing the concept map structure.
                          It must follow the Expected JSON Format Example below.

    Expected JSON Format Example:
    {
      "central_node": "Artificial Intelligence (AI)",
      "nodes": [
        {
          "id": "ml_fundamental",
          "label": "Machine Learning",
          "relationship": "is essential for",
          "subnodes": [
            {
              "id": "dl_branch",
              "label": "Deep Learning",
              "relationship": "for example",
              "subnodes": [
                {
                  "id": "cnn_example",
                  "label": "CNNs",
                  "relationship": "for example"
                },
                {
                  "id": "rnn_example",
                  "label": "RNNs",
                  "relationship": "for example"
                }
              ]
            },
            {
              "id": "rl_branch",
              "label": "Reinforcement Learning",
              "relationship": "for example",
              "subnodes": [
                {
                  "id": "qlearning_example",
                  "label": "Q-Learning",
                  "relationship": "example"
                },
                {
                  "id": "pg_example",
                  "label": "Policy Gradients",
                  "relationship": "example"
                }
              ]
            }
          ]
        },
        {
          "id": "ai_types",
          "label": "Types",
          "relationship": "formed by",
          "subnodes": [
            {
              "id": "agi_type",
              "label": "AGI",
              "relationship": "this is",
              "subnodes": [
                {
                  "id": "strong_ai",
                  "label": "Strong AI",
                  "relationship": "provoked by",
                  "subnodes": [
                    {
                      "id": "human_intel",
                      "label": "Human-level Intel.",
                      "relationship": "of"
                    }
                  ]
                }
              ]
            },
            {
              "id": "ani_type",
              "label": "ANI",
              "relationship": "this is",
              "subnodes": [
                {
                  "id": "weak_ai",
                  "label": "Weak AI",
                  "relationship": "provoked by",
                  "subnodes": [
                    {
                      "id": "narrow_tasks",
                      "label": "Narrow Tasks",
                      "relationship": "of"
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "id": "ai_capabilities",
          "label": "Capabilities",
          "relationship": "change",
          "subnodes": [
            {
              "id": "data_proc",
              "label": "Data Processing",
              "relationship": "can",
              "subnodes": [
                {
                  "id": "big_data",
                  "label": "Big Data",
                  "relationship": "as",
                  "subnodes": [
                    {
                      "id": "analysis_example",
                      "label": "Data Analysis",
                      "relationship": "example"
                    },
                    {
                      "id": "prediction_example",
                      "label": "Prediction",
                      "relationship": "example"
                    }
                  ]
                }
              ]
            },
            {
              "id": "decision_making",
              "label": "Decision Making",
              "relationship": "can be",
              "subnodes": [
                {
                  "id": "automation",
                  "label": "Automation",
                  "relationship": "as",
                  "subnodes": [
                    {
                      "id": "robotics_example",
                      "label": "Robotics",
                      "relationship": "Example"},
                    {
                      "id": "autonomous_example",
                      "label": "Autonomous Vehicles",
                      "relationship": "of one"
                    }
                  ]
                }
              ]
            },
            {
              "id": "problem_solving",
              "label": "Problem Solving",
              "relationship": "can",
              "subnodes": [
                {
                  "id": "optimization",
                  "label": "Optimization",
                  "relationship": "as is",
                  "subnodes": [
                    {
                      "id": "algorithms_example",
                      "label": "Algorithms",
                      "relationship": "for example"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }

    Returns:
        str: The filepath to the generated PNG image file.
    """
    try:
        if not json_input.strip():
            return "Error: Empty input"
            
        data = json.loads(json_input)
        
        if 'central_node' not in data or 'nodes' not in data:
            raise ValueError("Missing required fields: central_node or nodes")

        # ํ•œ๊ธ€ ํฐํŠธ ์„ค์ •
        # ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ํฐํŠธ ๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
        font_path = os.environ.get('KOREAN_FONT_PATH', '')
        
        # Graphviz๋Š” ์‹œ์Šคํ…œ ํฐํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ํฐํŠธ ์ด๋ฆ„์œผ๋กœ ์ง€์ •
        # NanumGothic์ด ์‹œ์Šคํ…œ์— ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•จ
        korean_font = 'NanumGothic'
        
        dot = graphviz.Digraph(
            name='ConceptMap',
            format='png',
            graph_attr={
                'rankdir': 'TB',        # Top-to-Bottom layout (vertical hierarchy)
                'splines': 'ortho',     # Straight lines
                'bgcolor': 'white',     # White background
                'pad': '0.5',           # Padding around the graph
                'fontname': korean_font,  # ๊ทธ๋ž˜ํ”„ ์ „์ฒด ํฐํŠธ ์„ค์ •
                'charset': 'UTF-8'      # UTF-8 ์ธ์ฝ”๋”ฉ
            },
            node_attr={
                'fontname': korean_font  # ๋ชจ๋“  ๋…ธ๋“œ์˜ ๊ธฐ๋ณธ ํฐํŠธ
            },
            edge_attr={
                'fontname': korean_font  # ๋ชจ๋“  ์—ฃ์ง€์˜ ๊ธฐ๋ณธ ํฐํŠธ
            }
        )
        
        base_color = '#19191a' # Hardcoded base color

        # Central node styling (rounded box, dark color)
        dot.node(
            'central',
            data['central_node'],
            shape='box',            # Rectangular shape
            style='filled,rounded', # Filled and rounded corners
            fillcolor=base_color,   # Darkest color
            fontcolor='white',      # White text for dark background
            fontsize='16',          # Larger font for central node
            fontname=korean_font    # ํ•œ๊ธ€ ํฐํŠธ ๋ช…์‹œ์  ์ง€์ •
        )
        
        # Add child nodes and edges recursively starting from depth 1
        add_nodes_and_edges(dot, 'central', data.get('nodes', []), current_depth=1, base_color=base_color)

        with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
            dot.render(tmp.name, format=output_format, cleanup=True)
            return f"{tmp.name}.{output_format}"

    except json.JSONDecodeError:
        return "Error: Invalid JSON format"
    except Exception as e:
        return f"Error: {str(e)}"