Update process_flow_generator.py
Browse files- process_flow_generator.py +207 -164
process_flow_generator.py
CHANGED
@@ -3,84 +3,70 @@ 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",
|
16 |
-
"nodes": [
|
17 |
-
{
|
18 |
-
"id": "user_input",
|
19 |
-
"label": "Receive User Input (Data)",
|
20 |
-
"type": "io"
|
21 |
-
},
|
22 |
-
{
|
23 |
-
"id": "preprocess_data",
|
24 |
-
"label": "Preprocess Data",
|
25 |
-
"type": "process"
|
26 |
-
},
|
27 |
-
{
|
28 |
-
"id": "validate_data",
|
29 |
-
"label": "Validate Data Format/Type",
|
30 |
-
"type": "decision"
|
31 |
-
},
|
32 |
-
{
|
33 |
-
"id": "data_valid_yes",
|
34 |
-
"label": "Data Valid?",
|
35 |
-
"type": "decision"
|
36 |
-
},
|
37 |
-
{
|
38 |
-
"id": "load_model",
|
39 |
-
"label": "Load AI Model (if not cached)",
|
40 |
-
"type": "process"
|
41 |
-
},
|
42 |
-
{
|
43 |
-
"id": "run_inference",
|
44 |
-
"label": "Run AI Model Inference",
|
45 |
-
"type": "process"
|
46 |
-
},
|
47 |
-
{
|
48 |
-
"id": "postprocess_output",
|
49 |
-
"label": "Postprocess Model Output",
|
50 |
-
"type": "process"
|
51 |
-
},
|
52 |
-
{
|
53 |
-
"id": "send_response",
|
54 |
-
"label": "Send Response to User",
|
55 |
-
"type": "io"
|
56 |
-
},
|
57 |
-
{
|
58 |
-
"id": "log_error",
|
59 |
-
"label": "Log Error & Notify User",
|
60 |
-
"type": "process"
|
61 |
-
},
|
62 |
-
{
|
63 |
-
"id": "end_inference_process",
|
64 |
-
"label": "End Inference Process",
|
65 |
-
"type": "end"
|
66 |
-
}
|
67 |
-
],
|
68 |
-
"connections": [
|
69 |
-
{"from": "start_node", "to": "user_input", "label": "Request"},
|
70 |
-
{"from": "user_input", "to": "preprocess_data", "label": "Data Received"},
|
71 |
-
{"from": "preprocess_data", "to": "validate_data", "label": "Cleaned"},
|
72 |
-
{"from": "validate_data", "to": "data_valid_yes", "label": "Check"},
|
73 |
-
{"from": "data_valid_yes", "to": "load_model", "label": "Yes"},
|
74 |
-
{"from": "data_valid_yes", "to": "log_error", "label": "No"},
|
75 |
-
{"from": "load_model", "to": "run_inference", "label": "Model Ready"},
|
76 |
-
{"from": "run_inference", "to": "postprocess_output", "label": "Output Generated"},
|
77 |
-
{"from": "postprocess_output", "to": "send_response", "label": "Ready"},
|
78 |
-
{"from": "send_response", "to": "end_inference_process", "label": "Response Sent"},
|
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 |
"""
|
85 |
try:
|
86 |
if not json_input.strip():
|
@@ -103,33 +89,41 @@ def generate_process_flow_diagram(json_input: str, output_format: str) -> str:
|
|
103 |
"default": "box" # Fallback
|
104 |
}
|
105 |
|
106 |
-
#
|
107 |
-
# GDFONTPATH๊ฐ ์ค์ ๋์ด ์์ผ๋ฉด ํฐํธ ํ์ผ๋ช
(ํ์ฅ์ ์ ์ธ) ์ฌ์ฉ
|
108 |
-
korean_font = 'NanumGothic-Regular'
|
109 |
-
|
110 |
dot = graphviz.Digraph(
|
111 |
name='ProcessFlowDiagram',
|
112 |
format='png',
|
|
|
113 |
graph_attr={
|
114 |
-
'rankdir': 'TB', # Top-to-Bottom flow
|
115 |
'splines': 'ortho', # Straight lines with 90-degree bends
|
116 |
-
'bgcolor': 'white',
|
117 |
-
'pad': '0.
|
118 |
-
'
|
119 |
-
'
|
120 |
-
'
|
121 |
-
'
|
|
|
|
|
|
|
|
|
122 |
},
|
123 |
node_attr={
|
124 |
-
'fontname':
|
|
|
|
|
|
|
|
|
|
|
125 |
},
|
126 |
edge_attr={
|
127 |
-
'fontname':
|
|
|
|
|
128 |
}
|
129 |
)
|
130 |
|
131 |
-
base_color = '#19191a'
|
132 |
-
|
133 |
fill_color_for_nodes = base_color
|
134 |
font_color_for_nodes = 'white' if base_color == '#19191a' or base_color.lower() in ['#000000', '#19191a'] else 'black'
|
135 |
|
@@ -140,38 +134,46 @@ def generate_process_flow_diagram(json_input: str, output_format: str) -> str:
|
|
140 |
start_node_id = data['start_node']
|
141 |
dot.node(
|
142 |
start_node_id,
|
143 |
-
start_node_id,
|
144 |
shape=node_shapes['start'],
|
145 |
style='filled,rounded',
|
146 |
-
fillcolor='#2196F3',
|
147 |
fontcolor='white',
|
148 |
-
fontsize='
|
149 |
-
|
|
|
150 |
)
|
151 |
|
152 |
-
# Add all other nodes
|
153 |
for node_id, node_info in all_defined_nodes.items():
|
154 |
-
if node_id == start_node_id:
|
155 |
continue
|
156 |
|
157 |
node_type = node_info.get("type", "default")
|
158 |
shape = node_shapes.get(node_type, "box")
|
159 |
-
|
160 |
node_label = node_info['label']
|
161 |
|
162 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
if node_type == 'end':
|
164 |
-
|
165 |
node_id,
|
166 |
node_label,
|
167 |
shape=shape,
|
168 |
style='filled,rounded',
|
169 |
-
fillcolor='#F44336',
|
170 |
fontcolor='white',
|
171 |
-
fontsize='
|
172 |
-
|
|
|
173 |
)
|
174 |
-
else:
|
175 |
dot.node(
|
176 |
node_id,
|
177 |
node_label,
|
@@ -179,8 +181,9 @@ def generate_process_flow_diagram(json_input: str, output_format: str) -> str:
|
|
179 |
style='filled,rounded',
|
180 |
fillcolor=fill_color_for_nodes,
|
181 |
fontcolor=font_color_for_nodes,
|
182 |
-
fontsize='
|
183 |
-
|
|
|
184 |
)
|
185 |
|
186 |
# Add connections (edges)
|
@@ -189,117 +192,157 @@ def generate_process_flow_diagram(json_input: str, output_format: str) -> str:
|
|
189 |
connection['from'],
|
190 |
connection['to'],
|
191 |
label=connection.get('label', ''),
|
192 |
-
color='#4a4a4a',
|
193 |
fontcolor='#4a4a4a',
|
194 |
-
fontsize='
|
195 |
-
fontname=korean_font # ํ๊ธ ํฐํธ ์ถ๊ฐ
|
196 |
)
|
197 |
|
|
|
198 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
199 |
dot.render(tmp.name, format=output_format, cleanup=True)
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
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 |
-
|
213 |
-
|
214 |
-
context: ์ฌ๋ผ์ด๋ ์ปจํ
์คํธ
|
215 |
-
style: ์คํ์ผ ํ์
|
216 |
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
#
|
221 |
-
if "
|
222 |
-
# ๋น์ฆ๋์ค ํ๋ก์ธ์ค ํ๋ก์ฐ
|
223 |
flow_json = {
|
224 |
"start_node": "์์",
|
225 |
"nodes": [
|
226 |
-
{"id": "
|
227 |
-
{"id": "
|
228 |
-
{"id": "
|
229 |
-
{"id": "
|
230 |
-
{"id": "
|
231 |
-
{"id": "
|
232 |
],
|
233 |
"connections": [
|
234 |
-
{"from": "์์", "to": "
|
235 |
-
{"from": "
|
236 |
-
{"from": "
|
237 |
-
{"from": "
|
238 |
-
{"from": "
|
239 |
-
{"from": "
|
240 |
-
{"from": "
|
241 |
]
|
242 |
}
|
243 |
-
elif "
|
244 |
-
# ํ๋ก์ ํธ ์ผ์ ํ๋ก์ฐ
|
245 |
flow_json = {
|
246 |
-
"start_node": "
|
247 |
"nodes": [
|
248 |
-
{"id": "
|
249 |
-
{"id": "
|
250 |
-
{"id": "
|
251 |
-
{"id": "
|
252 |
-
{"id": "
|
253 |
-
{"id": "end", "label": "ํ๋ก์ ํธ ์ข
๋ฃ", "type": "end"}
|
254 |
],
|
255 |
"connections": [
|
256 |
-
{"from": "
|
257 |
-
{"from": "
|
258 |
-
{"from": "
|
259 |
-
{"from": "
|
260 |
-
{"from": "
|
261 |
-
{"from": "
|
262 |
-
{"from": "launch", "to": "end", "label": ""}
|
263 |
]
|
264 |
}
|
265 |
else:
|
266 |
-
# ๊ธฐ๋ณธ
|
267 |
flow_json = {
|
268 |
"start_node": "์์",
|
269 |
"nodes": [
|
270 |
-
{"id": "
|
271 |
-
{"id": "
|
272 |
-
{"id": "
|
273 |
-
{"id": "
|
274 |
-
{"id": "
|
275 |
],
|
276 |
"connections": [
|
277 |
-
{"from": "์์", "to": "
|
278 |
-
{"from": "
|
279 |
-
{"from": "
|
280 |
-
{"from": "
|
281 |
-
{"from": "check", "to": "
|
282 |
-
{"from": "
|
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
|
295 |
-
img = Image.new('RGB', (
|
296 |
draw = ImageDraw.Draw(img)
|
297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
return img
|
299 |
|
|
|
|
|
300 |
# PNG ํ์ผ์ PIL Image๋ก ๋ณํ
|
301 |
with Image.open(png_path) as img:
|
302 |
-
# ์ด๋ฏธ์ง ๋ณต์ฌ๋ณธ ์์ฑ (ํ์ผ ๋ซ๊ธฐ ์ํด)
|
303 |
img_copy = img.copy()
|
304 |
|
305 |
# ์์ ํ์ผ ์ญ์
|
|
|
3 |
from tempfile import NamedTemporaryFile
|
4 |
import os
|
5 |
from PIL import Image
|
6 |
+
import platform
|
7 |
+
import subprocess
|
8 |
+
|
9 |
+
def setup_korean_font_env():
|
10 |
+
"""ํ๊ธ ํฐํธ ํ๊ฒฝ ์ค์ """
|
11 |
+
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
12 |
+
FONT_PATH = os.path.join(CURRENT_DIR, 'NanumGothic-Regular.ttf')
|
13 |
+
|
14 |
+
# ํฐํธ ํ์ผ ์กด์ฌ ํ์ธ
|
15 |
+
if not os.path.exists(FONT_PATH):
|
16 |
+
print(f"[๊ฒฝ๊ณ ] ํ๊ธ ํฐํธ ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค: {FONT_PATH}")
|
17 |
+
return None
|
18 |
+
|
19 |
+
# ํ๊ฒฝ ๋ณ์ ์ค์
|
20 |
+
os.environ['GDFONTPATH'] = CURRENT_DIR
|
21 |
+
|
22 |
+
# fonts.conf ์์ฑ
|
23 |
+
fonts_conf_path = os.path.join(CURRENT_DIR, 'fonts.conf')
|
24 |
+
fonts_conf_content = f"""<?xml version="1.0"?>
|
25 |
+
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
|
26 |
+
<fontconfig>
|
27 |
+
<dir>{CURRENT_DIR}</dir>
|
28 |
+
<cachedir>/tmp/fontconfig-cache</cachedir>
|
29 |
+
|
30 |
+
<match target="pattern">
|
31 |
+
<test name="family">
|
32 |
+
<string>NanumGothic</string>
|
33 |
+
</test>
|
34 |
+
<edit name="family" mode="assign" binding="same">
|
35 |
+
<string>NanumGothic-Regular</string>
|
36 |
+
</edit>
|
37 |
+
</match>
|
38 |
+
|
39 |
+
<match target="pattern">
|
40 |
+
<test name="family">
|
41 |
+
<string>NanumGothic-Regular</string>
|
42 |
+
</test>
|
43 |
+
<edit name="file" mode="assign" binding="same">
|
44 |
+
<string>{FONT_PATH}</string>
|
45 |
+
</edit>
|
46 |
+
</match>
|
47 |
+
|
48 |
+
<alias binding="same">
|
49 |
+
<family>NanumGothic</family>
|
50 |
+
<default>
|
51 |
+
<family>NanumGothic-Regular</family>
|
52 |
+
</default>
|
53 |
+
</alias>
|
54 |
+
</fontconfig>"""
|
55 |
+
|
56 |
+
with open(fonts_conf_path, 'w', encoding='utf-8') as f:
|
57 |
+
f.write(fonts_conf_content)
|
58 |
+
|
59 |
+
os.environ['FONTCONFIG_FILE'] = fonts_conf_path
|
60 |
+
os.environ['FONTCONFIG_PATH'] = CURRENT_DIR
|
61 |
+
|
62 |
+
return FONT_PATH
|
63 |
+
|
64 |
+
# ํฐํธ ์ค์ ์ด๊ธฐํ
|
65 |
+
FONT_PATH = setup_korean_font_env()
|
66 |
|
67 |
def generate_process_flow_diagram(json_input: str, output_format: str) -> str:
|
68 |
"""
|
69 |
Generates a Process Flow Diagram (Flowchart) from JSON input.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
"""
|
71 |
try:
|
72 |
if not json_input.strip():
|
|
|
89 |
"default": "box" # Fallback
|
90 |
}
|
91 |
|
92 |
+
# Graphviz ๋ค์ด์ด๊ทธ๋จ ์์ฑ - ํฌ๊ธฐ์ ์ฌ๋ฐฑ ์กฐ์
|
|
|
|
|
|
|
93 |
dot = graphviz.Digraph(
|
94 |
name='ProcessFlowDiagram',
|
95 |
format='png',
|
96 |
+
encoding='utf-8',
|
97 |
graph_attr={
|
98 |
+
'rankdir': 'TB', # Top-to-Bottom flow
|
99 |
'splines': 'ortho', # Straight lines with 90-degree bends
|
100 |
+
'bgcolor': 'white',
|
101 |
+
'pad': '0.2', # ์ ์ฒด ํจ๋ฉ ์ค์ (0.5 -> 0.2)
|
102 |
+
'margin': '0.2', # ๋ง์ง ์ถ๊ฐ
|
103 |
+
'nodesep': '0.4', # ๋
ธ๋ ๊ฐ ๊ฐ๊ฒฉ ์ค์ (0.6 -> 0.4)
|
104 |
+
'ranksep': '0.6', # ๋ญํฌ ๊ฐ ๊ฐ๊ฒฉ ์ค์ (0.8 -> 0.6)
|
105 |
+
'fontname': 'NanumGothic-Regular',
|
106 |
+
'charset': 'UTF-8',
|
107 |
+
'dpi': '96', # DPI ์ค์ (150 -> 96)
|
108 |
+
'size': '10,7.5', # ์ ์ฒด ๊ทธ๋ํ ํฌ๊ธฐ ์ ํ (์ธ์น ๋จ์)
|
109 |
+
'ratio': 'compress' # ๋น์จ ์์ถ
|
110 |
},
|
111 |
node_attr={
|
112 |
+
'fontname': 'NanumGothic-Regular',
|
113 |
+
'fontsize': '12', # ํฐํธ ํฌ๊ธฐ ์ค์ (14 -> 12)
|
114 |
+
'charset': 'UTF-8',
|
115 |
+
'height': '0.6', # ๋
ธ๋ ๋์ด ์ง์
|
116 |
+
'width': '2.0', # ๋
ธ๋ ๋๋น ์ง์
|
117 |
+
'margin': '0.2,0.1' # ๋
ธ๋ ๋ด๋ถ ๋ง์ง
|
118 |
},
|
119 |
edge_attr={
|
120 |
+
'fontname': 'NanumGothic-Regular',
|
121 |
+
'fontsize': '9', # ์ฃ์ง ํฐํธ ํฌ๊ธฐ ์ค์ (10 -> 9)
|
122 |
+
'charset': 'UTF-8'
|
123 |
}
|
124 |
)
|
125 |
|
126 |
+
base_color = '#19191a'
|
|
|
127 |
fill_color_for_nodes = base_color
|
128 |
font_color_for_nodes = 'white' if base_color == '#19191a' or base_color.lower() in ['#000000', '#19191a'] else 'black'
|
129 |
|
|
|
134 |
start_node_id = data['start_node']
|
135 |
dot.node(
|
136 |
start_node_id,
|
137 |
+
start_node_id,
|
138 |
shape=node_shapes['start'],
|
139 |
style='filled,rounded',
|
140 |
+
fillcolor='#2196F3',
|
141 |
fontcolor='white',
|
142 |
+
fontsize='12',
|
143 |
+
height='0.5',
|
144 |
+
width='1.8'
|
145 |
)
|
146 |
|
147 |
+
# Add all other nodes
|
148 |
for node_id, node_info in all_defined_nodes.items():
|
149 |
+
if node_id == start_node_id:
|
150 |
continue
|
151 |
|
152 |
node_type = node_info.get("type", "default")
|
153 |
shape = node_shapes.get(node_type, "box")
|
|
|
154 |
node_label = node_info['label']
|
155 |
|
156 |
+
# ๋
ธ๋ ํ์
์ ๋ฐ๋ฅธ ํฌ๊ธฐ ์กฐ์
|
157 |
+
if node_type == 'decision':
|
158 |
+
height = '0.8'
|
159 |
+
width = '1.5'
|
160 |
+
else:
|
161 |
+
height = '0.6'
|
162 |
+
width = '2.0'
|
163 |
+
|
164 |
if node_type == 'end':
|
165 |
+
dot.node(
|
166 |
node_id,
|
167 |
node_label,
|
168 |
shape=shape,
|
169 |
style='filled,rounded',
|
170 |
+
fillcolor='#F44336',
|
171 |
fontcolor='white',
|
172 |
+
fontsize='12',
|
173 |
+
height='0.5',
|
174 |
+
width='1.8'
|
175 |
)
|
176 |
+
else:
|
177 |
dot.node(
|
178 |
node_id,
|
179 |
node_label,
|
|
|
181 |
style='filled,rounded',
|
182 |
fillcolor=fill_color_for_nodes,
|
183 |
fontcolor=font_color_for_nodes,
|
184 |
+
fontsize='12',
|
185 |
+
height=height,
|
186 |
+
width=width
|
187 |
)
|
188 |
|
189 |
# Add connections (edges)
|
|
|
192 |
connection['from'],
|
193 |
connection['to'],
|
194 |
label=connection.get('label', ''),
|
195 |
+
color='#4a4a4a',
|
196 |
fontcolor='#4a4a4a',
|
197 |
+
fontsize='9'
|
|
|
198 |
)
|
199 |
|
200 |
+
# PNG๋ก ์ง์ ๋ ๋๋ง (ํฌ๊ธฐ ์กฐ์ ๋จ)
|
201 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
202 |
dot.render(tmp.name, format=output_format, cleanup=True)
|
203 |
+
png_path = f"{tmp.name}.{output_format}"
|
204 |
+
|
205 |
+
# ์์ฑ๋ ์ด๋ฏธ์ง๋ฅผ PIL๋ก ์ด์ด์ ํฌ๊ธฐ ํ์ธ ๋ฐ ์กฐ์
|
206 |
+
from PIL import Image
|
207 |
+
with Image.open(png_path) as img:
|
208 |
+
width, height = img.size
|
209 |
+
print(f"[ํ๋ก์ธ์ค ํ๋ก์ฐ] ์์ฑ๋ ์ด๋ฏธ์ง ํฌ๊ธฐ: {width}x{height}")
|
210 |
+
|
211 |
+
# ์ด๋ฏธ์ง๊ฐ ๋๋ฌด ํฌ๋ฉด ๋ฆฌ์ฌ์ด์ฆ
|
212 |
+
max_width = 1200
|
213 |
+
max_height = 900
|
214 |
+
|
215 |
+
if width > max_width or height > max_height:
|
216 |
+
# ๋น์จ ์ ์งํ๋ฉด์ ๋ฆฌ์ฌ์ด์ฆ
|
217 |
+
ratio = min(max_width/width, max_height/height)
|
218 |
+
new_width = int(width * ratio)
|
219 |
+
new_height = int(height * ratio)
|
220 |
+
|
221 |
+
img_resized = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
222 |
+
|
223 |
+
# ์ฌ๋ฐฑ ์ถ๊ฐํ์ฌ ์ค์ ์ ๋ ฌ
|
224 |
+
final_img = Image.new('RGB', (max_width, max_height), 'white')
|
225 |
+
x = (max_width - new_width) // 2
|
226 |
+
y = (max_height - new_height) // 2
|
227 |
+
final_img.paste(img_resized, (x, y))
|
228 |
+
|
229 |
+
# ์๋ก์ด ํ์ผ๋ก ์ ์ฅ
|
230 |
+
new_path = png_path.replace('.png', '_resized.png')
|
231 |
+
final_img.save(new_path)
|
232 |
+
os.unlink(png_path) # ์๋ณธ ์ญ์
|
233 |
+
return new_path
|
234 |
+
|
235 |
+
return png_path
|
236 |
|
237 |
except json.JSONDecodeError:
|
238 |
return "Error: Invalid JSON format"
|
239 |
except Exception as e:
|
240 |
return f"Error: {str(e)}"
|
241 |
|
|
|
242 |
def generate_process_flow_for_ppt(topic: str, context: str, style: str = "Business Workflow") -> Image.Image:
|
243 |
"""
|
244 |
PPT ์์ฑ๊ธฐ์์ ์ฌ์ฉํ ํ๋ก์ธ์ค ํ๋ก์ฐ ๋ค์ด์ด๊ทธ๋จ ์์ฑ
|
245 |
+
"""
|
246 |
+
print(f"[ํ๋ก์ธ์ค ํ๋ก์ฐ] ์์ฑ ์์ - ์ฃผ์ : {topic}, ์ปจํ
์คํธ: {context}")
|
247 |
|
248 |
+
# ํ๊ธ ํฐํธ ์ฌ์ค์ (๋งค๋ฒ ํ์ธ)
|
249 |
+
setup_korean_font_env()
|
|
|
|
|
250 |
|
251 |
+
# ์ปจํ
์คํธ ๋ถ์ํ์ฌ ์ ์ ํ ํ๋ก์ธ์ค ํ๋ก์ฐ JSON ์์ฑ
|
252 |
+
context_lower = context.lower()
|
253 |
+
|
254 |
+
# ๋
ธ๋ ์๋ฅผ ์ค์ด๊ณ ๋ ์ด๋ธ์ ์งง๊ฒ ๋ง๋ค์ด ๊ณต๊ฐ ์ ์ฝ
|
255 |
+
if "ํ๋ก์ ํธ" in context or "project" in context_lower:
|
|
|
256 |
flow_json = {
|
257 |
"start_node": "์์",
|
258 |
"nodes": [
|
259 |
+
{"id": "plan", "label": "๊ธฐํ", "type": "process"},
|
260 |
+
{"id": "design", "label": "์ค๊ณ", "type": "process"},
|
261 |
+
{"id": "develop", "label": "๊ฐ๋ฐ", "type": "process"},
|
262 |
+
{"id": "test", "label": "ํ
์คํธ", "type": "decision"},
|
263 |
+
{"id": "deploy", "label": "๋ฐฐํฌ", "type": "process"},
|
264 |
+
{"id": "end", "label": "์๋ฃ", "type": "end"}
|
265 |
],
|
266 |
"connections": [
|
267 |
+
{"from": "์์", "to": "plan", "label": ""},
|
268 |
+
{"from": "plan", "to": "design", "label": ""},
|
269 |
+
{"from": "design", "to": "develop", "label": ""},
|
270 |
+
{"from": "develop", "to": "test", "label": ""},
|
271 |
+
{"from": "test", "to": "deploy", "label": "ํต๊ณผ"},
|
272 |
+
{"from": "test", "to": "develop", "label": "์์ "},
|
273 |
+
{"from": "deploy", "to": "end", "label": ""}
|
274 |
]
|
275 |
}
|
276 |
+
elif "์๋" in context or "๊ธฐ๋ฅ" in context:
|
|
|
277 |
flow_json = {
|
278 |
+
"start_node": "์์",
|
279 |
"nodes": [
|
280 |
+
{"id": "input", "label": "์
๋ ฅ", "type": "io"},
|
281 |
+
{"id": "validate", "label": "๊ฒ์ฆ", "type": "decision"},
|
282 |
+
{"id": "process", "label": "์ฒ๋ฆฌ", "type": "process"},
|
283 |
+
{"id": "output", "label": "์ถ๋ ฅ", "type": "io"},
|
284 |
+
{"id": "end", "label": "์ข
๋ฃ", "type": "end"}
|
|
|
285 |
],
|
286 |
"connections": [
|
287 |
+
{"from": "์์", "to": "input", "label": ""},
|
288 |
+
{"from": "input", "to": "validate", "label": ""},
|
289 |
+
{"from": "validate", "to": "process", "label": "์ ํจ"},
|
290 |
+
{"from": "validate", "to": "input", "label": "์ฌ์
๋ ฅ"},
|
291 |
+
{"from": "process", "to": "output", "label": ""},
|
292 |
+
{"from": "output", "to": "end", "label": ""}
|
|
|
293 |
]
|
294 |
}
|
295 |
else:
|
296 |
+
# ๊ธฐ๋ณธ ํ๋ก์ธ์ค ํ๋ก์ฐ (๋ ๊ฐ๋จํ๊ฒ)
|
297 |
flow_json = {
|
298 |
"start_node": "์์",
|
299 |
"nodes": [
|
300 |
+
{"id": "analyze", "label": "๋ถ์", "type": "process"},
|
301 |
+
{"id": "plan", "label": "๊ณํ", "type": "process"},
|
302 |
+
{"id": "execute", "label": "์คํ", "type": "process"},
|
303 |
+
{"id": "check", "label": "๊ฒํ ", "type": "decision"},
|
304 |
+
{"id": "complete", "label": "์๋ฃ", "type": "end"}
|
305 |
],
|
306 |
"connections": [
|
307 |
+
{"from": "์์", "to": "analyze", "label": ""},
|
308 |
+
{"from": "analyze", "to": "plan", "label": ""},
|
309 |
+
{"from": "plan", "to": "execute", "label": ""},
|
310 |
+
{"from": "execute", "to": "check", "label": ""},
|
311 |
+
{"from": "check", "to": "complete", "label": "์น์ธ"},
|
312 |
+
{"from": "check", "to": "plan", "label": "์์ "}
|
313 |
]
|
314 |
}
|
315 |
|
316 |
# JSON์ ๋ฌธ์์ด๋ก ๋ณํ
|
317 |
json_str = json.dumps(flow_json, ensure_ascii=False)
|
318 |
|
319 |
+
print(f"[ํ๋ก์ธ์ค ํ๋ก์ฐ] JSON ์์ฑ ์๋ฃ")
|
320 |
+
|
321 |
# ํ๋ก์ธ์ค ํ๋ก์ฐ ๋ค์ด์ด๊ทธ๋จ ์์ฑ
|
322 |
png_path = generate_process_flow_diagram(json_str, 'png')
|
323 |
|
324 |
if png_path.startswith("Error:"):
|
325 |
+
print(f"[ํ๋ก์ธ์ค ํ๋ก์ฐ] ์์ฑ ์คํจ: {png_path}")
|
326 |
# ์๋ฌ ๋ฐ์ ์ ๊ธฐ๋ณธ ์ด๋ฏธ์ง ๋ฐํ
|
327 |
+
from PIL import ImageDraw, ImageFont
|
328 |
+
img = Image.new('RGB', (1200, 900), 'white')
|
329 |
draw = ImageDraw.Draw(img)
|
330 |
+
|
331 |
+
try:
|
332 |
+
if FONT_PATH and os.path.exists(FONT_PATH):
|
333 |
+
font = ImageFont.truetype(FONT_PATH, 20)
|
334 |
+
else:
|
335 |
+
font = ImageFont.load_default()
|
336 |
+
except:
|
337 |
+
font = ImageFont.load_default()
|
338 |
+
|
339 |
+
draw.text((600, 450), "ํ๋ก์ธ์ค ํ๋ก์ฐ ์์ฑ ์คํจ", fill='red', anchor='mm', font=font)
|
340 |
return img
|
341 |
|
342 |
+
print(f"[ํ๋ก์ธ์ค ํ๋ก์ฐ] PNG ์์ฑ ์ฑ๊ณต: {png_path}")
|
343 |
+
|
344 |
# PNG ํ์ผ์ PIL Image๋ก ๋ณํ
|
345 |
with Image.open(png_path) as img:
|
|
|
346 |
img_copy = img.copy()
|
347 |
|
348 |
# ์์ ํ์ผ ์ญ์
|