File size: 39,260 Bytes
c9cb3bc
ada947c
c9cb3bc
ada947c
 
c9cb3bc
ada947c
c9cb3bc
ada947c
 
 
 
e3abedf
c9cb3bc
ada947c
 
 
 
 
 
 
 
c9cb3bc
e3abedf
ada947c
f72d856
ada947c
f72d856
ada947c
f72d856
 
ada947c
f72d856
 
 
ada947c
f72d856
 
ada947c
f72d856
 
 
 
 
 
 
 
ada947c
 
f72d856
ada947c
 
f72d856
ada947c
beda045
f72d856
ada947c
f72d856
ada947c
f72d856
ada947c
 
 
f72d856
ada947c
f72d856
ada947c
f72d856
 
ada947c
f72d856
ada947c
f72d856
ada947c
f72d856
 
 
 
ada947c
f72d856
ada947c
 
f72d856
ada947c
beda045
f72d856
ada947c
 
 
f72d856
ada947c
 
 
f72d856
ada947c
f72d856
 
ada947c
f72d856
ada947c
 
 
f72d856
ada947c
f72d856
ada947c
 
f72d856
ada947c
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
 
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
 
 
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
 
ada947c
 
beda045
ada947c
 
 
 
 
beda045
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6f4b8a
ada947c
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6f4b8a
f72d856
f9a1d72
f72d856
ada947c
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9cb3bc
f72d856
 
 
ada947c
f9a1d72
ada947c
 
 
 
 
 
 
 
 
 
 
c9cb3bc
ada947c
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
c9cb3bc
ada947c
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
f72d856
ada947c
 
 
 
 
 
 
f72d856
 
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
f72d856
 
ada947c
 
 
 
f72d856
 
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
 
 
ada947c
 
f72d856
 
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9cb3bc
ada947c
 
 
beda045
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
ada947c
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d856
c9cb3bc
ada947c
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
import gradio as gr
import logging
import json
import re
import torch
import tempfile
import subprocess
from pathlib import Path
from typing import Dict, List, Tuple, Optional, Any
from dataclasses import dataclass
from enum import Enum
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler('gradio_builder.log')
    ]
)
logger = logging.getLogger(__name__)

# Constants and Default Configurations
COMPONENT_CATEGORIES = {
    "Basic": {
        "Textbox": {
            "description": "Text input component",
            "properties": {
                "label": "Text Input",
                "placeholder": "",
                "lines": 1,
                "type": "text"
            },
            "code_snippet": 'gr.Textbox(label="{label}", placeholder="{placeholder}", lines={lines}, type="{type}")'
        },
        "Number": {
            "description": "Numeric input component",
            "properties": {
                "label": "Number Input",
                "value": 0,
                "minimum": None,
                "maximum": None
            },
            "code_snippet": 'gr.Number(label="{label}", value={value}, minimum={minimum}, maximum={maximum})'
        },
        "Button": {
            "description": "Clickable button component",
            "properties": {
                "text": "Button",
                "variant": "primary"
            },
            "code_snippet": 'gr.Button(value="{text}", variant="{variant}")'
        }
    },
    "Media": {
        "Image": {
            "description": "Image display/upload component",
            "properties": {
                "label": "Image",
                "shape": None,
                "image_mode": "RGB",
                "source": "upload",
                "type": "numpy"
            },
            "code_snippet": 'gr.Image(label="{label}", shape={shape}, image_mode="{image_mode}", source="{source}", type="{type}")'
        },
        "Audio": {
            "description": "Audio player/recorder component",
            "properties": {
                "label": "Audio",
                "source": "upload",
                "type": "numpy"
            },
            "code_snippet": 'gr.Audio(label="{label}", source="{source}", type="{type}")'
        },
        "Video": {
            "description": "Video player component",
            "properties": {
                "label": "Video",
                "source": "upload"
            },
            "code_snippet": 'gr.Video(label="{label}", source="{source}")'
        }
    },
    "Selection": {
        "Dropdown": {
            "description": "Dropdown selection component",
            "properties": {
                "label": "Dropdown",
                "choices": [],
                "multiselect": False
            },
            "code_snippet": 'gr.Dropdown(label="{label}", choices={choices}, multiselect={multiselect})'
        },
        "Radio": {
            "description": "Radio button group component",
            "properties": {
                "label": "Radio Group",
                "choices": [],
                "type": "value"
            },
            "code_snippet": 'gr.Radio(label="{label}", choices={choices}, type="{type}")'
        },
        "Checkbox": {
            "description": "Checkbox component",
            "properties": {
                "label": "Checkbox",
                "value": False
            },
            "code_snippet": 'gr.Checkbox(label="{label}", value={value})'
        }
    }
}

TEMPLATE_CATEGORIES = {
    "Basic": {
        "Empty": {
            "description": "Empty application template",
            "components": [],
            "layout": "vertical",
            "css": "",
            "dependencies": []
        },
        "Text Input/Output": {
            "description": "Simple text input/output template",
            "components": [
                {
                    "type": "Textbox",
                    "properties": {
                        "label": "Input",
                        "lines": 3
                    }
                },
                {
                    "type": "Button",
                    "properties": {
                        "text": "Process",
                        "variant": "primary"
                    }
                },
                {
                    "type": "Textbox",
                    "properties": {
                        "label": "Output",
                        "lines": 3
                    }
                }
            ],
            "layout": "vertical",
            "css": "",
            "dependencies": []
        }
    },
    "Media": {
        "Image Processing": {
            "description": "Image processing template",
            "components": [
                {
                    "type": "Image",
                    "properties": {
                        "label": "Input Image",
                        "source": "upload"
                    }
                },
                {
                    "type": "Button",
                    "properties": {
                        "text": "Process",
                        "variant": "primary"
                    }
                },
                {
                    "type": "Image",
                    "properties": {
                        "label": "Output Image",
                        "source": "upload"
                    }
                }
            ],
            "layout": "vertical",
            "css": "",
            "dependencies": ["PIL"]
        }
    }
}

DEFAULT_THEMES = {
    "Light": """
        /* Light theme CSS */
        .gradio-container {
            font-family: 'Arial', sans-serif;
            background-color: #ffffff;
        }
        .gradio-button {
            background-color: #2196F3;
            color: white;
        }
    """,
    "Dark": """
        /* Dark theme CSS */
        .gradio-container {
            font-family: 'Arial', sans-serif;
            background-color: #2c2c2c;
            color: #ffffff;
        }
        .gradio-button {
            background-color: #bb86fc;
            color: black;
        }
    """
}

class ChatRole(Enum):
    SYSTEM = "system"
    USER = "user"
    ASSISTANT = "assistant"

@dataclass
class ChatMessage:
    role: ChatRole
    content: str

class GradioAIBuilder:
    def __init__(self, model_name: str = "HuggingFaceH4/starchat-beta"):
        """Initialize the AI Builder with HuggingFace model"""
        try:
            self.tokenizer = AutoTokenizer.from_pretrained(model_name)
            self.model = AutoModelForCausalLM.from_pretrained(
                model_name,
                torch_dtype=torch.float16,
                device_map="auto",
                trust_remote_code=True
            )
            self.chat_pipeline = pipeline(
                "text-generation",
                model=self.model,
                tokenizer=self.tokenizer,
                max_length=2048,
                do_sample=True,
                temperature=0.7,
                top_p=0.95,
                pad_token_id=self.tokenizer.eos_token_id
            )
            
            self.chat_history: List[ChatMessage] = []
            self.current_app: Dict = {}
            self.error_log: List[str] = []
            
            # Initialize system prompt
            self.initialize_system_prompt()
            
        except Exception as e:
            logger.error(f"Error initializing AI Builder: {str(e)}")
            raise

    def initialize_system_prompt(self):
        """Set up the system prompt for the AI"""
        system_prompt = """You are an expert Gradio application builder. Your role is to:
        1. Understand user requirements and convert them into Gradio components
        2. Generate and modify Python code for Gradio applications
        3. Help users customize their applications through natural language instructions
        4. Provide clear explanations of changes and suggestions for improvements

        When generating code:
        1. Use proper Python and Gradio syntax
        2. Include necessary imports
        3. Structure the code clearly
        4. Add comments for clarity
        5. Handle errors appropriately

        When responding to modifications:
        1. Explain what changes will be made
        2. Show the modified code
        3. Highlight any potential issues
        4. Suggest improvements if applicable

        Available Components:
        {components}

        Available Templates:
        {templates}

        Response Format:
        1. Brief explanation of understanding
        2. Proposed solution
        3. Code block:
        ```python
        # Code here
        ```
        4. Additional explanations or suggestions
        """

        # Format prompt with available components and templates
        components_str = self._format_components_for_prompt()
        templates_str = self._format_templates_for_prompt()

        self.system_prompt = system_prompt.format(
            components=components_str,
            templates=templates_str
        )
        self.chat_history.append(ChatMessage(ChatRole.SYSTEM, self.system_prompt))

    def _format_components_for_prompt(self) -> str:
        """Format component information for the system prompt"""
        components_info = []
        for category, components in COMPONENT_CATEGORIES.items():
            category_info = [f"\n{category}:"]
            for name, info in components.items():
                props = ", ".join(info["properties"].keys())
                category_info.append(f"  - {name}: {info['description']} (Properties: {props})")
            components_info.extend(category_info)
        return "\n".join(components_info)

    def _format_templates_for_prompt(self) -> str:
        """Format template information for the system prompt"""
        templates_info = []
        for category, templates in TEMPLATE_CATEGORIES.items():
            category_info = [f"\n{category}:"]
            for name, info in templates.items():
                category_info.append(f"  - {name}: {info['description']}")
            templates_info.extend(category_info)
        return "\n".join(templates_info)

    async def generate_response(self, user_input: str) -> str:
        """Generate AI response using HuggingFace model"""
        try:
            # Format conversation history
            conversation = self._format_conversation_history()
            conversation += f"\nuser: {user_input}\nassistant:"

            # Generate response
            response = self.chat_pipeline(
                conversation,
                max_new_tokens=1024,
                do_sample=True,
                temperature=0.7,
                top_p=0.95
            )[0]['generated_text']

            # Extract assistant's response
            response = response.split("assistant:")[-1].strip()
            
            # Update chat history
            self.chat_history.append(ChatMessage(ChatRole.USER, user_input))
            self.chat_history.append(ChatMessage(ChatRole.ASSISTANT, response))
            
            return response

        except Exception as e:
            error_msg = f"Error generating response: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            raise

    def _format_conversation_history(self) -> str:
        """Format the conversation history for the model"""
        return "\n".join([
            f"{msg.role.value}: {msg.content}"
            for msg in self.chat_history
        ])

    async def process_user_request(self, user_input: str) -> Dict[str, Any]:
        """Process user request and return appropriate response"""
        try:
            response = await self.generate_response(user_input)
            
            # Extract code if present
            code_blocks = re.findall(r"```python\n(.*?)```", response, re.DOTALL)
            
            result = {
                "response": response,
                "code_changes": code_blocks[0] if code_blocks else None,
                "status": "success",
                "preview_available": bool(code_blocks)
            }

            # If code changes were generated, update the current app
            if code_blocks:
                self.update_current_app(code_blocks[0])

            return result

        except Exception as e:
            error_msg = f"Error processing request: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return {
                "response": error_msg,
                "status": "error",
                "error": str(e)
            }

    def update_current_app(self, code: str):
        """Update the current app with new code"""
        try:
            # Parse the code to extract components and layout
            components = self.extract_components_from_code(code)
            if components:
                self.current_app["components"] = components

            # Update layout if present
            layout_match = re.search(r"with gr\.(Row|Column|Tabs)\(\):", code)
            if layout_match:
                layout_type = layout_match.group(1).lower()
                self.current_app["layout"] = layout_type

            # Extract CSS if present
            css_match = re.search(r'css = """(.*?)"""', code, re.DOTALL)
            if css_match:
                self.current_app["css"] = css_match.group(1)

        except Exception as e:
            error_msg = f"Error updating current app: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            raise

class ComponentManager:
    def __init__(self):
        self.components: List[Dict] = []
        self.error_log: List[str] = []

    def add_component(self, category: str, component_type: str, properties: Dict = None) -> Optional[Dict]:
        """Add a new component with specified properties"""
        try:
            if category not in COMPONENT_CATEGORIES or component_type not in COMPONENT_CATEGORIES[category]:
                raise ValueError(f"Invalid component type: {category}/{component_type}")

            component_info = COMPONENT_CATEGORIES[category][component_type].copy()
            if properties:
                component_info["properties"].update(properties)

            component = {
                "id": len(self.components),
                "category": category,
                "type": component_type,
                "properties": component_info["properties"]
            }
            
            self.components.append(component)
            return component

        except Exception as e:
            error_msg = f"Error adding component: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return None

    def update_component(self, component_id: int, properties: Dict) -> bool:
        """Update properties of an existing component"""
        try:
            if not 0 <= component_id < len(self.components):
                raise ValueError(f"Invalid component ID: {component_id}")

            component = self.components[component_id]
            component["properties"].update(properties)
            return True

        except Exception as e:
            error_msg = f"Error updating component: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return False

    def remove_component(self, component_id: int) -> bool:
        """Remove a component by ID"""
        try:
            if not 0 <= component_id < len(self.components):
                raise ValueError(f"Invalid component ID: {component_id}")

            self.components.pop(component_id)
            # Update remaining component IDs
            for i, component in enumerate(self.components):
                component["id"] = i
            return True

        except Exception as e:
            error_msg = f"Error removing component: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return False

    def get_component(self, component_id: int) -> Optional[Dict]:
        """Get component by ID"""
        try:
            if not 0 <= component_id < len(self.components):
                raise ValueError(f"Invalid component ID: {component_id}")
            return self.components[component_id]

        except Exception as e:
            error_msg = f"Error getting component: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return None

    def get_components_json(self) -> str:
        """Get all components as JSON string"""
        try:
            return json.dumps(self.components, indent=2)
        except Exception as e:
            error_msg = f"Error converting components to JSON: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return "[]"

    def load_components_from_json(self, components_json: str) -> bool:
        """Load components from JSON string"""
        try:
            if isinstance(components_json, str):
                components = json.loads(components_json)
            else:
                components = components_json
                
            self.components = components
            return True

        except Exception as e:
            error_msg = f"Error loading components from JSON: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return False

class TemplateManager:
    def __init__(self):
        self.templates = TEMPLATE_CATEGORIES
        self.error_log: List[str] = []

    def get_template(self, category: str, template_name: str) -> Optional[Dict]:
        """Get a specific template by category and name"""
        try:
            if category not in self.templates or template_name not in self.templates[category]:
                raise ValueError(f"Invalid template: {category}/{template_name}")
            return self.templates[category][template_name]
        except Exception as e:
            error_msg = f"Error getting template: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return None

    def add_custom_template(self, category: str, template_name: str, template_data: Dict) -> bool:
        """Add a new custom template"""
        try:
            if not all(key in template_data for key in ["description", "components", "layout", "css", "dependencies"]):
                raise ValueError("Invalid template data structure")

            if category not in self.templates:
                self.templates[category] = {}
            
            self.templates[category][template_name] = template_data
            return True
        except Exception as e:
            error_msg = f"Error adding custom template: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return False

    def get_template_list(self) -> List[Dict]:
        """Get list of all available templates"""
        template_list = []
        for category, templates in self.templates.items():
            for name, data in templates.items():
                template_list.append({
                    "category": category,
                    "name": name,
                    "description": data["description"]
                })
        return template_list

class CodeGenerator:
    def __init__(self, template_manager: TemplateManager):
        self.template_manager = template_manager
        self.error_log: List[str] = []

    def generate_app_code(self, components: List[Dict], layout: str = "vertical", 
                         theme: str = "Light", css: str = "") -> str:
        """Generate complete Gradio application code"""
        try:
            code_parts = []
            
            # Add imports
            code_parts.append(self._generate_imports(components))
            
            # Add any required helper functions
            code_parts.append(self._generate_helper_functions(components))
            
            # Generate the main app code
            code_parts.append(self._generate_main_app_code(components, layout, theme, css))
            
            return "\n\n".join(code_parts)
        except Exception as e:
            error_msg = f"Error generating app code: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return ""

    def _generate_imports(self, components: List[Dict]) -> str:
        """Generate necessary import statements"""
        imports = ["import gradio as gr"]
        
        # Add additional imports based on components
        additional_imports = set()
        for component in components:
            if component["type"] == "Image":
                additional_imports.add("from PIL import Image")
            elif component["type"] in ["Audio", "Video"]:
                additional_imports.add("import numpy as np")

        return "\n".join(imports + sorted(list(additional_imports)))

    def _generate_helper_functions(self, components: List[Dict]) -> str:
        """Generate helper functions needed by components"""
        helper_functions = []
        
        # Add processing functions based on component types
        processing_functions = set()
        for component in components:
            if component["type"] == "Image":
                processing_functions.add("""
def process_image(image):
    \"\"\"Process the input image\"\"\"
    # Add your image processing logic here
    return image
""")
            elif component["type"] == "Audio":
                processing_functions.add("""
def process_audio(audio):
    \"\"\"Process the input audio\"\"\"
    # Add your audio processing logic here
    return audio
""")

        return "\n".join(helper_functions + sorted(list(processing_functions)))

    def _generate_main_app_code(self, components: List[Dict], layout: str,
                              theme: str, css: str) -> str:
        """Generate the main application code"""
        code_lines = [
            "# Create Gradio interface",
            "with gr.Blocks(theme=gr.themes.Default(), css=css) as demo:",
            "    gr.Markdown(\"# Gradio Application\")\n"
        ]

        # Add CSS
        if css:
            code_lines.insert(0, f'css = """{css}"""\n')
        
        # Generate layout
        if layout == "horizontal":
            code_lines.append("    with gr.Row():")
            indent = "        "
        else:
            code_lines.append("    with gr.Column():")
            indent = "        "

        # Add components
        component_vars = []
        for i, component in enumerate(components):
            var_name = f"{component['type'].lower()}_{i}"
            component_vars.append(var_name)
            
            props = ", ".join([f"{k}={repr(v)}" for k, v in component["properties"].items()])
            code_lines.append(f"{indent}{var_name} = gr.{component['type']}({props})")

        # Add event handlers
        if len(component_vars) > 1:
            code_lines.extend(self._generate_event_handlers(component_vars))

        # Add launch code
        code_lines.extend([
            "\n# Launch the application",
            "if __name__ == \"__main__\":",
            "    demo.launch()"
        ])

        return "\n".join(code_lines)

    def _generate_event_handlers(self, component_vars: List[str]) -> List[str]:
        """Generate event handlers for components"""
        handlers = []
        
        # Look for input/output pairs
        for i in range(len(component_vars) - 1):
            if "button" in component_vars[i]:
                handlers.extend([
                    f"\n    def process_{i}({component_vars[i-1]}):",
                    f"        # Add your processing logic here",
                    f"        return {component_vars[i-1]}",
                    f"\n    {component_vars[i]}.click(",
                    f"        fn=process_{i},",
                    f"        inputs=[{component_vars[i-1]}],",
                    f"        outputs=[{component_vars[i+1]}]",
                    f"    )"
                ])

        return handlers

class AppBuilder:
    def __init__(self):
        """Initialize the AppBuilder with necessary managers"""
        self.component_manager = ComponentManager()
        self.template_manager = TemplateManager()
        self.code_generator = CodeGenerator(self.template_manager)
        self.ai_builder = None  # Will be initialized later if needed
        self.current_app = {
            "components": [],
            "layout": "vertical",
            "theme": "Light",
            "css": DEFAULT_THEMES["Light"]
        }
        self.error_log: List[str] = []

    def create_app_from_template(self, category: str, template_name: str) -> bool:
        """Create a new app from a template"""
        try:
            template = self.template_manager.get_template(category, template_name)
            if not template:
                raise ValueError(f"Template not found: {category}/{template_name}")

            self.current_app = {
                "components": template["components"],
                "layout": template["layout"],
                "theme": "Light",
                "css": template.get("css", DEFAULT_THEMES["Light"])
            }
            
            self.component_manager.load_components_from_json(template["components"])
            return True
        except Exception as e:
            error_msg = f"Error creating app from template: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return False

    def update_app_layout(self, layout: str) -> bool:
        """Update the application layout"""
        try:
            if layout not in ["vertical", "horizontal", "tabs"]:
                raise ValueError(f"Invalid layout: {layout}")
            
            self.current_app["layout"] = layout
            return True
        except Exception as e:
            error_msg = f"Error updating layout: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return False

    def update_app_theme(self, theme: str) -> bool:
        """Update the application theme"""
        try:
            if theme not in DEFAULT_THEMES:
                raise ValueError(f"Invalid theme: {theme}")
            
            self.current_app["theme"] = theme
            self.current_app["css"] = DEFAULT_THEMES[theme]
            return True
        except Exception as e:
            error_msg = f"Error updating theme: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return False

    def generate_app_code(self) -> str:
        """Generate the complete application code"""
        try:
            return self.code_generator.generate_app_code(
                self.current_app["components"],
                self.current_app["layout"],
                self.current_app["theme"],
                self.current_app["css"]
            )
        except Exception as e:
            error_msg = f"Error generating app code: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return ""

    def preview_app(self) -> Optional[gr.Blocks]:
        """Generate and preview the current application"""
        try:
            code = self.generate_app_code()
            if not code:
                raise ValueError("No code generated")

            # Create temporary file
            with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
                f.write(code)
                temp_file = f.name

            # Execute the code in a separate process
            result = subprocess.run(['python', temp_file], capture_output=True, text=True)
            
            if result.returncode != 0:
                raise ValueError(f"Error executing code: {result.stderr}")

            return True
        except Exception as e:
            error_msg = f"Error previewing app: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            return None

class GradioInterface:
    def __init__(self):
        """Initialize the Gradio interface"""
        self.app_builder = AppBuilder()
        self.current_tab = "builder"
        self.error_log: List[str] = []

    def create_interface(self) -> gr.Blocks:
        """Create the main Gradio interface"""
        try:
            with gr.Blocks(theme=gr.themes.Default()) as interface:
                gr.Markdown("# 🚀 Advanced Gradio App Builder")
                
                with gr.Tabs() as tabs:
                    # Component Builder Tab
                    with gr.TabItem("🔧 Builder"):
                        self._create_builder_tab()
                    
                    # Code Editor Tab
                    with gr.TabItem("📝 Code"):
                        self._create_code_tab()
                    
                    # Preview Tab
                    with gr.TabItem("👁️ Preview"):
                        self._create_preview_tab()
                    
                    # AI Assistant Tab
                    with gr.TabItem("🤖 AI Assistant"):
                        self._create_ai_tab()
                    
                    # Settings Tab
                    with gr.TabItem("⚙️ Settings"):
                        self._create_settings_tab()

                return interface

        except Exception as e:
            error_msg = f"Error creating interface: {str(e)}"
            logger.error(error_msg)
            self.error_log.append(error_msg)
            raise

    def _create_builder_tab(self):
        """Create the component builder tab"""
        with gr.Row():
            # Left Column - Component Selection
            with gr.Column(scale=1):
                gr.Markdown("### 📦 Components")
                category = gr.Dropdown(
                    choices=list(COMPONENT_CATEGORIES.keys()),
                    label="Category",
                    value="Basic"
                )
                component_type = gr.Dropdown(
                    choices=list(COMPONENT_CATEGORIES["Basic"].keys()),
                    label="Component Type"
                )
                add_component = gr.Button("Add Component", variant="primary")

            # Middle Column - Component Properties
            with gr.Column(scale=2):
                gr.Markdown("### ⚙️ Properties")
                properties_json = gr.JSON(label="Component Properties")
                update_properties = gr.Button("Update Properties")

            # Right Column - Component List
            with gr.Column(scale=1):
                gr.Markdown("### 📋 Current Components")
                component_list = gr.JSON(label="Components")
                remove_component = gr.Button("Remove Selected", variant="stop")

        # Template Selection
        with gr.Row():
            gr.Markdown("### 📑 Templates")
            template_category = gr.Dropdown(
                choices=list(TEMPLATE_CATEGORIES.keys()),
                label="Template Category"
            )
            template_name = gr.Dropdown(
                choices=list(TEMPLATE_CATEGORIES["Basic"].keys()),
                label="Template"
            )
            load_template = gr.Button("Load Template", variant="primary")

        # Event handlers
        def update_component_choices(category):
            return gr.Dropdown(choices=list(COMPONENT_CATEGORIES[category].keys()))

        category.change(
            update_component_choices,
            inputs=[category],
            outputs=[component_type]
        )

        def add_new_component(category, comp_type):
            success = self.app_builder.component_manager.add_component(category, comp_type)
            components = self.app_builder.component_manager.get_components_json()
            return components

        add_component.click(
            add_new_component,
            inputs=[category, component_type],
            outputs=[component_list]
        )

        def update_component_properties(props_json):
            try:
                props = json.loads(props_json)
                success = self.app_builder.component_manager.update_component(
                    props["id"], props["properties"]
                )
                return self.app_builder.component_manager.get_components_json()
            except Exception as e:
                logger.error(f"Error updating properties: {str(e)}")
                return None

        update_properties.click(
            update_component_properties,
            inputs=[properties_json],
            outputs=[component_list]
        )

    def _create_code_tab(self):
        """Create the code editor tab"""
        with gr.Row():
            with gr.Column():
                code_editor = gr.Code(
                    label="Application Code",
                    language="python",
                    value="# Your Gradio app code will appear here"
                )
                update_code = gr.Button("Update Code")
                
            with gr.Column():
                code_output = gr.Markdown("Code validation output will appear here")

        def update_app_code():
            try:
                code = self.app_builder.generate_app_code()
                return code, "✅ Code generated successfully"
            except Exception as e:
                return None, f"❌ Error generating code: {str(e)}"

        update_code.click(
            update_app_code,
            outputs=[code_editor, code_output]
        )

    def _create_preview_tab(self):
        """Create the preview tab"""
        with gr.Row():
            preview_button = gr.Button("Generate Preview", variant="primary")
            preview_status = gr.Markdown("Click button to generate preview")

        with gr.Row():
            preview_frame = gr.HTML(label="App Preview")

        def generate_preview():
            try:
                success = self.app_builder.preview_app()
                if success:
                    return "✅ Preview generated successfully"
                return "❌ Error generating preview"
            except Exception as e:
                return f"❌ Error: {str(e)}"

        preview_button.click(
            generate_preview,
            outputs=[preview_status]
        )

    def _create_ai_tab(self):
        """Create the AI assistant tab"""
        with gr.Row():
            with gr.Column(scale=2):
                chat_history = gr.Chatbot(label="Chat History")
                user_input = gr.Textbox(
                    label="Your Instructions",
                    placeholder="Describe what you want to build or modify...",
                    lines=3
                )
                with gr.Row():
                    send_btn = gr.Button("Send", variant="primary")
                    clear_btn = gr.Button("Clear Chat")
            
            with gr.Column(scale=1):
                suggestions = gr.JSON(label="Suggested Components")
                preview_btn = gr.Button("Update Preview")
                preview_frame = gr.HTML(label="Live Preview")

        chat_error = gr.Markdown(visible=False)

        async def process_chat(message, history):
            if not self.app_builder.ai_builder:
                self.app_builder.ai_builder = GradioAIBuilder()
            
            try:
                result = await self.app_builder.ai_builder.process_user_request(message)
                
                if result["status"] == "error":
                    return (
                        history + [[message, "Error: " + result["response"]]],
                        [],
                        gr.Markdown(f"⚠️ {result['error']}", visible=True)
                    )
                
                new_history = history + [[message, result["response"]]]
                
                return new_history, [], gr.Markdown(visible=False)
            
            except Exception as e:
                error_msg = f"Error processing chat: {str(e)}"
                logger.error(error_msg)
                return (
                    history + [[message, "An error occurred"]],
                    [],
                    gr.Markdown(f"⚠️ {error_msg}", visible=True)
                )

        def clear_chat_history():
            if self.app_builder.ai_builder:
                self.app_builder.ai_builder.chat_history = []
            return [], [], gr.Markdown(visible=False)

        send_btn.click(
            process_chat,
            inputs=[user_input, chat_history],
            outputs=[chat_history, suggestions, chat_error]
        )
        
        clear_btn.click(
            clear_chat_history,
            outputs=[chat_history, suggestions, chat_error]
        )

    def _create_settings_tab(self):
        """Create the settings tab"""
        with gr.Row():
            with gr.Column():
                gr.Markdown("### 🎨 Appearance")
                theme_dropdown = gr.Dropdown(
                    choices=list(DEFAULT_THEMES.keys()),
                    label="Theme",
                    value="Light"
                )
                layout_dropdown = gr.Dropdown(
                    choices=["vertical", "horizontal", "tabs"],
                    label="Layout",
                    value="vertical"
                )
                
            with gr.Column():
                gr.Markdown("### 📝 Custom CSS")
                css_editor = gr.Code(
                    label="Custom CSS",
                    language="css",
                    value=DEFAULT_THEMES["Light"]
                )

        update_settings = gr.Button("Apply Settings", variant="primary")

        def update_app_settings(theme, layout, css):
            try:
                self.app_builder.update_app_theme(theme)
                self.app_builder.update_app_layout(layout)
                self.app_builder.current_app["css"] = css
                return "✅ Settings updated successfully"
            except Exception as e:
                return f"❌ Error updating settings: {str(e)}"

        update_settings.click(
            update_app_settings,
            inputs=[theme_dropdown, layout_dropdown, css_editor],
            outputs=[gr.Markdown()]
        )

def main():
    """Main execution function"""
    try:
        interface = GradioInterface()
        demo = interface.create_interface()
        demo.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=True,
            debug=True
        )
    except Exception as e:
        logger.error(f"Application failed to start: {str(e)}")
        raise

if __name__ == "__main__":
    main()