File size: 85,324 Bytes
432e12b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bbaec5
432e12b
 
 
 
 
 
 
 
 
 
 
 
 
1bbaec5
 
 
 
 
 
432e12b
1bbaec5
 
 
 
 
 
 
 
 
 
 
432e12b
1bbaec5
 
 
 
 
 
 
 
 
 
432e12b
 
 
 
 
 
 
706ad74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432e12b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248d0e
432e12b
b2f8727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d248d0e
 
 
 
 
432e12b
d248d0e
 
 
 
 
 
 
 
 
 
 
 
432e12b
d248d0e
b2f8727
 
 
 
 
 
 
 
 
432e12b
d248d0e
 
 
 
 
 
 
 
 
 
 
 
432e12b
d248d0e
 
 
 
432e12b
d248d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432e12b
d248d0e
432e12b
d248d0e
 
432e12b
d248d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432e12b
d248d0e
432e12b
d248d0e
 
432e12b
d248d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2f8727
d248d0e
 
 
 
 
 
 
 
b2f8727
d248d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2f8727
d248d0e
 
 
 
 
 
 
 
b2f8727
d248d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2f8727
d248d0e
 
 
 
 
 
 
 
 
 
b2f8727
d248d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2f8727
d248d0e
432e12b
d248d0e
 
 
 
 
 
 
 
 
b2f8727
d248d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432e12b
d248d0e
 
 
 
 
 
 
 
 
432e12b
d248d0e
 
 
 
 
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
# content_utils.py
"""Content generation and utility functions for PPT Generator"""

import re
import requests
import logging
import base64
import time
import traceback
from io import BytesIO
from PIL import Image
from typing import List, Dict, Tuple
import PyPDF2
import pandas as pd
import chardet
import replicate
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import urllib.request
from datetime import datetime
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
from pptx.enum.shapes import MSO_SHAPE

logger = logging.getLogger(__name__)

# Import process flow generator
try:
    from process_flow_generator import generate_process_flow_for_ppt
    PROCESS_FLOW_AVAILABLE = True
except ImportError:
    PROCESS_FLOW_AVAILABLE = False
    print("[Warning] Cannot import process_flow_generator. Process flow diagram feature disabled.")

def parse_executor_response(executor_response: str, slides_config: List[Dict], language: str = "English") -> Dict[int, Dict]:
    """실행자 AI의 응답을 파싱하여 슬라이드 데이터로 변환 - 개선된 버전"""
    parsed_data = {}
    
    # 슬라이드 구분 패턴
    slide_patterns = [
        r'\[슬라이드\s*(\d+)\]',
        r'\[Slide\s*(\d+)\]', 
        r'Slide\s*(\d+):',
        r'#\s*Slide\s*(\d+)',
        r'---\s*\n\s*\[슬라이드\s*(\d+)\]',
        r'슬라이드\s*(\d+)\.',
        r'Slide\s+(\d+)\s*-',
        r'###\s*슬라이드\s*(\d+)',
        r'##\s*\[슬라이드\s*(\d+)\]',
    ]
    
    # 전체 텍스트에서 슬라이드 찾기
    all_matches = []
    for pattern in slide_patterns:
        matches = list(re.finditer(pattern, executor_response, re.IGNORECASE | re.MULTILINE))
        for m in matches:
            all_matches.append((m.start(), m))
    
    # 위치 순으로 정렬
    all_matches.sort(key=lambda x: x[0])
    slide_matches = [m[1] for m in all_matches]
    
    # 슬라이드별로 내용 파싱
    for i, match in enumerate(slide_matches):
        try:
            start_pos = match.start()
            end_pos = slide_matches[i + 1].start() if i + 1 < len(slide_matches) else len(executor_response)
            
            slide_content = executor_response[start_pos:end_pos]
            
            # 슬라이드 번호 추출
            slide_num = None
            for group in match.groups():
                if group:
                    slide_num = int(group)
                    break
            
            if slide_num is None:
                continue
            
            # 제목 추출
            title = ""
            title_patterns = [
                r'제목:\s*(.+?)(?=\n|$)', 
                r'Title:\s*(.+?)(?=\n|$)', 
                r'제목\s*:\s*(.+?)(?=\n|$)',
            ]
            for pattern in title_patterns:
                title_match = re.search(pattern, slide_content, re.MULTILINE)
                if title_match:
                    title = title_match.group(1).strip()
                    if title:
                        break
            
            # 부제목 추출
            subtitle = ""
            subtitle_patterns = [
                r'부제목:\s*(.+?)(?=\n|$)', 
                r'Subtitle:\s*(.+?)(?=\n|$)', 
                r'부제\s*:\s*(.+?)(?=\n|$)',
            ]
            for pattern in subtitle_patterns:
                subtitle_match = re.search(pattern, slide_content, re.MULTILINE)
                if subtitle_match:
                    subtitle = subtitle_match.group(1).strip()
                    if subtitle:
                        break
            
            # 불릿 포인트 추출 (이모지가 있는 라인들)
            bullet_points = []
            lines = slide_content.strip().split('\n')
            
            for line in lines:
                line = line.strip()
                if not line:
                    continue
                
                # 이모지로 시작하거나 • 로 시작하는 라인 찾기
                if line.startswith('•') or (len(line) > 2 and ord(line[0]) >= 0x1F300):
                    bullet_points.append(line)
                
            # 발표자 노트 추출 - 개선된 패턴
            speaker_notes = ""
            notes_patterns = [
                r'발표자 노트:\s*(.+?)(?=\n\n|\n발표자 노트:|\n\[슬라이드|\Z)',
                r'Speaker Notes:\s*(.+?)(?=\n\n|\nSpeaker Notes:|\n\[Slide|\Z)',
                r'발표자노트:\s*(.+?)(?=\n\n|\n발표자노트:|\n\[슬라이드|\Z)',
                r'Notes:\s*(.+?)(?=\n\n|\nNotes:|\n\[Slide|\Z)',
            ]
            
            for pattern in notes_patterns:
                notes_match = re.search(pattern, slide_content, re.DOTALL | re.MULTILINE)
                if notes_match:
                    speaker_notes = notes_match.group(1).strip()
                    # 여러 줄의 노트를 합치기
                    speaker_notes = ' '.join(speaker_notes.split('\n'))
                    if speaker_notes and len(speaker_notes) > 20:  # 최소 20자 이상의 의미있는 노트
                        break
            
            # 발표자 노트가 너무 짧으면 더 찾기
            if len(speaker_notes) < 50:
                # "발표자 노트:" 다음의 모든 텍스트 찾기
                notes_start = slide_content.find("발표자 노트:")
                if notes_start == -1:
                    notes_start = slide_content.find("Speaker Notes:")
                if notes_start == -1:
                    notes_start = slide_content.find("발표자노트:")
                
                if notes_start > -1:
                    notes_text = slide_content[notes_start:].split(":", 1)
                    if len(notes_text) > 1:
                        speaker_notes = notes_text[1].strip()
                        # 다음 섹션 시작 전까지의 텍스트 가져오기
                        next_section_markers = ["시각 자료:", "Visual:", "제목:", "Title:", "[슬라이드", "[Slide"]
                        for marker in next_section_markers:
                            marker_pos = speaker_notes.find(marker)
                            if marker_pos > 0:
                                speaker_notes = speaker_notes[:marker_pos].strip()
                                break
                        speaker_notes = ' '.join(speaker_notes.split('\n')).strip()
            
            parsed_data[slide_num - 1] = {
                "title": title,
                "subtitle": subtitle,
                "bullet_points": bullet_points,
                "speaker_notes": speaker_notes
            }
            
        except Exception as e:
            logger.error(f"[Parser] 슬라이드 파싱 중 오류: {str(e)}")
    
    return parsed_data

def extract_relevant_content_from_executor(executor_response: str, keywords: List[str], slide_num: int, slide_title: str) -> Dict:
    """실행자 응답에서 특정 슬라이드와 관련된 내용 추출"""
    try:
        # 여러 방법으로 관련 내용 찾기
        
        # 1. 슬라이드 번호로 찾기
        slide_patterns = [
            f"\\[슬라이드\\s*{slide_num}\\]([\\s\\S]*?)(?=\\[슬라이드|$)",
            f"\\[Slide\\s*{slide_num}\\]([\\s\\S]*?)(?=\\[Slide|$)",
            f"Slide\\s*{slide_num}:([\\s\\S]*?)(?=Slide\\s*\\d+:|$)"
        ]
        
        for pattern in slide_patterns:
            match = re.search(pattern, executor_response, re.IGNORECASE | re.MULTILINE)
            if match:
                section = match.group(1)
                parsed = parse_slide_section(section, slide_title)
                if parsed and len(parsed.get("bullet_points", [])) > 0:
                    return parsed
        
        # 2. 키워드로 찾기
        for keyword in keywords:
            if not keyword:
                continue
            
            # 키워드 주변에서 불릿 포인트 찾기
            keyword_pos = executor_response.lower().find(keyword.lower())
            if keyword_pos >= 0:
                # 키워드 전후 1000자 범위에서 찾기
                start = max(0, keyword_pos - 300)
                end = min(len(executor_response), keyword_pos + 1000)
                context = executor_response[start:end]
                
                # 불릿 포인트 추출
                bullet_points = []
                lines = context.split('\n')
                
                for line in lines:
                    line = line.strip()
                    if not line:
                        continue
                    
                    # 불릿 포인트 패턴
                    if (line.startswith(('•', '-', '*', '·')) or 
                        re.match(r'^\d+\.', line) or
                        (len(line) > 10 and not ':' in line and not line.startswith(('[', '제목', 'Title', '부제', 'Subtitle')))):
                        
                        clean_line = re.sub(r'^[•\-\*·\d+\.]', '', line).strip()
                        if clean_line and len(clean_line) > 5 and not any(skip in clean_line for skip in ["Point", "포인트"]):
                            bullet_points.append(f"• {clean_line}")
                
                if len(bullet_points) >= 3:
                    return {
                        "subtitle": keyword,
                        "bullet_points": bullet_points[:5],
                        "speaker_notes": f"{slide_title}에 대한 내용입니다."
                    }
        
    except Exception as e:
        logger.error(f"실행자 응답에서 콘텐츠 추출 중 오류: {str(e)}")
    
    return None

def extract_slide_section_from_executor(executor_response: str, slide_num: int) -> str:
    """실행자 응답에서 특정 슬라이드 섹션 추출"""
    try:
        # 다양한 슬라이드 구분 패턴
        patterns = [
            f"\\[슬라이드\\s*{slide_num}\\]([\\s\\S]*?)(?=\\[슬라이드\\s*{slide_num+1}\\]|$)",
            f"\\[Slide\\s*{slide_num}\\]([\\s\\S]*?)(?=\\[Slide\\s*{slide_num+1}\\]|$)",
            f"Slide\\s*{slide_num}:([\\s\\S]*?)(?=Slide\\s*{slide_num+1}:|$)",
            f"슬라이드\\s*{slide_num}\\.([\\s\\S]*?)(?=슬라이드\\s*{slide_num+1}\\.|$)"
        ]
        
        for pattern in patterns:
            match = re.search(pattern, executor_response, re.IGNORECASE | re.MULTILINE)
            if match:
                return match.group(0)  # 전체 매치 반환 (슬라이드 번호 포함)
        
    except Exception as e:
        logger.error(f"슬라이드 섹션 추출 중 오류: {str(e)}")
    
    return ""

def parse_slide_section(section: str, default_title: str) -> Dict:
    """슬라이드 섹션에서 콘텐츠 파싱 - 더 유연하게"""
    try:
        content = {
            "subtitle": default_title,
            "bullet_points": []
        }
        
        lines = section.split('\n')
        
        for line in lines:
            line = line.strip()
            if not line:
                continue
            
            # 부제목 찾기 (더 유연한 패턴)
            if any(marker in line.lower() for marker in ['부제목:', 'subtitle:', '부제:', '제목:']):
                if ':' in line:
                    subtitle = line.split(':', 1)[1].strip()
                    if subtitle:
                        content["subtitle"] = subtitle
            
            # 불릿 포인트 수집 (더 포괄적으로)
            elif (
                # 이모지로 시작
                (len(line) > 0 and ord(line[0]) >= 0x1F300) or
                # 불릿 기호로 시작
                line.startswith(('•', '-', '*', '·', '▪', '▸')) or
                # 숫자로 시작
                re.match(r'^\d+[.)]\s', line) or
                # "핵심 내용:" 이후의 모든 줄
                ('핵심' in section and line and len(line) > 10 and 
                 not any(skip in line for skip in [':', '제목', 'Title', '노트', 'Notes']))
            ):
                # 불필요한 기호 제거
                clean_line = re.sub(r'^[•\-\*·▪▸\d+.)]\s*', '', line).strip()
                
                # 의미 있는 내용인지 확인
                if clean_line and len(clean_line) > 5:
                    # 이모지가 있으면 그대로, 없으면 • 추가
                    if len(line) > 0 and ord(line[0]) >= 0x1F300:
                        content["bullet_points"].append(line)
                    else:
                        content["bullet_points"].append(f"• {clean_line}")
        
        return content if len(content["bullet_points"]) > 0 else None
        
    except Exception as e:
        logger.error(f"슬라이드 섹션 파싱 중 오류: {str(e)}")
        return None

def parse_slide_section_improved(section: str, default_title: str, slide_num: int) -> Dict:
    """슬라이드 섹션에서 콘텐츠 파싱 - 슬라이드 3 문제 해결"""
    try:
        content = {
            "subtitle": default_title,
            "bullet_points": []
        }
        
        # 부제목 추출 (더 많은 패턴)
        subtitle_patterns = [
            r'부제목:\s*(.+?)(?=\n|$)',
            r'Subtitle:\s*(.+?)(?=\n|$)',
            r'부제:\s*(.+?)(?=\n|$)',
            r'Sub:\s*(.+?)(?=\n|$)'
        ]
        
        for pattern in subtitle_patterns:
            match = re.search(pattern, section, re.MULTILINE)
            if match:
                subtitle = match.group(1).strip()
                if subtitle and len(subtitle) > 2:
                    content["subtitle"] = subtitle
                    break
        
        # 불릿 포인트 추출 - 더 정확한 패턴
        # 1. 명시적인 "핵심 내용:" 섹션 찾기
        content_section_match = re.search(
            r'(?:핵심\s*내용|Key\s*Points|내용):\s*\n(.+?)(?=발표자|Speaker|시각|Visual|$)',
            section, 
            re.DOTALL | re.IGNORECASE
        )
        
        if content_section_match:
            content_text = content_section_match.group(1)
            lines = content_text.strip().split('\n')
            
            for line in lines:
                line = line.strip()
                if not line:
                    continue
                
                # 이모지로 시작하는 라인
                if len(line) > 0 and ord(line[0]) >= 0x1F300:
                    content["bullet_points"].append(line)
                # 불릿으로 시작하는 라인
                elif line.startswith(('•', '-', '*', '·', '▪', '▸')):
                    content["bullet_points"].append(line)
                # 의미있는 내용인 경우
                elif len(line) > 10 and not any(skip in line for skip in [':', '제목', 'Title', '노트']):
                    # 숫자로 시작하면 제거
                    clean_line = re.sub(r'^\d+[.)]\s*', '', line).strip()
                    if clean_line:
                        content["bullet_points"].append(f"• {clean_line}")
        
        # 2. 불릿 포인트가 부족하면 전체 섹션에서 찾기
        if len(content["bullet_points"]) < 3:
            lines = section.split('\n')
            for line in lines:
                line = line.strip()
                if not line or len(content["bullet_points"]) >= 5:
                    continue
                
                # 메타 정보 스킵
                if any(skip in line.lower() for skip in ['제목:', 'title:', '부제목:', 'subtitle:', '발표자', 'speaker', '시각']):
                    continue
                
                # 이모지로 시작하거나 불릿으로 시작
                if (len(line) > 0 and ord(line[0]) >= 0x1F300) or line.startswith(('•', '-', '*')):
                    if line not in content["bullet_points"]:
                        content["bullet_points"].append(line)
                # 일반 텍스트지만 의미있는 내용
                elif len(line) > 15 and ':' not in line:
                    formatted_line = f"• {line}"
                    if formatted_line not in content["bullet_points"]:
                        content["bullet_points"].append(formatted_line)
        
        # 중복 제거
        content["bullet_points"] = list(dict.fromkeys(content["bullet_points"]))[:5]
        
        return content if len(content["bullet_points"]) > 0 else None
        
    except Exception as e:
        logger.error(f"슬라이드 {slide_num} 파싱 중 오류: {str(e)}")
        return None



    
def extract_speaker_notes_from_section(section: str) -> str:
    """슬라이드 섹션에서 발표자 노트 추출"""
    try:
        patterns = [
            r'발표자 노트:\s*(.+?)(?=시각 자료:|Visual:|\n제목:|\nTitle:|\n\[슬라이드|\n\[Slide|$)',
            r'Speaker Notes:\s*(.+?)(?=Visual:|시각 자료:|\nTitle:|\n제목:|\n\[Slide|\n\[슬라이드|$)',
            r'Notes:\s*(.+?)(?=Visual:|\nTitle:|$)',
            r'발표자노트:\s*(.+?)(?=시각 자료:|Visual:|$)'
        ]
        
        for pattern in patterns:
            match = re.search(pattern, section, re.DOTALL | re.MULTILINE)
            if match:
                notes = match.group(1).strip()
                # 여러 줄의 노트를 하나로 합치기
                notes = ' '.join(notes.split('\n')).strip()
                if notes and len(notes) > 20:  # 최소 20자 이상의 의미있는 노트
                    return notes
        
        # 패턴 매칭 실패시 더 넓은 범위로 찾기
        notes_markers = ["발표자 노트:", "Speaker Notes:", "발표자노트:", "Notes:"]
        for marker in notes_markers:
            marker_pos = section.find(marker)
            if marker_pos > -1:
                notes_text = section[marker_pos + len(marker):].strip()
                # 다음 섹션 시작 전까지의 텍스트 가져오기
                end_markers = ["시각 자료:", "Visual:", "제목:", "Title:", "[슬라이드", "[Slide", "\n\n"]
                min_end_pos = len(notes_text)
                for end_marker in end_markers:
                    end_pos = notes_text.find(end_marker)
                    if end_pos > 0 and end_pos < min_end_pos:
                        min_end_pos = end_pos
                
                notes_text = notes_text[:min_end_pos].strip()
                notes_text = ' '.join(notes_text.split('\n')).strip()
                
                if notes_text and len(notes_text) > 20:
                    return notes_text
        
    except Exception as e:
        logger.error(f"발표자 노트 추출 중 오류: {str(e)}")
    
    return ""

def extract_relevant_content(executor_response: str, keywords: List[str], slide_num: int) -> Dict:
    """실행자 응답에서 특정 슬라이드와 관련된 내용 추출"""
    try:
        # 키워드 기반으로 관련 섹션 찾기
        for keyword in keywords:
            if not keyword:
                continue
                
            # 키워드 주변 텍스트 찾기
            keyword_lower = keyword.lower()
            response_lower = executor_response.lower()
            
            if keyword_lower in response_lower:
                # 키워드 위치 찾기
                pos = response_lower.find(keyword_lower)
                
                # 주변 텍스트 추출 (앞뒤 500자)
                start = max(0, pos - 200)
                end = min(len(executor_response), pos + 800)
                context = executor_response[start:end]
                
                # 불릿 포인트 추출
                bullet_points = []
                lines = context.split('\n')
                for line in lines:
                    line = line.strip()
                    if line.startswith(('•', '-', '*')) or re.match(r'^\d+\.', line):
                        clean_line = re.sub(r'^[•\-\*\d+\.]', '', line).strip()
                        if clean_line and len(clean_line) > 5:
                            bullet_points.append(f"• {clean_line}")
                
                if bullet_points:
                    return {
                        "subtitle": keyword,
                        "bullet_points": bullet_points[:5],  # 최대 5개
                        "speaker_notes": f"슬라이드 {slide_num}에 대한 내용입니다."
                    }
        
    except Exception as e:
        logger.error(f"관련 콘텐츠 추출 중 오류: {str(e)}")
    
    return None

def read_uploaded_file(file_path: str) -> str:
    """Read uploaded file (PDF, CSV, TXT)"""
    print(f"[File Reading] {file_path}")
    
    try:
        ext = file_path.lower().rsplit('.', 1)[-1] if '.' in file_path else ''
        
        if ext == 'pdf':
            with open(file_path, 'rb') as file:
                pdf_reader = PyPDF2.PdfReader(file)
                text = ""
                for page in pdf_reader.pages:
                    text += page.extract_text() + "\n"
                return text[:5000]
                
        elif ext == 'csv':
            with open(file_path, 'rb') as file:
                raw_data = file.read()
                result = chardet.detect(raw_data)
                encoding = result['encoding'] or 'utf-8'
            
            df = pd.read_csv(file_path, encoding=encoding)
            return f"CSV Data:\n{df.head(20).to_string()}\n\nSummary: {len(df)} rows, {len(df.columns)} columns"
            
        elif ext in ['txt', 'text']:
            with open(file_path, 'rb') as file:
                raw_data = file.read()
                result = chardet.detect(raw_data)
                encoding = result['encoding'] or 'utf-8'
            
            with open(file_path, 'r', encoding=encoding) as file:
                return file.read()[:5000]
        else:
            return "Unsupported file format."
            
    except Exception as e:
        return f"File reading error: {str(e)}"

def generate_dynamic_slides(topic: str, template: Dict, slide_count: int) -> List[Dict]:
    """Dynamically generate slides based on selected count"""
    core_slides = template.get("core_slides", [])
    optional_slides = template.get("optional_slides", [])
    
    # Content slide count
    content_slide_count = slide_count
    
    # Adjust if core slides exceed requested count
    if len(core_slides) > content_slide_count:
        selected_slides = core_slides[:content_slide_count]
    else:
        # Core slides + optional slides
        selected_slides = core_slides.copy()
        remaining = content_slide_count - len(core_slides)
        
        if remaining > 0 and optional_slides:
            # Add from optional slides
            additional = optional_slides[:remaining]
            selected_slides.extend(additional)
    
    # Add cover slide (first)
    slides = [{"title": "Cover", "style": "Title Slide (Hero)", "prompt_hint": "Presentation cover"}]
    
    # Add content slides
    slides.extend(selected_slides)
    
    # Add Thank You slide (last)
    slides.append({"title": "Thank You", "style": "Thank You Slide", "prompt_hint": "Presentation closing and key message"})
    
    return slides

def brave_search(query: str, api_token: str = None) -> List[Dict]:
    """Web search using Brave Search API"""
    if not api_token:
        print("[Brave Search] No API token, skipping search.")
        return []
    
    print(f"[Brave Search] Query: {query}")
    
    headers = {
        "Accept": "application/json",
        "X-Subscription-Token": api_token
    }
    
    params = {
        "q": query,
        "count": 5
    }
    
    try:
        response = requests.get(
            "https://api.search.brave.com/res/v1/web/search",
            headers=headers,
            params=params,
            timeout=10
        )
        
        if response.status_code == 200:
            data = response.json()
            results = []
            for item in data.get("web", {}).get("results", [])[:3]:
                results.append({
                    "title": item.get("title", ""),
                    "description": item.get("description", ""),
                    "url": item.get("url", "")
                })
            print(f"[Brave Search] Got {len(results)} results")
            return results
        else:
            print(f"[Brave Search] Error: {response.status_code}")
            return []
    except Exception as e:
        print(f"[Brave Search] Exception: {str(e)}")
        return []

def generate_slide_content(topic: str, slide_title: str, slide_context: str, audience_type: str, 
                          language: str = "English", uploaded_content: str = None, 
                          web_search_results: List[Dict] = None, friendli_token: str = None,
                          api_url: str = None, model_id: str = None,
                          audience_types: Dict = None) -> Dict[str, str]:
    """Generate text content for each slide"""
    
    audience_info = audience_types.get(audience_type, {}) if audience_types else {}
    
    system_prompt = f"""당신은 PPT 슬라이드 콘텐츠 작성 전문가입니다.

청중: {audience_type}
언어: {language}

작성 규칙:
1. 부제목: 최대 10단어
2. 각 불릿 포인트는 반드시 관련 이모지로 시작
3. 이모지 다음에 바로 내용 (• 기호 제외)
4. 각 포인트는 8-12단어로 간결하게
5. 명사형 종결 사용

출력 형식:
Subtitle: [부제목]
🎯 [첫 번째 포인트 내용]
💡 [두 번째 포인트 내용]
🚀 [세 번째 포인트 내용]
✅ [네 번째 포인트 내용]
📊 [다섯 번째 포인트 내용]

청중별 이모지 가이드:
- 경영진: 📊 🎯 💰 🏆 🚀 📈 🔝 💡
- 투자자: 💰 📈 💎 🏦 💸 📊 🚀 🔒
- 기술팀: 🔧 💻 🛠️ ⚙️ 🔐 🌐 📱 🤖
- 일반직원: 🤝 💡 📋 ✅ 🎯 🌟 📅 💪
- 고객: ⭐ 🎁 💝 🛡️ 🌟 ✨ 🏅 👍
- 일반대중: 😊 🏠 🌍 ❤️ 🎉 🌈 ✨ 🎯"""
    
    user_message = f"""Topic: {topic}
Slide Title: {slide_title}
Context: {slide_context}
Target Audience: {audience_type}
Language: {language}"""
    
    if uploaded_content:
        user_message += f"\n\nReference Material:\n{uploaded_content[:1000]}"
    
    if web_search_results:
        search_context = "\n\nWeb Search Results:\n"
        try:
            if isinstance(web_search_results, list) and len(web_search_results) > 0:
                for i, result in enumerate(web_search_results[:3]):
                    if isinstance(result, dict):
                        search_context += f"- {result.get('title', 'N/A')}: {result.get('description', 'N/A')}\n"
            user_message += search_context
        except Exception as e:
            print(f"[Slide Content] Error processing search results: {str(e)}")
    
    user_message += f"\n\nCreate compelling content for this presentation slide specifically tailored for {audience_type} in {language}. Remember to write COMPLETE bullet points, NOT placeholders like 'Point 1'!"
    
    headers = {
        "Authorization": f"Bearer {friendli_token}",
        "Content-Type": "application/json"
    }
    
    payload = {
        "model": model_id,
        "messages": [
            {
                "role": "system",
                "content": system_prompt
            },
            {
                "role": "user",
                "content": user_message
            }
        ],
        "max_tokens": 400,
        "top_p": 0.8,
        "temperature": 0.7,
        "stream": False
    }
    
    try:
        response = requests.post(api_url, json=payload, headers=headers, timeout=30)
        if response.status_code == 200:
            result = response.json()
            content = result['choices'][0]['message']['content'].strip()
            
            print(f"[Slide Content] LLM Response:\n{content}")
            
            # 개선된 파싱 로직
            lines = content.split('\n')
            subtitle = ""
            bullet_points = []
            
            for line in lines:
                line = line.strip()
                if not line:
                    continue
                    
                # Subtitle 파싱
                if line.lower().startswith("subtitle:") or line.startswith("Subtitle:") or line.startswith("부제목:"):
                    subtitle = line.split(':', 1)[1].strip()
                # Bullet point 파싱
                elif line.startswith("•") or line.startswith("-") or (len(line) > 2 and line[1] in [' ', '\t'] and ord(line[0]) >= 128):
                    # 이미 • 로 시작하지 않으면 추가
                    if not line.startswith("•"):
                        line = "• " + line.lstrip("- ")
                    
                    # "Point X" 패턴 체크 및 거부
                    if not any(placeholder in line for placeholder in ["Point 1", "Point 2", "Point 3", "Point 4", "Point 5", "📌 Point"]):
                        bullet_points.append(line)
            
            # Subtitle이 없으면 기본값
            if not subtitle:
                subtitle = f"{slide_title} Overview"
            
            # 불릿 포인트가 부족하면 재시도 또는 기본값
            if len(bullet_points) < 5:
                print(f"[Slide Content] Warning: Only {len(bullet_points)} bullet points found. Retrying...")
                
                # 재시도를 위한 더 명확한 프롬프트
                retry_message = f"""The previous response didn't include 5 complete bullet points. 
Please provide EXACTLY 5 bullet points for the slide titled "{slide_title}" about "{topic}".

Each bullet must be:
- A complete, meaningful statement (NOT "Point 1", "Point 2", etc.)
- 8-12 words long
- Starting with an emoji
- Relevant to {audience_type}
- In {language}

Example format:
- 📊 Increased efficiency through automated processes
- 💰 30% cost reduction in operations
- 🚀 Faster response times for customers
- 🔧 Seamless integration with existing systems
- 📈 Measurable ROI within 6 months"""
                
                retry_payload = {
                    "model": model_id,
                    "messages": [
                        {"role": "system", "content": system_prompt},
                        {"role": "user", "content": retry_message}
                    ],
                    "max_tokens": 300,
                    "temperature": 0.8,
                    "stream": False
                }
                
                try:
                    retry_response = requests.post(api_url, json=retry_payload, headers=headers, timeout=30)
                    if retry_response.status_code == 200:
                        retry_result = retry_response.json()
                        retry_content = retry_result['choices'][0]['message']['content'].strip()
                        
                        # 재시도 파싱
                        retry_lines = retry_content.split('\n')
                        bullet_points = []
                        for line in retry_lines:
                            line = line.strip()
                            if line and (line.startswith("•") or line.startswith("-") or (len(line) > 2 and ord(line[0]) >= 128)):
                                if not line.startswith("•"):
                                    line = "• " + line.lstrip("- ")
                                if not any(placeholder in line for placeholder in ["Point 1", "Point 2", "Point 3", "Point 4", "Point 5"]):
                                    bullet_points.append(line)
                except:
                    pass
            
            # 여전히 부족하면 의미있는 기본값 생성
            default_bullets = {
                "English": [
                    "• 📊 Data-driven insights for better decisions",
                    "• 💡 Innovative solutions to complex challenges",
                    "• 🚀 Accelerated growth through optimization",
                    "• 🎯 Targeted strategies for maximum impact",
                    "• ✅ Proven results with measurable outcomes"
                ],
                "Korean": [
                    "• 📊 데이터 기반 의사결정 지원",
                    "• 💡 복잡한 문제에 대한 혁신적 해결책",
                    "• 🚀 최적화를 통한 성장 가속화",
                    "• 🎯 최대 효과를 위한 타겟 전략",
                    "• ✅ 측정 가능한 검증된 결과"
                ]
            }
            
            while len(bullet_points) < 5:
                default_idx = len(bullet_points)
                if default_idx < len(default_bullets.get(language, default_bullets["English"])):
                    bullet_points.append(default_bullets[language][default_idx])
                else:
                    bullet_points.append(f"• ⚡ Key insight {len(bullet_points) + 1}")
            
            bullet_points = bullet_points[:5]
            
            print(f"[Slide Content] Final subtitle: {subtitle}")
            print(f"[Slide Content] Final bullets: {bullet_points}")
            
            return {
                "subtitle": subtitle,
                "bullet_points": bullet_points
            }
        else:
            print(f"[Slide Content] API Error: {response.status_code}")
            return {
                "subtitle": slide_title,
                "bullet_points": [
                    "• 📊 Strategic insights and analysis",
                    "• 💡 Innovative approaches and solutions",
                    "• 🚀 Growth opportunities identified",
                    "• 🎯 Targeted implementation strategies",
                    "• ✅ Measurable success metrics"
                ]
            }
    except Exception as e:
        print(f"[Slide Content] Error: {str(e)}")
        return {
            "subtitle": slide_title,
            "bullet_points": [
                "• 📊 Key strategic insights",
                "• 💡 Innovative solutions proposed",
                "• 🚀 Growth acceleration opportunities",
                "• 🎯 Targeted action plans",
                "• ✅ Success measurement framework"
            ]
        }

def generate_presentation_notes(topic: str, slide_title: str, content: Dict, audience_type: str, 
                               language: str = "English", friendli_token: str = None,
                               api_url: str = None, model_id: str = None,
                               audience_types: Dict = None) -> str:
    """Generate speaker notes for each slide"""
    print(f"[Speaker Notes] Generating for {slide_title}...")
    
    audience_info = audience_types.get(audience_type, {}) if audience_types else {}
    
    headers = {
        "Authorization": f"Bearer {friendli_token}",
        "Content-Type": "application/json"
    }
    
    if language == "Korean":
        system_prompt = f"""당신은 자연스러운 한국어 구어체로 발표하는 전문 프레젠터입니다.

청중: {audience_type} - {audience_info.get('description', '')}
언어: 한국어

발표자 노트 작성 규칙:
1. 완전한 구어체로 작성 (마치 실제로 말하는 것처럼)
2. 청중과 소통하는 느낌의 자연스러운 대화체
3. 각 불릿 포인트를 풀어서 설명
4. 전환 문구와 연결어 활용
5. 150-200자로 풍부하게 작성
6. 실제 발표 시나리오처럼 작성

예시:
"자, 이제 우리가 주목해야 할 핵심 포인트들을 하나씩 살펴보겠습니다. 첫 번째로 보실 부분은 바로 시장 규모인데요, 현재 3.4조 달러라는 어마어마한 규모로 성장했습니다. 이게 얼마나 큰 숫자인지 감이 안 오시죠? 우리나라 GDP의 두 배가 넘는 규모입니다. 두 번째 포인트는..."

지시문이나 설명 제외, 오직 발표 내용만 작성하세요."""
    else:
        system_prompt = f"""You are a professional presentation coach creating natural, conversational speaker notes.

Audience: {audience_type} - {audience_info.get('description', '')}
Tone: {audience_info.get('tone', '')}
Language: English

Create speaker notes that:
1. Sound completely natural and conversational (as if actually speaking)
2. Expand on each bullet point with context and examples
3. Use smooth transitions between points
4. Engage the audience with inclusive language ("we", "let's", "as you can see")
5. Be 100-150 words long with rich detail
6. Include rhetorical questions or audience engagement
7. NO stage directions or meta-commentary - only spoken words

Example:
"Now, let's dive into the key points that really matter here. First up, we're looking at a market size of 3.4 trillion dollars. That's trillion with a T! To put that in perspective, that's larger than the GDP of most countries. What's even more exciting is the growth rate - we're seeing 16% year-over-year expansion. Moving to our second point about digital adoption..."

Write ONLY what the speaker would say out loud, no instructions or descriptions."""
    
    bullet_text = "\n".join(content.get("bullet_points", []))
    
    if language == "Korean":
        user_message = f"""주제: {topic}
슬라이드 제목: {slide_title}
부제목: {content.get('subtitle', '')}
내용:
{bullet_text}

위 슬라이드를 {audience_type} 청중에게 발표할 때 사용할 자연스러운 구어체 발표 스크립트를 작성하세요. 
각 포인트를 풀어서 설명하고, 청중과 소통하는 느낌으로 작성해주세요."""
    else:
        user_message = f"""Topic: {topic}
Slide Title: {slide_title}
Subtitle: {content.get('subtitle', '')}
Content:
{bullet_text}

Write natural, conversational speaker notes for presenting this slide to {audience_type}. 
Expand on each point with context and examples, using engaging language that connects with the audience."""
    
    payload = {
        "model": model_id,
        "messages": [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_message}
        ],
        "max_tokens": 400,
        "temperature": 0.8,
        "top_p": 0.9,
        "stream": False
    }
    
    try:
        response = requests.post(api_url, json=payload, headers=headers, timeout=30)
        if response.status_code == 200:
            result = response.json()
            notes = result['choices'][0]['message']['content'].strip()
            
            # 노트가 너무 짧으면 재시도
            if len(notes) < 100:
                print(f"[Speaker Notes] Notes too short ({len(notes)} chars), retrying...")
                retry_message = user_message + f"\n\n이전 응답이 너무 짧았습니다. 최소 150자 이상으로 각 포인트를 자세히 설명해주세요." if language == "Korean" else "\n\nThe previous response was too short. Please provide at least 100 words, expanding on each point with specific examples and context."
                
                retry_payload = {
                    "model": model_id,
                    "messages": [
                        {"role": "system", "content": system_prompt},
                        {"role": "user", "content": retry_message}
                    ],
                    "max_tokens": 500,
                    "temperature": 0.85,
                    "stream": False
                }
                
                retry_response = requests.post(api_url, json=retry_payload, headers=headers, timeout=30)
                if retry_response.status_code == 200:
                    retry_result = retry_response.json()
                    notes = retry_result['choices'][0]['message']['content'].strip()
            
            return notes
        else:
            print(f"[Speaker Notes] API Error: {response.status_code}")
            if language == "Korean":
                return f"자, 이제 {slide_title}에 대해 살펴보겠습니다. 여기서 우리가 주목해야 할 핵심 포인트들이 있는데요, 첫 번째는 {content.get('subtitle', '')}입니다. 이 부분이 왜 중요한지 하나씩 짚어보면서 설명드리겠습니다. 특히 여러분의 비즈니스에 어떤 영향을 미칠 수 있는지 함께 생각해보시면 좋을 것 같습니다."
            else:
                return f"Now let's explore {slide_title}. What I want to highlight here are the key points about {content.get('subtitle', '')}. Each of these elements plays a crucial role in our overall strategy, and I'll walk you through why they matter for your business. As we go through these points, think about how they might apply to your specific situation."
    except Exception as e:
        print(f"[Speaker Notes] Error: {str(e)}")
        if language == "Korean":
            return f"자, 이제 {slide_title}에 대해 살펴보겠습니다. 여기서 우리가 주목해야 할 핵심 포인트들이 있는데요, 첫 번째는 {content.get('subtitle', '')}입니다. 이 부분이 왜 중요한지 하나씩 짚어보면서 설명드리겠습니다. 특히 여러분의 비즈니스에 어떤 영향을 미칠 수 있는지 함께 생각해보시면 좋을 것 같습니다."
        else:
            return f"Now let's explore {slide_title}. What I want to highlight here are the key points about {content.get('subtitle', '')}. Each of these elements plays a crucial role in our overall strategy, and I'll walk you through why they matter for your business. As we go through these points, think about how they might apply to your specific situation."

def generate_closing_notes(topic: str, conclusion_phrase: str, audience_type: str, 
                          language: str = "English", friendli_token: str = None,
                          api_url: str = None, model_id: str = None,
                          audience_types: Dict = None) -> str:
    """Generate speaker notes for the closing slide"""
    print(f"[Closing Notes] Generating...")
    
    audience_info = audience_types.get(audience_type, {}) if audience_types else {}
    
    headers = {
        "Authorization": f"Bearer {friendli_token}",
        "Content-Type": "application/json"
    }
    
    if language == "Korean":
        system_prompt = f"""당신은 프레젠테이션을 마무리하는 전문 발표자입니다.

청중: {audience_type} - {audience_info.get('description', '')}
톤: 따뜻하고 전문적이며 {audience_type}에게 적합한 어조
언어: 한국어

마무리 발표 노트 작성 규칙:
1. 발표 내용 핵심 요약 (2-3문장)
2. 결론 문구를 자연스럽게 활용
3. 청중에게 감사 인사
4. 질문 유도 또는 다음 단계 제안
5. 150-200자로 풍부하게 작성
6. 완전한 구어체로 작성
7. 지시문 제외, 오직 발표 내용만

예시:
"오늘 우리가 함께 살펴본 디지털 트랜스포메이션의 핵심은 바로 '기술이 아닌 사람'이라는 점입니다. 시장 규모, 성공 사례, 그리고 실행 전략까지 다양한 관점에서 접근해 보았는데요. 제가 드리고 싶은 메시지는 명확합니다. '함께 만드는 미래' - 이것이 우리가 추구해야 할 방향입니다. 긴 시간 경청해 주셔서 감사합니다. 혹시 궁금하신 점이나 더 논의하고 싶은 부분이 있으시면 편하게 말씀해 주세요."
"""
    else:
        system_prompt = f"""You are a professional presenter closing a presentation.

Audience: {audience_type} - {audience_info.get('description', '')}
Tone: Warm, professional, and appropriate for {audience_type}
Language: English

Create closing remarks that:
1. Briefly summarize the key takeaways (2-3 sentences)
2. Reference the conclusion phrase naturally
3. Thank the audience warmly
4. Invite questions or suggest next steps
5. Be 100-150 words with rich detail
6. Sound completely conversational
7. NO stage directions - only spoken words

Example:
"So, what we've explored today is truly transformative - from the 3.4 trillion dollar market opportunity to the real-world success stories of Amazon and Starbucks. The key takeaway? Digital transformation isn't about technology, it's about people and culture. That's why our theme 'Building Tomorrow Together' resonates so strongly. Your engagement and questions throughout have been fantastic, and I hope you're as excited about these possibilities as I am. Thank you for your time and attention today. I'd love to hear your thoughts - what aspects resonated most with you? Are there specific areas you'd like to explore further?"

Write ONLY what the speaker would say."""
    
    if language == "Korean":
        user_message = f"""주제: {topic}
청중: {audience_type}
화면의 결론 문구: {conclusion_phrase}

위 정보를 바탕으로 {audience_type} 청중을 위한 자연스럽고 따뜻한 마무리 발표 노트를 작성하세요. 
핵심 내용을 요약하고, 결론 문구를 활용하여 강력한 마무리를 만들어주세요."""
    else:
        user_message = f"""Topic: {topic}
Audience: {audience_type}
Conclusion phrase on screen: {conclusion_phrase}

Create natural, warm closing speaker notes for {audience_type} that wrap up the presentation effectively. 
Summarize key points and use the conclusion phrase to create a powerful ending."""
    
    payload = {
        "model": model_id,
        "messages": [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_message}
        ],
        "max_tokens": 400,
        "temperature": 0.8,
        "stream": False
    }
    
    try:
        response = requests.post(api_url, json=payload, headers=headers, timeout=30)
        if response.status_code == 200:
            result = response.json()
            notes = result['choices'][0]['message']['content'].strip()
            
            # 노트가 너무 짧으면 재시도
            if len(notes) < 100:
                print(f"[Closing Notes] Notes too short ({len(notes)} chars), retrying...")
                retry_message = user_message + f"\n\n이전 응답이 너무 짧았습니다. 최소 150자 이상으로 풍부하게 작성해주세요." if language == "Korean" else "\n\nThe previous response was too short. Please provide at least 100 words with more detail."
                
                retry_payload = {
                    "model": model_id,
                    "messages": [
                        {"role": "system", "content": system_prompt},
                        {"role": "user", "content": retry_message}
                    ],
                    "max_tokens": 500,
                    "temperature": 0.85,
                    "stream": False
                }
                
                retry_response = requests.post(api_url, json=retry_payload, headers=headers, timeout=30)
                if retry_response.status_code == 200:
                    retry_result = retry_response.json()
                    notes = retry_result['choices'][0]['message']['content'].strip()
            
            return notes
        else:
            print(f"[Closing Notes] API Error: {response.status_code}")
            if language == "Korean":
                return f"오늘 우리가 함께 살펴본 {topic}의 핵심은 바로 변화와 혁신입니다. 시장의 기회부터 실행 전략까지 다양한 관점에서 접근해 보았는데요, 제가 드리고 싶은 메시지는 '{conclusion_phrase}'입니다. 이것이 우리가 함께 추구해야 할 방향이라고 생각합니다. 오늘 이 자리에 함께해 주신 모든 분들께 감사드립니다. 혹시 궁금하신 점이나 더 깊이 논의하고 싶은 부분이 있으시면 편하게 질문해 주세요."
            else:
                return f"What we've explored today about {topic} represents a significant opportunity for transformation. From market insights to execution strategies, we've covered the essential elements you need to succeed. The message I want to leave you with is simple yet powerful: '{conclusion_phrase}'. This encapsulates everything we've discussed today. Thank you all for your attention and engagement. I'm excited to hear your thoughts and answer any questions you might have. What resonated most with you from today's presentation?"
    except Exception as e:
        print(f"[Closing Notes] Error: {str(e)}")
        if language == "Korean":
            return f"오늘 우리가 함께 살펴본 {topic}의 핵심은 바로 변화와 혁신입니다. 시장의 기회부터 실행 전략까지 다양한 관점에서 접근해 보았는데요, 제가 드리고 싶은 메시지는 '{conclusion_phrase}'입니다. 이것이 우리가 함께 추구해야 할 방향이라고 생각합니다. 오늘 이 자리에 함께해 주신 모든 분들께 감사드립니다. 혹시 궁금하신 점이나 더 깊이 논의하고 싶은 부분이 있으시면 편하게 질문해 주세요."
        else:
            return f"What we've explored today about {topic} represents a significant opportunity for transformation. From market insights to execution strategies, we've covered the essential elements you need to succeed. The message I want to leave you with is simple yet powerful: '{conclusion_phrase}'. This encapsulates everything we've discussed today. Thank you all for your attention and engagement. I'm excited to hear your thoughts and answer any questions you might have. What resonated most with you from today's presentation?"

def generate_conclusion_phrase(topic: str, audience_type: str, language: str = "English",
                              friendli_token: str = None, api_url: str = None, 
                              model_id: str = None, audience_types: Dict = None) -> str:
    """Generate a powerful conclusion phrase"""
    print(f"[Conclusion Phrase] Generating...")
    
    audience_info = audience_types.get(audience_type, {}) if audience_types else {}
    
    headers = {
        "Authorization": f"Bearer {friendli_token}",
        "Content-Type": "application/json"
    }
    
    system_prompt = f"""You are a professional copywriter creating powerful closing statements for presentations.

The audience is: {audience_type} - {audience_info.get('description', '')}
Focus on: {audience_info.get('focus', '')}
Language: {language}

Create a concise, impactful closing phrase that:
1. Captures the essence of the presentation topic
2. Resonates with {audience_type}
3. Is memorable and inspirational
4. Maximum 5-7 words
5. Uses powerful, action-oriented language appropriate for {audience_type}
6. Leaves a lasting impression
7. Written in {language}

Examples for different audiences:
- For executives: "Excellence Through Strategic Innovation" / "전략적 혁신을 통한 탁월함"
- For investors: "Maximum Returns, Minimal Risk" / "최대 수익, 최소 리스크"
- For technical teams: "Code Today, Transform Tomorrow" / "오늘의 코드, 내일의 변화"
- For customers: "Your Success, Our Mission" / "고객의 성공이 우리의 사명"

Output only the phrase in {language}, no explanation."""
    
    user_message = f"""Presentation topic: {topic}
Target audience: {audience_type}
Language: {language}

Create a powerful closing phrase in {language} that encapsulates the main message for {audience_type}."""
    
    payload = {
        "model": model_id,
        "messages": [
            {
                "role": "system",
                "content": system_prompt
            },
            {
                "role": "user",
                "content": user_message
            }
        ],
        "max_tokens": 50,
        "temperature": 0.9,
        "stream": False
    }
    
    try:
        response = requests.post(api_url, json=payload, headers=headers, timeout=30)
        if response.status_code == 200:
            result = response.json()
            phrase = result['choices'][0]['message']['content'].strip()
            return phrase
        else:
            if language == "Korean":
                return "함께 만드는 미래"
            else:
                return "Building Tomorrow Together"
    except Exception as e:
        print(f"[Conclusion Phrase] Error: {str(e)}")
        if language == "Korean":
            return "함께 만드는 미래"
        else:
            return "Building Tomorrow Together"

def translate_to_english(text: str, friendli_token: str = None, 
                        api_url: str = None, model_id: str = None) -> str:
    """Translate Korean text to English"""
    if not any(ord('가') <= ord(char) <= ord('힣') for char in text):
        return text
    
    print(f"[Translation] Korean detected, translating to English")
    
    headers = {
        "Authorization": f"Bearer {friendli_token}",
        "Content-Type": "application/json"
    }
    
    payload = {
        "model": model_id,
        "messages": [
            {
                "role": "system",
                "content": "You are a translator. Translate the given Korean text to English. Only return the translation without any explanation."
            },
            {
                "role": "user",
                "content": text
            }
        ],
        "max_tokens": 500,
        "top_p": 0.8,
        "stream": False
    }
    
    try:
        response = requests.post(api_url, json=payload, headers=headers, timeout=30)
        if response.status_code == 200:
            result = response.json()
            translated = result['choices'][0]['message']['content'].strip()
            print(f"[Translation] Complete")
            return translated
        else:
            print(f"[Translation] Failed, using original")
            return text
    except Exception as e:
        print(f"[Translation] Error: {str(e)}, using original")
        return text

def generate_prompt_with_llm(topic: str, style_example: str = None, slide_context: str = None, 
                            uploaded_content: str = None, friendli_token: str = None,
                            api_url: str = None, model_id: str = None) -> str:
    """Generate image prompt using LLM"""
    print(f"[LLM] Generating prompt: {slide_context}")
    
    headers = {
        "Authorization": f"Bearer {friendli_token}",
        "Content-Type": "application/json"
    }
    
    system_prompt = """You are an expert image prompt engineer specializing in creating prompts for professional presentation slides. 

Your task is to create prompts that:
1. Are highly specific and visual, perfect for PPT backgrounds or main visuals
2. Consider the slide's purpose and maintain consistency across a presentation
3. Include style references matching the given example
4. Focus on clean, professional visuals that won't distract from text overlays
5. Ensure high contrast areas for text readability when needed
6. Maintain brand consistency and professional aesthetics

Important guidelines:
- If given a style example, adapt the topic to match that specific visual style
- Consider the slide context to create appropriate visuals
- Always output ONLY the prompt without any explanation
- Keep prompts between 50-150 words for optimal results
- Ensure the visual supports rather than overwhelms the slide content"""
    
    user_message = f"Topic: {topic}"
    if style_example:
        user_message += f"\n\nStyle reference to follow:\n{style_example}"
    if slide_context:
        user_message += f"\n\nSlide context: {slide_context}"
    if uploaded_content:
        user_message += f"\n\nAdditional context from document:\n{uploaded_content[:500]}"
    
    payload = {
        "model": model_id,
        "messages": [
            {
                "role": "system",
                "content": system_prompt
            },
            {
                "role": "user",
                "content": user_message
            }
        ],
        "max_tokens": 300,
        "top_p": 0.8,
        "temperature": 0.7,
        "stream": False
    }
    
    try:
        response = requests.post(api_url, json=payload, headers=headers, timeout=30)
        if response.status_code == 200:
            result = response.json()
            prompt = result['choices'][0]['message']['content'].strip()
            print(f"[LLM] Prompt generated: {prompt[:50]}...")
            return prompt
        else:
            error_msg = f"Prompt generation failed: {response.status_code}"
            print(f"[LLM] {error_msg}")
            return error_msg
    except Exception as e:
        error_msg = f"Prompt generation error: {str(e)}"
        print(f"[LLM] {error_msg}")
        return error_msg

def generate_image(prompt: str, seed: int = 10, slide_info: str = "", 
                  replicate_token: str = None, current_topic: str = "",
                  current_language: str = "English", friendli_token: str = None,
                  api_url: str = None, model_id: str = None) -> Tuple[Image.Image, str]:
    """Generate image using Replicate API or process flow diagram"""
    print(f"\n[Image Generation] {slide_info}")
    print(f"[Image Generation] Prompt: {prompt[:50]}...")
    
    # Check if process flow diagram should be generated
    should_generate_process_flow = False
    
    # Extract slide title
    slide_title = ""
    if ":" in slide_info:
        parts = slide_info.split(":")
        if len(parts) >= 2:
            slide_title = parts[1].strip()
    
    print(f"[Image Generation] Extracted slide title: '{slide_title}'")
    
    # Process keywords for both English and Korean
    process_keywords_en = [
        "process", "workflow", "flow", "procedure", "steps", "phases", "overview"
    ]
    process_keywords_kr = [
        "프로세스", "작동", "플로우", "흐름", "워크플로우", 
        "절차", "단계", "처리", "진행", "개요"
    ]
    
    # Check if workflow style
    is_workflow_style = False
    if any(style in prompt for style in ["Business Workflow", "Flowchart", "Business Process"]):
        is_workflow_style = True
        print(f"[Image Generation] Business Workflow or Flowchart style detected")
    
    # Check title for process keywords
    title_has_process = any(keyword in slide_title.lower() for keyword in process_keywords_en) or \
                       any(keyword in slide_title for keyword in process_keywords_kr)
    prompt_has_process = any(keyword in prompt.lower() for keyword in ["process", "flow", "workflow"])
    
    print(f"[Image Generation] Title has process keyword: {title_has_process}")
    print(f"[Image Generation] Workflow style: {is_workflow_style}")
    print(f"[Image Generation] Prompt has process keyword: {prompt_has_process}")
    
    if title_has_process and (is_workflow_style or prompt_has_process):
        should_generate_process_flow = True
        print(f"[Image Generation] ✅ Process flow diagram conditions met!")
    
    # Special case: exclude table of contents
    if "Contents" in slide_title or "목차" in slide_title:
        should_generate_process_flow = False
        print(f"[Image Generation] Table of contents excluded from process flow")
    
    # Generate process flow diagram
    if PROCESS_FLOW_AVAILABLE and should_generate_process_flow:
        try:
            print("[Image Generation] 🔧 Generating process flow diagram...")
            
            # Use current language setting
            use_korean = (current_language == "Korean")
            
            img = generate_process_flow_for_ppt(
                topic=current_topic,
                context=slide_info,
                style="Business Workflow",
                use_korean=use_korean
            )
            
            if isinstance(img, Image.Image):
                print("[Image Generation] ✅ Process flow diagram generated successfully!")
                return img, f"Process flow diagram generated with {current_language} support"
            else:
                print("[Image Generation] ❌ Process flow generation failed, falling back to regular image")
        except Exception as e:
            print(f"[Image Generation] ❌ Process flow generation error: {str(e)}")
            import traceback
            traceback.print_exc()
    else:
        if not PROCESS_FLOW_AVAILABLE:
            print("[Image Generation] ⚠️ Process flow generator not available")
        else:
            print("[Image Generation] ℹ️ Process flow conditions not met, generating regular image")
    
    # Generate regular image
    try:
        english_prompt = translate_to_english(prompt, friendli_token, api_url, model_id)
        
        if not replicate_token:
            error_msg = "RAPI_TOKEN environment variable not set."
            print(f"[Image Generation] Error: {error_msg}")
            return None, error_msg
        
        print(f"[Image Generation] Calling Replicate API...")
        client = replicate.Client(api_token=replicate_token)
        
        input_params = {
            "seed": seed,
            "prompt": english_prompt,
            "speed_mode": "Extra Juiced 🚀 (even more speed)",
            "output_quality": 100
        }
        
        start_time = time.time()
        output = client.run(
            "prunaai/hidream-l1-fast:17c237d753218fed0ed477cb553902b6b75735f48c128537ab829096ef3d3645",
            input=input_params
        )
        
        elapsed = time.time() - start_time
        print(f"[Image Generation] API response received ({elapsed:.1f}s)")
        
        if output:
            if isinstance(output, str) and output.startswith('http'):
                print(f"[Image Generation] Downloading image from URL...")
                response = requests.get(output, timeout=30)
                img = Image.open(BytesIO(response.content))
                print(f"[Image Generation] Complete!")
                return img, english_prompt
            else:
                print(f"[Image Generation] Processing binary data...")
                img = Image.open(BytesIO(output.read()))
                print(f"[Image Generation] Complete!")
                return img, english_prompt
        else:
            error_msg = "Image generation failed - empty response"
            print(f"[Image Generation] {error_msg}")
            return None, error_msg
            
    except Exception as e:
        error_msg = f"Error: {str(e)}"
        print(f"[Image Generation] {error_msg}")
        print(f"[Image Generation] Detailed error:\n{traceback.format_exc()}")
        return None, error_msg

def create_slide_preview_html(slide_data: Dict) -> str:
    """Create 16:9 ratio slide preview HTML"""
    
    # Encode image to base64
    img_base64 = ""
    if slide_data.get("image"):
        buffered = BytesIO()
        slide_data["image"].save(buffered, format="PNG")
        img_base64 = base64.b64encode(buffered.getvalue()).decode()
    
    # Get text content
    subtitle = slide_data.get("subtitle", "")
    bullet_points = slide_data.get("bullet_points", [])
    
    # Generate HTML
    html = f"""
    <div class="slide-container" style="
        width: 100%;
        max-width: 1200px;
        margin: 20px auto;
        background: white;
        border-radius: 12px;
        box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
        overflow: hidden;
    ">
        <div class="slide-header" style="
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            color: white;
            padding: 15px 30px;
            font-size: 18px;
            font-weight: bold;
        ">
            Slide {slide_data.get('slide_number', '')}: {slide_data.get('title', '')}
        </div>
        
        <div class="slide-content" style="
            display: flex;
            height: 0;
            padding-bottom: 56.25%; /* 16:9 ratio */
            position: relative;
            background: #fafbfc;
        ">
            <div class="slide-inner" style="
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                display: flex;
                padding: 20px;
                gap: 20px;
            ">
    """
    
    # Title and Thank You slides use full screen images
    if slide_data.get('title') in ['Cover', 'Thank You', '표지']:
        html += f"""
                <!-- Full screen image -->
                <div style="
                    width: 100%;
                    height: 100%;
                    position: relative;
                    background: #e9ecef;
                    border-radius: 12px;
                    overflow: hidden;
                ">
        """
        
        if img_base64:
            html += f"""
                    <img src="data:image/png;base64,{img_base64}" style="
                        width: 100%;
                        height: 100%;
                        object-fit: cover;
                    " alt="Slide Image">
                    <div style="
                        position: absolute;
                        top: 50%;
                        left: 50%;
                        transform: translate(-50%, -50%);
                        text-align: center;
                        background: rgba(255, 255, 255, 0.9);
                        padding: 30px 60px;
                        border-radius: 20px;
                        box-shadow: 0 10px 40px rgba(0,0,0,0.3);
                        backdrop-filter: blur(10px);
                    ">
            """
            
            if slide_data.get('title') in ['Cover', '표지']:
                html += f"""
                        <h1 style="font-size: 48px; margin-bottom: 10px; color: #000000; font-weight: 700;">{slide_data.get('topic', '')}</h1>
                        <h2 style="font-size: 24px; margin-top: 0; color: #212529; font-weight: 400;">{subtitle}</h2>
                """
            else:  # Thank You
                html += f"""
                        <h1 style="font-size: 42px; color: #000000; font-weight: 700; line-height: 1.2;">{subtitle}</h1>
                """
            
            html += """
                    </div>
            """
        
        html += """
                </div>
        """
    else:
        # Regular slide layout
        html += f"""
                <!-- Text area (left) -->
                <div class="text-area" style="
                    flex: 1;
                    padding: 30px;
                    display: flex;
                    flex-direction: column;
                    justify-content: center;
                    background: rgba(255, 255, 255, 0.95);
                    border-radius: 12px;
                    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.08);
                ">
                    <h2 style="
                        color: #212529;
                        font-size: 28px;
                        margin-bottom: 30px;
                        font-weight: 600;
                    ">{subtitle}</h2>
                    
                    <ul style="
                        list-style: none;
                        padding: 0;
                        margin: 0;
                    ">
        """
        
        for point in bullet_points:
            clean_point = point.replace('•', '').strip()
            html += f"""
                        <li style="
                            margin-bottom: 16px;
                            padding-left: 28px;
                            position: relative;
                            color: #495057;
                            font-size: 16px;
                            line-height: 1.6;
                        ">
                            <span style="
                                position: absolute;
                                left: 0;
                                color: #007bff;
                                font-size: 18px;
                            ">•</span>
                            {clean_point}
                        </li>
            """
        
        html += f"""
                    </ul>
                </div>
                
                <!-- Image area (right) -->
                <div class="image-area" style="
                    flex: 1;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    padding: 20px;
                ">
        """
        
        if img_base64:
            html += f"""
                    <img src="data:image/png;base64,{img_base64}" style="
                        max-width: 100%;
                        max-height: 100%;
                        object-fit: contain;
                        border-radius: 8px;
                        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
                    " alt="Slide Image">
            """
        else:
            html += """
                    <div style="
                        color: #6c757d;
                        text-align: center;
                    ">
                        <div style="font-size: 48px;">🖼️</div>
                        <p>Generating image...</p>
                    </div>
            """
        
        html += """
                </div>
        """
    
    html += """
            </div>
        </div>
    </div>
    """
    
    return html

def create_pptx_file(results: List[Dict], topic: str, template_name: str, 
                    theme_name: str = "Minimal Light", design_themes: Dict = None) -> str:
    """Create PPTX file with speaker notes"""
    print(f"[PPTX] Creating file... Theme: {theme_name}")
    print(f"[PPTX] Processing {len(results)} slides")
    
    # Helper function to convert color values to RGBColor
    def get_rgb_color(color_value):
        """Convert various color formats to RGBColor"""
        if isinstance(color_value, RGBColor):
            return color_value
        elif isinstance(color_value, tuple) and len(color_value) == 3:
            return RGBColor(color_value[0], color_value[1], color_value[2])
        elif isinstance(color_value, str) and color_value.startswith('#'):
            # Convert hex to RGB
            hex_color = color_value.lstrip('#')
            r = int(hex_color[0:2], 16)
            g = int(hex_color[2:4], 16)
            b = int(hex_color[4:6], 16)
            return RGBColor(r, g, b)
        else:
            # Default color if conversion fails
            return RGBColor(128, 128, 128)
    
    try:
        # Create presentation (16:9 ratio)
        prs = Presentation()
        prs.slide_width = Inches(16)
        prs.slide_height = Inches(9)
        
        # Get selected theme with proper defaults
        default_theme = {
            "background": RGBColor(250, 250, 252),
            "title_color": RGBColor(33, 37, 41),
            "subtitle_color": RGBColor(52, 58, 64),
            "text_color": RGBColor(73, 80, 87),
            "accent_color": RGBColor(0, 123, 255),
            "box_fill": RGBColor(255, 255, 255),
            "box_opacity": 0.95,
            "shadow": True,
            "gradient": False
        }
        
        if design_themes and theme_name in design_themes:
            raw_theme = design_themes[theme_name]
            # Convert all color values to RGBColor objects
            theme = {}
            for key, value in raw_theme.items():
                if key in ["background", "title_color", "subtitle_color", "text_color", "accent_color", "box_fill"]:
                    theme[key] = get_rgb_color(value)
                else:
                    theme[key] = value
            print(f"[PPTX] Using theme: {theme_name}")
        else:
            theme = default_theme
            print(f"[PPTX] Using default theme as {theme_name} not found")
        
        # Add slides
        slide_count = 0
        for i, result in enumerate(results):
            if not result.get("success", False):
                print(f"[PPTX] Skipping slide {i+1} - not successful")
                continue
                
            slide_data = result.get("slide_data", {})
            print(f"[PPTX] Adding slide {i+1}: {slide_data.get('title', 'Unknown')}")
            
            # Use blank layout
            blank_layout = prs.slide_layouts[6]
            slide = prs.slides.add_slide(blank_layout)
            slide_count += 1
            
            # Title slide
            if slide_data.get('title') in ['Cover', '표지']:
                # Add background image
                if slide_data.get('image'):
                    try:
                        img_buffer = BytesIO()
                        slide_data['image'].save(img_buffer, format='PNG')
                        img_buffer.seek(0)
                        
                        # Full screen background image
                        pic = slide.shapes.add_picture(
                            img_buffer, 
                            0, 0,
                            width=prs.slide_width, 
                            height=prs.slide_height
                        )
                        # Send to back
                        slide.shapes._spTree.remove(pic._element)
                        slide.shapes._spTree.insert(2, pic._element)
                        print(f"[PPTX] Added title background image")
                    except Exception as e:
                        print(f"[PPTX] Failed to add title image: {str(e)}")
                
                # Title background box (semi-transparent)
                title_bg = slide.shapes.add_shape(
                    MSO_SHAPE.ROUNDED_RECTANGLE,
                    Inches(2), Inches(2.8),
                    Inches(12), Inches(3.2)
                )
                title_bg.fill.solid()
                title_bg.fill.fore_color.rgb = RGBColor(255, 255, 255)
                title_bg.fill.transparency = 0.8
                title_bg.line.fill.background()
                
                # Shadow effect
                shadow = title_bg.shadow
                shadow.visible = True
                shadow.distance = Pt(6)
                shadow.size = 100
                shadow.blur_radius = Pt(12)
                shadow.transparency = 0.8
                shadow.angle = 45
                
                # Title text
                title_box = slide.shapes.add_textbox(
                    Inches(2), Inches(3.2), 
                    Inches(12), Inches(1.5)
                )
                title_frame = title_box.text_frame
                title_frame.text = topic
                title_para = title_frame.paragraphs[0]
                title_para.font.size = Pt(48)
                title_para.font.bold = True
                title_para.font.color.rgb = RGBColor(0, 0, 0)
                title_para.alignment = PP_ALIGN.CENTER
                
                # Subtitle
                subtitle_box = slide.shapes.add_textbox(
                    Inches(2), Inches(4.3), 
                    Inches(12), Inches(1.0)
                )
                subtitle_frame = subtitle_box.text_frame
                subtitle_frame.text = slide_data.get('subtitle', f'{template_name} - AI Presentation')
                subtitle_para = subtitle_frame.paragraphs[0]
                subtitle_para.font.size = Pt(28)
                subtitle_para.font.color.rgb = RGBColor(33, 37, 41)
                subtitle_para.alignment = PP_ALIGN.CENTER
                
            # Thank You slide
            elif slide_data.get('title') == 'Thank You':
                # Add background image
                if slide_data.get('image'):
                    try:
                        img_buffer = BytesIO()
                        slide_data['image'].save(img_buffer, format='PNG')
                        img_buffer.seek(0)
                        
                        # Full screen background image
                        pic = slide.shapes.add_picture(
                            img_buffer, 
                            0, 0,
                            width=prs.slide_width, 
                            height=prs.slide_height
                        )
                        # Send to back
                        slide.shapes._spTree.remove(pic._element)
                        slide.shapes._spTree.insert(2, pic._element)
                    except Exception as e:
                        print(f"[PPTX] Failed to add Thank You image: {str(e)}")
                
                # Thank You background box
                thanks_bg = slide.shapes.add_shape(
                    MSO_SHAPE.ROUNDED_RECTANGLE,
                    Inches(2), Inches(3.5),
                    Inches(12), Inches(2.5)
                )
                thanks_bg.fill.solid()
                thanks_bg.fill.fore_color.rgb = RGBColor(255, 255, 255)
                thanks_bg.fill.transparency = 0.8
                thanks_bg.line.fill.background()
                
                # Shadow effect
                shadow = thanks_bg.shadow
                shadow.visible = True
                shadow.distance = Pt(6)
                shadow.size = 100
                shadow.blur_radius = Pt(12)
                shadow.transparency = 0.8
                shadow.angle = 45
                
                # Thank You text (conclusion phrase)
                thanks_box = slide.shapes.add_textbox(
                    Inches(2), Inches(4), 
                    Inches(12), Inches(1.5)
                )
                thanks_frame = thanks_box.text_frame
                thanks_frame.text = slide_data.get('subtitle', 'Thank You')
                thanks_para = thanks_frame.paragraphs[0]
                thanks_para.font.size = Pt(42)
                thanks_para.font.bold = True
                thanks_para.font.color.rgb = RGBColor(0, 0, 0)
                thanks_para.alignment = PP_ALIGN.CENTER
                
            # Regular slides
            else:
                # Background color
                background = slide.background
                fill = background.fill
                fill.solid()
                fill.fore_color.rgb = get_rgb_color(theme.get("background", RGBColor(250, 250, 252)))
                
                # Slide title background box
                title_box_bg = slide.shapes.add_shape(
                    MSO_SHAPE.ROUNDED_RECTANGLE,
                    Inches(0.3), Inches(0.2),
                    Inches(15.4), Inches(1.0)
                )
                title_box_bg.fill.solid()
                title_box_bg.fill.fore_color.rgb = get_rgb_color(theme.get("box_fill", RGBColor(255, 255, 255)))
                title_box_bg.fill.transparency = 1 - theme.get("box_opacity", 0.95)
                
                # Shadow effect
                if theme.get("shadow", True):
                    shadow = title_box_bg.shadow
                    shadow.visible = True
                    shadow.distance = Pt(4)
                    shadow.size = 100
                    shadow.blur_radius = Pt(8)
                    shadow.transparency = 0.75
                    shadow.angle = 45
                
                title_box_bg.line.fill.background()
                
                # Slide title
                title_box = slide.shapes.add_textbox(
                    Inches(0.5), Inches(0.3), 
                    Inches(15), Inches(0.8)
                )
                title_frame = title_box.text_frame
                title_frame.text = f"{slide_data.get('title', '')}"
                title_para = title_frame.paragraphs[0]
                title_para.font.size = Pt(28)
                title_para.font.bold = True
                title_para.font.color.rgb = get_rgb_color(theme.get("title_color", RGBColor(33, 37, 41)))
                
                # Left text area background box
                text_box_bg = slide.shapes.add_shape(
                    MSO_SHAPE.ROUNDED_RECTANGLE,
                    Inches(0.3), Inches(1.4),
                    Inches(7.8), Inches(6.8)
                )
                text_box_bg.fill.solid()
                text_box_bg.fill.fore_color.rgb = get_rgb_color(theme.get("box_fill", RGBColor(255, 255, 255)))
                text_box_bg.fill.transparency = 1 - theme.get("box_opacity", 0.95)
                
                if theme.get("shadow", True):
                    shadow = text_box_bg.shadow
                    shadow.visible = True
                    shadow.distance = Pt(5)
                    shadow.size = 100
                    shadow.blur_radius = Pt(10)
                    shadow.transparency = 0.7
                    shadow.angle = 45
                
                text_box_bg.line.fill.background()
                
                # Left text area
                text_box = slide.shapes.add_textbox(
                    Inches(0.8), Inches(1.8),
                    Inches(7.0), Inches(6.0)
                )
                text_frame = text_box.text_frame
                text_frame.word_wrap = True
                
                # Subtitle
                subtitle_para = text_frame.paragraphs[0]
                subtitle_para.text = slide_data.get('subtitle', '')
                subtitle_para.font.size = Pt(20)
                subtitle_para.font.bold = True
                subtitle_para.font.color.rgb = get_rgb_color(theme.get("subtitle_color", RGBColor(52, 58, 64)))
                subtitle_para.space_after = Pt(20)
                
                # Bullet points
                bullet_points = slide_data.get('bullet_points', [])
                for point in bullet_points:
                    p = text_frame.add_paragraph()
                    # Remove • and add text only (keep emojis)
                    clean_text = point.replace('•', '').strip()
                    p.text = clean_text
                    p.font.size = Pt(16)
                    p.font.color.rgb = get_rgb_color(theme.get("text_color", RGBColor(73, 80, 87)))
                    p.level = 0
                    p.space_after = Pt(12)
                    p.line_spacing = 1.5
                    p.left_indent = Pt(0)
                
                # Right image
                if slide_data.get('image'):
                    try:
                        img_buffer = BytesIO()
                        slide_data['image'].save(img_buffer, format='PNG')
                        img_buffer.seek(0)
                        
                        pic = slide.shapes.add_picture(
                            img_buffer, 
                            Inches(8.5), Inches(1.6),
                            width=Inches(6.8), height=Inches(6.4)
                        )
                        
                        pic.line.fill.background()
                        
                    except Exception as e:
                        print(f"[PPTX] Failed to add image: {str(e)}")
                
                # Page number
                page_num = slide.shapes.add_textbox(
                    Inches(15), Inches(8.5),
                    Inches(1), Inches(0.5)
                )
                page_frame = page_num.text_frame
                page_frame.text = str(i + 1)
                page_para = page_frame.paragraphs[0]
                page_para.font.size = Pt(12)
                page_para.font.color.rgb = get_rgb_color(theme.get("text_color", RGBColor(73, 80, 87)))
                page_para.alignment = PP_ALIGN.RIGHT
            
            # Add speaker notes
            notes_slide = slide.notes_slide
            notes_slide.notes_text_frame.text = slide_data.get('speaker_notes', '')
            print(f"[PPTX] Added speaker notes for slide {i+1}")
        
        print(f"[PPTX] Total slides added: {slide_count}")
        
        # Save file using tempfile for better compatibility
        import tempfile
        import os
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        filename = f"presentation_{timestamp}.pptx"
        
        # Create a temporary file
        with tempfile.NamedTemporaryFile(mode='wb', suffix='.pptx', delete=False) as tmp_file:
            prs.save(tmp_file)
            temp_path = tmp_file.name
            print(f"[PPTX] File saved to temporary path: {temp_path}")
        
        # Verify file exists and has content
        if os.path.exists(temp_path):
            file_size = os.path.getsize(temp_path)
            print(f"[PPTX] File created successfully: {filename}")
            print(f"[PPTX] File size: {file_size} bytes")
            print(f"[PPTX] File path: {temp_path}")
            
            if file_size > 0:
                return temp_path
            else:
                print(f"[PPTX] ERROR: File created but has 0 bytes")
                os.remove(temp_path)
                return None
        else:
            print(f"[PPTX] ERROR: File was not created at {temp_path}")
            return None
            
    except Exception as e:
        print(f"[PPTX] CRITICAL ERROR during file creation: {str(e)}")
        import traceback
        traceback.print_exc()
        return None