File size: 28,623 Bytes
be58ecd
 
 
 
 
f31213f
 
 
 
 
b4e0bee
be58ecd
b4e0bee
 
 
 
f31213f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e0bee
2c0d348
f31213f
 
 
 
b4e0bee
 
f31213f
 
 
b4e0bee
f31213f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e0bee
f31213f
 
 
 
 
 
 
 
 
 
b4e0bee
 
 
 
 
 
 
 
 
 
f31213f
 
 
 
2c0d348
b4e0bee
2c0d348
f31213f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e0bee
f31213f
 
 
b4e0bee
2c0d348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e0bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9432e38
2c0d348
b4e0bee
 
 
 
9bbbd14
 
 
 
 
 
 
 
 
b4e0bee
 
 
 
 
2d6564f
b4e0bee
 
 
 
2d6564f
b4e0bee
 
 
 
 
 
 
 
2c0d348
 
 
b4e0bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0426d64
 
 
 
b4e0bee
0426d64
 
 
 
b4e0bee
0426d64
 
 
 
b4e0bee
0426d64
 
 
b4e0bee
0426d64
b4e0bee
0426d64
 
 
 
 
 
 
 
 
b4e0bee
 
0426d64
 
 
b4e0bee
 
 
 
2c0d348
9432e38
b4e0bee
 
2c0d348
b4e0bee
 
 
4822903
 
b4e0bee
 
4822903
 
 
 
 
 
 
 
 
b4e0bee
 
4822903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e0bee
 
 
 
 
 
 
 
2c0d348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e0bee
 
 
f31213f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e0bee
f31213f
 
 
 
 
 
 
 
 
b4e0bee
f31213f
 
 
 
b4e0bee
 
 
 
 
 
 
 
 
 
f31213f
b4e0bee
 
f31213f
b4e0bee
 
f31213f
b4e0bee
f31213f
 
 
 
 
 
 
 
 
b4e0bee
 
 
f31213f
b4e0bee
 
f31213f
b4e0bee
 
f31213f
b4e0bee
f31213f
b4e0bee
f31213f
b4e0bee
 
 
f31213f
b4e0bee
f31213f
 
b4e0bee
 
f31213f
 
b4e0bee
 
f31213f
b4e0bee
 
 
 
f31213f
b4e0bee
 
 
f31213f
b4e0bee
f31213f
b4e0bee
f31213f
b4e0bee
 
 
 
f31213f
b4e0bee
 
f31213f
b4e0bee
f31213f
b4e0bee
 
f31213f
b4e0bee
 
f31213f
b4e0bee
 
 
f31213f
b4e0bee
f31213f
b4e0bee
f31213f
b4e0bee
f31213f
b4e0bee
 
f31213f
b4e0bee
 
 
 
f31213f
 
b4e0bee
 
f31213f
b4e0bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), )))

from imports import *
import importlib.util
import os
import sys
import joblib
import time
import torch
# from transformers.utils import move_cache_to_trash 
# from huggingface_hub import delete_cache
from transformers.utils.hub import TRANSFORMERS_CACHE
import shutil


# from hmv_cfg_base_stage1.model1 import load_model as load_model1
# from hmv_cfg_base_stage1.model1 import predict as predict1

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_STAGE1 = os.path.join(BASE_DIR, "config", "stage1_models.json")
LOADERS_STAGE1 = os.path.join(BASE_DIR, "hmv-cfg-base-stage1")


SENTIMENT_POLARITY_LABELS = [
    "negative", "neutral", "positive"
]

current_model = None
current_tokenizer = None

# Enabling Resource caching


# @st.cache_resource
def load_model_config():
    with open(CONFIG_STAGE1, "r") as f:
        model_data = json.load(f)

    # Extract names for dropdown
    model_options = {v["name"]: v for v in model_data.values()}
    return model_data, model_options


MODEL_DATA, MODEL_OPTIONS = load_model_config()


# ✅ Dynamically Import Model Functions
def import_from_module(module_name, function_name):
    try:
        module = importlib.import_module(module_name)
        return getattr(module, function_name)
    except (ModuleNotFoundError, AttributeError) as e:
        st.error(f"❌ Import Error: {e}")
        return None


def free_memory():
    #  """Free up CPU & GPU memory before loading a new model."""
    global current_model, current_tokenizer

    if current_model is not None:
        del current_model  # Delete the existing model
        current_model = None  # Reset reference

    if current_tokenizer is not None:
        del current_tokenizer  # Delete the tokenizer
        current_tokenizer = None

    gc.collect()  # Force garbage collection for CPU memory

    if torch.cuda.is_available():
        torch.cuda.empty_cache()  # Free GPU memory
        torch.cuda.ipc_collect()  # Clean up PyTorch GPU cache

    # If running on CPU, reclaim memory using OS-level commands
    try:
        if torch.cuda.is_available() is False:
            psutil.virtual_memory()  # Refresh memory stats
    except Exception as e:
        print(f"Memory cleanup error: {e}")

    # Delete cached Hugging Face models
    try:
        cache_dir = TRANSFORMERS_CACHE
        if os.path.exists(cache_dir):
            shutil.rmtree(cache_dir)
            print("Cache cleared!")
    except Exception as e:
        print(f"❌ Cache cleanup error: {e}")



def load_selected_model(model_name):
    global current_model, current_tokenizer

    # st.cache_resource.clear()

    # free_memory()

    # st.write("DEBUG: Available Models:", MODEL_OPTIONS.keys())  # ✅ See available models
    # st.write("DEBUG: Selected Model:", MODEL_OPTIONS[model_name])  # ✅ Check selected model
    # st.write("DEBUG: Model Name:", model_name)  # ✅ Check selected model

    if model_name not in MODEL_OPTIONS:
        st.error(f"⚠️ Model '{model_name}' not found in config!")
        return None, None, None

    model_info = MODEL_OPTIONS[model_name]
    hf_location = model_info["hf_location"]

    model_module = model_info["module_path"]
    load_function = model_info["load_function"]
    predict_function = model_info["predict_function"]

    load_model_func = import_from_module(model_module, load_function)
    predict_func = import_from_module(model_module, predict_function)

    if load_model_func is None or predict_func is None:
        st.error("❌ Model functions could not be loaded!")
        return None, None, None

    model, tokenizer = load_model_func()

    current_model, current_tokenizer = model, tokenizer
    return model, tokenizer, predict_func


def disable_ui():
    st.components.v1.html(
        """
        <style>
        #ui-disable-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            background-color: rgba(200, 200, 200, 0.5);
            z-index: 9999;
        }
        </style>
        <div id="ui-disable-overlay"></div>
        """,
        height=0,
        scrolling=False
    )


def enable_ui():
    st.components.v1.html(
        """
        <script>
        var overlay = document.getElementById("ui-disable-overlay");
        if (overlay) {
            overlay.parentNode.removeChild(overlay);
        }
        </script>
        """,
        height=0,
        scrolling=False
    )

# Function to increment progress dynamically
def update_progress(progress_bar, start, end, delay=0.1):
    for i in range(start, end + 1, 5):  # Increment in steps of 5%
        progress_bar.progress(i)
        time.sleep(delay)  # Simulate processing time
        # st.experimental_rerun() # Refresh the page


# Function to update session state when model changes
def on_model_change():
    st.session_state.model_changed = True  # Mark model as changed
    

# Function to update session state when text changes


def on_text_change():
    st.session_state.text_changed = True  # Mark text as changed


# Initialize session state variables
if "selected_model" not in st.session_state:
    st.session_state.selected_model = list(MODEL_OPTIONS.keys())[
        0]  # Default model
if "user_input" not in st.session_state:
    st.session_state.user_input = ""
if "last_processed_input" not in st.session_state:
    st.session_state.last_processed_input = ""
if "model_changed" not in st.session_state:
    st.session_state.model_changed = False
if "text_changed" not in st.session_state:
    st.session_state.text_changed = False
if "disabled" not in st.session_state:
    st.session_state.disabled = False


def show_sentiment_analysis():

    model_names = list(MODEL_OPTIONS.keys())

    # Check if the stored selected model is valid; if not, reset it
    if "selected_model" in st.session_state:
        if st.session_state.selected_model not in model_names:
            st.session_state.selected_model = model_names[0]
    else:
        st.session_state.selected_model = model_names[0]

    st.title("Stage 1: Sentiment Polarity Analysis")
    st.write("This section handles sentiment analysis.")

    # Model selection with change detection
    selected_model = st.selectbox(
        "Choose a model:", list(MODEL_OPTIONS.keys()), key="selected_model_stage1", on_change=on_model_change
    )

    # Text input with change detection
    user_input = st.text_input(
        "Enter text for sentiment analysis:", key="user_input_stage1", on_change=on_text_change
    )
    user_input_copy = user_input

    # Only run inference if:
    # 1. The text is NOT empty
    # 2. The text has changed OR the model has changed
    if user_input.strip() and (st.session_state.text_changed or st.session_state.model_changed):

        # disable_ui()


        # Reset session state flags
        st.session_state.last_processed_input = user_input
        st.session_state.model_changed = False
        st.session_state.text_changed = False   # Store selected model

        # ADD A DYNAMIC PROGRESS BAR
        progress_bar = st.progress(0)
        update_progress(progress_bar, 0, 10)
        # status_text = st.empty()

        # update_progress(0, 10)
        # status_text.text("Loading model...")

        # Make prediction

        # model, tokenizer = load_model()
        # model, tokenizer = load_selected_model(selected_model)

        col_spinner, col_warning = st.columns(2)
        with col_warning:
            warning_placeholder = st.empty()
            warning_placeholder.warning("Don't change the text data or any input parameters or switch models or pages while inference is loading...")

        with col_spinner:
            with st.spinner("Please wait, inference is loading..."):
                model, tokenizer, predict_func = load_selected_model(selected_model)
                device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

                if model is None:
                    st.error(
                        "⚠️ Error: Model failed to load! Check model selection or configuration.")
                    st.stop()

                # model.to(device)
                if hasattr(model, "to"):
                    model.to(device)

                # predictions = predict(user_input, model, tokenizer, device)

                predictions = predict_func(user_input, model, tokenizer, device)

                # Squeeze predictions to remove extra dimensions
                predictions_array = predictions.squeeze()

                # Convert to binary predictions (argmax)
                binary_predictions = np.zeros_like(predictions_array)
                max_indices = np.argmax(predictions_array)
                binary_predictions[max_indices] = 1

            # Update progress bar for prediction and model loading
        update_progress(progress_bar, 10, 100)

        warning_placeholder.empty()

        # Display raw predictions
        st.write(f"**Predicted Sentiment Scores:** {predictions_array}")

        # enable_ui()
##
        # Display binary classification result
        st.write(f"**Predicted Sentiment:**")
        st.write(f"**NEGATIVE:** {binary_predictions[0]}, **NEUTRAL:** {binary_predictions[1]}, **POSITIVE:** {binary_predictions[2]}")
        # st.write(f"**NEUTRAL:** {binary_predictions[1]}")
        # st.write(f"**POSITIVE:** {binary_predictions[2]}")

        col1, col2 = st.columns(2)

        sentiment_polarities = predictions_array.tolist()

        with col1:
        # 1️⃣ **Polar Plot (Plotly)**
            fig_polar = px.line_polar(
                pd.DataFrame(dict(r=sentiment_polarities,
                            theta=SENTIMENT_POLARITY_LABELS)),
                r='r', theta='theta', line_close=True
            )
            st.plotly_chart(fig_polar)

        normalized_predictions = predictions_array / predictions_array.sum()

        with col2:
        # 2️⃣ **Normalized Horizontal Bar Chart (Matplotlib)**

            fig, ax = plt.subplots(figsize=(8, 2))
            left = 0
            for i in range(len(normalized_predictions)):
                ax.barh(0, normalized_predictions[i], color=plt.cm.tab10(
                    i), left=left, label=SENTIMENT_POLARITY_LABELS[i])
                left += normalized_predictions[i]

            # Configure the chart
            ax.set_xlim(0, 1)
            ax.set_yticks([])
            ax.set_xticks(np.arange(0, 1.1, 0.1))
            ax.legend(loc='upper center', bbox_to_anchor=(
                0.5, -0.15), ncol=len(SENTIMENT_POLARITY_LABELS))
            plt.title("Sentiment Polarity Prediction Distribution")

            # Display in Streamlit
            st.pyplot(fig)

        progress_bar.empty()


if __name__ == "__main__":
    show_sentiment_analysis()










#########


# def show_sentiment_analysis():
#     st.cache_resource.clear()
#     free_memory()

#     st.title("Stage 1: Sentiment Polarity Analysis")
#     st.write("This section handles sentiment analysis.")

#     # Model selection with change detection
#     selected_model = st.selectbox(
#         "Choose a model:", list(MODEL_OPTIONS.keys()), key="selected_model", on_change=on_model_change
#     )

#     # Text input with change detection
#     user_input = st.text_input(
#         "Enter text for sentiment analysis:", key="user_input", on_change=on_text_change
#     )
#     user_input_copy = user_input

#     # Only run inference if:
#     # 1. The text is NOT empty
#     # 2. The text has changed OR the model has changed
#     if user_input.strip() and (st.session_state.text_changed or st.session_state.model_changed):

#         # Reset session state flags
#         st.session_state.last_processed_input = user_input
#         st.session_state.model_changed = False
#         st.session_state.text_changed = False   # Store selected model

#         # ADD A DYNAMIC PROGRESS BAR
#         progress_bar = st.progress(0)
#         update_progress(progress_bar, 0, 10)
#         # status_text = st.empty()

#         # update_progress(0, 10)
#         # status_text.text("Loading model...")

#         # Make prediction

#         # model, tokenizer = load_model()
#         # model, tokenizer = load_selected_model(selected_model)
#         with st.spinner("Please wait..."):
#             model, tokenizer, predict_func = load_selected_model(selected_model)
#             device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

#             if model is None:
#                 st.error(
#                     "⚠️ Error: Model failed to load! Check model selection or configuration.")
#                 st.stop()

#             model.to(device)

#             # predictions = predict(user_input, model, tokenizer, device)

#             predictions = predict_func(user_input, model, tokenizer, device)

#             # Squeeze predictions to remove extra dimensions
#             predictions_array = predictions.squeeze()

#             # Convert to binary predictions (argmax)
#             binary_predictions = np.zeros_like(predictions_array)
#             max_indices = np.argmax(predictions_array)
#             binary_predictions[max_indices] = 1

#             # Update progress bar for prediction and model loading
#             update_progress(progress_bar, 10, 100)

#         # Display raw predictions
#         st.write(f"**Predicted Sentiment Scores:** {predictions_array}")

#         # Display binary classification result
#         st.write(f"**Predicted Sentiment:**")
#         st.write(
#             f"**NEGATIVE:** {binary_predictions[0]}, **NEUTRAL:** {binary_predictions[1]}, **POSITIVE:** {binary_predictions[2]}")
#         # st.write(f"**NEUTRAL:** {binary_predictions[1]}")
#         # st.write(f"**POSITIVE:** {binary_predictions[2]}")

#         # 1️⃣ **Polar Plot (Plotly)**
#         sentiment_polarities = predictions_array.tolist()
#         fig_polar = px.line_polar(
#             pd.DataFrame(dict(r=sentiment_polarities,
#                          theta=SENTIMENT_POLARITY_LABELS)),
#             r='r', theta='theta', line_close=True
#         )
#         st.plotly_chart(fig_polar)

#         # 2️⃣ **Normalized Horizontal Bar Chart (Matplotlib)**
#         normalized_predictions = predictions_array / predictions_array.sum()

#         fig, ax = plt.subplots(figsize=(8, 2))
#         left = 0
#         for i in range(len(normalized_predictions)):
#             ax.barh(0, normalized_predictions[i], color=plt.cm.tab10(
#                 i), left=left, label=SENTIMENT_POLARITY_LABELS[i])
#             left += normalized_predictions[i]

#         # Configure the chart
#         ax.set_xlim(0, 1)
#         ax.set_yticks([])
#         ax.set_xticks(np.arange(0, 1.1, 0.1))
#         ax.legend(loc='upper center', bbox_to_anchor=(
#             0.5, -0.15), ncol=len(SENTIMENT_POLARITY_LABELS))
#         plt.title("Sentiment Polarity Prediction Distribution")

#         # Display in Streamlit
#         st.pyplot(fig)

#         progress_bar.empty()

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



# def show_sentiment_analysis():
#     st.cache_resource.clear()
#     free_memory()

#     st.title("Stage 1: Sentiment Polarity Analysis")
#     st.write("This section handles sentiment analysis.")

#     # Model selection with change detection
#     selected_model = st.selectbox(
#         "Choose a model:", list(MODEL_OPTIONS.keys()), key="selected_model", on_change=on_model_change, disabled=st.session_state.disabled
#     )

#     # Text input with change detection
#     user_input = st.text_input(
#         "Enter text for sentiment analysis:", key="user_input", on_change=on_text_change, disabled=st.session_state.disabled
#     )
#     user_input_copy = user_input

#     # progress_bar = st.progress(0)
#     progress_bar = st.empty()

#     if st.session_state.disabled is False and st.session_state.predictions is not None:
#         st.write(f"**Predicted Sentiment Scores:** {st.session_state.predictions}")
#         st.write(f"**NEGATIVE:** {st.session_state.binary_predictions[0]}, **NEUTRAL:** {st.session_state.binary_predictions[1]}, **POSITIVE:** {st.session_state.binary_predictions[2]}")
#         st.plotly_chart(st.session_state.polar_plot)
#         st.pyplot(st.session_state.bar_chart)

#         update_progress(progress_bar, 95, 100)

#         st.session_state.predictions = None
#         st.session_state.binary_predictions = None
#         st.session_state.polar_plot = None
#         st.session_state.bar_chart = None
        
#         st.session_state.disabled = False
            
#         progress_bar.empty()


#     if user_input.strip() and (st.session_state.text_changed or st.session_state.model_changed) and st.session_state.disabled is False:
#         st.session_state.disabled = True
#         st.rerun()
#         return
        
    
#     if user_input.strip() and (st.session_state.text_changed or st.session_state.model_changed) and st.session_state.disabled is True:
#         # Mark processing as True to
        

#         # Reset session state flags
#         st.session_state.last_processed_input = user_input
#         st.session_state.model_changed = False
#         st.session_state.text_changed = False   # Store selected model

#         # ADD A DYNAMIC PROGRESS BAR
#         progress_bar = st.progress(0)
#         update_progress(progress_bar, 0, 10)
#         # status_text = st.empty()

#         # update_progress(0, 10)
#         # status_text.text("Loading model...")

#         # Make prediction

#         # model, tokenizer = load_model()
#         # model, tokenizer = load_selected_model(selected_model)
#         with st.spinner("Please wait..."):
#             model, tokenizer, predict_func = load_selected_model(selected_model)
#             device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

#             if model is None:
#                 st.error(
#                     "⚠️ Error: Model failed to load! Check model selection or configuration.")
#                 st.session_state.disabled = False
#                 st.rerun()
#                 st.stop()
#                 return

#             model.to(device)

#             # predictions = predict(user_input, model, tokenizer, device)

#             predictions = predict_func(user_input, model, tokenizer, device)

#             # Squeeze predictions to remove extra dimensions
#             predictions_array = predictions.squeeze()

#             # Convert to binary predictions (argmax)
#             binary_predictions = np.zeros_like(predictions_array)
#             max_indices = np.argmax(predictions_array)
#             binary_predictions[max_indices] = 1

#             # Update progress bar for prediction and model loading
#             update_progress(progress_bar, 10, 75)

#         # Display raw predictions
#         # st.write(f"**Predicted Sentiment Scores:** {predictions_array}")
#         st.session_state.predictions = predictions_array

#         # Display binary classification result
#         # st.write(f"**Predicted Sentiment:**")
#         # st.write(f"**NEGATIVE:** {binary_predictions[0]}, **NEUTRAL:** {binary_predictions[1]}, **POSITIVE:** {binary_predictions[2]}")
#         st.session_state.binary_predictions = binary_predictions


#         # 1️⃣ **Polar Plot (Plotly)**
#         sentiment_polarities = predictions_array.tolist()
#         fig_polar = px.line_polar(
#             pd.DataFrame(dict(r=sentiment_polarities,
#                         theta=SENTIMENT_POLARITY_LABELS)),
#             r='r', theta='theta', line_close=True
#         )
#         # st.plotly_chart(fig_polar)
#         st.session_state.polar_plot = fig_polar

#         # 2️⃣ **Normalized Horizontal Bar Chart (Matplotlib)**
#         normalized_predictions = predictions_array / predictions_array.sum()

#         fig, ax = plt.subplots(figsize=(8, 2))
#         left = 0
#         for i in range(len(normalized_predictions)):
#             ax.barh(0, normalized_predictions[i], color=plt.cm.tab10(
#                 i), left=left, label=SENTIMENT_POLARITY_LABELS[i])
#             left += normalized_predictions[i]

#         # Configure the chart
#         ax.set_xlim(0, 1)
#         ax.set_yticks([])
#         ax.set_xticks(np.arange(0, 1.1, 0.1))
#         ax.legend(loc='upper center', bbox_to_anchor=(
#             0.5, -0.15), ncol=len(SENTIMENT_POLARITY_LABELS))
#         # plt.title("Sentiment Polarity Prediction Distribution")
#         # st.pyplot(fig)
#         st.session_state.bar_chart = fig
#         update_progress(progress_bar, 75, 95)

#         # progress_bar.empty()

#         if st.session_state.disabled is True:
#             st.session_state.disabled = False
#             st.rerun()
#             return
#         else:
#             return
        



#####


### COMMENTED OUT CODE ###


# def load_selected_model(model_name):
#     # """Load model and tokenizer based on user selection."""
#     global current_model, current_tokenizer

#     # Free memory before loading a new model
#     free_memory()

#     if model_name not in MODEL_OPTIONS:
#         st.error(f"⚠️ Model '{model_name}' not found in config!")
#         return None, None

#     model_info = MODEL_OPTIONS[model_name]
#     hf_location = model_info["hf_location"]

#     model_module = model_info["module_path"]
#     # load_function = "load_model"
#     # predict_function = "predict"

#     load_function = model_info["load_function"]
#     predict_function = model_info["predict_function"]

#     # tokenizer_class = globals()[model_info["tokenizer_class"]]
#     # model_class = globals()[model_info["model_class"]]

#     # tokenizer = tokenizer_class.from_pretrained(hf_location)


#     load_model_func = import_from_module(model_module, load_function)
#     predict_func = import_from_module(model_module, predict_function)

#     # # Load model
#     # if model_info["type"] == "custom_checkpoint" or model_info["type"] == "custom_model":
#     #     model = torch.load(hf_location, map_location="cpu")  # Load PyTorch model
#     # elif model_info["type"] == "hf_automodel_finetuned_dbt3":
#     #     tokenizer_class = globals()[model_info["tokenizer_class"]]
#     #     model_class = globals()[model_info["model_class"]]
#     #     tokenizer = tokenizer_class.from_pretrained(hf_location)
#     #     model = model_class.from_pretrained(hf_location,
#     #                                         problem_type=model_info["problem_type"],
#     #                                         num_labels=model_info["num_labels"]
#     #     )
#     # else:
#     #     st.error("Invalid model selection")
#     #     return None, None


#     if load_model_func is None or predict_func is None:
#         st.error("❌ Model functions could not be loaded!")
#         return None, None

#     # current_model, current_tokenizer = model, tokenizer  # Store references
#     # return model, tokenizer

#     model, tokenizer = load_model_func(hf_location)

#     current_model, current_tokenizer = model, tokenizer
#     return model, tokenizer, predict_func


# def predict(text, model, tokenizer, device, max_len=128):
#     # Tokenize and pad the input text
#     inputs = tokenizer(
#         text,
#         add_special_tokens=True,
#         padding=True,
#         truncation=False,
#         return_tensors="pt",
#         return_token_type_ids=False,
#     ).to(device)  # Move input tensors to the correct device

#     with torch.no_grad():
#         outputs = model(**inputs)

#     # Apply sigmoid activation (for BCEWithLogitsLoss)
#     probabilities = outputs.logits.cpu().numpy()

#     return probabilities

# def show_sentiment_analysis():

    # Add your sentiment analysis code here

    # user_input = st.text_input("Enter text for sentiment analysis:")
    # user_input = st.text_area("Enter text for sentiment analysis:", height=200)
    # user_input = st.text_area("Enter text for sentiment analysis:", max_chars=500)

# def show_sentiment_analysis():
#     st.title("Stage 1: Sentiment Polarity Analysis")
#     st.write("This section will handle sentiment analysis.")

#     if "selected_model" not in st.session_state:
#         st.session_state.selected_model = list(MODEL_OPTIONS.keys())[0]  # Default selection

#     if "clear_output" not in st.session_state:
#         st.session_state.clear_output = False

#     st.selectbox("Choose a model:", list(MODEL_OPTIONS.keys()), key="selected_model")

#     selected_model = st.session_state.selected_model

#     if selected_model not in MODEL_OPTIONS:
#         st.error(f"❌ Selected model '{selected_model}' not found!")
#         st.stop()

#     st.session_state.clear_output = True  # Reset output when model changes


#     # st.write("DEBUG: Available Models:", MODEL_OPTIONS.keys())  # ✅ See available models
#     # st.write("DEBUG: Selected Model:", MODEL_OPTIONS[selected_model])  # ✅ Check selected model


#     user_input = st.text_input("Enter text for sentiment analysis:")
#     user_input_copy = user_input

#     # if st.button("Run Analysis"):
#     #     if not user_input.strip():
#     #         st.warning("⚠️ Please enter some text before running analysis.")
#     #         return

#     # with st.form(key="sentiment_form"):
#     #     user_input = st.text_input("Enter text for sentiment analysis:")
#     #     submit_button = st.form_submit_button("Run Analysis")

#     #     user_input_copy = user_input

#     if user_input.strip():

#         ADD A DYNAMIC PROGRESS BAR
#         progress_bar = st.progress(0)
#         update_progress(progress_bar, 0, 10)
#         # status_text = st.empty()

#         # update_progress(0, 10)
#         # status_text.text("Loading model...")

#         # Make prediction

#         # model, tokenizer = load_model()
#         # model, tokenizer = load_selected_model(selected_model)

#         model, tokenizer, predict_func = load_selected_model(selected_model)
#         device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

#         if model is None:
#             st.error("⚠️ Error: Model failed to load! Check model selection or configuration.")
#             st.stop()

#         model.to(device)

#         # predictions = predict(user_input, model, tokenizer, device)

#         predictions = predict_func(user_input, model, tokenizer, device)

#         # Squeeze predictions to remove extra dimensions
#         predictions_array = predictions.squeeze()

#         # Convert to binary predictions (argmax)
#         binary_predictions = np.zeros_like(predictions_array)
#         max_indices = np.argmax(predictions_array)
#         binary_predictions[max_indices] = 1


#         # Update progress bar for prediction and model loading
#         update_progress(progress_bar, 10, 100)

#         # Display raw predictions
#         st.write(f"**Predicted Sentiment Scores:** {predictions_array}")

#         # Display binary classification result
#         st.write(f"**Predicted Sentiment:**")
#         st.write(f"**NEGATIVE:** {binary_predictions[0]}, **NEUTRAL:** {binary_predictions[1]}, **POSITIVE:** {binary_predictions[2]}")
#         # st.write(f"**NEUTRAL:** {binary_predictions[1]}")
#         # st.write(f"**POSITIVE:** {binary_predictions[2]}")

#         # 1️⃣ **Polar Plot (Plotly)**
#         sentiment_polarities = predictions_array.tolist()
#         fig_polar = px.line_polar(
#             pd.DataFrame(dict(r=sentiment_polarities, theta=SENTIMENT_POLARITY_LABELS)),
#             r='r', theta='theta', line_close=True
#         )
#         st.plotly_chart(fig_polar)

#         # 2️⃣ **Normalized Horizontal Bar Chart (Matplotlib)**
#         normalized_predictions = predictions_array / predictions_array.sum()

#         fig, ax = plt.subplots(figsize=(8, 2))
#         left = 0
#         for i in range(len(normalized_predictions)):
#             ax.barh(0, normalized_predictions[i], color=plt.cm.tab10(i), left=left, label=SENTIMENT_POLARITY_LABELS[i])
#             left += normalized_predictions[i]

#         # Configure the chart
#         ax.set_xlim(0, 1)
#         ax.set_yticks([])
#         ax.set_xticks(np.arange(0, 1.1, 0.1))
#         ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=len(SENTIMENT_POLARITY_LABELS))
#         plt.title("Sentiment Polarity Prediction Distribution")

#         # Display in Streamlit
#         st.pyplot(fig)

#         progress_bar.empty()