Views
No views yet
1special_tokens_dict = {
2 'eos_token': '<|STOP|>',
3 'bos_token': '<|STOP|>',
4 'pad_token': '<|PAD|>',
5 'additional_special_tokens': ['<|BEGIN_QUERY|>', '<|BEGIN_QUERY|>',
6 '<|BEGIN_ANALYSIS|>', '<|END_ANALYSIS|>',
7 '<|BEGIN_RESPONSE|>', '<|END_RESPONSE|>',
8 '<|BEGIN_SENTIMENT|>', '<|END_SENTIMENT|>',
9 '<|BEGIN_CLASSIFICATION|>', '<|END_CLASSIFICATION|>',]
10}
11
12tokenizer.add_special_tokens(special_tokens_dict)
13model.resize_token_embeddings(len(tokenizer))
14
15tokenizer.eos_token_id = tokenizer.convert_tokens_to_ids('<|STOP|>')
16tokenizer.bos_token_id = tokenizer.convert_tokens_to_ids('<|STOP|>')
17tokenizer.pad_token_id = tokenizer.convert_tokens_to_ids('<|PAD|>')1def combine_text(user_prompt, analysis, sentiment, new_response, classification):
2 user_q = f"<|STOP|><|BEGIN_QUERY|>{user_prompt}<|END_QUERY|>"
3 analysis = f"<|BEGIN_ANALYSIS|>{analysis}<|END_ANALYSIS|>"
4 new_response = f"<|BEGIN_RESPONSE|>{new_response}<|END_RESPONSE|>"
5 sentiment = f"<|BEGIN_SENTIMENT|>Sentiment: {sentiment}<|END_SENTIMENT|><|STOP|>"
6 classification = f"<|BEGIN_CLASSIFICATION|>{classification}<|END_CLASSIFICATION|>"
7 return user_q + analysis + new_response + classification + sentiment1import torch
2from transformers import AutoModelForCausalLLM, AutoTokenizer
3
4models_folder = "Deeokay/DialoGPT-special-tokens-medium4"
5
6model = AutoModelForCausalLM.from_pretrained(models_folder)
7tokenizer = AutoTokenizer.from_pretrained(models_folder)
8
9# Device configuration <<change as needed>>
10device = torch.device("cpu")
11model.to(device)
121import time
2
3class Stopwatch:
4 def __init__(self):
5 self.start_time = None
6 self.end_time = None
7
8 def start(self):
9 self.start_time = time.time()
10
11 def stop(self):
12 self.end_time = time.time()
13
14 def elapsed_time(self):
15 if self.start_time is None:
16 return "Stopwatch hasn't been started"
17 if self.end_time is None:
18 return "Stopwatch hasn't been stopped"
19 return self.end_time - self.start_time
20
21stopwatch1 = Stopwatch()
22
23def generate_response(input_text, max_length=250):
24
25 stopwatch1.start()
26
27 # Prepare the input
28 # input_text = f"<|BEGIN_QUERY|>{input_text}<|END_QUERY|><|BEGIN_ANALYSIS|>{input_text}<|END_ANALYSIS|><|BEGIN_RESPONSE|>"
29 input_text = f"<|BEGIN_QUERY|>{input_text}<|END_QUERY|><|BEGIN_ANALYSIS|>"
30
31 input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
32
33 # Create attention mask
34 attention_mask = torch.ones_like(input_ids).to(device)
35
36 # Generate
37 output = model.generate(
38 input_ids,
39 max_new_tokens=max_length,
40 num_return_sequences=1,
41 no_repeat_ngram_size=2,
42 attention_mask=attention_mask,
43 pad_token_id=tokenizer.eos_token_id,
44 eos_token_id=tokenizer.convert_tokens_to_ids('<|STOP|>'),
45 )
46
47 stopwatch1.stop()
48 return tokenizer.decode(output[0], skip_special_tokens=False)1import time
2
3class Stopwatch:
4 def __init__(self):
5 self.start_time = None
6 self.end_time = None
7
8 def start(self):
9 self.start_time = time.time()
10
11 def stop(self):
12 self.end_time = time.time()
13
14 def elapsed_time(self):
15 if self.start_time is None:
16 return "Stopwatch hasn't been started"
17 if self.end_time is None:
18 return "Stopwatch hasn't been stopped"
19 return self.end_time - self.start_time
20
21stopwatch2 = Stopwatch()
22
23def generate_response2(input_text, max_length=250):
24
25 stopwatch2.start()
26
27 # Prepare the input
28 # input_text = f"<|BEGIN_QUERY|>{input_text}<|END_QUERY|><|BEGIN_ANALYSIS|>{input_text}<|END_ANALYSIS|><|BEGIN_RESPONSE|>"
29 input_text = f"<|BEGIN_QUERY|>{input_text}<|END_QUERY|><|BEGIN_ANALYSIS|>"
30 input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
31
32 # Create attention mask
33 attention_mask = torch.ones_like(input_ids).to(device)
34
35 # # 2ND OPTION FOR : Generate
36 output = model.generate(
37 input_ids,
38 max_new_tokens=max_length,
39 attention_mask=attention_mask,
40 do_sample=True,
41 temperature=0.4,
42 top_k=60,
43 no_repeat_ngram_size=2,
44 pad_token_id=tokenizer.pad_token_id,
45 eos_token_id=tokenizer.eos_token_id,
46 )
47
48 stopwatch2.stop()
49 return tokenizer.decode(output[0], skip_special_tokens=False)1def decode(text):
2 full_text = text
3
4 # Extract the response part
5 start_token = "<|BEGIN_RESPONSE|>"
6 end_token = "<|END_RESPONSE|>"
7 start_idx = full_text.find(start_token)
8 end_idx = full_text.find(end_token)
9
10 if start_idx != -1 and end_idx != -1:
11 response = full_text[start_idx + len(start_token):end_idx].strip()
12 else:
13 response = full_text.strip()
14
15 return response1input_text = "Who is Steve Jobs and what was contribution?"
2response1_full = generate_response(input_text)
3#response1 = decode(response1_full)
4print(f"Input: {input_text}")
5print("=======================================")
6print(f"Response1: {response1_full}")
7elapsed1 = stopwatch1.elapsed_time()
8print(f"Process took {elapsed1:.4f} seconds")
9print("=======================================")
10response2_full = generate_response2(input_text)
11#response2 = decode(response2_full)
12print(f"Response2: {response2_full}")
13elapsed2 = stopwatch2.elapsed_time()
14print(f"Process took {elapsed2:.4f} seconds")
15print("=======================================")