aledraa commited on
Commit
b53c3eb
·
verified ·
1 Parent(s): ace442b

Create main_kaggle.py

Browse files
Files changed (1) hide show
  1. main_kaggle.py +173 -0
main_kaggle.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import nltk
3
+ import numpy as np
4
+ import os
5
+ import kagglehub
6
+ from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSequenceClassification
7
+ from bert_score import score as bert_score_calculator
8
+
9
+ try:
10
+ nltk.data.find('tokenizers/punkt')
11
+ except nltk.downloader.DownloadError:
12
+ nltk.download('punkt')
13
+
14
+ class LLM_Generator:
15
+ def __init__(self, model_handle, device='cuda'):
16
+ self.device = device
17
+ print(f"Downloading model from Kaggle Hub: {model_handle}")
18
+ model_path = kagglehub.model_download(model_handle)
19
+ print(f"Model downloaded to: {model_path}")
20
+
21
+ self.tokenizer = AutoTokenizer.from_pretrained(model_path)
22
+ self.model = AutoModelForCausalLM.from_pretrained(
23
+ model_path,
24
+ torch_dtype="auto",
25
+ device_map="auto"
26
+ )
27
+
28
+ def generate(self, prompt, num_samples=1, temperature=0.7, max_new_tokens=150):
29
+ messages = [
30
+ {"role": "system", "content": "you are a helpful assistant."},
31
+ {"role": "user", "content": prompt}
32
+ ]
33
+ text = self.tokenizer.apply_chat_template(
34
+ messages,
35
+ tokenize=False,
36
+ add_generation_prompt=True,
37
+ enable_thinking=True
38
+ )
39
+ model_inputs = self.tokenizer([text] * num_samples, return_tensors="pt").to(self.device)
40
+
41
+ generated_ids_batch = self.model.generate(
42
+ **model_inputs,
43
+ max_new_tokens=max_new_tokens,
44
+ do_sample=True,
45
+ temperature=temperature,
46
+ num_return_sequences=num_samples
47
+ )
48
+
49
+ input_ids_len = model_inputs.input_ids.shape[1]
50
+ final_responses = []
51
+
52
+ for generated_ids in generated_ids_batch:
53
+ output_ids = generated_ids[input_ids_len:].tolist()
54
+
55
+ try:
56
+ # Find the start of the final content after the "thinking" part
57
+ # The token ID 151668 corresponds to the end of the thinking block for Qwen-3
58
+ index = len(output_ids) - output_ids[::-1].index(151668)
59
+ except ValueError:
60
+ index = 0
61
+
62
+ content = self.tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
63
+ final_responses.append(content)
64
+
65
+ return final_responses
66
+
67
+ class SelfCheckGPT:
68
+ def __init__(self, device=None):
69
+ if device:
70
+ self.device = device
71
+ else:
72
+ self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
73
+
74
+ self.nli_tokenizer = None
75
+ self.nli_model = None
76
+
77
+ def _load_nli_model(self):
78
+ if self.nli_model is None:
79
+ nli_model_name = "microsoft/deberta-v3-large-mnli"
80
+ try:
81
+ self.nli_tokenizer = AutoTokenizer.from_pretrained(nli_model_name)
82
+ self.nli_model = AutoModelForSequenceClassification.from_pretrained(nli_model_name).to(self.device)
83
+ except Exception as e:
84
+ print(f"Error loading NLI model: {e}")
85
+ raise
86
+
87
+ def _check_bertscore(self, sentences, sample_responses):
88
+ all_scores = []
89
+ for sent in sentences:
90
+ refs = [sent] * len(sample_responses)
91
+ cands = sample_responses
92
+
93
+ _, _, F1 = bert_score_calculator(
94
+ cands, refs, lang="en", verbose=False, idf=False, device=self.device
95
+ )
96
+
97
+ avg_bert_score = F1.mean().item()
98
+ score = 1.0 - avg_bert_score
99
+ all_scores.append(score)
100
+ return all_scores
101
+
102
+ def _check_nli(self, sentences, sample_responses):
103
+ self._load_nli_model()
104
+ all_scores = []
105
+
106
+ for sent in sentences:
107
+ contradiction_probs = []
108
+ for sample in sample_responses:
109
+ tokenized_input = self.nli_tokenizer(
110
+ sample, sent, return_tensors="pt", truncation=True, max_length=512
111
+ ).to(self.device)
112
+
113
+ with torch.no_grad():
114
+ logits = self.nli_model(**tokenized_input).logits
115
+
116
+ entailment_logit = logits[0, self.nli_model.config.label2id['entailment']]
117
+ contradiction_logit = logits[0, self.nli_model.config.label2id['contradiction']]
118
+
119
+ prob_contradiction = torch.exp(contradiction_logit) / (torch.exp(entailment_logit) + torch.exp(contradiction_logit))
120
+ contradiction_probs.append(prob_contradiction.item())
121
+
122
+ avg_contradiction_prob = np.mean(contradiction_probs)
123
+ all_scores.append(avg_contradiction_prob)
124
+
125
+ return all_scores
126
+
127
+ def check(self, main_response, sample_responses, method='nli'):
128
+ sentences = nltk.sent_tokenize(main_response)
129
+ if not sentences:
130
+ return []
131
+
132
+ if method.lower() == 'bertscore':
133
+ scores = self._check_bertscore(sentences, sample_responses)
134
+ elif method.lower() == 'nli':
135
+ scores = self._check_nli(sentences, sample_responses)
136
+ else:
137
+ raise ValueError(f"Invalid method '{method}'. Choose from 'bertscore', 'nli'.")
138
+
139
+ results = [{"sentence": sent, "score": score} for sent, score in zip(sentences, scores)]
140
+ return results
141
+
142
+ def main():
143
+ model_handle = "qwen-lm/qwen-3/transformers/0.6b"
144
+
145
+ print("Initializing LLM Generator...")
146
+ generator = LLM_Generator(model_handle=model_handle)
147
+
148
+ prompt = "Write a short biography of Neil Armstrong, the first man on the moon. Include the name of the spacecraft he used."
149
+ print(f"Generating responses for prompt: '{prompt}'")
150
+
151
+ responses = generator.generate(prompt, num_samples=6, temperature=0.8, max_new_tokens=150)
152
+ main_response = responses[0]
153
+ sample_responses = responses[1:]
154
+
155
+ print("\n--- Generated Main Response ---")
156
+ print(main_response)
157
+ print("\n--- Generated Sample Responses ---")
158
+ for i, r in enumerate(sample_responses):
159
+ print(f"{i+1}. {r[:100]}...")
160
+
161
+ checker = SelfCheckGPT()
162
+
163
+ print("\n\n--- Running SelfCheckGPT with 'nli' method ---")
164
+ nli_results = checker.check(main_response, sample_responses, method='nli')
165
+ print("Higher scores suggest a higher probability of being a hallucination.")
166
+ for result in nli_results:
167
+ print(f"Score: {result['score']:.4f}\tSentence: {result['sentence']}")
168
+
169
+ print("\n--- Running SelfCheckGPT with 'bertscore' method ---")
170
+ bertscore_results = checker.check(main_response, sample_responses, method='bertscore')
171
+ print("Higher scores suggest a higher probability of being a hallucination.")
172
+ for result in bertscore_results:
173
+ print(f"Score: {result['score']:.4f}\tSentence: {result['sentence']}")