File size: 38,255 Bytes
3fcec00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import streamlit as st
import pandas as pd
import csv
import re
import plotly.express as px
from scipy.stats import skew
from prompt_models import *
from execution_programs import *
from llm_models import text_llm, code_llm

st.set_page_config(layout="wide")
    
# Custom style
st.markdown("""
<style>
    .block-container {
        padding-top: 1rem;
        padding-bottom: 1rem;
        padding-left: 0rem;
        padding-right: 0rem;
    }
    .element-container:has(.stColumn) {
        gap: 0rem !important;
    }
    div[data-testid="column"] {
        padding-left: 0rem !important;
        padding-right: 0rem !important;
    }
    .stApp {
        max-width: 1200px;
        margin: 0 auto;
        padding: 1rem;
    }
    .cell {
        border: 1px solid #e0e0e0;
        border-radius: 4px;
        padding: 1rem;
        margin-bottom: 1.5rem;
        background-color: #f9f9f9;
    }
    .output {
        border-left: 3px solid #4285f4;
        padding-left: 1rem;
        margin-top: 0.5rem;
    }
    .input-area {
        background-color: #f5f5f5;
        padding: 1rem;
        border-radius: 4px;
        margin-bottom: 1rem;
    }
    .stButton > button {
        padding: 0rem 0.5rem; 
        border-radius: 5px; 
        font-size: 16px; 
        font-weight: bold; 
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); 
        transition: all 0.3s ease; 
    }

    .stButton > button:hover {
        background-color: #357ae8; 
        border-color: #357ae8; 
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15); 
    }
    .history-item {
        margin-bottom: 2rem;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    }
    .chat-message {
        padding: 1rem;
        border-radius: 0.5rem;
        margin-bottom: 1rem;
        display: flex;
    }
    .chat-message.user {
        background-color: #262626;
        align-self: flex-start;  
    }
    .chat-message.assistant {
        background-color: #262626;
        align-self: flex-end; 
    }
    .chat-message {
            font-size: 14px; 
    }
    .chat-message.user {
            font-size: 16px;  
    }
    .chat-message.assistant {
            font-size: 14px; 
    }
    .sidebar {
        width: 300px;
        padding-right: 2rem;
    }
    .delete-btn {
        background-color: #ff4b4b !important;
        color: white !important;
        border: none !important;
        padding: 0.25rem 0.5rem !important;
        font-size: 0.8rem !important;
        margin-top: 0.5rem;
    }
    .delete-btn:hover {
        background-color: #ff0000 !important;
    }
</style>
""", unsafe_allow_html=True)

# Initialize session state
if 'execution_history' not in st.session_state:
    st.session_state.execution_history = []
if 'df' not in st.session_state:
    st.session_state.df = None


# Main app layout
col1a, col2a = st.columns([4, 7], gap="small")
st.markdown("<div style='margin-top: 5px'></div>", unsafe_allow_html=True)
show_col1 = st.toggle("Ask chat bot", value=True)

if show_col1:
    col1a, col2a = st.columns([4,7]) 
else:
    _ , col2a = st.columns([0.0001, 2]) 
    
with col1a:
  if show_col1:
    with st.container(height=570):
        st.header("πŸ’¬ Chatbot Assistant")
        
        # Initial state
        if 'chatbot' not in st.session_state:
            st.session_state.chatbot = {
                'chat_history': [],
                'prev_df': None,
                'prev_raw_dataframes': None
            }
        
        # button clear all chat
        if st.button("πŸ—‘οΈ Clear all chat", key="clear_all_chat"):
            st.session_state.chatbot['chat_history'] = []
        
        # show history chatbot
        for i in range(0, len(st.session_state.chatbot['chat_history']), 2):
            user_msg = st.session_state.chatbot['chat_history'][i]
            assistant_msg = st.session_state.chatbot['chat_history'][i+1] if i+1 < len(st.session_state.chatbot['chat_history']) else None

            with st.expander(f"Chat {i//2 + 1}", expanded=False):
                with st.chat_message("user"):
                    st.markdown(user_msg["content"])
                if assistant_msg:
                    with st.chat_message("assistant"):
                        st.markdown(assistant_msg["content"])

        
        # Input chat
        user_query = st.chat_input("Ask coding questions...", key="chatbot_input")

        if user_query:
            # save previous state
            st.session_state.chatbot['prev_df'] = st.session_state.get('df')
            st.session_state.chatbot['prev_raw_dataframes'] = st.session_state.get('raw_dataframes')
            
            # show chat user
            with st.chat_message("user"):
                st.markdown(user_query)
            
            # add to history chat
            st.session_state.chatbot['chat_history'].append({"role": "user", "content": user_query})
            
            with st.spinner("Thinking..."):
                if "df" in st.session_state:
                    try:
                        df = st.session_state.df
                        columns = df.columns
                        shape = df.shape
                        dtypes = df.dtypes
                        response = text_llm(prompt_chatbot(user_query,st.session_state.df.columns, st.session_state.df.shape, st.session_state.df.dtypes))
                        clean_response = re.sub(r'<\s*think\s*>.*?<\s*/\s*think\s*>', '', response, flags=re.DOTALL | re.IGNORECASE)
                        
                        # show and save chatbot response
                        with st.chat_message("assistant"):
                            st.markdown(clean_response)
                        st.session_state.chatbot['chat_history'].append({"role": "assistant", "content": clean_response})
                    except AttributeError as e:
                        st.error(f"Please confirm dataset before starting the chat: {e}")
                else:
                    st.write('You need to upload the data first')

with col2a:  # Sidebar for chatbot
    with st.container(height=570):
        st.header("πŸ“Š DataCoder AI")
        st.markdown("Analyze your CSV data with LLM Models")

        def reset_data_state():
            st.session_state.df = None
            st.session_state.raw_dataframes = None
            st.session_state.preview_df = None
            st.session_state.file_names = [f.name for f in uploaded_files] if uploaded_files else []

        # Initialize session state if not already present
        if 'df' not in st.session_state:
            st.session_state.df = None

        uploaded_files = st.file_uploader(
            "Upload CSV file(s)", 
            type=["csv"], 
            accept_multiple_files=True,
            help="Upload one or more CSV files to combine"
        )

        if uploaded_files:
            # Reset processing if files change
            if 'prev_uploaded_files' not in st.session_state or st.session_state.prev_uploaded_files != [f.name for f in uploaded_files]:
                st.session_state.df = None
                st.session_state.raw_dataframes = None
                st.session_state.preview_df = None
                st.session_state.prev_uploaded_files = [f.name for f in uploaded_files]
            
            # Process files but don't merge yet
            if st.session_state.raw_dataframes is None:
                dataframes = []
                processed_files = 0
                
                for uploaded_file in uploaded_files:
                    with st.expander(f"πŸ“„ File: {uploaded_file.name}", expanded=False):
                        try:
                            # Try to detect delimiter
                            raw_data = uploaded_file.getvalue().decode("utf-8")
                            sniffer = csv.Sniffer()
                            
                            try:
                                dialect = sniffer.sniff(raw_data.splitlines()[0])
                                delimiter = dialect.delimiter
                            except (csv.Error, IndexError):
                                # Try common delimiters if sniffing fails
                                for test_delim in [',', ';', '\t', '|']:
                                    if test_delim in raw_data:
                                        delimiter = test_delim
                                        break
                                else:
                                    delimiter = ','  # default
                            
                            uploaded_file.seek(0)  # Reset file pointer
                            
                            # Read CSV with error handling
                            try:
                                df = pd.read_csv(uploaded_file, delimiter=delimiter, on_bad_lines='warn')
                            except Exception as e:
                                st.warning(f"Using Python engine for {uploaded_file.name} due to parsing issues")
                                df = pd.read_csv(uploaded_file, delimiter=delimiter, engine='python')
                            
                            # Display file info
                            st.write(f"Shape: {df.shape}")
                            st.write(f"Detected delimiter: '{delimiter}'")
                            st.dataframe(df.head(5), use_container_width=True)
                            
                            dataframes.append(df)
                            processed_files += 1
                            
                        except Exception as e:
                            st.error(f"Error processing {uploaded_file.name}: {str(e)}")
                            continue
                
                if processed_files == 0:
                    st.error("No files were successfully processed")
                    st.stop()
                
                # Store raw dataframes in session state
                st.session_state.raw_dataframes = dataframes
            
            # Only proceed if we have raw dataframes
            if st.session_state.raw_dataframes:
                dataframes = st.session_state.raw_dataframes
                
                col1, col2 = st.columns([3, 1])
                
                with col1:
                    merge_option = st.radio(
                        "Merge method:",
                        ("single data","Horizontal (concat columns)", "Vertical (concat rows)", "Horizontal (merge on common columns)"),
                        index=0,
                        horizontal=True,
                        key="merge_option"
                    )
                    
                    # Additional merge parameters
                    if merge_option == "Horizontal (merge on common columns)":
                        if len(dataframes) > 0:
                            common_cols = set(dataframes[0].columns)
                            for df in dataframes[1:]:
                                common_cols.intersection_update(df.columns)
                            
                            if common_cols:
                                selected_cols = st.multiselect(
                                    "Select columns to merge on:",
                                    options=list(common_cols),
                                    default=list(common_cols),
                                    key="merge_columns"
                                )
                                how_merge = st.selectbox(
                                    "Merge type:",
                                    ["inner", "outer", "left", "right"],
                                    index=1,
                                    key="how_merge"
                                )
                            else:
                                None
                
                with col2:
                    preview_button = st.button("πŸ”„ Preview")
                    confirm_button = st.button("βœ… Confirm")
                    reset_btn = st.button("πŸ”„ Reset")
                
                # Reset logic
                if reset_btn:
                    reset_data_state()
                    st.rerun()
                
                # Preview logic
                if preview_button:
                    with st.spinner("Generating preview..."):
                        try:
                            if merge_option == "single data":
                                preview_df = dataframes[0]
                            elif merge_option == "Horizontal (concat columns)":
                                # Find common indices across ALL dataframes
                                common_indices = dataframes[0].index
                                for df in dataframes[1:]:
                                    common_indices = common_indices.intersection(df.index)
                                
                                if len(common_indices) == 0:
                                    st.error("No common indices found across all dataframes")
                                    st.stop()
                                
                                # Filter all dataframes to only common indices
                                filtered_dfs = [df.loc[common_indices] for df in dataframes]
                                
                                # Handle duplicate column names
                                all_columns = []
                                duplicate_counter = {}
                                
                                for i, df in enumerate(filtered_dfs):
                                    new_columns = []
                                    for col in df.columns:
                                        if col in all_columns:
                                            if col not in duplicate_counter:
                                                duplicate_counter[col] = 1
                                            duplicate_counter[col] += 1
                                            new_col = f"{col}_df{duplicate_counter[col]}"
                                            new_columns.append(new_col)
                                        else:
                                            new_columns.append(col)
                                    all_columns.extend(new_columns)
                                    filtered_dfs[i].columns = new_columns
                                
                                # Concatenate horizontally
                                preview_df = pd.concat(filtered_dfs, axis=1)
                                
                            elif merge_option == "Vertical (concat rows)":
                                preview_df = pd.concat(dataframes, axis=0, ignore_index=True)
                                
                            elif merge_option == "Horizontal (merge on common columns)":
                                def auto_merge_many(dfs):
                                    if not dfs:
                                        return None
                                    
                                    merged_df = dfs[0]
                                    
                                    for next_df in dfs[1:]:
                                        common_keys = list(set(merged_df.columns) & set(next_df.columns))
                                        if common_keys:
                                            merged_df = pd.merge(merged_df, next_df, on=common_keys)
                                        else:
                                            print(f"[WARNING] There is no matching key between:\n{merged_df.columns}\n&\n{next_df.columns}")
                                    
                                    return merged_df
                                
                                preview_df = auto_merge_many(dataframes)
                            
                            st.session_state.preview_df = preview_df
                            st.success("Preview generated!")
                            
                            # Show preview stats
                            st.subheader("Preview Results")
                            st.write(f"Shape: {preview_df.shape}")
                            
                            # Show sample data with tabs
                            tab1, tab2 = st.tabs(["First Rows", "Last Rows"])
                            with tab1:
                                st.dataframe(preview_df.head(20), use_container_width=True)
                            with tab2:
                                st.dataframe(preview_df.tail(20), use_container_width=True)
                            
                            # Show quick stats
                            with st.expander("Preview Statistics"):
                                st.write("Column types:")
                                st.dataframe(preview_df.dtypes.astype(str).reset_index().rename(
                                    columns={'index': 'Column', 0: 'DataType'}
                                ))
                                
                                st.write("Missing values:")
                                missing = preview_df.isnull().sum()
                                missing = missing[missing > 0].reset_index().rename(
                                    columns={'index': 'Column', 0: 'Missing Count'}
                                )
                                if len(missing) > 0:
                                    st.dataframe(missing)
                                else:
                                    st.success("No missing values found!")
                        
                        except Exception as e:
                            st.error(f"Preview failed: {str(e)}")
                            st.stop()
                
                # Confirm merge logic
                if confirm_button and st.session_state.preview_df is not None:
                    st.session_state.df = st.session_state.preview_df
                    st.session_state.preview_df = None  # Clear preview after confirmation
                    st.success("Data is now available for analysis.")
                    st.balloons()
                    st.rerun()
                
                # Show raw data info
                with st.expander("πŸ“¦ Raw Data Summary", expanded=False):
                    st.write(f"Total files loaded: {len(dataframes)}")
                    for i, df in enumerate(dataframes, 1):
                        st.write(f"### Dataframe {i}")
                        st.write(f"- Shape: {df.shape}")
                        st.write("- Columns:")
                        st.dataframe(pd.DataFrame({
                            'Column': df.columns,
                            'Type': df.dtypes.values,
                            'Missing %': (df.isnull().mean() * 100).round(2)
                        }), hide_index=True)
                        
                        if st.checkbox(f"Show sample data for Dataframe {i}", key=f"show_raw_{i}"):
                            st.dataframe(df.head(5), use_container_width=True)
            
            # If merge is already confirmed, show the final data
            if st.session_state.df is not None:
                
                # Data cleaning options (only after merge is confirmed)
                with st.expander("**🧹 Data Cleaning**", expanded=True):
                    # Inisialisasi df_cleaned di session state jika belum ada

                    st.session_state.df_cleaned = st.session_state.df.copy()
                    
                    clean_options = st.columns(4)
                    
                    with clean_options[0]:
                        clean_nan = st.selectbox(
                            "Handle NaN values:",
                            ["Keep", "Remove rows", "Fill with zero", "Fill with mean"],
                            index=0,
                            key="clean_nan"
                        )
                        
                        if st.button("Apply NaN Handling"):
                            if clean_nan == "Fill with zero":
                                st.session_state.df_cleaned = st.session_state.df_cleaned.fillna(0)
                                st.success("Filled NaN with zeros!")
                            elif clean_nan == "Fill with mean":
                                st.session_state.df_cleaned = st.session_state.df_cleaned.fillna(st.session_state.df_cleaned.mean())
                                st.success("Filled NaN with mean values!")
                            elif clean_nan == "Remove rows":
                                initial_rows = len(st.session_state.df_cleaned)
                                st.session_state.df_cleaned = st.session_state.df_cleaned.dropna()
                                removed_nan = initial_rows - len(st.session_state.df_cleaned)
                                st.success(f"Removed {removed_nan} rows with NaN values!")
                    
                    with clean_options[1]:
                        clean_duplicates = st.selectbox(
                            "Handle duplicates:",
                            ["Keep", "Remove all", "Keep first", "Keep last"],
                            index=0,
                            key="clean_duplicates"
                        )
                        
                        if st.button("Apply Deduplication"):
                            if clean_duplicates != "Keep":
                                initial_rows = len(st.session_state.df_cleaned)
                                if clean_duplicates == "Remove all":
                                    st.session_state.df_cleaned = st.session_state.df_cleaned.drop_duplicates(keep=False)
                                elif clean_duplicates == "Keep first":
                                    st.session_state.df_cleaned = st.session_state.df_cleaned.drop_duplicates(keep='first')
                                elif clean_duplicates == "Keep last":
                                    st.session_state.df_cleaned = st.session_state.df_cleaned.drop_duplicates(keep='last')
                                
                                removed_dups = initial_rows - len(st.session_state.df_cleaned)
                                st.success(f"Removed {removed_dups} duplicate rows!")
                    
                    with clean_options[2]:
                        col_rename = st.text_input("Rename ('old:new')")
                        if st.button("Rename Column") and col_rename and ":" in col_rename:
                            old, new = col_rename.split(":", 1)
                            if old in st.session_state.df_cleaned.columns:
                                st.session_state.df_cleaned = st.session_state.df_cleaned.rename(columns={old: new})
                                st.success(f"Renamed column '{old}' to '{new}'")
                            else:
                                st.warning(f"Column '{old}' not found")
                                
                    with clean_options[3]:
                        # Fitur Drop Columns
                        cols_to_drop = st.text_input(
                            "Drop columns", 
                            placeholder="column1, column2, ...",
                            key="drop_cols_input"
                        )
                        
                        if st.button("Drop Columns"):
                            if cols_to_drop:
                                # Proses input: hilangkan spasi, split by comma, dan bersihkan
                                columns_list = [col.strip() for col in cols_to_drop.split(",") if col.strip()]
                                
                                # Fungsi untuk mencocokkan nama kolom dengan fleksibilitas
                                def find_matching_column(col_name, df_columns):
                                    col_name = col_name.lower().strip()
                                    for df_col in df_columns:
                                        if df_col.lower().strip() == col_name:
                                            return df_col
                                    return None
                                
                                # Cari kolom yang cocok
                                matched_cols = []
                                not_found = []
                                
                                for col in columns_list:
                                    matched = find_matching_column(col, st.session_state.df_cleaned.columns)
                                    if matched:
                                        matched_cols.append(matched)
                                    else:
                                        not_found.append(col)
                                
                                # Drop kolom yang ditemukan
                                if matched_cols:
                                    initial_cols = st.session_state.df_cleaned.columns.tolist()
                                    st.session_state.df_cleaned = st.session_state.df_cleaned.drop(columns=matched_cols)
                                    st.success(f"Successfully dropped columns: {', '.join(matched_cols)}")
                                    
                                    # Tampilkan warning untuk kolom yang tidak ditemukan
                                    if not_found:
                                        st.warning(f"Columns not found: {', '.join(not_found)}")
                                    
                                    # Tampilkan perubahan
                                    st.write(f"Remaining columns: {len(st.session_state.df_cleaned.columns)}")
                                else:
                                    st.error("No matching columns found to drop")
                            else:
                                st.warning("Please enter column names to drop")

                    # Tampilkan info missing values setelah cleaning
                    missing_values = st.session_state.df_cleaned.isnull().sum().sum()
                    duplicate_values = st.session_state.df_cleaned.duplicated().sum().sum()
                    if missing_values > 0 or duplicate_values > 0:
                        st.warning(f"⚠️ Warning: There are still {missing_values} missing values and {duplicate_values} ducplicate rows in the data")
                    else:
                        st.success("βœ… No missing values and duplicate rows remaining!")
                
                # Final data display
                st.session_state.df = st.session_state.df_cleaned
                st.subheader("πŸ“Š Final Data")
                st.write(f"Shape: {st.session_state.df.shape}")
                
                # Interactive data explorer
                tab1, tab2, tab3, tab4 = st.tabs(["Data View", "Statistics", "Column Analysis", 'Type Data'])
                
                with tab1:
                    num_rows = st.slider(
                        "Number of rows to display:",
                        min_value=5,
                        max_value=100,
                        value=5,
                        key="num_rows_view"
                    )
                    st.dataframe(st.session_state.df.head(num_rows), use_container_width=True)
                
                with tab2:
                    st.write("### Descriptive Statistics")
                    st.dataframe(st.session_state.df.describe(include='all'), use_container_width=True)
                    
                
                with tab3:
                    selected_col = st.selectbox(
                        "Select column to analyze:",
                        st.session_state.df.columns,
                        key="col_analyze"
                    )
                    
                    col_data = st.session_state.df[selected_col]
                    st.write(f"**Type:** {col_data.dtype}")
                    st.write(f"**Unique values:** {len(col_data.unique())}")
                    st.write(f"**Missing values:** {col_data.isnull().sum()} ({col_data.isnull().mean()*100:.2f}%)")
                    

                    if col_data.nunique() < 20 and not pd.api.types.is_numeric_dtype(col_data):
                        ascending = st.checkbox("Ascending", value=False)
                        st.write("**Distribution**")
                        column_name = col_data.name

                        # sum and sort data
                        counts = col_data.value_counts(ascending=ascending)
                        ordered_categories = counts.index.tolist()

                        # create dataframe and category for data
                        df_temp = pd.DataFrame({column_name: col_data})
                        df_temp[column_name] = pd.Categorical(df_temp[column_name], categories=ordered_categories, ordered=True)

                        # create histogram
                        fig = px.histogram(
                            df_temp,
                            x=column_name,
                            opacity=0.7,
                            color_discrete_sequence=["#d06200"],
                        )

                        fig.update_traces(marker_line_color='gray', marker_line_width=1)

                        # Show x axis based on category sorting
                        fig.update_layout(
                            template='seaborn',
                            title=f"Histogram of {column_name}",
                            xaxis_title=column_name,
                            yaxis_title="Count",
                            bargap=0.1,
                            font=dict(family="Verdana", size=13),
                            xaxis=dict(
                                categoryorder="array",
                                categoryarray=ordered_categories,
                            )
                        )

                        st.plotly_chart(fig, use_container_width=True)

                    elif pd.api.types.is_numeric_dtype(col_data):
                        st.write("**Distribution**")
                        column_name = col_data.name

                        df_temp = pd.DataFrame({column_name: col_data.dropna()})
                        
                        fig = px.histogram(
                            df_temp,
                            x=column_name,
                            nbins=30,
                            opacity=0.85,
                            color_discrete_sequence=["#d06200"],
                        )

                        fig.update_traces(marker_line_color='gray', marker_line_width=0.9)

                        fig.update_layout(
                            template='seaborn',
                            title=f"Histogram of {column_name}",
                            xaxis_title=column_name,
                            yaxis_title="Count",
                            bargap=0.1,
                            font=dict(family="Verdana", size=13),
                        )
                        fig.update_layout(
                            annotations=[
                                dict(
                                    text=f"Skewness: {skew(col_data):.2f}",
                                    showarrow=False,
                                    xref="paper", yref="paper",
                                    x=0.95, y=0.95,
                                    bordercolor='black',
                                    borderwidth=1
                                )
                            ]
                        )

                        st.plotly_chart(fig, use_container_width=True)

                    else:
                        st.write("**Value counts (top 20):**")
                        st.dataframe(col_data.value_counts().head(20))


                with tab4:
                    buffer = io.StringIO()
                    st.session_state.df.info(buf=buffer)
                    st.text(buffer.getvalue())

                    st.markdown("**Data Types:**")
                    st.json(st.session_state.df.dtypes.astype(str).to_dict())

                    st.markdown("**Missing Values:**")
                    st.json(st.session_state.df.isnull().sum().to_dict())

                    st.markdown("**Duplicate:**")
                    duplicate_count = st.session_state.df.duplicated().sum()
                    st.write(f"Total duplicate row: {duplicate_count}")

                    # Menampilkan jumlah duplikat dalam format JSON yang benar
                    st.json({"duplicate_count": duplicate_count})

                
                # Export options
                with st.expander("πŸ’Ύ Export Data", expanded=False):
                    export_format = st.radio(
                        "Export format:",
                        ["CSV", "Excel", "JSON"],
                        horizontal=True
                    )
                    
                    export_filename = st.text_input("Filename (without extension)", "clean_data")
                    
                    if st.button("Export Data"):
                        try:
                            if export_format == "CSV":
                                csv = st.session_state.df.to_csv(index=False).encode('utf-8')
                                st.download_button(
                                    label="Download CSV",
                                    data=csv,
                                    file_name=f"{export_filename}.csv",
                                    mime='text/csv'
                                )
                            elif export_format == "Excel":
                                excel_buffer = io.BytesIO()
                                st.session_state.df.to_excel(excel_buffer, index=False)
                                st.download_button(
                                    label="Download Excel",
                                    data=excel_buffer,
                                    file_name=f"{export_filename}.xlsx",
                                    mime='application/vnd.ms-excel'
                                )
                            elif export_format == "JSON":
                                json_str = st.session_state.df.to_json(orient='records', indent=2)
                                st.download_button(
                                    label="Download JSON",
                                    data=json_str,
                                    file_name=f"{export_filename}.json",
                                    mime='application/json'
                                )
                            st.success("Export ready!")
                        except Exception as e:
                            st.error(f"Export failed: {str(e)}")
                
        # Input area
        input_method = st.radio("Input method:", ["Auto run code","Manual run code"])

        user_input = st.text_area("Enter your Python code or question:", height=150,
                                placeholder="Write code here or ask a question about your data...",
                                key="user_input")
        
        

        # Mode handling
        if input_method == "Auto run code":
            col1, col2, col3 = st.columns(3)
            with col1: execute_btn = st.button("▢️ Execute")
            with col2: clear_btn = st.button("πŸ—‘οΈ Clear History")
            with col3: show_vars = st.button("πŸ“Š Show Variables")
            # Common button handling
            if clear_btn:
                st.session_state.execution_history = []
                
            if show_vars and st.session_state.execution_history:
                latest_vars = st.session_state.execution_history[-1].get('variables', {})
                st.json({k: str(type(v)) for k, v in latest_vars.items()})
            if execute_btn and user_input:
                with st.spinner("Execute code..."):
                    generated_code = code_llm(generate_prompt(user_input, st.session_state.df.columns.tolist()))
                    st.markdown("**Generated Code:**")
                    st.code(generated_code, language='python')
                    
                    result = execute_code(generated_code, st.session_state.df)
                    add_to_history(result)
                st.rerun()

        elif input_method == "Manual run code":
            col1, col2, col3, col4 = st.columns(4)
            with col1: code_btn = st.button("▢️ Code")
            with col2: execute_btn = st.button("▢️ Execute")
            with col3: clear_btn = st.button("πŸ—‘οΈ Clear History")
            with col4: show_vars = st.button("πŸ“Š Show Variables")

            if clear_btn:
                st.session_state.execution_history = []
                
            if show_vars and st.session_state.execution_history:
                latest_vars = st.session_state.execution_history[-1].get('variables', {})
                st.json({k: str(type(v)) for k, v in latest_vars.items()})

            if code_btn and user_input:
                with st.spinner("Generate Code"):
                    generated_code = code_llm(generate_prompt(user_input, st.session_state.df.columns.tolist()))
                    st.session_state.generated_code = generated_code
                    st.markdown("**Generated Code:**")
                    st.code(generated_code, language='python')
            
            if execute_btn:
                if 'generated_code' in st.session_state:
                    result = execute_code(st.session_state.generated_code, st.session_state.df)
                    add_to_history(result)
                elif user_input:
                    result = execute_code(user_input, st.session_state.df)
                    add_to_history(result)
                st.rerun()
                    
        # Display execution history
        display_history()