File size: 57,968 Bytes
0d30c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31f5e9e
0d30c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
import streamlit as st
import pandas as pd
import os
import json
import base64
import random
from streamlit_pdf_viewer import pdf_viewer
from langchain.prompts import PromptTemplate
from datetime import datetime
from pathlib import Path
from openai import OpenAI
from dotenv import load_dotenv
import warnings

warnings.filterwarnings('ignore')

os.getenv("OAUTH_CLIENT_ID")


# Load environment variables and initialize the OpenAI client to use Hugging Face Inference API.
load_dotenv()
client = OpenAI(
    base_url="https://api-inference.huggingface.co/v1",
    api_key=os.environ.get('TOKEN2') # Hugging Face API token
)

# Create necessary directories
for dir_name in ['data', 'feedback']:
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)

# Custom CSS
st.markdown("""
<style>
    .stButton > button {
        width: 100%;
        margin-bottom: 10px;
        background-color: #4CAF50;
        color: white;
        border: none;
        padding: 10px;
        border-radius: 5px;
    }
    .task-button {
        background-color: #2196F3 !important;
    }
    .stSelectbox {
        margin-bottom: 20px;
    }
    .output-container {
        padding: 20px;
        border-radius: 5px;
        border: 1px solid #ddd;
        margin: 10px 0;
    }
    .status-container {
        padding: 10px;
        border-radius: 5px;
        margin: 10px 0;
    }
    .sidebar-info {
        padding: 10px;
        background-color: #f0f2f6;
        border-radius: 5px;
        margin: 10px 0;
    }
    .feedback-button {
        background-color: #ff9800 !important;
    }
    .feedback-container {
        padding: 15px;
        background-color: #f5f5f5;
        border-radius: 5px;
        margin: 15px 0;
    }
</style>
""", unsafe_allow_html=True)

# Helper functions
def read_csv_with_encoding(file):
    encodings = ['utf-8', 'latin1', 'iso-8859-1', 'cp1252']
    for encoding in encodings:
        try:
            return pd.read_csv(file, encoding=encoding)
        except UnicodeDecodeError:
            continue
    raise UnicodeDecodeError("Failed to read file with any supported encoding")

#def save_feedback(feedback_data):
    #feedback_file = 'feedback/user_feedback.csv'
    #feedback_df = pd.DataFrame([feedback_data])
    
    #if os.path.exists(feedback_file):
        #feedback_df.to_csv(feedback_file, mode='a', header=False, index=False)
    #else:
        #feedback_df.to_csv(feedback_file, index=False)

def reset_conversation():
    st.session_state.conversation = []
    st.session_state.messages = []
    if 'task_choice' in st.session_state:
        del st.session_state.task_choice
    return None
    #new 24 March
    #user_input = st.text_input("Enter your prompt:")
###########33

# Initialize session state variables
if "messages" not in st.session_state:
    st.session_state.messages = []
if "examples_to_classify" not in st.session_state:
    st.session_state.examples_to_classify = []
if "system_role" not in st.session_state:
    st.session_state.system_role = ""

    

# Main app title
st.title("πŸ€–πŸ¦™ Text Data Labeling and Generation App")
# def embed_pdf_sidebar(pdf_path):
#     with open(pdf_path, "rb") as f:
#         base64_pdf = base64.b64encode(f.read()).decode('utf-8')
#     pdf_display = f"""
#         <iframe src="data:application/pdf;base64,{base64_pdf}" 
#         width="100%" height="400" type="application/pdf"></iframe>
#     """
#     st.markdown(pdf_display, unsafe_allow_html=True)
# 
        
    
# Sidebar settings
with st.sidebar:
    st.title("βš™οΈ Settings")
    # Add PDF upload section
    # 
    # if st.button("πŸ“˜ Show Instructions"):
    #     # This should be a path to a local file
    #     pdf_path = os.path.join("Streamlit.pdf")
    #     pdf_viewer(
    #         pdf_path,
    #         width="100%",
    #         height=300,
    #         render_text=True
       # )
    # with st.sidebar:
    #     with st.expander("πŸ“˜ View Instructions"):
    #         pdf_viewer("Streamlit.pdf", width="100%", height=300, render_text=True)

    # 
    ###4
    # with st.sidebar:
    #     st.markdown("### πŸ“˜ Instructions")
    #     st.markdown("[πŸ“„ Open Instructions PDF](/file/instructions.pdf)")

    


# 
    ####2
# #with st.sidebar:
#     st.markdown("### πŸ“˜ Instructions")

#     # PDF served from Space's file system
#     pdf_url = "/file/instructions.pdf"

#     st.markdown(f"""
#         <a href="{pdf_url}" target="_blank">
#             <button style='padding:10px;width:100%;font-size:16px;'>πŸ“„ Open Instructions PDF</button>
#         </a>
#     """, unsafe_allow_html=True)
# ###3 working code
# with st.sidebar:
#     with open("instructions.pdf", "rb") as f:
#         st.sidebar.download_button(
#         label="πŸ“„ Download Instructions PDF",
#         data=f,
#         file_name="instructions.pdf",
#         mime="application/pdf"
#     )

###6 
#this last code works
with st.sidebar:
    st.markdown("### πŸ“˜Data Generation and Labeling Instructions")
    #st.markdown("<h4 style='color: #4A90E2;'>πŸ“˜    Instructions</h4>", unsafe_allow_html=True)
    with open("User instructions.pdf", "rb") as f:
        st.download_button(
            label="πŸ“„ Download Instructions PDF",
            data=f,
            #file_name="instructions.pdf",
            file_name="User instructions.pdf",
            mime="application/pdf"
        )


#works with blu color text
# with st.sidebar:
#     # Stylish "Instructions" label
#     st.markdown("<h4 style='color: #4A90E2;'>πŸ“˜ Instructions</h4>", unsafe_allow_html=True)

#     # PDF download button
#     with open("instructions.pdf", "rb") as f:
#         st.download_button(
#             label="πŸ“„ Download Instructions PDF",
#             data=f,
#             file_name="instructions.pdf",
#             mime="application/pdf"
#         )

        ###5

#with st.sidebar:
    # st.markdown("### πŸ“˜ Instructions")

    # # PDF served from Space's file system
    # pdf_url = "/file/instructions.pdf"

    # st.markdown(f"""
    #     <a href="{pdf_url}" target="_blank">
    #         <button style='padding:15px;width:100%;font-size:16px;'>  πŸ“„ Open Instructions PDF</button>
    #     </a>
    # """, unsafe_allow_html=True)


    
    selected_model = st.selectbox(
        "Select Model",
        ["meta-llama/Llama-3.3-70B-Instruct", "meta-llama/Llama-3.2-3B-Instruct","meta-llama/Llama-4-Scout-17B-16E-Instruct", "meta-llama/Meta-Llama-3-8B-Instruct",
         "meta-llama/Llama-3.1-70B-Instruct"],
        key='model_select'
    )

    temperature = st.slider(
        "Temperature",
        0.0, 1.0, 0.7,
        help="Controls randomness in generation"
    )
    
    st.button("πŸ”„ New Conversation", on_click=reset_conversation)
    # st.markdown("### πŸ“˜ Instructions")
    # embed_pdf_sidebar("Streamlit.pdf")
    #Add PDF Instructions
    # with st.expander("πŸ“š Instructions"):
    #     st.write("View or download instruction guides:")
        
    #     # Option 1: Using st.download_button for PDFs stored in your app
    #     with open("file:///C:/Users/hp/Downloads/Streamlit.pdf", "rb") as file:
    #         first_pdf = file.read()
    #     st.download_button(
    #         label="Download Guide 1",
    #         data=first_pdf,
    #         file_name="user_guide.pdf",
    #         mime="application/pdf"
    #     )
        
    #     #with open("https://huggingface.co/spaces/Wedyan2023/COPY/blob/main/Streamlit.pdf", "rb") as file:
    #     with open("file:///C:/Users/hp/Downloads/Streamlit.pdf", "rb") as file:
    #         second_pdf = file.read()
    #     st.download_button(
    #         label="Download Guide 2",
    #         data=second_pdf,
    #         file_name="technical_guide.pdf",
    #         mime="application/pdf"
    #     )

        
    
    with st.container():
        st.markdown(f"""
           <div class="sidebar-info">
               <h4>Current Model: {selected_model}</h4>
               <p><em>Note: Generated content may be inaccurate or false. Check important info.</em></p>
           </div>
            """, unsafe_allow_html=True)
    
# with st.sidebar:
#     st.markdown("### πŸ“˜ Instructions")
#     if pdf_file := st.file_uploader("Upload Instruction PDF", type="pdf"):
#         embed_pdf(pdf_file)


    feedback_url = "https://docs.google.com/forms/d/e/1FAIpQLSdZ_5mwW-pjqXHgxR0xriyVeRhqdQKgb5c-foXlYAV55Rilsg/viewform?usp=header"
    st.sidebar.markdown(
        f'<a href="{feedback_url}" target="_blank"><button style="width: 100%;">Feedback Form</button></a>',
        unsafe_allow_html=True
    )

# Display conversation
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Main content
if 'task_choice' not in st.session_state:
    col1, col2 = st.columns(2)
    with col1:
        if st.button("πŸ“ Data Generation", key="gen_button", help="Generate new data"):
            st.session_state.task_choice = "Data Generation"
    with col2:
        if st.button("🏷️ Data Labeling", key="label_button", help="Label existing data"):
            st.session_state.task_choice = "Data Labeling"

if "task_choice" in st.session_state:
    if st.session_state.task_choice == "Data Generation":
        st.header("πŸ“ Data Generation")

       # 1. Domain selection
        domain_selection = st.selectbox("Domain", [
            "Restaurant reviews", "E-Commerce reviews", "News", "AG News", "Tourism", "Custom"
        ])
        
        # 2. Handle custom domain input
        custom_domain_valid = True  # Assume valid until proven otherwise
        
        if domain_selection == "Custom":
            domain = st.text_input("Specify custom domain")
            if not domain.strip():
                st.error("Please specify a domain name.")
                custom_domain_valid = False
        else:
            domain = domain_selection


 

        # Classification type selection
        classification_type = st.selectbox(
            "Classification Type",
            ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
        )



        
        
#system role before
       
####
        # Labels setup based on classification type
        #labels = []
        labels = []
        labels_valid = False
        errors = []

        def validate_binary_labels(labels):
            errors = []
            normalized = [label.strip().lower() for label in labels]
        
            if not labels[0].strip():
                errors.append("First class name is required.")
            if not labels[1].strip():
                errors.append("Second class name is required.")
            if normalized[0] == normalized[1] and all(normalized):
                errors.append("Class names must be different.")
            return errors
        
        if classification_type == "Sentiment Analysis":
            st.write("### Sentiment Analysis Labels (Fixed)")
            col1, col2, col3 = st.columns(3)
            with col1:
                st.text_input("First class", "Positive", disabled=True)
            with col2:
                st.text_input("Second class", "Negative", disabled=True)
            with col3:
                st.text_input("Third class", "Neutral", disabled=True)
            labels = ["Positive", "Negative", "Neutral"]
        
        elif classification_type == "Binary Classification":
            st.write("### Binary Classification Labels")
            col1, col2 = st.columns(2)
            with col1:
                label_1 = st.text_input("First class", "Positive")
            with col2:
                label_2 = st.text_input("Second class", "Negative")
            
            labels = [label_1, label_2]
            errors = validate_binary_labels(labels)
        
            if errors:
                st.error("\n".join(errors))
            else:
                st.success("Binary class names are valid and unique!")


        # if classification_type == "Sentiment Analysis":
        #     st.write("### Sentiment Analysis Labels (Fixed)")
        #     col1, col2, col3 = st.columns(3)
        #     with col1:
        #         label_1 = st.text_input("First class", "Positive", disabled=True)
        #     with col2:
        #         label_2 = st.text_input("Second class", "Negative", disabled=True)
        #     with col3:
        #         label_3 = st.text_input("Third class", "Neutral", disabled=True)
        #     labels = ["Positive", "Negative", "Neutral"]


        # elif classification_type == "Binary Classification":
        #     st.write("### Binary Classification Labels")
        #     col1, col2 = st.columns(2)
        
        #     with col1:
        #         label_1 = st.text_input("First class", "Positive")
        #     with col2:
        #         label_2 = st.text_input("Second class", "Negative")
        
        #     errors = []
        #     labels = [label_1.strip(), label_2.strip()]
        
        #     # Check for empty class names
        #     if not labels[0]:
        #         errors.append("First class name is required.")
        #     if not labels[1]:
        #         errors.append("Second class name is required.")
        
        #     # Check for duplicates
        #     if labels[0].lower() == labels[1].lower():
        #         errors.append("Class names must be different.")
        
        #     # Show errors or success
        #     if errors:
        #         for error in errors:
        #             st.error(error)
        #     else:
        #         st.success("Binary class names are valid and unique!")

        #########
        
        elif classification_type == "Multi-Class Classification":
            st.write("### Multi-Class Classification Labels")
        
            default_labels_by_domain = {
                "News": ["Political", "Sports", "Entertainment", "Technology", "Business"],
                "AG News": ["World", "Sports", "Business", "Sci/Tech"],
                "Tourism": ["Accommodation", "Transportation", "Tourist Attractions", 
                            "Food & Dining", "Local Experience", "Adventure Activities",
                            "Wellness & Spa", "Eco-Friendly Practices", "Family-Friendly",
                            "Luxury Tourism"],
                "Restaurant reviews": ["Italian", "French", "American"],
                "E-Commerce reviews": ["Mobile Phones & Accessories", "Laptops & Computers","Kitchen & Dining",
                                       "Beauty & Personal Care", "Home & Furniture", "Clothing & Fashion",
                                      "Shoes & Handbags", "Health & Wellness", "Electronics & Gadgets",
                                       "Books & Stationery","Toys & Games", "Sports & Fitness",
                                       "Grocery & Gourmet Food","Watches & Accessories", "Baby Products"]
            }
        
            num_classes = st.slider("Number of classes", 3, 15, 3)
        
            # Get defaults for selected domain, or empty list
            defaults = default_labels_by_domain.get(domain, [])
        
            labels = []
            errors = []
            cols = st.columns(3)
        
            for i in range(num_classes):
                with cols[i % 3]:
                    default_value = defaults[i] if i < len(defaults) else ""
                    label_input = st.text_input(f"Class {i+1}", default_value)
                    normalized_label = label_input.strip().title()
        
                    if not normalized_label:
                        errors.append(f"Class {i+1} name is required.")
                    else:
                        labels.append(normalized_label)
        
            # Check for duplicates (case-insensitive)
            if len(labels) != len(set(labels)):
                errors.append("Labels names must be unique (case-insensitive, normalized to Title Case).")

            # Show validation results
            if errors:
                for error in errors:
                    st.error(error)
            else:
                st.success("All Labels names are valid and unique!")
            labels_valid = not errors  # Will be True only if there are no label errors

                   
                

                    ##############

        # Generation parameters
        col1, col2 = st.columns(2)
        with col1:
            min_words = st.number_input("Min words", 1, 100, 20)
        with col2:
            max_words = st.number_input("Max words", min_words, 100, 50)

        # Few-shot examples
        use_few_shot = st.toggle("Use few-shot examples")
        few_shot_examples = []
        if use_few_shot:
            num_examples = st.slider("Number of few-shot examples", 1, 10, 1)
            for i in range(num_examples):
                with st.expander(f"Example {i+1}"):
                    content = st.text_area(f"Content", key=f"few_shot_content_{i}")
                    label = st.selectbox(f"Label", labels, key=f"few_shot_label_{i}")
                    if content and label:
                        few_shot_examples.append({"content": content, "label": label})

        num_to_generate = st.number_input("Number of examples", 1, 200, 10)

        # System role customization
        default_system_role = (
    f"You are a seasoned expert in {classification_type}, specializing in the {domain} domain. "
    f"Your primary responsibility is to generate high-quality, diverse, and unique text examples "
    f"tailored to this domain. Please ensure that each example adheres to the specified length "
    f"requirements, ranging from {min_words} to {max_words} words, and avoid any repetition in the generated content."
)
        
        # Allow user to modify the system role
        system_role = st.text_area("Modify System Role (optional)", 
                                value=default_system_role,
                                key="system_role_input")

        # Store the system role in session state
        st.session_state['system_role'] = system_role if system_role else default_system_role
                                                                        

        #  # System role customization
        # default_system_role = f"You are a professional {classification_type} expert, your role is to generate text examples"
        # f"for {domain} domain. Always generate unique diverse examples and do not repeat the generated data."
        # f"The generated text should be between {min_words} to {max_words} words long."
        
        # # Allow user to modify the system role
        # system_role = st.text_area("Modify System Role (optional)", 
        #                         value=default_system_role,
        #                         key="system_role_input")

        # # Store the system role in session state
        # st.session_state['system_role'] = system_role if system_role else default_system_role
        
        user_prompt = st.text_area("User Prompt (optional)")

        # Data Generation system prompt template including system role
        
        prompt_template = PromptTemplate(
            input_variables=["system_role", "classification_type", "domain", "num_examples", 
                           "min_words", "max_words", "labels", "user_prompt", "few_shot_examples"],
            template=(
                "{system_role}\n"
                "- Use the following parameters:\n"
                "- Generate {num_examples} examples\n"
                "- Each example should be between {min_words} to {max_words} words long\n"
                #"- Word range: {min_words} - {max_words} words\n "
                "- Use these labels: {labels}.\n"
                "- Generate the examples in this format: 'Example text. Label: label'\n"
                "- Do not include word counts or any additional information\n"
                "- Always use your creativity and intelligence to generate unique and diverse text data\n"
                "- Write unique examples every time.\n"
                "- DO NOT REPEAT your gnerated text. \n"
                "- For each Output, describe it once and move to the next.\n"
                "- List each Output only once, and avoid repeating details.\n"
                "- Additional instructions: {user_prompt}\n\n"
                "- Use the following examples as a reference in the generation process\n\n {few_shot_examples}. \n"
                "- Think step by step, generate numbered examples, and check each newly generated example to ensure it has not been generated before. If it has, modify it"
                
            )
        )
                        
               

        # Generate system prompt
        system_prompt = prompt_template.format(
            system_role=st.session_state['system_role'],
            classification_type=classification_type,
            domain=domain,
            num_examples=num_to_generate,
            min_words=min_words,
            max_words=max_words,
            labels=", ".join(labels),
            user_prompt=user_prompt,
            few_shot_examples="\n".join([f"{ex['content']}\nLabel: {ex['label']}" for ex in few_shot_examples]) if few_shot_examples else ""
        )

        # Store system prompt in session state
        st.session_state['system_prompt'] = system_prompt

        # Display system prompt
        st.write("System Prompt:")
        st.text_area("Current System Prompt", value=st.session_state['system_prompt'], 
                    height=400, disabled=True)
        

        if st.button("🎯 Generate Examples"):
            #
            errors = []
            if domain_selection == "Custom" and not domain.strip():
                st.warning("Custom domain name is required.")
            elif len(labels) != len(set(labels)):
                st.warning("Class names must be unique.")
            elif any(not lbl.strip() for lbl in labels):
                st.warning("All class labels must be filled in.")
            #else:
                #st.success("Generating examples for domain: {domain}")
                    
            #if not custom_domain_valid:
                #st.warning("Custom domain name is required.")
            #elif not labels_valid:
                #st.warning("Please fix the label errors before generating examples.")
            #else:
                # Proceed to generate examples
                #st.success(f"Generating examples for domain: {domain}")
                
            with st.spinner("Generating examples..."):
                try:
                    stream = client.chat.completions.create(
                        model=selected_model,
                        messages=[{"role": "system", "content": st.session_state['system_prompt']}],
                        temperature=temperature,
                        stream=True,
                        max_tokens=80000,
                        top_p=0.9,
                       # repetition_penalty=1.2,
                        #frequency_penalty=0.5,      # Discourages frequent words
                        #presence_penalty=0.6,  
                    )
 #st.session_state['system_prompt'] = system_prompt
                    #new 24 march
                    st.session_state.messages.append({"role": "user", "content": system_prompt})
                 # # ####################
                    response = st.write_stream(stream)
                    st.session_state.messages.append({"role": "assistant", "content": response})
                        # Initialize session state variables if they don't exist
                    if 'system_prompt' not in st.session_state:
                        st.session_state.system_prompt = system_prompt
                    
                    if 'response' not in st.session_state:
                        st.session_state.response = response
                    
                    if 'generated_examples' not in st.session_state:
                        st.session_state.generated_examples = []
                    
                    if 'generated_examples_csv' not in st.session_state:
                        st.session_state.generated_examples_csv = None
                    
                    if 'generated_examples_json' not in st.session_state:
                        st.session_state.generated_examples_json = None
                    
                    # Parse response and generate examples list
                    examples_list = []
                    for line in response.split('\n'):
                        if line.strip():
                            parts = line.rsplit('Label:', 1)
                            if len(parts) == 2:
                                text = parts[0].strip()
                                label = parts[1].strip()
                                if text and label:
                                    examples_list.append({
                                        'text': text,
                                        'label': label,
                                        'system_prompt': st.session_state.system_prompt,
                                        'system_role': st.session_state.system_role,
                                        'task_type': 'Data Generation',
                                        'Use few-shot example?': 'Yes' if use_few_shot else 'No',
                                    })

                    if examples_list:
                        # Update session state with new data
                        st.session_state.generated_examples = examples_list
                        
                        # Generate CSV and JSON data
                        df = pd.DataFrame(examples_list)
                        st.session_state.generated_examples_csv = df.to_csv(index=False).encode('utf-8')
                        st.session_state.generated_examples_json = json.dumps(examples_list, indent=2).encode('utf-8')

                       # Vertical layout with centered "or" between buttons
                        st.download_button(
                            "πŸ“₯ Download Generated Examples (CSV)",
                            st.session_state.generated_examples_csv,
                            "generated_examples.csv",
                            "text/csv",
                            key='download-csv-persistent'
                        )
                        
                        # Add space and center the "or"
                        st.markdown("""
                        <div style='text-align: left; margin:15px 0; font-weight: 600; color: #666;'>. . . . . .         or</div>
                        """, unsafe_allow_html=True)
                        
                        st.download_button(
                            "πŸ“₯ Download Generated Examples (JSON)",
                            st.session_state.generated_examples_json,
                            "generated_examples.json",
                            "application/json",
                            key='download-json-persistent'
                        )
                        # # Display the labeled examples
                        # st.markdown("##### πŸ“‹ Labeled Examples Preview")
                        # st.dataframe(df, use_container_width=True)
                        
                    if st.button("Continue"):
                        if follow_up == "Generate more examples":
                            st.experimental_rerun()
                        elif follow_up == "Data Labeling":
                            st.session_state.task_choice = "Data Labeling"
                            st.experimental_rerun()

                except Exception as e:
                    st.error("An error occurred during generation.")
                    st.error(f"Details: {e}")

    
# Lableing Process
    elif st.session_state.task_choice == "Data Labeling":
        st.header("🏷️ Data Labeling")
        #new new new 
        # 1. Domain selection
        # 1. Domain selection
       
        
        domain_selection = st.selectbox("Domain", ["Restaurant reviews", "E-Commerce reviews", "News", "AG News", "Tourism", "Custom"])
            # 2. Handle custom domain input
        custom_domain_valid = True  # Assume valid until proven otherwise
        
        if domain_selection == "Custom":
            domain = st.text_input("Specify custom domain")
            if not domain.strip():
                st.error("Please specify a domain name.")
                custom_domain_valid = False
        else:
            domain = domain_selection


        # # Classification type selection
        # classification_type = st.selectbox(
        #     "Classification Type",
        #     ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
        # )
    #NNew edit
        # classification_type = st.selectbox(
        #     "Classification Type",
        #     #["Sentiment Analysis", "Binary Classification", "Multi-Class Classification", "Named Entity Recognition (NER)"],
        #     ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"],
        #     key="label_class_type"
        # )

        # Classification type selection
        classification_type = st.selectbox(
            "Classification Type",
            ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification", "Named Entity Recognition (NER)"]
        )
#NNew edit
        # Labels setup based on classification type
        labels = []
        labels_valid = False
        errors = []

        if classification_type == "Sentiment Analysis":
            st.write("### Sentiment Analysis Labels (Fixed)")
            col1, col2, col3 = st.columns(3)
            with col1:
                label_1 = st.text_input("First class", "Positive", disabled=True)
            with col2:
                label_2 = st.text_input("Second class", "Negative", disabled=True)
            with col3:
                label_3 = st.text_input("Third class", "Neutral", disabled=True)
            labels = ["Positive", "Negative", "Neutral"]


        elif classification_type == "Binary Classification":
            st.write("### Binary Classification Labels")
            col1, col2 = st.columns(2)
        
            with col1:
                label_1 = st.text_input("First class", "Positive")
            with col2:
                label_2 = st.text_input("Second class", "Negative")
        
            errors = []
            labels = [label_1.strip(), label_2.strip()]
            

            # Strip and lower-case labels for validation
            label_1 = labels[0].strip()
            label_2 = labels[1].strip()
            
            # Check for empty class names
            if not label_1:
                errors.append("First class name is required.")
            if not label_2:
                errors.append("Second class name is required.")
            
            # Check for duplicates (case insensitive)
            if label_1.lower() == label_2.lower() and label_1 and label_2:
                errors.append("Class names must be different.")
            
            # Show errors or success
            if errors:
                for error in errors:
                    st.error(error)
            else:
                st.success("Binary class names are valid and unique!")

                               
        elif classification_type == "Multi-Class Classification":
                st.write("### Multi-Class Classification Labels")
            
                default_labels_by_domain = {
                    "News": ["Political", "Sports", "Entertainment", "Technology", "Business"],
                    "AG News": ["World", "Sports", "Business", "Sci/Tech"],
                    "Tourism": ["Accommodation", "Transportation", "Tourist Attractions", 
                                "Food & Dining", "Local Experience", "Adventure Activities",
                                "Wellness & Spa", "Eco-Friendly Practices", "Family-Friendly",
                                "Luxury Tourism"],
                    "Restaurant reviews": ["Italian", "French", "American"],
                    "E-Commerce reviews": ["Mobile Phones & Accessories", "Laptops & Computers","Kitchen & Dining",
                                       "Beauty & Personal Care", "Home & Furniture", "Clothing & Fashion",
                                      "Shoes & Handbags", "Health & Wellness", "Electronics & Gadgets",
                                       "Books & Stationery","Toys & Games", "Sports & Fitness",
                                       "Grocery & Gourmet Food","Watches & Accessories", "Baby Products"]
            }
        
              
            
                # Ask user how many classes they want to define
                num_classes = st.slider("Select the number of classes (labels)", min_value=3, max_value=10, value=3)
            
                # Use default labels based on selected domain, if available
                defaults = default_labels_by_domain.get(domain, [])
            
                labels = []
                errors = []
                cols = st.columns(3)  # For nicely arranged label inputs
            
                for i in range(num_classes):
                    with cols[i % 3]:  # Distribute inputs across columns
                        default_value = defaults[i] if i < len(defaults) else ""
                        label_input = st.text_input(f"Label {i + 1}", default_value)
                        normalized_label = label_input.strip().title()
            
                        if not normalized_label:
                            errors.append(f"Label {i + 1} is required.")
                        else:
                            labels.append(normalized_label)
            
                # Check for duplicates (case-insensitive)
                normalized_set = {label.lower() for label in labels}
                if len(labels) != len(normalized_set):
                    errors.append("Label names must be unique (case-insensitive).")
            
                # Show validation results
                if errors:
                    for error in errors:
                        st.error(error)
                else:
                    st.success("All label names are valid and unique!")
            
                labels_valid = not errors  # True if no validation errors

        elif classification_type == "Named Entity Recognition (NER)":
            # NER entity options
            ner_entities = [
                "PERSON - Names of people, fictional characters, historical figures",
                "ORG - Companies, institutions, agencies, teams",
                "LOC - Physical locations (mountains, oceans, etc.)",
                "GPE - Countries, cities, states, political regions",
                "DATE - Calendar dates, years, centuries",
                "TIME - Times, durations",
                "MONEY - Monetary values with currency"
            ]
            selected_entities = st.multiselect(
                 "Select entities to recognize", 
                ner_entities,
                default=["PERSON - Names of people, fictional characters, historical figures",
                         "ORG - Companies, institutions, agencies, teams",
                        "LOC - Physical locations (mountains, oceans, etc.)",
                        "GPE - Countries, cities, states, political regions",
                        "DATE - Calendar dates, years, centuries",
                        "TIME - Times, durations",
                        "MONEY - Monetary values with currency"],
                        key="ner_entity_selection"
            )
        
            # Extract just the entity type (before the dash)
            labels = [entity.split(" - ")[0] for entity in selected_entities]
        
            if not labels:
                st.warning("Please select at least one entity type")
                labels = ["PERSON"]  # Default if nothing selected

            
                            


    #NNew edit
            # elif classification_type == "Multi-Class Classification":
            #     st.write("### Multi-Class Classification Labels")
            
            #     default_labels_by_domain = {
            #         "News": ["Political", "Sports", "Entertainment", "Technology", "Business"],
            #         "AG News": ["World", "Sports", "Business", "Sci/Tech"],
            #         "Tourism": ["Accommodation", "Transportation", "Tourist Attractions", 
            #                     "Food & Dining", "Local Experience", "Adventure Activities",
            #                     "Wellness & Spa", "Eco-Friendly Practices", "Family-Friendly",
            #                     "Luxury Tourism"],
            #         "Restaurant reviews": ["Italian", "French", "American"]
            #     }
            #     num_classes = st.slider("Number of classes", 3, 10, 3)
            
            #     # Get defaults for selected domain, or empty list
            #     defaults = default_labels_by_domain.get(domain, [])
            
            #     labels = []
            #     errors = []
            #     cols = st.columns(3)
            
            #     for i in range(num_classes):
            #         with cols[i % 3]:
            #             default_value = defaults[i] if i < len(defaults) else ""
            #             label_input = st.text_input(f"Class {i+1}", default_value)
            #             normalized_label = label_input.strip().title()
            
            #             if not normalized_label:
            #                 errors.append(f"Class {i+1} name is required.")
            #             else:
            #                 labels.append(normalized_label)
            
            #     # Check for duplicates (case-insensitive)
            #     if len(labels) != len(set(labels)):
                #     errors.append("Labels names must be unique (case-insensitive, normalized to Title Case).")
    
                # # Show validation results
                # if errors:
                #     for error in errors:
                #         st.error(error)
                # else:
                #     st.success("All Labels names are valid and unique!")
                # labels_valid = not errors  # Will be True only if there are no label errors

                   

            
        # else:
        #     num_classes = st.slider("Number of classes", 3, 23, 3, key="label_num_classes")
        #     labels = []
        #     cols = st.columns(3)
        #     for i in range(num_classes):
        #         with cols[i % 3]:
        #             label = st.text_input(f"Class {i+1}", f"Class_{i+1}", key=f"label_class_{i}")
        #             labels.append(label)

        use_few_shot = st.toggle("Use few-shot examples for labeling")
        few_shot_examples = []
        if use_few_shot:
            num_few_shot = st.slider("Number of few-shot examples", 1, 10, 1)
            for i in range(num_few_shot):
                with st.expander(f"Few-shot Example {i+1}"):
                    content = st.text_area(f"Content", key=f"label_few_shot_content_{i}")
                    label = st.selectbox(f"Label", labels, key=f"label_few_shot_label_{i}")
                    if content and label:
                        few_shot_examples.append(f"{content}\nLabel: {label}")

        num_examples = st.number_input("Number of examples to classify", 1, 100, 1)
    
        examples_to_classify = []
        if num_examples <= 20:
            for i in range(num_examples):
                example = st.text_area(f"Example {i+1}", key=f"example_{i}")
                if example:
                    examples_to_classify.append(example)
        else:
            examples_text = st.text_area(
                "Enter examples (one per line)",
                height=300,
                help="Enter each example on a new line"
            )
            if examples_text:
                examples_to_classify = [ex.strip() for ex in examples_text.split('\n') if ex.strip()]
                if len(examples_to_classify) > num_examples:
                    examples_to_classify = examples_to_classify[:num_examples]

        # System role customization  
        default_system_role = (f"You are a highly skilled {classification_type} expert."
        f"Your task is to accurately classify the provided text examples within the {domain} domain."
        f"Ensure that all classifications are precise, context-aware, and aligned with domain-specific standards and best practices."
    )

        # Allow user to modify the system role
        system_role = st.text_area("Modify System Role (optional)", 
                                value=default_system_role,
                                key="system_role_input")

        # Allow user to modify the system role
        st.session_state['system_role'] = system_role if system_role else default_system_role

        
        # #New Wedyan
        # default_system_role = f"You are a professional {classification_type} expert, your role is to classify the provided text examples for {domain} domain."
        # system_role = st.text_area("Modify System Role (optional)", 
        #                         value=default_system_role,
        #                         key="system_role_input")
        # st.session_state['system_role'] = system_role if system_role else default_system_role
        # # Labels initialization
        # #labels = []
        # ####

        user_prompt = st.text_area("User prompt (optional)", key="label_instructions")

        few_shot_text = "\n\n".join(few_shot_examples) if few_shot_examples else ""
        examples_text = "\n".join([f"{i+1}. {ex}" for i, ex in enumerate(examples_to_classify)])
    
        # Customize prompt template based on classification type
        if classification_type == "Named Entity Recognition (NER)":
            label_prompt_template = PromptTemplate(
                input_variables=["system_role", "labels", "few_shot_examples", "examples", "domain", "user_prompt"],
                template=(
                    "{system_role}\n"
                    #"- You are a professional Named Entity Recognition (NER) expert in {domain} domain. Your role is to identify and extract the following entity types: {labels}.\n"
                    "- For each text example provided, identify all entities of the requested types.\n"
                    "- Use the following entities: {labels}.\n"
                    "- Return each example followed by the entities you found in this format: 'Example text.\n Entities: [ENTITY_TYPE: entity text\n, ENTITY_TYPE: entity text\n, ...] or [No entities found]'\n"
                    "- If no entities of the requested types are found, indicate 'No entities found' in this text.\n"
                    "- Be precise about entity boundaries - don't include unnecessary words.\n"
                    "- Do not provide any additional information or explanations.\n"
                    "- Additional instructions:\n {user_prompt}\n\n"
                    "- Use user few-shot examples as guidance if provided:\n{few_shot_examples}\n\n"
                    "- Examples to analyze:\n{examples}\n\n"
                    "Output:\n"
                )
            )
        else:
            # Data Labeling system prompt template
            
                label_prompt_template = PromptTemplate(

                input_variables=["system_role", "classification_type", "labels", "few_shot_examples", "examples","domain", "user_prompt"],
                template=(
                    "{system_role}\n"
                    # "- You are a professional {classification_type} expert in {domain} domain. Your role is to classify the following examples using these labels: {labels}.\n"
                    "- Use the following instructions:\n"
                    "- Use the following labels: {labels}.\n"
                    "- Return the classified text followed by the label in this format: 'text. Label: [label]'\n"
                    "- Do not provide any additional information or explanations\n"
                    "- User prompt:\n {user_prompt}\n\n"
                    "- Use user provided examples as guidence in the classification process:\n\n {few_shot_examples}\n"
                    "- Examples to classify:\n{examples}\n\n"
                    "- Think step by step then classify the examples" 
                    #"Output:\n"
                ))

             
                
        # Check if few_shot_examples is already a formatted string
           # Check if few_shot_examples is already a formatted string
        if isinstance(few_shot_examples, str):
            formatted_few_shot = few_shot_examples
        # If it's a list of already formatted strings
        elif isinstance(few_shot_examples, list) and all(isinstance(ex, str) for ex in few_shot_examples):
            formatted_few_shot = "\n".join(few_shot_examples)
        # If it's a list of dictionaries with 'content' and 'label' keys
        elif isinstance(few_shot_examples, list) and all(isinstance(ex, dict) and 'content' in ex and 'label' in ex for ex in few_shot_examples):
            formatted_few_shot = "\n".join([f"{ex['content']}\nLabel: {ex['label']}" for ex in few_shot_examples])
        else:
            formatted_few_shot = ""
        
        system_prompt = label_prompt_template.format(
            system_role=st.session_state['system_role'],
            classification_type=classification_type,
            domain=domain,
            examples="\n".join(examples_to_classify),
            labels=", ".join(labels),
            user_prompt=user_prompt,
            few_shot_examples=formatted_few_shot
        )

        # Step 2: Store the system_prompt in st.session_state
        st.session_state['system_prompt'] = system_prompt           
#::contentReference[oaicite:0]{index=0}
        st.write("System Prompt:")
        #st.code(system_prompt)
        #st.code(st.session_state['system_prompt'])
        st.text_area("System Prompt", value=st.session_state['system_prompt'], height=300, max_chars=None, key=None, help=None, disabled=True)
            
        

        if st.button("🏷️ Label Data"):
            if examples_to_classify:
                with st.spinner("Labeling data..."):
                    # Generate the system prompt based on classification type
                    if classification_type == "Named Entity Recognition (NER)":
                        system_prompt = label_prompt_template.format(
                            system_role=st.session_state['system_role'],
                            labels=", ".join(labels),
                            domain = domain,
                            few_shot_examples=few_shot_text,
                            examples=examples_text,
                            user_prompt=user_prompt
                        )
                    else:
                        system_prompt = label_prompt_template.format(
                            classification_type=classification_type,
                            system_role=st.session_state['system_role'],
                            domain = domain,
                            labels=", ".join(labels),
                            few_shot_examples=few_shot_text,
                            examples=examples_text,
                            user_prompt=user_prompt
                        )
                    try:
                        stream = client.chat.completions.create(
                            model=selected_model,
                            messages=[{"role": "system", "content": system_prompt}],
                            temperature=temperature,
                            stream=True,
                            max_tokens=20000,
                            top_p = 0.9,
                          
                        )
                        #new 24 March
                        # Append user message
                        st.session_state.messages.append({"role": "user", "content": system_prompt})
                        #################
                        response = st.write_stream(stream)
                        st.session_state.messages.append({"role": "assistant", "content": response})
                         # Display the labeled examples
                        #    # Optional: If you want to add it as a chat-style message log
                        # preview_str = st.session_state.labeled_preview.to_markdown(index=False)
                        # st.session_state.messages.append({"role": "assistant", "content": f"Here is a preview of the labeled examples:\n\n{preview_str}"})
                                                                                                    

                        # # Stream response and append assistant message
                        # #14/4/2024
                        # response = st.write_stream(stream)
                        # st.session_state.messages.append({"role": "assistant", "content": response})

                          # Initialize session state variables if they don't exist
                        if 'system_prompt' not in st.session_state:
                            st.session_state.system_prompt = system_prompt
                        
                        if 'response' not in st.session_state:
                            st.session_state.response = response
                        
                        if 'generated_examples' not in st.session_state:
                            st.session_state.generated_examples = []
                        
                        if 'generated_examples_csv' not in st.session_state:
                            st.session_state.generated_examples_csv = None
                        
                        if 'generated_examples_json' not in st.session_state:
                            st.session_state.generated_examples_json = None
                    

                      
                    
                        # Save labeled examples to CSV
                        #new 14/4/2025
                        labeled_examples = []
                        if classification_type == "Named Entity Recognition (NER)":
                            labeled_examples = []
                            for line in response.split('\n'):
                                if line.strip():
                                    parts = line.rsplit('Entities:', 1)
                                    if len(parts) == 2:
                                        text = parts[0].strip()
                                        entities = parts[1].strip()
                                        if text and entities:
                                            labeled_examples.append({
                                                'text': text,
                                                'entities': entities,
                                                'system_prompt': st.session_state.system_prompt,
                                                'system_role': st.session_state.system_role,
                                                'task_type': 'Named Entity Recognition (NER)',
                                                'Use few-shot example?': 'Yes' if use_few_shot else 'No',
                                            })
                       
                                            
                        else:
                            labeled_examples = []
                            for line in response.split('\n'):
                                if line.strip():
                                    parts = line.rsplit('Label:', 1)
                                    if len(parts) == 2:
                                        text = parts[0].strip()
                                        label = parts[1].strip()
                                        if text and label:
                                            labeled_examples.append({
                                                'text': text,
                                                'label': label,
                                                'system_prompt': st.session_state.system_prompt,
                                                'system_role': st.session_state.system_role,
                                                'task_type': 'Data Labeling',
                                                'Use few-shot example?': 'Yes' if use_few_shot else 'No',  
                                            })
                       # Save and provide download options
                        if labeled_examples:
                            # Update session state
                            st.session_state.labeled_examples = labeled_examples
                        
                            # Convert to CSV and JSON
                            df = pd.DataFrame(labeled_examples)
                            st.session_state.labeled_examples_csv = df.to_csv(index=False).encode('utf-8')
                            st.session_state.labeled_examples_json = json.dumps(labeled_examples, indent=2).encode('utf-8')
                        
                            # Download buttons
                            st.download_button(
                                "πŸ“₯ Download Labeled Examples (CSV)",
                                st.session_state.labeled_examples_csv,
                                "labeled_examples.csv",
                                "text/csv",
                                key='download-labeled-csv'
                            )
                        
                            st.markdown("""
                            <div style='text-align: left; margin:15px 0; font-weight: 600; color: #666;'>. . . . . .         or</div>
                            """, unsafe_allow_html=True)
                        
                            st.download_button(
                                "πŸ“₯ Download Labeled Examples (JSON)",
                                st.session_state.labeled_examples_json,
                                "labeled_examples.json",
                                "application/json",
                                key='download-labeled-json'
                            )
                            # Display the labeled examples
                            st.markdown("##### πŸ“‹ Labeled Examples Preview")
                            st.dataframe(df, use_container_width=True)
                            # Display section
                            #st.markdown("### πŸ“‹ Labeled Examples Preview")
                            #st.dataframe(st.session_state.labeled_preview, use_container_width=True)

                                                                            
                                                                        
                        # if labeled_examples:
                        #     df = pd.DataFrame(labeled_examples)
                        #     csv = df.to_csv(index=False).encode('utf-8')
                        #     st.download_button(
                        #         "πŸ“₯ Download Labeled Examples",
                        #         csv,
                        #         "labeled_examples.csv",
                        #         "text/csv",
                        #         key='download-labeled-csv'
                        #     )
                        # # Add space and center the "or"
                        # st.markdown("""
                        # <div style='text-align: left; margin:15px 0; font-weight: 600; color: #666;'>. . . . . .         or</div>
                        # """, unsafe_allow_html=True)
                        
                        # if labeled_examples:
                        #     df = pd.DataFrame(labeled_examples)
                        #     csv = df.to_csv(index=False).encode('utf-8')
                        #     st.download_button(
                        #         "πŸ“₯ Download Labeled Examples",
                        #         csv,
                        #         "labeled_examples.json",
                        #         "text/json",
                        #         key='download-labeled-JSON'
                        #     )
                            
                        # Add follow-up interaction options
                        #st.markdown("---")
                        #follow_up = st.radio(
                            #"What would you like to do next?",
                             #["Label more data", "Data Generation"],
                           # key="labeling_follow_up"
                      #  )
                    
                        if st.button("Continue"):
                            if follow_up == "Label more data":
                                st.session_state.examples_to_classify = []
                                st.experimental_rerun()
                            elif follow_up == "Data Generation":
                                st.session_state.task_choice = "Data Labeling"
                                st.experimental_rerun()
                            
                    except Exception as e:
                        st.error("An error occurred during labeling.")
                        st.error(f"Details: {e}")
            else:
                st.warning("Please enter at least one example to classify.")

    #st.session_state.messages.append({"role": "assistant", "content": response})
       
        
                   

# Footer
st.markdown("---")
st.markdown(
    """
    <div style='text-align: center'>
        <p>Made with ❀️ by Wedyan AlSakran 2025</p>
    </div>
    """, 
    unsafe_allow_html=True
)