File size: 31,010 Bytes
5c7385e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import numpy as np
from typing import Dict, Any
import math
import re
import os
from os.path import isdir
import transformers
from .base import ModelBase
import traceback
from huggingface_hub import login, HfFolder
from transformers import (
      BitsAndBytesConfig,
      AutoModelForCausalLM,
      LlamaTokenizer,
      AutoTokenizer,
      AutoConfig,
      LlamaForCausalLM
  )
from torch.nn.functional import log_softmax
from transformers.generation.logits_process import LogitsProcessor, LogitsProcessorList

def setup_hf_authentication():
    """
    Setup Hugging Face authentication for gated models like Llama.
    Tries multiple authentication methods in order of preference.
    """
    # Method 1: Check if already authenticated
    try:
        token = HfFolder.get_token()
        if token:
            print("✓ Already authenticated with Hugging Face")
            return True
    except:
        pass
    
    # Method 2: Try environment variable
    hf_token = os.getenv('HF_TOKEN') or os.getenv('HUGGINGFACE_HUB_TOKEN')
    if hf_token:
        try:
            login(token=hf_token, add_to_git_credential=False)
            print("✓ Authenticated with HF_TOKEN environment variable")
            return True
        except Exception as e:
            print(f"⚠ Failed to authenticate with HF_TOKEN: {e}")
    
    # Method 3: Check for local token file
    try:
        login(add_to_git_credential=False)
        print("✓ Authenticated with local Hugging Face credentials")
        return True
    except Exception as e:
        print(f"⚠ No local Hugging Face credentials found: {e}")
    
    print("⚠ No Hugging Face authentication found. Gated models may fail to load.")
    print("💡 For Hugging Face Spaces: Set HF_TOKEN in your Space settings")
    print("💡 For local development: Run 'huggingface-cli login' or set HF_TOKEN environment variable")
    return False

class BERTModel(ModelBase):
    """Model wrapper for BERT-based classifiers"""
   
    def __init__(self, model, tokenizer, id2label=None, max_length=512):
        """
        Initialize BERT-based classifier       
        Args:
            model: BERT-based financial classifier model: FinBert, DeBERTa, DistilRoBERTa, etc.,
            tokenizer: BERT tokenizer
            id2label: Label mapping dictionary
            max_length: Maximum sequence length
        """
        self.model = model
        self.tokenizer = tokenizer
        self.max_length = max_length
        self.device = model.device
       
        if torch.cuda.is_available():
            if not str(self.device).startswith('cuda'):
                print(f"Warning: Model not on GPU. Moving to GPU...")
                self.model = self.model.cuda()
                self.device = self.model.device
            print(f"Model running on: {self.device}")
       
        # Set label mapping
        self.id2label = id2label or getattr(model.config, "id2label", {0: "positive", 1: "negative", 2: "neutral"})
   
    def generate(self, prompt: str) -> Dict[str, Any]:
        """
        Generate prediction for prompt with probabilities
       
        Args:
            prompt: Input text
           
        Returns:
            Dictionary containing predicted label and probabilities
        """
        # Tokenize input
        inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=self.max_length)
        # Move to model's device
        inputs = {k: v.to(self.device) for k, v in inputs.items()}

        # Generate prediction
        with torch.no_grad():
            outputs = self.model(**inputs)
        logits = outputs.logits
        probabilities = torch.nn.functional.softmax(logits, dim=1)[0].cpu().numpy()
       
        pred_idx = torch.argmax(logits, dim=1).item()  
        # Get label string
        if pred_idx in self.id2label:
            predicted_label = self.id2label[pred_idx]
        elif str(pred_idx) in self.id2label:
            predicted_label = self.id2label[str(pred_idx)]
        else:
            predicted_label = str(pred_idx)
       
        result = {
            "label": predicted_label,
            "probabilities": {self.id2label[i] if i in self.id2label else (self.id2label[str(i)] if str(i) in self.id2label else str(i)):
                             float(prob) for i, prob in enumerate(probabilities)}
        }        
        return result

    def generate_batch(self, prompts):
        """Generate predictions for multiple prompts at once"""
        inputs = self.tokenizer(prompts, return_tensors="pt", padding=True, truncation=True, max_length=self.max_length)
        inputs = {k: v.to(self.device) for k, v in inputs.items()}
        with torch.no_grad():
            outputs = self.model(**inputs)
        logits = outputs.logits
        probs = torch.nn.functional.softmax(logits, dim=1).cpu().numpy()
        pred_idxs = np.argmax(probs, axis=1)
        results = []
        for i in range(len(prompts)):
            pred_idx = pred_idxs[i]
            if pred_idx in self.id2label:
                predicted_label = self.id2label[pred_idx]
            elif str(pred_idx) in self.id2label:
                predicted_label = self.id2label[str(pred_idx)]
            else:
                predicted_label = str(pred_idx)
            results.append({
                "label": predicted_label,
                "probabilities": {self.id2label[j] if j in self.id2label else (self.id2label[str(j)] if str(j) in self.id2label else str(j)): float(probs[i][j]) for j in range(len(probs[i]))}
            })
        return results

class LlamaModelWrapper:
    """
    Wrapper for quantized Llama financial models that predict sentiment using fixed label tokens.
    """
    def __init__(self, model, tokenizer, label_ids, max_length=512):
        """
        label_ids: dict mapping label names (e.g., 'positive') to tokenizer IDs
        """
        self.model = model
        self.tokenizer = tokenizer
        self.label_ids = label_ids  # e.g., {'positive': 6374, ...}
        self.max_length = max_length
        self.device = model.device
        vocab_size = self.tokenizer.vocab_size
        if (self.tokenizer.pad_token_id is None or self.tokenizer.pad_token_id < 0 or self.tokenizer.pad_token_id >= vocab_size):
            self.tokenizer.pad_token = self.tokenizer.convert_ids_to_tokens(2)
            self.tokenizer.pad_token_id = 2

    # ---------- Debug helper ----------
    def _print_topk_for_step(self, step_logits, tokenizer, k=30, header=None):
        if header:
            print(header)
        topk_vals, topk_idx = torch.topk(step_logits, k=min(k, step_logits.shape[-1]))
        print("\n[DEBUG] Top tokens at this step:")
        for rank in range(topk_vals.numel()):
            tid = topk_idx[rank].item()
            tok = tokenizer.decode([tid])
            print(f"{rank+1:2d}. id {tid:>5}: {repr(tok)} (logit={topk_vals[rank].item():.4f})")

    # ---------- Build label token sequences dynamically ----------
    def _build_label_sequences(self, tokenizer):
        variants = {
            "Positive": [" positive", "positive", "Positive", " positive.", "Positive."],
            "Negative": [" negative", "negative", "Negative", " negative.", "Negative."],
            "Neutral":  [" neutral",  "neutral",  "Neutral",  " neutral.",  "Neutral."],
        }
        seqs = {}
        for lab, forms in variants.items():
            seen, cand = set(), []
            for s in forms + [lab.lower()]:
                ids = tokenizer.encode(s, add_special_tokens=False)
                if ids:
                    t = tuple(ids)
                    if t not in seen:
                        seen.add(t)
                        cand.append(ids)
            seqs[lab] = cand
        return seqs

    # ---------- Span finder over generated token ids ----------
    def _find_label_span(self, new_ids, label_seqs):
        best = (None, None, None)  # (label, start_pos, seq_used)
        n = len(new_ids)
        for label, seq_list in label_seqs.items():
            for seq in seq_list:
                m = len(seq)
                if m == 0 or m > n:
                    continue
                for i in range(0, n - m + 1):
                    if new_ids[i:i+m] == seq:
                        if best[1] is None or i < best[1]:
                            best = (label, i, seq)
                        break
        return best

    # ---------- build label-id sets from label mapping ----------
    def _build_label_id_sets(self):
        # {"Positive":[6374], "Negative":[8178,22198], "Neutral":[21104]}
        lab_sets = {"Positive": set(), "Negative": set(), "Neutral": set()}
        for k, ids in self.label_ids.items():
            lab = k.capitalize()
            for t in (ids if isinstance(ids, list) else [ids]):
                lab_sets[lab].add(int(t))
        union = set().union(*lab_sets.values())
        return lab_sets, union

    # ---------- Logits processor to force label on the FIRST step ----------
    class FirstStepLabelOnly(LogitsProcessor):
        """
        At the FIRST generation step, allow only tokens that are valid FIRST tokens
        of any label variant (e.g., 'positive', 'negative', 'neutral', or cased/dotted forms).
        Later steps are unconstrained.
        """
        def __init__(self, allowed_first_token_ids):
            super().__init__()
            self.allowed = None
            if allowed_first_token_ids:
                self.allowed = torch.tensor(sorted(set(allowed_first_token_ids)), dtype=torch.long)

        def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
            if self.allowed is None:
                return scores
            mask = torch.full_like(scores, float("-inf"))
            mask[:, self.allowed] = 0.0
            return scores + mask

    def _restricted_label_softmax(self, step_logits):
        """
        Compute P(label | step) using only the label token logits.
        Handles multi-id Negative via log-sum-exp over its ids.
        """
        pos_ids = self.label_ids["Positive"] if isinstance(self.label_ids["Positive"], list) else [self.label_ids["Positive"]]
        neg_ids = self.label_ids["Negative"] if isinstance(self.label_ids["Negative"], list) else [self.label_ids["Negative"]]
        neu_ids = self.label_ids["Neutral"]  if isinstance(self.label_ids["Neutral"], list) else [self.label_ids["Neutral"]]

        # pull logits
        v_pos = step_logits[pos_ids[0]].item() 
        v_neu = step_logits[neu_ids[0]].item() 

        # Negative can have multiple ids -> log-sum-exp across them
        neg_vec = step_logits[torch.tensor(neg_ids, dtype=torch.long, device=step_logits.device)]
        v_neg = torch.logsumexp(neg_vec, dim=0).item()

        # softmax across the three label scores
        m = max(v_pos, v_neg, v_neu)
        s_pos = math.exp(v_pos - m)
        s_neg = math.exp(v_neg - m)
        s_neu = math.exp(v_neu - m)
        Z = s_pos + s_neg + s_neu

        probs = {
            "Positive": s_pos / Z,
            "Negative": s_neg / Z,
            "Neutral":  s_neu / Z,
        }
        return probs


    def generate(self, prompt, debug=True, topk=30, enforce_label_first_token=True):
        tokenizer, model, device = self.tokenizer, self.model, self.device

        # Build label text variants and allowed first-token ids (for step-0 constraint)
        label_seqs = self._build_label_sequences(tokenizer)
        allowed_first_ids = list({seq[0] for seqs in label_seqs.values() for seq in seqs if len(seq) > 0})

        # Label id sets and skip-set (EOS + empty)
        label_id_sets, label_union = self._build_label_id_sets()
        EOS_TID = getattr(tokenizer, "eos_token_id", 2)
        EMPTY_TID = 29871
        SKIP_TIDS = {EOS_TID, EMPTY_TID}

        if debug:
            print(f"Processing 1 prompt")

        try:
            enc = tokenizer(
                [prompt],
                return_tensors="pt",
                padding=True,          
                truncation=True,
                max_length=self.max_length
            ).to(device)

            lp = None
            if enforce_label_first_token:
                lp = LogitsProcessorList([self.FirstStepLabelOnly(allowed_first_ids)])

            with torch.no_grad():
                out = model.generate(
                    **enc,
                    max_new_tokens=2,        
                    min_new_tokens=1,        
                    do_sample=False,        
                    output_scores=True,
                    return_dict_in_generate=True,
                    logits_processor=lp,
                    eos_token_id=getattr(tokenizer, "eos_token_id", None),
                    pad_token_id=getattr(tokenizer, "eos_token_id", None),
                )

            sequences   = out.sequences                  # [1, seq_len]
            scores_list = out.scores                     # list len==gen_steps; each [1, V]
            gen_steps   = len(scores_list)

            seq_ids_all = sequences[0].tolist()
            gen_ids     = seq_ids_all[-gen_steps:] if gen_steps > 0 else []

            answer_part = tokenizer.decode(gen_ids,    skip_special_tokens=True, clean_up_tokenization_spaces=False).strip()
            full_text   = tokenizer.decode(seq_ids_all, skip_special_tokens=True, clean_up_tokenization_spaces=False)

            if debug:
                print(f"\n— Prompt [0] generated answer: {repr(answer_part)}  gen_ids={gen_ids}")

            # pick the first sentiment token id within the generated window, skipping EOS/empty
            pos = None
            for i, tid in enumerate(gen_ids):
                tid = int(tid)
                if tid in SKIP_TIDS:
                    continue
                if tid in label_union:
                    pos = i
                    if debug:
                        print(f"[ANCHOR] pos={pos} (tid={tid}) within generated window; skipped {SKIP_TIDS}")
                    break

            # if still not found, try text span finder among variants (within the generated window)
            if pos is None and gen_steps > 0:
                label_found_span, pos_span, _ = self._find_label_span(gen_ids, label_seqs)
                if (label_found_span is not None) and (pos_span is not None) and (pos_span < gen_steps):
                    pos = pos_span
                    if debug:
                        print(f"[ANCHOR] pos={pos} (from span finder in generated window)")

            # ----- Scoring at anchor step or fallback -----
            if pos is not None and gen_steps > 0 and pos < gen_steps:
                step_logits = scores_list[pos][0]
                prob_dict = self._restricted_label_softmax(step_logits)
                logits_sentiment = max(prob_dict, key=prob_dict.get)

                if debug:
                    self._print_topk_for_step(step_logits, tokenizer, k=topk,
                        header=f"\n==== TOP-K (ANCHOR STEP {pos}) ====")
                    print(f"[P(Positive), P(Negative), P(Neutral)] = "
                        f"{prob_dict['Positive']}, {prob_dict['Negative']}, {prob_dict['Neutral']}")

            else:
                # fallback: use first step’s logits
                if gen_steps == 0:
                    prob_dict = {"Positive": 1/3, "Negative": 1/3, "Neutral": 1/3}
                    logits_sentiment = "Neutral"
                else:
                    step0 = scores_list[0][0]
                    if debug:
                        self._print_topk_for_step(step0, tokenizer, k=topk,
                            header="\n==== FIRST-STEP FALLBACK TOP-K ====")
                    prob_dict = self._restricted_label_softmax(step0)
                    logits_sentiment = max(prob_dict, key=prob_dict.get)
                pos = 0

            # surface label from generated text
            al = answer_part.lower()
            if   "positive" in al: text_label = "Positive"
            elif "negative" in al: text_label = "Negative"
            elif "neutral"  in al: text_label = "Neutral"
            else:                   text_label = "NA"

            is_match = (text_label == logits_sentiment)
            if debug:
                print(f"\n[RESULT] text={text_label}  logits={logits_sentiment}  match={is_match}")

            return {
                "label": text_label,
                "probabilities": prob_dict,
                "generated_text": full_text,
                "answer_part": answer_part,
                "sentiment_position": pos,
                "match": is_match,
            }

        except Exception as e:
            import traceback
            traceback.print_exc()
            return {
                "label": "ERROR",
                "probabilities": {"Positive": 1/3, "Negative": 1/3, "Neutral": 1/3},
                "generated_text": f"Error: {str(e)}",
                "answer_part": "",
                "sentiment_position": 0,
                "match": False,
            }

    def generate_batch(self, prompts, batch_size=128, debug=True, topk=30, enforce_label_first_token=True):
        tokenizer, model, device = self.tokenizer, self.model, self.device
        label_seqs = self._build_label_sequences(tokenizer)

        # Allowed first-token ids: first id of every variant of every label
        allowed_first_ids = list({seq[0] for seqs in label_seqs.values() for seq in seqs if len(seq) > 0})

        # Label id sets and skip-set
        label_id_sets, label_union = self._build_label_id_sets()
        EOS_TID = getattr(tokenizer, "eos_token_id", 2)
        EMPTY_TID = 29871
        SKIP_TIDS = {EOS_TID, EMPTY_TID}

        if debug:
            print(f"Processing {len(prompts)} prompts with batch_size={batch_size}")

        all_results = []
        true_matches = 0
        false_matches = 0
        for start in range(0, len(prompts), batch_size):
            batch_prompts = prompts[start:start+batch_size]
            if debug:
                print(f"\nProcessing batch {start//batch_size + 1}/{(len(prompts)-1)//batch_size + 1} "
                    f"({len(batch_prompts)} prompts)")

            try:
                batch_inputs = tokenizer(
                    batch_prompts,
                    return_tensors="pt",
                    padding=True,          
                    truncation=True,
                    max_length=self.max_length
                ).to(device)

                input_lengths = batch_inputs["attention_mask"].sum(dim=1).tolist()

                lp = None
                if enforce_label_first_token:
                    lp = LogitsProcessorList([self.FirstStepLabelOnly(allowed_first_ids)])

                with torch.no_grad():
                    outputs = model.generate(
                        **batch_inputs,
                        max_new_tokens=2,       
                        min_new_tokens=1,       
                        do_sample=False,
                        output_scores=True,
                        return_dict_in_generate=True,
                        logits_processor=lp,
                        eos_token_id=getattr(tokenizer, "eos_token_id", None),
                        pad_token_id=getattr(tokenizer, "eos_token_id", None)
                    )

                sequences    = outputs.sequences                    # [B, in_len + gen_len]
                scores_list  = outputs.scores                       # list len==gen_len; each [B, V]
                gen_steps    = len(scores_list)
                logprob_list = [log_softmax(s, dim=-1) for s in scores_list] if gen_steps > 0 else []

                bsz_now = sequences.size(0)
                assert bsz_now == len(batch_prompts)

                for b in range(bsz_now):
                    seq_ids_all = sequences[b].tolist()

                    gen_ids = seq_ids_all[-gen_steps:] if gen_steps > 0 else []

                    answer_part = tokenizer.decode(gen_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False).strip()
                    full_text   = tokenizer.decode(seq_ids_all, skip_special_tokens=True, clean_up_tokenization_spaces=False)

                    if debug:
                        print(f"\n— Prompt [{b}] generated answer: {repr(answer_part)}  gen_ids={gen_ids}")

                    # === pick the first *label* token within the generated window, skipping {eos, ''} ===
                    pos = None
                    for i, tid in enumerate(gen_ids):
                        tid = int(tid)
                        if tid in SKIP_TIDS:
                            continue
                        if tid in label_union:
                            pos = i
                            if debug: print(f"[ANCHOR] pos={pos} (tid={tid}) within generated window; skipped {SKIP_TIDS}")
                            break

                    # If still not found, try span finder inside the generated window
                    if pos is None and gen_steps > 0:
                        label_found_span, pos_span, _ = self._find_label_span(gen_ids, label_seqs)
                        if (label_found_span is not None) and (pos_span is not None) and (pos_span < gen_steps):
                            pos = pos_span
                            if debug: print(f"[ANCHOR] pos={pos} (from span finder in generated window)")

                    if pos is not None and gen_steps > 0 and pos < gen_steps:
                        step_logits = scores_list[pos][b]
                        prob_dict = self._restricted_label_softmax(step_logits)
                        logits_sentiment = max(prob_dict, key=prob_dict.get)

                        if debug:
                            self._print_topk_for_step(step_logits, tokenizer, k=topk,
                                header=f"\n==== TOP-K (ANCHOR STEP {pos}) ====")
                            print(f"[P(Positive), P(Negative), P(Neutral)] = "
                                f"{prob_dict['Positive']}, {prob_dict['Negative']}, {prob_dict['Neutral']}")
 
                        # surface label from text
                        al = answer_part.lower()
                        if   "positive" in al: text_label = "Positive"
                        elif "negative" in al: text_label = "Negative"
                        elif "neutral"  in al: text_label = "Neutral"
                        else:                   text_label = "NA"

                        is_match = (text_label == logits_sentiment)  # NEW

                        if debug:
                            print(f"\n[RESULT] text={text_label}  logits={logits_sentiment}  match={text_label==logits_sentiment}")

                        if is_match: true_matches += 1
                        else:        false_matches += 1

                        all_results.append({
                            "label": text_label,
                            "probabilities": prob_dict,
                            "generated_text": full_text,
                            "answer_part": answer_part,
                            "sentiment_position": pos if pos is not None else 0,
                            "match": (text_label == logits_sentiment),
                        })

                    else:
                        # fallback using first step
                        if gen_steps == 0:
                            prob_dict = {"Positive": 1/3, "Negative": 1/3, "Neutral": 1/3}
                            logits_sentiment = "NG"
                        else:
                            step0 = scores_list[0][b]
                            if debug:
                                self._print_topk_for_step(step0, tokenizer, k=topk,
                                    header="\n==== FIRST-STEP FALLBACK TOP-K ====")
                            prob_dict = self._restricted_label_softmax(step0)
                            logits_sentiment = max(prob_dict, key=prob_dict.get)
                        al = answer_part.lower()
                        if   "positive" in al: text_label = "Positive"
                        elif "negative" in al: text_label = "Negative"
                        elif "neutral"  in al: text_label = "Neutral"
                        else:                   text_label = "NA"
                        is_match = (text_label == logits_sentiment)  

                        if debug:
                            print(f"\n[RESULT] (fallback) text={text_label}  logits={logits_sentiment}  match={text_label==logits_sentiment}")
                        if is_match: true_matches += 1
                        else:        false_matches += 1
                        all_results.append({
                            "label": text_label,
                            "probabilities": prob_dict,
                            "generated_text": full_text,
                            "answer_part": answer_part,
                            "sentiment_position": 0,
                            "match": (text_label == logits_sentiment),
                        })

            except Exception as e:
                traceback.print_exc()
                all_results.extend([
                    {
                        "label": "ERROR",
                        "probabilities": {"Positive": 1/3, "Negative": 1/3, "Neutral": 1/3},
                        "generated_text": f"Error in batch {start//batch_size + 1}: {str(e)}",
                        "answer_part": ""
                    }
                    for _ in batch_prompts
                ])

        if debug:
            total = true_matches + false_matches
            acc = (true_matches / total) if total else 0.0
            print(f"\n[STATS] match=True: {true_matches} | match=False: {false_matches} |" 
                f"accuracy={acc:.3%} over {total} scored items") 
        return all_results


def load_llama_model(base_tokenizer_id, model_id, cache_dir, device_map="auto", **kwargs):
    """
    Loads a quantized Llama model with tokenizer, bypassing auto-detection.
    """    
    setup_hf_authentication()
    
    # Load the tokenizer
    try:
        hf_token = os.getenv('HF_TOKEN') or os.getenv('HUGGINGFACE_HUB_TOKEN')
        token_kwargs = {'token': hf_token} if hf_token else {}
        
        tok = LlamaTokenizer.from_pretrained(base_tokenizer_id, **token_kwargs, **kwargs)
    except Exception as e:
        print(f"LlamaTokenizer failed: {e}, trying AutoTokenizer...")
        try:
            tok = AutoTokenizer.from_pretrained(base_tokenizer_id, **token_kwargs, **kwargs)
        except Exception as e2:
            print(f"⚠ Tokenizer loading failed. This might be due to missing authentication for gated models.")
            print(f"Original error: {e2}")
            raise e2

    if tok.pad_token is None:
        tok.pad_token = tok.eos_token

    bnb_config = BitsAndBytesConfig(
                load_in_4bit=True,
                bnb_4bit_use_double_quant=True,
                bnb_4bit_quant_type="nf4",
                bnb_4bit_compute_dtype=torch.bfloat16,
            )
            
    # Load the model with explicit class instead of Auto
    try:
        # Try loading with BitsAndBytesConfig
        try:            
            mod = LlamaForCausalLM.from_pretrained(
                model_id,
                trust_remote_code=True,
                use_safetensors=True,
                quantization_config=bnb_config,
                low_cpu_mem_usage=True,
                device_map=device_map,
                **token_kwargs,  # Added token authentication
                **kwargs
            )
            
        except (ImportError, AttributeError):
            # Direct params approach
            mod = LlamaForCausalLM.from_pretrained(
                model_id,
                trust_remote_code=True,
                use_safetensors=True,
                load_in_4bit=True,
                bnb_4bit_use_double_quant=True,
                bnb_4bit_quant_type="nf4",
                bnb_4bit_compute_dtype=torch.bfloat16,
                low_cpu_mem_usage=True,
                device_map=device_map,
                **token_kwargs,  # Added token authentication
                **kwargs
            )
            
    except Exception as e:
        print(f"Failed to load with LlamaForCausalLM: {e}")
        # As a last resort, use AutoModel with config_overrides        
        try:
            mod = AutoModelForCausalLM.from_pretrained(
                model_id,
                quantization_config=bnb_config,
                trust_remote_code=True,
                device_map=device_map,
                low_cpu_mem_usage=True,
                **token_kwargs,  # Added token authentication
                **kwargs
            )
        except Exception as e2:
            print(f"⚠ Model loading failed. This might be due to missing authentication for gated models.")
            print(f"Original error: {e2}")
            raise e2
        
    print(f"Model loaded successfully to {device_map}")
    return mod, tok

def load_bert_model(model_name: str):
    """
    Load bert-based model and tokenizer
   
    Args:
        model_name: HuggingFace model name
       
    Returns:
        Tuple of (model, tokenizer)
    """
    hf_token = os.getenv('HF_TOKEN') or os.getenv('HUGGINGFACE_HUB_TOKEN')
    token_kwargs = {'token': hf_token} if hf_token else {}
    
    try:
        tokenizer = transformers.AutoTokenizer.from_pretrained(model_name, **token_kwargs)
        model = transformers.AutoModelForSequenceClassification.from_pretrained(model_name, **token_kwargs)
    except Exception as e:
        print(f"⚠ BERT model loading failed: {e}")
        print("This might be due to missing authentication for gated models.")
        raise e
    
    # Move to GPU if available
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = model.to(device)

    return model, tokenizer

def checkModelType(model) -> str:
    """
    Determine the model type by examining the config and class name
   
    Args:
        model: HuggingFace model
       
    Returns:
        String indicating model type ('bert', 'llama', etc.)
    """
    # Get model class name as a string
    model_class = model.__class__.__name__.lower()
   
    # Check config type if available
    if hasattr(model, 'config'):
        model_type = getattr(model.config, 'model_type', '').lower()
       
        # Return based on config's model_type
        if 'bert' in model_type:
            return 'bert'
        elif 'llama' in model_type:
            return 'llama'
   
    # Fallback to class name check
    if 'bert' in model_class:
        return 'bert'
    elif 'llama' in model_class:
        return 'llama'
   
    # If still can't determine, print debug info
    print(f"Unknown model type: {model_class}")
    if hasattr(model, 'config'):
        print(f"Config type: {getattr(model.config, 'model_type', 'unknown')}")
   
    return 'unknown'