File size: 52,485 Bytes
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
c1ffb0b
0c54d38
 
 
29f1c69
 
 
 
 
 
 
 
 
 
 
 
 
84273d9
f4844c4
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29f1c69
 
84273d9
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1ffb0b
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
402763b
 
63a8740
0c54d38
402763b
0c54d38
402763b
0c54d38
402763b
 
0c54d38
402763b
 
 
 
 
0c54d38
402763b
 
0c54d38
402763b
 
bed4703
8256632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402763b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c54d38
402763b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c54d38
402763b
0c54d38
402763b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c54d38
bed4703
402763b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c54d38
bed4703
402763b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c54d38
402763b
 
0c54d38
402763b
0c54d38
402763b
 
 
 
0c54d38
402763b
0c54d38
402763b
 
0c54d38
29f1c69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bd7ff7
 
29f1c69
9bd7ff7
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e740d72
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e740d72
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
bed4703
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29f1c69
 
0c54d38
 
29f1c69
 
 
 
 
eccaf3b
29f1c69
eccaf3b
291d3ab
 
 
 
29f1c69
92683f9
0c54d38
29f1c69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29f1c69
 
 
 
 
0c54d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import base64
import asyncio
import json
import os
from mcp import ClientSession, StdioServerParameters, types
from mcp.client.stdio import stdio_client
from typing import List
import nest_asyncio
import threading
import queue
import time
from PIL import Image
from io import BytesIO
os.makedirs("AI-Marketing-Content-Creator/created_image", exist_ok=True)

nest_asyncio.apply()

def get_api_keys():
    """Get API keys from user input or fall back to space secrets"""
    return {
        "MODAL_API_URL": os.environ.get("MODAL_API_URL", ""),
        "MISTRAL_API_KEY": os.environ.get("MISTRAL_API_KEY", "")
    }

# Global variables for API keys
user_modal_url = ""
user_mistral_key = ""





class MCP_Modal_Marketing_Tool:
    def __init__(self):
        self.session: ClientSession = None
        self.available_tools: List[dict] = []
        self.is_connected = False
        self.request_queue = queue.Queue()
        self.result_queue = queue.Queue()

    async def call_mcp_tool(self, tool_name: str, arguments: dict):
        """Generic method to call any MCP tool"""
        try:
            result = await self.session.call_tool(tool_name, arguments=arguments)
            if hasattr(result, 'content') and result.content:
                return result.content[0].text
            return None
        except Exception as e:
            print(f"Error calling tool {tool_name}: {str(e)}")
            raise e

    async def process_queue(self):
        """Process requests from the queue"""
        while True:
            try:
                if not self.request_queue.empty():
                    item = self.request_queue.get()
                    if item == "STOP":
                        break

                    tool_name, arguments, request_id = item
                    try:
                        result = await self.call_mcp_tool(tool_name, arguments)
                        self.result_queue.put(("success", result, request_id))
                    except Exception as e:
                        self.result_queue.put(("error", str(e), request_id))
                else:
                    await asyncio.sleep(0.1)
            except Exception as e:
                print(f"Error in process_queue: {str(e)}")

    async def connect_to_server_and_run(self):
        """Connect to MCP server and start processing"""
        server_params = StdioServerParameters(
            command="python",
            args=["mcp_server.py"],
            env={"MODAL_API_URL": user_modal_url or os.environ.get("MODAL_API_URL"),
                 "MISTRAL_API_KEY": user_mistral_key or os.environ.get("MISTRAL_API_KEY"),
            },
        )

        async with stdio_client(server_params) as (read, write):
            async with ClientSession(read, write) as session:
                self.session = session
                await session.initialize()

                response = await session.list_tools()
                tools = response.tools
                print("Connected to MCP server with tools:",
                      [tool.name for tool in tools])

                self.available_tools = [{
                    "name": tool.name,
                    "description": tool.description,
                    "input_schema": tool.inputSchema
                } for tool in tools]

                self.is_connected = True
                print("Marketing Tool MCP Server connected!")

                # Check Modal health
                health_result = await self.call_mcp_tool("health_check", {})
                print(f"Modal API Status: {health_result}")

                await self.process_queue()



marketing_tool = MCP_Modal_Marketing_Tool()


def wait_for_result(request_id, timeout=300):
    """Wait for a result with a specific request ID"""
    start_time = time.time()
    while time.time() - start_time < timeout:
        if not marketing_tool.result_queue.empty():
            status, result, result_id = marketing_tool.result_queue.get()
            if result_id == request_id:
                return status, result
            else:
                # Put it back if it's not our result
                marketing_tool.result_queue.put((status, result, result_id))
        time.sleep(0.1)
    return "error", "Timeout"


def decode_and_save_image(image_b64, filename):
    """Decode base64 and save image"""
    import base64
    from PIL import Image
    from io import BytesIO

    # Ensure the path is inside created_image/
    full_path = os.path.join("AI-Marketing-Content-Creator/created_image", filename)
    image_b64 = image_b64.strip()
    missing_padding = len(image_b64) % 4
    if missing_padding:
        image_b64 += '=' * (4 - missing_padding)

    image_data = base64.b64decode(image_b64)
    image = Image.open(BytesIO(image_data))
    image.save(full_path)
    return full_path


def single_image_generation(prompt, num_steps, style):
    """Generate a single image with optional style"""
    if not marketing_tool.is_connected:
        return None, "⚠️ MCP Server not connected. Please wait a few seconds and try again."

    try:
        request_id = f"single_{time.time()}"

        # Apply style if selected
        if style != "none":
            style_request_id = f"style_{time.time()}"
            marketing_tool.request_queue.put((
                "add_style_modifier",
                {"prompt": prompt, "style": style},
                style_request_id
            ))

            status, result = wait_for_result(style_request_id, timeout=50)
            if status == "success":
                style_data = json.loads(result)
                prompt = style_data["enhanced_prompt"]

        # Generate image
        marketing_tool.request_queue.put((
            "generate_and_save_image",
            {"prompt": prompt, "num_inference_steps": num_steps},
            request_id
        ))

        status, result = wait_for_result(request_id)

        if status == "success":
            filename = decode_and_save_image(
                result, f"generated_{int(time.time())}.png")
            return filename, f"βœ… Image generated successfully!\nπŸ“ Final prompt: {prompt}"
        else:
            return None, f"❌ Error: {result}"

    except Exception as e:
        return None, f"❌ Error: {str(e)}"


# Update the batch generation function in app.py
def enhanced_batch_generation(prompt, variation_type, count, num_steps):
    """Generate strategic variations for A/B testing"""
    if not marketing_tool.is_connected:
        return None, "⚠️ MCP Server not connected. Please wait a few seconds and try again."
        
    try:
        request_id = f"smart_batch_{time.time()}"
        marketing_tool.request_queue.put((
            "batch_generate_smart_variations",
            {
                "prompt": prompt, 
                "count": count, 
                "variation_type": variation_type,
                "num_inference_steps": num_steps
            },
            request_id
        ))
        
        status, result = wait_for_result(request_id, timeout=300) 
        
        if status == "success":
            batch_data = json.loads(result)
            images = []
            variation_details = []
            
            for i, img_data in enumerate(batch_data["images"]):
                filename = decode_and_save_image(
                    img_data["image_base64"], 
                    f"variation_{i+1}_{int(time.time())}.png"
                )
                images.append(filename)
                
                variation_details.append(
                    f"**Variation {i+1}:** {img_data['variation_description']}\n"
                    f"*Testing Purpose:* {img_data['testing_purpose']}\n"
                )
            
            strategy_explanation = batch_data.get("testing_strategy", "")
            
            status_message = (
                f"βœ… Generated {len(images)} strategic variations!\n\n"
                f"**Testing Strategy:** {strategy_explanation}\n\n"
                f"**Variations Created:**\n" + 
                "\n".join(variation_details) +
                f"\nπŸ’‘ **Next Steps:** Post each variation and track engagement metrics to see which performs best!"
            )
            
            return images, status_message
        else:
            return None, f"❌ Error: {result}"
            
    except Exception as e:
        return None, f"❌ Error: {str(e)}"


def update_strategy_info(variation_type):
    strategy_descriptions = {
        "mixed": {
            "title": "Mixed Strategy Testing",
            "description": "Tests multiple variables (colors, layout, mood) to find overall best approach",
            "use_case": "Best for comprehensive optimization when you're not sure what to test first"
        },
        "color_schemes": {
            "title": "Color Psychology Testing", 
            "description": "Tests how different color schemes affect emotional response and engagement",
            "use_case": "Great for brand content, product launches, and emotional marketing"
        },
        "composition_styles": {
            "title": "Layout & Composition Testing",
            "description": "Tests different visual arrangements and focal points",
            "use_case": "Perfect for optimizing visual hierarchy and user attention flow"
        },
        "emotional_tones": {
            "title": "Emotional Tone Testing",
            "description": "Tests different moods and feelings to see what resonates with your audience", 
            "use_case": "Ideal for brand personality and audience connection optimization"
        },
        "social_media": {
            "title": "Platform Optimization Testing",
            "description": "Tests platform-specific elements and styles",
            "use_case": "Essential for multi-platform content strategies"
        },
        "engagement_hooks": {
            "title": "Attention-Grabbing Testing",
            "description": "Tests different ways to capture and hold viewer attention",
            "use_case": "Critical for improving reach and stopping scroll behavior"
        },
        "brand_positioning": {
            "title": "Brand Positioning Testing", 
            "description": "Tests how different brand personalities affect audience perception",
            "use_case": "Important for brand development and target audience alignment"
        }
    }
    
    info = strategy_descriptions.get(variation_type, strategy_descriptions["mixed"])
    return f"""
    **πŸ’‘ Current Strategy:** {info['title']}
    
    **What this tests:** {info['description']}
    
    **Best for:** {info['use_case']}
    """

def social_media_generation(prompt, platforms, num_steps):
    """Generate images for multiple social media platforms with correct resolutions"""
    if not marketing_tool.is_connected:
        return None, "MCP Server not connected"
        
    try:
        request_id = f"social_{time.time()}"
        marketing_tool.request_queue.put((
            "generate_social_media_set",
            {"prompt": prompt, "platforms": platforms, "num_inference_steps": num_steps},
            request_id
        ))
        
        status, result = wait_for_result(request_id)
        
        if status == "success":
            social_data = json.loads(result)
            results = []
            
            for platform_data in social_data["results"]:
                filename = decode_and_save_image(
                    platform_data["image_base64"],
                    f"{platform_data['platform']}_{platform_data['resolution']}_{int(time.time())}.png"
                )
                results.append((platform_data["platform"], filename, platform_data["resolution"]))
                
            # Create a status message with resolutions
            if results:
                status_msg = "Generated images:\n" + "\n".join([
                    f"β€’ {r[0]}: {r[2]}" for r in results
                ])
                return [r[1] for r in results], status_msg
            else:
                return None, "No images generated"
        else:
            return None, f"Error: {result}"
            
    except Exception as e:
        return None, f"Error: {str(e)}"


def start_mcp_server():
    """Start MCP server in background"""
    def run_server():
        asyncio.run(marketing_tool.connect_to_server_and_run())

    thread = threading.Thread(target=run_server, daemon=True)
    thread.start()
    return thread



SIZE_PRESETS = {
    "instagram_post": (1080, 1080),
    "instagram_story": (1080, 1920),
    "twitter_post": (1200, 675),
    "linkedin_post": (1200, 1200),
    "facebook_cover": (1200, 630),
    "youtube_thumbnail": (1280, 720)
}


with gr.Blocks(title="AI Marketing Content Generator") as demo:
    gr.Markdown("""
    # 🎨 AI Marketing Content Generator
    ### Powered by Flux AI on Modal GPU via MCP
    
    Generate professional marketing images with AI - optimized for content creators and marketers!
    
    ⏰ **Please wait 5-10 seconds after launching for the MCP server to connect**
    """)

    # Connection status
    connection_status = gr.Markdown("πŸ”„ Connecting to MCP server...")

    with gr.Tabs():

        with gr.TabItem("πŸ“– Quick Start"):
            gr.Markdown("""
            # πŸš€ Welcome to AI Marketing Content Generator!
            ### Create professional marketing images in minutes - no design skills needed!
            - You can use your own API keys ,Scroll down!!
            
            ---
            
            ## ⚑ Get Started in 3 Simple Steps
            
            ### Step 1: βœ… Check Connection
            Look at the status above - wait for "βœ… Connected" before starting
            
            ### Step 2: 🎯 Choose What You Need
            - **πŸ–ΌοΈ Single Image** β†’ One perfect marketing image
            - **πŸ”„ A/B Testing** β†’ Multiple versions to see what works best
            - **πŸ“± Social Media** β†’ Images sized for different platforms
            - **πŸ€– AI Assistant** β†’ Let AI write the perfect prompt for you
            
            ### Step 3: 🎨 Create & Download
            Enter your details, click generate, and download your professional images!
            
            ---
            """)

            with gr.Accordion("πŸŽ₯ Video Demo - See It In Action!", open=True):
                gr.Markdown("""
                ### πŸ“Ή Watch How Easy It Is!
                See the AI Marketing Content Generator in action - from prompt to professional image in seconds.
                """)
                
                try:
                    
                    video_path = "created_image/Live video demo.mp4"  
                    if os.path.exists(video_path):
                        gr.Video(
                            value=video_path,
                            label="Demo Video",
                            height=400,
                            show_label=False,
                            interactive=False,
                            autoplay=False
                        )
                    else:
                        gr.Markdown("πŸ“Ή **Demo video will be available soon!**")
                        
                except Exception as e:
                    gr.Markdown("πŸ“Ή **Demo video will be available soon!**")
                
                gr.Markdown("""
                **🎯 What you'll see in the demo:**
                - How to create single marketing images
                - A/B testing for better engagement
                - Multi-platform social media content
                - AI-powered prompt generation
                
                **⏱️ Total demo time:** ~3 minutes
                """)

            with gr.Row():
                with gr.Column():
                    gr.Markdown("""
                    ## πŸ–ΌοΈ Single Image
                    **Perfect for beginners!**
                    
                    ✨ **What it does:** Creates one professional marketing image
                    
                    🎯 **Best for:**
                    - Blog post headers
                    - Social media posts
                    - Product announcements
                    - Website banners
                    
                    πŸ’‘ **How to use:**
                    1. Describe what you want
                    2. Pick a style (optional)
                    3. Click "Generate Image"
                    
                    **Example:** "Professional photo of a coffee cup on wooden table"
                    """)
                
                with gr.Column():
                    gr.Markdown("""
                    ## πŸ”„ A/B Testing Batch
                    **For optimizing performance**
                    
                    ✨ **What it does:** Creates 2-5 different versions to test
                    
                    🎯 **Best for:**
                    - Finding what your audience likes
                    - Improving engagement rates
                    - Testing different approaches
                    
                    πŸ’‘ **How to use:**
                    1. Describe your content idea
                    2. Choose testing strategy
                    3. Post each version and see which performs best
                    
                    **Example:** Test different colors for your sale announcement
                    """)
            
            with gr.Row():
                with gr.Column():
                    gr.Markdown("""
                    ## πŸ“± Social Media Pack
                    **Multi-platform made easy**
                    
                    ✨ **What it does:** Creates perfectly sized images for each platform
                    
                    🎯 **Best for:**
                    - Cross-platform campaigns
                    - Consistent branding
                    - Saving time
                    
                    πŸ’‘ **How to use:**
                    1. Describe your content
                    2. Check platforms you need
                    3. Get all sizes at once
                    
                    **Platforms:** Instagram, Twitter, LinkedIn, Facebook, YouTube
                    """)
                
                with gr.Column():
                    gr.Markdown("""
                    ## πŸ€– AI Assistant
                    **Let AI do the thinking**
                    
                    ✨ **What it does:** Writes professional prompts for you
                    
                    🎯 **Best for:**
                    - When you're not sure how to describe what you want
                    - Getting professional results
                    - Learning better prompting
                    
                    πŸ’‘ **How to use:**
                    1. Tell AI what you're creating in plain English
                    2. AI writes the perfect prompt
                    3. Generate your image
                    
                    **Example Input:** "I need a hero image for my water bottle business"
                    """)
            
            gr.Markdown("---")
            
            with gr.Accordion("🎯 Real-World Examples", open=False):
                gr.Markdown("""
                ## See What You Can Create
                
                ### πŸ›οΈ E-commerce Business Owner
                **Need:** Product photos for online store
                **Use:** Single Image tab
                **Prompt:** "Professional product photography of [your product], white background, studio lighting"
                **Result:** Clean, professional product images
                
                ### πŸ“± Social Media Manager
                **Need:** Content that gets engagement
                **Use:** A/B Testing tab
                **Prompt:** "Eye-catching announcement for Black Friday sale"
                **Result:** 3-5 different versions to test which gets more likes/shares
                
                ### 🏒 Small Business Owner
                **Need:** Content for multiple platforms
                **Use:** Social Media Pack tab
                **Prompt:** "Grand opening celebration announcement"
                **Result:** Perfect sizes for Instagram, Facebook, Twitter, LinkedIn
                
                ### πŸ€” First-Time User
                **Need:** Not sure how to describe what you want
                **Use:** AI Assistant tab
                **Input:** "I need marketing images for my yoga studio"
                **Result:** AI creates perfect prompts for you
                """)
            

            with gr.Accordion("πŸ’‘ Tips for Amazing Results", open=False):
                gr.Markdown("""
                ## Make Your Images Stand Out
                
                ### βœ… Do This:
                - **Be specific:** "Red sports car in garage" vs "car"
                - **Mention the mood:** "professional," "fun," "elegant"
                - **Include details:** "wooden background," "bright lighting"
                - **Use style presets:** They make everything look more professional
                
                ### ❌ Avoid This:
                - Vague descriptions like "nice image"
                - Too many conflicting ideas in one prompt
                - Forgetting to mention important details
                
                ### 🎨 Style Guide:
                - **Professional:** For business, corporate, formal content
                - **Playful:** For fun brands, kids products, casual content
                - **Minimalist:** For clean, modern, simple designs
                - **Luxury:** For high-end products, premium brands
                - **Tech:** For software, apps, modern technology
                
                ### ⚑ Speed vs Quality:
                - **Quick test:** 30-40 steps (faster, good for trying ideas)
                - **Final image:** 70-100 steps (slower, best quality)
                """)
            

            with gr.Accordion("πŸ”§ Common Issues & Solutions", open=False):
                gr.Markdown("""
                ## Troubleshooting Guide
                
                ### ❗ "MCP Server not connected"
                **Solution:** Wait 10-15 seconds after opening the app, then refresh the page
                
                ### ❗ "Timeout" errors
                **Solution:** The AI might be starting up - wait 30 seconds and try again
                
                ### ❗ Image quality is poor
                **Solution:** Increase the "Quality" slider to 70+ steps
                
                ### ❗ Image doesn't match what I wanted
                **Solution:** 
                - Be more specific in your description
                - Try the AI Assistant tab for better prompts
                - Use style presets
                
                ### ❗ Generation is too slow
                **Solution:** Lower the quality steps to 30-40 for faster results
                
                ### πŸ’¬ Still need help?
                - Check if your internet connection is stable
                - Try refreshing the page
                - Make sure you're being specific in your prompts
                """)
            
            gr.Markdown("""
            ---
            
            ## πŸš€ Ready to Start?
            
            1. **Check the connection status** at the top of the page
            2. **Choose a tab** based on what you need to create
            3. **Start with simple prompts** and experiment
            4. **Have fun creating!** 🎨
            
            ---
            
            ### 🎯 Pro Tip for Beginners
            Start with the **πŸ€– AI Assistant** tab if you're unsure - it will guide you through creating the perfect prompt!
            """)
            
            gr.Markdown("""
            ---
            
            ## πŸ”§ Optional: Use Your Own API Keys
            
            **Default Mode:** Uses shared keys (free but limited)  
            **Custom Mode:** Use your own keys (unlimited usage, you pay)
            """)
            
            with gr.Accordion("πŸ”‘ Configure Your Own API Keys", open=False):
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("### Enter Your Keys")
                        
                        modal_url_input = gr.Textbox(
                            label="Modal API URL",
                            placeholder="Enter your Modal API URL (optional)",
                            type="password",
                            info="Leave empty to use default"
                        )
                        
                        mistral_key_input = gr.Textbox(
                            label="Mistral API Key", 
                            placeholder="Enter your Mistral API key (optional)",
                            type="password",
                            info="Leave empty to use default"
                        )
                        
                        update_keys_btn = gr.Button(
                            "πŸ”„ Update Keys & Restart Connection", 
                            variant="primary"
                        )
                        
                        keys_status = gr.Textbox(
                            label="Status",
                            lines=2,
                            interactive=False
                        )
                        
                    with gr.Column():
                        gr.Markdown("""
                        ### πŸ’‘ How to Get Your Keys
                        
                        **Modal API URL:**
                        1. Sign up at [modal.com](https://modal.com)
                        2. Download modal_server.py from files/src/modal_server.py in your device in a folder
                        3. Open CMD in the same folder, type modal token new ,This will set your pc connected to your modal labs
                        4. Now in CMD type modal deploy modal_server.py (make sure modal is installed in your pc (pip install modal))
                        3. Copy the Fast API url you will see in CMD and paste it in MODAL_API_URL
                        
                        **Mistral API Key:**
                        1. Sign up at [mistral.ai](https://mistral.ai)  
                        2. Go to your dashboard
                        3. Generate an API key
                        
                        ### πŸ”’ Privacy & Security
                        - Keys stored temporarily in memory only
                        - Not saved or logged anywhere
                        - Connection is encrypted

                        ### If your API keys not working or If you want to fall back to Default Keys, Click Update Keys & Restart Connection to get back to Default keys
                        """)
                        
        with gr.TabItem("πŸ–ΌοΈ Single Image"):
            with gr.Row():
                with gr.Column():
                    single_prompt = gr.Textbox(
                        label="Prompt",
                        placeholder="Describe your image in detail...\nExample: Professional headshot of business person in modern office",
                        lines=3
                    )
                    with gr.Row():
                        single_style = gr.Dropdown(
                            choices=["none", "professional", "playful",
                                     "minimalist", "luxury", "tech"],
                            value="none",
                            label="Style Preset",
                            info="Apply a consistent style to your image"
                        )
                        single_steps = gr.Slider(
                            10, 100, 50,
                            step=10,
                            label="Quality (Inference Steps)",
                            info="Higher = better quality but slower"
                        )
                    single_btn = gr.Button(
                        "🎨 Generate Image", variant="primary", size="lg")

                  
                    with gr.Accordion("πŸ’­ Example Ideas",open=False):
                        gr.Examples(
                            examples=[
                                ["""This poster is dominated by blue-purple neon lights, with the background of a hyper city at night, with towering skyscrapers surrounded by colorful LED light strips. In the center of the picture is a young steampunk modern robot with virtual information interfaces and digital codes floating around him. The future fonted title "CYNAPTICS" is in neon blue, glowing, as if outlined by laser, exuding a sense of technology and a cold and mysterious atmosphere. The small words "FUTURE IS NOW" seem to be calling the audience to the future, full of science fiction and trendy charm""", "professional", 50],
                                ["poster of,a white girl,A young korean woman pose with a white Vespa scooter on a sunny day,dressed in a stylish red and white jacket .inside a jacket is strapless,with a casual denim skirt. She wears a helmet with vintage-style goggles,and converse sneakers,adding a retro touch to her outfit. The bright sunlight highlights her relaxed and cheerful expression,and the VespaҀ™s white color pops against the clear blue sky. The background features a vibrant,sunlit scene with a few trees or distant buildings,creating a fresh and joyful atmosphere. Art style: realistic,high detail,vibrant colors,warm and cheerful.,f1.4 50mm,commercial photo style,with text around is 'Chasing the sun on my Vespa nothing but the open road ahead'", "playful", 40],
                                ["""Badminton is not just about winning, it’s about daring to challenge the limits of speed and precision. It’s a game where every strike is a test of reflexes, every point a moment of courage. To play badminton is to engage in a battle of endurance, strategy, and passion.""", "minimalist", 50],
                                ],
                            inputs=[single_prompt, single_style, single_steps],
                            label="Quick Examples"
                            )

                with gr.Column():
                    single_output = gr.Image(
                        label="Generated Image", type="filepath")
                    single_status = gr.Textbox(
                        label="Status", lines=3, interactive=False)

        with gr.TabItem("πŸ”„ A/B Testing Batch"):
            gr.Markdown("""
                        ### Generate Strategic Variations for Testing
                        Create different versions that test specific elements to optimize your content performance.
                        Each variation tests a different hypothesis about what works best for your audience.
                        """)
            with gr.Row():
                with gr.Column():
                    batch_prompt = gr.Textbox(
                        label="Base Content Prompt",
                        placeholder="Describe your core content idea...\nExample: Professional announcement for new product launch",
                        lines=3
                    )
                    batch_variation_type = gr.Dropdown(
                        choices=[
                            ("🎨 Mixed Strategy (Recommended)", "mixed"),
                            ("🌈 Color Psychology Test", "color_schemes"),
                            ("πŸ“ Layout & Composition Test", "composition_styles"),
                            ("😊 Emotional Tone Test", "emotional_tones"),
                            ("πŸ“± Platform Optimization Test", "social_media"),
                            ("πŸ‘οΈ Attention-Grabbing Test", "engagement_hooks"),
                            ("🏷️ Brand Positioning Test", "brand_positioning")
                            ],
                        value="mixed",
                        label="Testing Strategy",
                        info="Choose what aspect you want to test"
                        )
                    with gr.Row():
                        batch_count = gr.Slider(
                            2, 5, 3,
                            step=1,
                            label="Number of Variations",
                            info="How many different versions to generate"
                            )
                        batch_steps = gr.Slider(
                            10, 100, 40,
                            step=5,
                            label="Quality (Inference Steps)",info="Lower steps for quick testing")

                    batch_btn = gr.Button(
                        "πŸ”„ Generate Variations", variant="primary", size="lg")
                    
                    strategy_info = gr.Markdown("""
                                                **πŸ’‘ Current Strategy:** Mixed approach testing multiple variables
                                                **What this tests:** Different colors, layouts, and styles to find what works best
                                                **How to use results:** Post each variation and compare engagement metrics
                                                """)


                with gr.Column():
                    batch_output = gr.Gallery(
                        label="Generated Test Variations",
                        columns=2,
                        height="auto"
                    )
                    batch_status = gr.Textbox(
                        label="Variation Details", lines=6, interactive=False)
                    with gr.Accordion("πŸ“Š A/B Testing Guide",open=False):
                        gr.Markdown("""
                                **Step 1:** Generate variations above
                                **Step 2:** Post each variation to your platform
                                **Step 3:** Track these metrics for each:
                                - Engagement rate (likes, comments, shares)
                                - Click-through rate (if applicable)
                                - Reach and impressions
                                - Save/bookmark rate
                                
                                **Step 4:** Use the best performer for future content
                                
                                **πŸ’‘ Pro Tips:**
                                - Test one element at a time for clear results
                                - Run tests for at least 7 days
                                - Use the same posting time and hashtags
                                - Need 1000+ views per variation for statistical significance
                                """)
      
        with gr.TabItem("πŸ“± Social Media Pack"):
            gr.Markdown("""
            ### Generate Platform-Optimized Images
            Create perfectly sized images for multiple social media platforms at once.
            """)
            with gr.Row():
                with gr.Column():
                    social_prompt = gr.Textbox(
                        label="Content Prompt",
                        placeholder="Describe your social media content...\nExample: Exciting announcement for new product launch",
                        lines=3
                    )
                    social_platforms = gr.CheckboxGroup(
                        choices=[
                            ("Instagram Post (1080x1080)", "instagram_post"),
                            ("Instagram Story (1080x1920)", "instagram_story"),
                            ("Twitter Post (1200x675)", "twitter_post"),
                            ("LinkedIn Post (1200x1200)", "linkedin_post"),
                            ("Facebook Cover (1200x630)", "facebook_cover"),
                            ("YouTube Thumbnail (1280x720)", "youtube_thumbnail")
                        ],
                        value=["instagram_post", "twitter_post"],
                        label="Select Platforms",
                        info="Each platform will get an optimized image"
                    )
                    social_steps = gr.Slider(
                        10, 100, 50,
                        step=5,
                        label="Quality (Inference Steps)"
                    )
                    social_btn = gr.Button(
                        "πŸ“± Generate Social Pack", variant="primary", size="lg")

                with gr.Column():
                    social_output = gr.Gallery(
                        label="Platform-Optimized Images",
                        columns=2,
                        height="auto"
                    )
                    social_status = gr.Textbox(
                        label="Status", lines=4, interactive=False)

        with gr.TabItem("πŸ€– AI Prompt Assistant"):
            
            with gr.Column():
                gr.Markdown("### πŸ€– AI-Powered Prompt Creation")
                with gr.Accordion("πŸ’‘ How This Works", open=False):
                    gr.Markdown("""
                    **Simple 3-step process:**
                    1. Describe what you want in plain English
                    2. AI creates an optimized prompt  
                    3. Generate your professional image
                    """)
            
           
            with gr.Row():
                
                with gr.Column(scale=1, min_width=300):
                    ai_user_input = gr.Textbox(
                        label="What do you want to create?",
                        placeholder="Example: A hero image for my new eco-friendly water bottle product launch",
                        lines=4,
                        info="Describe your vision in plain language"
                    )
                    
                    with gr.Group():
                        gr.Markdown("#### Settings")
                        
                        ai_context = gr.Dropdown(
                            choices=[
                                ("General Marketing", "marketing"),
                                ("Product Photography", "product"),
                                ("Social Media Post", "social"),
                                ("Blog/Article Header", "blog"),
                                ("Event Promotion", "event"),
                                ("Brand Identity", "brand")
                            ],
                            value="marketing",
                            label="Content Type",
                            info="What are you creating?"
                        )
                        ai_style = gr.Dropdown(
                            choices=[
                                ("Professional", "professional"),
                                ("Playful & Fun", "playful"),
                                ("Minimalist", "minimalist"),
                                ("Luxury", "luxury"),
                                ("Tech/Modern", "tech"),
                                ("Natural/Organic", "natural")
                            ],
                            value="professional",
                            label="Style",
                            info="What mood to convey?"
                        )
                        ai_platform = gr.Dropdown(
                            choices=[
                                ("General Use", "general"),
                                ("Instagram", "instagram"),
                                ("Twitter/X", "twitter"),
                                ("LinkedIn", "linkedin"),
                                ("Facebook", "facebook"),
                                ("Website Hero", "website")
                            ],
                            value="general",
                            label="Platform",
                            info="Where will this be used?"
                        )
                    

                    ai_generate_btn = gr.Button(
                        "πŸ€– Generate AI Prompt", 
                        variant="primary", 
                        size="lg",
                        scale=1
                    )
                    
                    
                    with gr.Accordion("πŸ’­ Example Ideas", open=False):
                        gr.Examples(
                            examples=[
                                ["A hero image for my new eco-friendly water bottle", "product", "natural", "website"],
                                ["Announcement for our Black Friday sale", "social", "playful", "instagram"],
                                ["Professional headshots for company about page", "marketing", "professional", "linkedin"],
                                ["Blog header about AI in marketing", "blog", "tech", "general"],
                                ["Product showcase for luxury watch collection", "product", "luxury", "instagram"]
                            ],
                            inputs=[ai_user_input, ai_context, ai_style, ai_platform],
                            label=None
                        )

 
                with gr.Column(scale=1, min_width=300):
                    ai_generated_prompt = gr.Textbox(
                        label="AI-Generated Prompt",
                        lines=6,
                        interactive=True,
                        info="Edit this prompt if needed"
                    )
                    
                    ai_status = gr.Textbox(
                        label="Status",
                        lines=2,
                        interactive=False
                    )
                    

                    with gr.Row():
                        ai_use_prompt_btn = gr.Button(
                            "🎨 Generate Image", 
                            variant="primary",
                            scale=2
                        )
                        ai_save_prompt_btn = gr.Button(
                            "πŸ’Ύ Save to Single Tab", 
                            variant="secondary",
                            scale=1
                        )
                    

                    with gr.Accordion("πŸ”§ Advanced Prompt Refinement", open=False):
                        ai_improvement_request = gr.Textbox(
                            label="How to improve this prompt?",
                            placeholder="Example: Add more dramatic lighting, make it more colorful, include people",
                            lines=2
                        )
                        ai_improve_btn = gr.Button(
                            "✨ Improve Prompt", 
                            variant="secondary",
                            size="sm"
                        )
                    

                    ai_preview_image = gr.Image(
                        label="Generated Image Preview",
                        type="filepath",
                        visible=False,
                        height=300
                    )
            
  
            with gr.Accordion("🎯 Pro Tips for Better Results", open=False):
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("""
                        **Be Specific About:**
                        - **Subject**: What's the main focus?
                        - **Setting**: Where is it happening?
                        - **Mood**: What feeling to convey?
                        - **Colors**: Any specific palette?
                        """)
                    with gr.Column():
                        gr.Markdown("""
                        **Good Examples:**
                        - βœ… "Minimalist product photo of smartphone on marble"
                        - βœ… "Vibrant Instagram post for summer sale"
                        - ❌ "Product photo" (too vague)
                        - ❌ "Social media post" (not specific)
                        """)

    # Footer
    gr.Markdown("""
    ---
    ### πŸ› οΈ Powered by:
    - **Flux AI Model** - State-of-the-art image generation
    - **Modal Labs** - GPU infrastructure  
    - **AI Prompt Assistant** - Mistral AI
    - **MCP Protocol** - Tool integration
    - **Gradio** - User interface

    ### πŸ”§ Configuration:
    - **Default Mode**: Uses shared API keys (free, limited usage)
    - **Custom Mode**: Use your own API keys for unlimited usage (see Quick Start tab)

    Made by RajputVansh

    Member of Cynaptics Cub, IIT Indore, India
    - πŸ’Ό **LinkedIn**: [Vansh Ruhela](https://www.linkedin.com/in/vansh-ruhela-707889262/)
    - πŸ“Έ **Instagram**: [@rajputvansh4391](https://www.instagram.com/rajputvansh4391)

    

    **Made with ❀️ for content creators and marketers**
    """)
    
    def update_api_keys(modal_url, mistral_key):
        """Update API keys and restart connection"""
        global user_modal_url, user_mistral_key, marketing_tool
        
        try:
            # Update global variables
            user_modal_url = modal_url.strip() if modal_url else ""
            user_mistral_key = mistral_key.strip() if mistral_key else ""
            
            # Stop current connection
            marketing_tool.request_queue.put("STOP")
            marketing_tool.is_connected = False
            
            # Create new instance
            marketing_tool = MCP_Modal_Marketing_Tool()
            
            # Start new connection with updated keys
            start_mcp_server()
            
            status_msg = "πŸ”„ Keys updated! Restarting connection..."
            if user_modal_url:
                status_msg += f"\nβœ… Using your Modal URL"
            else:
                status_msg += f"\nπŸ”§ Using default Modal URL"
                
            if user_mistral_key:
                status_msg += f"\nβœ… Using your Mistral key"
            else:
                status_msg += f"\nπŸ”§ Using default Mistral key"
                
            return status_msg
            
        except Exception as e:
            return f"❌ Error updating keys: {str(e)}"

    # Event handlers
    single_btn.click(
        single_image_generation,
        inputs=[single_prompt, single_steps, single_style],
        outputs=[single_output, single_status]
    )

    batch_btn.click(
        enhanced_batch_generation,
        inputs=[batch_prompt,batch_variation_type, batch_count, batch_steps],
        outputs=[batch_output, batch_status]
    )
    batch_variation_type.change(
        update_strategy_info,
        inputs=[batch_variation_type],
        outputs=[strategy_info]
        )

    social_btn.click(
        social_media_generation,
        inputs=[social_prompt, social_platforms, social_steps],
        outputs=[social_output, social_status]
    )
    update_keys_btn.click(
        update_api_keys,
        inputs=[modal_url_input, mistral_key_input],
        outputs=[keys_status]
    )


    def generate_ai_prompt(user_input, context, style, platform):
        """Generate an optimized prompt using AI"""
        if not marketing_tool.is_connected:
            return "", "⚠️ MCP Server not connected. Please wait a few seconds and try again."

        if not user_input.strip():
            return "", "⚠️ Please describe what you want to create."

        try:
            request_id = f"ai_prompt_{time.time()}"
            marketing_tool.request_queue.put((
                "generate_prompt_with_ai",
                {
                    "user_input": user_input,
                    "context": context,
                    "style": style,
                    "platform": platform
                    },
                request_id
                ))
            status, result = wait_for_result(request_id, timeout=60)
            if status == "success":
                result_data = json.loads(result)
                if result_data.get("success"):
                    return result_data["prompt"], "βœ… AI prompt generated successfully!"
                else:
                    return result_data.get("fallback_prompt", ""), f"⚠️ Using fallback prompt: {result_data.get('error', 'Unknown error')}"
            else:
                return "", f"❌ Error: {result}"
        except Exception as e:
            return "", f"❌ Error: {str(e)}"
    ai_generate_btn.click(
        generate_ai_prompt,
        inputs=[ai_user_input, ai_context, ai_style, ai_platform],
        outputs=[ai_generated_prompt, ai_status]
    )
    
    def improve_ai_prompt(current_prompt, improvement_request):
        if not marketing_tool.is_connected:
            return current_prompt, "⚠️ MCP Server not connected."
        if not current_prompt.strip():
            return "", "⚠️ No prompt to improve. Generate one first."
        if not improvement_request.strip():
            return current_prompt, "⚠️ Please describe how you'd like to improve the prompt."
        try:
            enhanced_base = f"{current_prompt}. {improvement_request}"
            request_id = f"improve_prompt_{time.time()}"
            marketing_tool.request_queue.put((
                "enhance_prompt_with_details",  # Use the same tool
                {
                    "base_prompt": enhanced_base,
                    "enhancement_type": "detailed"
                    },
                request_id
                ))
            status, result = wait_for_result(request_id, timeout=60)
            if status == "success":
                if not result:
                    return current_prompt, "⚠️ Received empty response from server."
                try:
                    result_data = json.loads(result)
                    if result_data.get("success"):
                        return result_data["enhanced_prompt"], "βœ… Prompt improved successfully!"
                    else:
                        return current_prompt, f"⚠️ Could not improve prompt: {result_data.get('error', 'Unknown error')}"
                except json.JSONDecodeError as json_error:
                    print(f"JSON decode error: {json_error}")
                    print(f"Raw result: {repr(result)}")
                    return result if result else current_prompt, "βœ… Prompt improved (received as text)!"
                
            else:
                return current_prompt, f"❌ Error: {result}"
            
        except Exception as e:
            print(f"Exception in improve_ai_prompt: {str(e)}")
            return current_prompt, f"❌ Error: {str(e)}"
        
    ai_improve_btn.click(
        improve_ai_prompt,
        inputs=[ai_generated_prompt, ai_improvement_request],
        outputs=[ai_generated_prompt, ai_status]
    )
    
    def generate_image_from_ai_prompt(prompt, show_preview=True):
        if not prompt.strip():
            return None, "⚠️ Please generate a prompt first."
        image_path, status = single_image_generation(prompt, 50, "none")
        if show_preview and image_path:
            return gr.update(value=image_path, visible=True), status
        else:
            return gr.update(visible=False), status
        
    ai_use_prompt_btn.click(
        lambda prompt: generate_image_from_ai_prompt(prompt, True),
        inputs=[ai_generated_prompt],
        outputs=[ai_preview_image, ai_status]
    )
    ai_save_prompt_btn.click(
        lambda prompt: (prompt, "βœ… Prompt copied to Single Image tab!"),
        inputs=[ai_generated_prompt],
        outputs=[single_prompt, ai_status]
    ).then(
        lambda: gr.update(selected="πŸ–ΌοΈ Single Image"),
        outputs=[]
    )

    # Update connection status
    def update_connection_status():
        if marketing_tool.is_connected:
            return "βœ… **Connected to MCP Server** - Ready to generate!"
        else:
            return "πŸ”„ Connecting to MCP server... (please wait)"

    # Periodic status update
    demo.load(update_connection_status, outputs=[connection_status])

if __name__ == "__main__":
    print("Starting Marketing Content Generator...")
    print("Please wait for MCP server to initialize...")
    start_mcp_server()
    time.sleep(5)
    print("Launching Gradio interface...")
    demo.launch(share=False, mcp_server=True)