Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,44 @@
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
from graphviz import Digraph
|
4 |
-
import
|
|
|
5 |
|
6 |
def generate_concept_map(json_input: str) -> str:
|
7 |
"""
|
8 |
-
Generate concept map from JSON and return as
|
9 |
|
10 |
Args:
|
11 |
json_input (str): JSON describing the concept map structure.
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
Returns:
|
14 |
-
str:
|
15 |
"""
|
16 |
try:
|
17 |
if not json_input.strip():
|
@@ -101,10 +128,10 @@ def generate_concept_map(json_input: str) -> str:
|
|
101 |
fontsize='8'
|
102 |
)
|
103 |
|
104 |
-
#
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
|
109 |
except json.JSONDecodeError:
|
110 |
return "Error: Invalid JSON format"
|
@@ -112,7 +139,7 @@ def generate_concept_map(json_input: str) -> str:
|
|
112 |
return f"Error: {str(e)}"
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
-
#
|
116 |
sample_json = """
|
117 |
{
|
118 |
"central_node": "Artificial Intelligence (AI)",
|
@@ -120,65 +147,60 @@ if __name__ == "__main__":
|
|
120 |
{
|
121 |
"id": "ml",
|
122 |
"label": "Machine Learning",
|
123 |
-
"relationship": "
|
124 |
"subnodes": [
|
125 |
{
|
126 |
"id": "sl",
|
127 |
"label": "Supervised Learning",
|
128 |
-
"relationship": "
|
129 |
"subnodes": [
|
130 |
-
{
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
]
|
133 |
},
|
134 |
{
|
135 |
"id": "ul",
|
136 |
"label": "Unsupervised Learning",
|
137 |
-
"relationship": "
|
138 |
"subnodes": [
|
139 |
-
{
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
]
|
142 |
}
|
143 |
]
|
144 |
},
|
145 |
{
|
146 |
"id": "nlp",
|
147 |
-
"label": "
|
148 |
-
"relationship": "
|
149 |
"subnodes": [
|
150 |
{
|
151 |
"id": "sa",
|
152 |
"label": "Sentiment Analysis",
|
153 |
-
"relationship": "
|
154 |
-
"subnodes": [
|
155 |
-
{"id": "tc", "label": "Text Classification", "relationship": "method"},
|
156 |
-
{"id": "absa", "label": "Aspect-Based Sentiment Analysis", "relationship": "method"}
|
157 |
-
]
|
158 |
-
},
|
159 |
-
{
|
160 |
-
"id": "tr",
|
161 |
-
"label": "Translation",
|
162 |
-
"relationship": "task",
|
163 |
-
"subnodes": [
|
164 |
-
{"id": "nmt", "label": "Neural Machine Translation", "relationship": "method"},
|
165 |
-
{"id": "rbt", "label": "Rule-Based Translation", "relationship": "method"}
|
166 |
-
]
|
167 |
-
}
|
168 |
-
]
|
169 |
-
},
|
170 |
-
{
|
171 |
-
"id": "cv",
|
172 |
-
"label": "Computer Vision",
|
173 |
-
"relationship": "application_area",
|
174 |
-
"subnodes": [
|
175 |
-
{
|
176 |
-
"id": "od",
|
177 |
-
"label": "Object Detection",
|
178 |
-
"relationship": "task",
|
179 |
"subnodes": [
|
180 |
-
{"id": "
|
181 |
-
{"id": "rcnn", "label": "R-CNN", "relationship": "algorithm"}
|
182 |
]
|
183 |
}
|
184 |
]
|
@@ -190,18 +212,18 @@ if __name__ == "__main__":
|
|
190 |
demo = gr.Interface(
|
191 |
fn=generate_concept_map,
|
192 |
inputs=gr.Textbox(
|
193 |
-
value=sample_json,
|
194 |
-
placeholder="Paste
|
195 |
-
label="JSON Input",
|
196 |
-
lines=
|
197 |
),
|
198 |
outputs=gr.Image(
|
199 |
-
label="Concept Map",
|
200 |
type="filepath",
|
201 |
-
|
202 |
),
|
203 |
title="Advanced Concept Map Generator",
|
204 |
-
description="Create
|
205 |
)
|
206 |
|
207 |
demo.launch(
|
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
from graphviz import Digraph
|
4 |
+
import os
|
5 |
+
from tempfile import NamedTemporaryFile
|
6 |
|
7 |
def generate_concept_map(json_input: str) -> str:
|
8 |
"""
|
9 |
+
Generate concept map from JSON and return as image file
|
10 |
|
11 |
Args:
|
12 |
json_input (str): JSON describing the concept map structure.
|
13 |
|
14 |
+
REQUIRED FORMAT EXAMPLE:
|
15 |
+
{
|
16 |
+
"central_node": "AI",
|
17 |
+
"nodes": [
|
18 |
+
{
|
19 |
+
"id": "ml",
|
20 |
+
"label": "Machine Learning",
|
21 |
+
"relationship": "subcategory",
|
22 |
+
"subnodes": [
|
23 |
+
{
|
24 |
+
"id": "dl",
|
25 |
+
"label": "Deep Learning",
|
26 |
+
"relationship": "type",
|
27 |
+
"subnodes": [
|
28 |
+
{
|
29 |
+
"id": "cnn",
|
30 |
+
"label": "CNN",
|
31 |
+
"relationship": "architecture"
|
32 |
+
}
|
33 |
+
]
|
34 |
+
}
|
35 |
+
]
|
36 |
+
}
|
37 |
+
]
|
38 |
+
}
|
39 |
+
|
40 |
Returns:
|
41 |
+
str: Path to generated PNG image file
|
42 |
"""
|
43 |
try:
|
44 |
if not json_input.strip():
|
|
|
128 |
fontsize='8'
|
129 |
)
|
130 |
|
131 |
+
# Save to temporary file
|
132 |
+
with NamedTemporaryFile(delete=False, suffix='.png') as tmp:
|
133 |
+
dot.render(tmp.name, format='png', cleanup=True)
|
134 |
+
return tmp.name + '.png'
|
135 |
|
136 |
except json.JSONDecodeError:
|
137 |
return "Error: Invalid JSON format"
|
|
|
139 |
return f"Error: {str(e)}"
|
140 |
|
141 |
if __name__ == "__main__":
|
142 |
+
# Complex sample JSON
|
143 |
sample_json = """
|
144 |
{
|
145 |
"central_node": "Artificial Intelligence (AI)",
|
|
|
147 |
{
|
148 |
"id": "ml",
|
149 |
"label": "Machine Learning",
|
150 |
+
"relationship": "Core Component",
|
151 |
"subnodes": [
|
152 |
{
|
153 |
"id": "sl",
|
154 |
"label": "Supervised Learning",
|
155 |
+
"relationship": "Learning Type",
|
156 |
"subnodes": [
|
157 |
+
{
|
158 |
+
"id": "reg",
|
159 |
+
"label": "Regression",
|
160 |
+
"relationship": "Technique",
|
161 |
+
"subnodes": [
|
162 |
+
{"id": "lr", "label": "Linear Regression", "relationship": "Algorithm"}
|
163 |
+
]
|
164 |
+
},
|
165 |
+
{
|
166 |
+
"id": "clf",
|
167 |
+
"label": "Classification",
|
168 |
+
"relationship": "Technique",
|
169 |
+
"subnodes": [
|
170 |
+
{"id": "svm", "label": "SVM", "relationship": "Algorithm"},
|
171 |
+
{"id": "rf", "label": "Random Forest", "relationship": "Algorithm"}
|
172 |
+
]
|
173 |
+
}
|
174 |
]
|
175 |
},
|
176 |
{
|
177 |
"id": "ul",
|
178 |
"label": "Unsupervised Learning",
|
179 |
+
"relationship": "Learning Type",
|
180 |
"subnodes": [
|
181 |
+
{
|
182 |
+
"id": "clus",
|
183 |
+
"label": "Clustering",
|
184 |
+
"relationship": "Technique",
|
185 |
+
"subnodes": [
|
186 |
+
{"id": "kmeans", "label": "K-Means", "relationship": "Algorithm"}
|
187 |
+
]
|
188 |
+
}
|
189 |
]
|
190 |
}
|
191 |
]
|
192 |
},
|
193 |
{
|
194 |
"id": "nlp",
|
195 |
+
"label": "NLP",
|
196 |
+
"relationship": "Application Domain",
|
197 |
"subnodes": [
|
198 |
{
|
199 |
"id": "sa",
|
200 |
"label": "Sentiment Analysis",
|
201 |
+
"relationship": "Task",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
"subnodes": [
|
203 |
+
{"id": "tb", "label": "Transformer-Based", "relationship": "Approach"}
|
|
|
204 |
]
|
205 |
}
|
206 |
]
|
|
|
212 |
demo = gr.Interface(
|
213 |
fn=generate_concept_map,
|
214 |
inputs=gr.Textbox(
|
215 |
+
value=sample_json,
|
216 |
+
placeholder="Paste JSON following the documented format",
|
217 |
+
label="Structured JSON Input",
|
218 |
+
lines=25
|
219 |
),
|
220 |
outputs=gr.Image(
|
221 |
+
label="Generated Concept Map",
|
222 |
type="filepath",
|
223 |
+
show_download_button=True
|
224 |
),
|
225 |
title="Advanced Concept Map Generator",
|
226 |
+
description="Create multi-level concept maps from properly formatted JSON"
|
227 |
)
|
228 |
|
229 |
demo.launch(
|