Update process_flow_generator.py
Browse files- process_flow_generator.py +108 -4
process_flow_generator.py
CHANGED
@@ -2,15 +2,14 @@ import graphviz
|
|
2 |
import json
|
3 |
from tempfile import NamedTemporaryFile
|
4 |
import os
|
|
|
5 |
|
6 |
def generate_process_flow_diagram(json_input: str, output_format: str) -> str:
|
7 |
"""
|
8 |
Generates a Process Flow Diagram (Flowchart) from JSON input.
|
9 |
-
|
10 |
Args:
|
11 |
json_input (str): A JSON string describing the process flow structure.
|
12 |
It must follow the Expected JSON Format Example below.
|
13 |
-
|
14 |
Expected JSON Format Example:
|
15 |
{
|
16 |
"start_node": "Start Inference Request",
|
@@ -80,7 +79,6 @@ def generate_process_flow_diagram(json_input: str, output_format: str) -> str:
|
|
80 |
{"from": "log_error", "to": "end_inference_process", "label": "Error Handled"}
|
81 |
]
|
82 |
}
|
83 |
-
|
84 |
Returns:
|
85 |
str: The filepath to the generated PNG image file.
|
86 |
"""
|
@@ -204,4 +202,110 @@ def generate_process_flow_diagram(json_input: str, output_format: str) -> str:
|
|
204 |
except json.JSONDecodeError:
|
205 |
return "Error: Invalid JSON format"
|
206 |
except Exception as e:
|
207 |
-
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import json
|
3 |
from tempfile import NamedTemporaryFile
|
4 |
import os
|
5 |
+
from PIL import Image
|
6 |
|
7 |
def generate_process_flow_diagram(json_input: str, output_format: str) -> str:
|
8 |
"""
|
9 |
Generates a Process Flow Diagram (Flowchart) from JSON input.
|
|
|
10 |
Args:
|
11 |
json_input (str): A JSON string describing the process flow structure.
|
12 |
It must follow the Expected JSON Format Example below.
|
|
|
13 |
Expected JSON Format Example:
|
14 |
{
|
15 |
"start_node": "Start Inference Request",
|
|
|
79 |
{"from": "log_error", "to": "end_inference_process", "label": "Error Handled"}
|
80 |
]
|
81 |
}
|
|
|
82 |
Returns:
|
83 |
str: The filepath to the generated PNG image file.
|
84 |
"""
|
|
|
202 |
except json.JSONDecodeError:
|
203 |
return "Error: Invalid JSON format"
|
204 |
except Exception as e:
|
205 |
+
return f"Error: {str(e)}"
|
206 |
+
|
207 |
+
# PPT ํตํฉ์ ์ํ ์ถ๊ฐ ํจ์
|
208 |
+
def generate_process_flow_for_ppt(topic: str, context: str, style: str = "Business Workflow") -> Image.Image:
|
209 |
+
"""
|
210 |
+
PPT ์์ฑ๊ธฐ์์ ์ฌ์ฉํ ํ๋ก์ธ์ค ํ๋ก์ฐ ๋ค์ด์ด๊ทธ๋จ ์์ฑ
|
211 |
+
|
212 |
+
Args:
|
213 |
+
topic: ํ๋ ์ ํ
์ด์
์ฃผ์
|
214 |
+
context: ์ฌ๋ผ์ด๋ ์ปจํ
์คํธ
|
215 |
+
style: ์คํ์ผ ํ์
|
216 |
+
|
217 |
+
Returns:
|
218 |
+
PIL Image ๊ฐ์ฒด
|
219 |
+
"""
|
220 |
+
# ์ปจํ
์คํธ์ ๋ฐ๋ฅธ ํ๋ก์ธ์ค ํ๋ก์ฐ JSON ์์ฑ
|
221 |
+
if "ํ๋ก์ธ์ค" in context or "process" in context.lower():
|
222 |
+
# ๋น์ฆ๋์ค ํ๋ก์ธ์ค ํ๋ก์ฐ
|
223 |
+
flow_json = {
|
224 |
+
"start_node": "์์",
|
225 |
+
"nodes": [
|
226 |
+
{"id": "analyze", "label": "ํํฉ ๋ถ์", "type": "process"},
|
227 |
+
{"id": "plan", "label": "๊ณํ ์๋ฆฝ", "type": "process"},
|
228 |
+
{"id": "review", "label": "๊ฒํ ํ์?", "type": "decision"},
|
229 |
+
{"id": "implement", "label": "์คํ", "type": "process"},
|
230 |
+
{"id": "monitor", "label": "๋ชจ๋ํฐ๋ง", "type": "process"},
|
231 |
+
{"id": "complete", "label": "์๋ฃ", "type": "end"}
|
232 |
+
],
|
233 |
+
"connections": [
|
234 |
+
{"from": "์์", "to": "analyze", "label": ""},
|
235 |
+
{"from": "analyze", "to": "plan", "label": "๋ถ์ ์๋ฃ"},
|
236 |
+
{"from": "plan", "to": "review", "label": ""},
|
237 |
+
{"from": "review", "to": "implement", "label": "์น์ธ"},
|
238 |
+
{"from": "review", "to": "plan", "label": "์ฌ๊ฒํ "},
|
239 |
+
{"from": "implement", "to": "monitor", "label": ""},
|
240 |
+
{"from": "monitor", "to": "complete", "label": "๋ชฉํ ๋ฌ์ฑ"}
|
241 |
+
]
|
242 |
+
}
|
243 |
+
elif "์ผ์ " in context or "timeline" in context.lower():
|
244 |
+
# ํ๋ก์ ํธ ์ผ์ ํ๋ก์ฐ
|
245 |
+
flow_json = {
|
246 |
+
"start_node": "ํ๋ก์ ํธ ์์",
|
247 |
+
"nodes": [
|
248 |
+
{"id": "phase1", "label": "1๋จ๊ณ: ๊ธฐํ", "type": "process"},
|
249 |
+
{"id": "milestone1", "label": "๊ธฐํ ๊ฒํ ", "type": "decision"},
|
250 |
+
{"id": "phase2", "label": "2๋จ๊ณ: ๊ฐ๋ฐ", "type": "process"},
|
251 |
+
{"id": "phase3", "label": "3๋จ๊ณ: ํ
์คํธ", "type": "process"},
|
252 |
+
{"id": "launch", "label": "๋ฐ์นญ", "type": "io"},
|
253 |
+
{"id": "end", "label": "ํ๋ก์ ํธ ์ข
๋ฃ", "type": "end"}
|
254 |
+
],
|
255 |
+
"connections": [
|
256 |
+
{"from": "ํ๋ก์ ํธ ์์", "to": "phase1", "label": ""},
|
257 |
+
{"from": "phase1", "to": "milestone1", "label": "4์ฃผ"},
|
258 |
+
{"from": "milestone1", "to": "phase2", "label": "์น์ธ"},
|
259 |
+
{"from": "milestone1", "to": "phase1", "label": "๋ณด์"},
|
260 |
+
{"from": "phase2", "to": "phase3", "label": "8์ฃผ"},
|
261 |
+
{"from": "phase3", "to": "launch", "label": "2์ฃผ"},
|
262 |
+
{"from": "launch", "to": "end", "label": ""}
|
263 |
+
]
|
264 |
+
}
|
265 |
+
else:
|
266 |
+
# ๊ธฐ๋ณธ ์ํฌํ๋ก์ฐ
|
267 |
+
flow_json = {
|
268 |
+
"start_node": "์์",
|
269 |
+
"nodes": [
|
270 |
+
{"id": "input", "label": "์
๋ ฅ ๋จ๊ณ", "type": "io"},
|
271 |
+
{"id": "process", "label": "์ฒ๋ฆฌ ๋จ๊ณ", "type": "process"},
|
272 |
+
{"id": "check", "label": "๊ฒ์ฆ", "type": "decision"},
|
273 |
+
{"id": "output", "label": "์ถ๋ ฅ ๋จ๊ณ", "type": "io"},
|
274 |
+
{"id": "end", "label": "์ข
๋ฃ", "type": "end"}
|
275 |
+
],
|
276 |
+
"connections": [
|
277 |
+
{"from": "์์", "to": "input", "label": ""},
|
278 |
+
{"from": "input", "to": "process", "label": "๋ฐ์ดํฐ"},
|
279 |
+
{"from": "process", "to": "check", "label": ""},
|
280 |
+
{"from": "check", "to": "output", "label": "ํต๊ณผ"},
|
281 |
+
{"from": "check", "to": "process", "label": "์ฌ์ฒ๋ฆฌ"},
|
282 |
+
{"from": "output", "to": "end", "label": ""}
|
283 |
+
]
|
284 |
+
}
|
285 |
+
|
286 |
+
# JSON์ ๋ฌธ์์ด๋ก ๋ณํ
|
287 |
+
json_str = json.dumps(flow_json, ensure_ascii=False)
|
288 |
+
|
289 |
+
# ํ๋ก์ธ์ค ํ๋ก์ฐ ๋ค์ด์ด๊ทธ๋จ ์์ฑ
|
290 |
+
png_path = generate_process_flow_diagram(json_str, 'png')
|
291 |
+
|
292 |
+
if png_path.startswith("Error:"):
|
293 |
+
# ์๋ฌ ๋ฐ์ ์ ๊ธฐ๋ณธ ์ด๋ฏธ์ง ๋ฐํ
|
294 |
+
from PIL import Image, ImageDraw, ImageFont
|
295 |
+
img = Image.new('RGB', (800, 600), 'white')
|
296 |
+
draw = ImageDraw.Draw(img)
|
297 |
+
draw.text((400, 300), png_path, fill='red', anchor='mm')
|
298 |
+
return img
|
299 |
+
|
300 |
+
# PNG ํ์ผ์ PIL Image๋ก ๋ณํ
|
301 |
+
with Image.open(png_path) as img:
|
302 |
+
# ์ด๋ฏธ์ง ๋ณต์ฌ๋ณธ ์์ฑ (ํ์ผ ๋ซ๊ธฐ ์ํด)
|
303 |
+
img_copy = img.copy()
|
304 |
+
|
305 |
+
# ์์ ํ์ผ ์ญ์
|
306 |
+
try:
|
307 |
+
os.unlink(png_path)
|
308 |
+
except:
|
309 |
+
pass
|
310 |
+
|
311 |
+
return img_copy
|