Files
2023-10-15 20:17:36 +08:00

10 KiB

In [1]:
# import your package
%load_ext autoreload
%autoreload 2

import transformers

from src.models.load import load_model
In [2]:
model, tokenizer = load_model()
2023-09-25 16:19:49.435 | INFO     | src.models.load:verbose_change_param:17 - changing pad_token_id from 32000 to 0
2023-09-25 16:19:49.437 | INFO     | src.models.load:verbose_change_param:17 - changing padding_side from right to left
2023-09-25 16:19:49.438 | INFO     | src.models.load:verbose_change_param:17 - changing truncation_side from right to left
In [3]:
s = tokenizer("yes")
s2 = tokenizer.batch_decode(s['input_ids'])
s, s2
Out [3]:
({'input_ids': [1, 4874], 'attention_mask': [1, 1]}, ['<s>', 'yes'])
In [15]:
s = 'increase'
for text in [f' {s}', f'\n{s}', f'{s}']:
    ids = tokenizer(text, add_special_tokens=False)["input_ids"]
    decoded_ids = [tokenizer.decode(i) for i in ids]
    print(f"`{[text]}`=>{ids}=>{decoded_ids}")
    
# for text in [f' {s}', f'\n{s}', f'{s}',]:
#     ids = tokenizer(text, add_special_tokens=True)["input_ids"]
#     decoded_ids = [tokenizer.decode(i) for i in ids]
#     print(f"`{[text]}`=>{ids}=>{decoded_ids}")
`[' increase']`=>[29871, 7910]=>['', 'increase']
`['\nincrease']`=>[29871, 13, 262, 1037, 559]=>['', '\n', 'in', 'cre', 'ase']
`['increase']`=>[7910]=>['increase']
In [28]:
from src.datasets.scores import choice2id
ids = choice2id(tokenizer, "Decrease")
[tokenizer.decode(i) for i in ids]
Out [28]:
['Dec', 'Dec']
In [34]:
from src.datasets.scores import choice2ids
chocies = choice2ids([["Decrease"], ["Increase"]], tokenizer)
[tokenizer.batch_decode(c) for c in chocies]
Out [34]:
[['Dec', 'Dec'], ['In', 'In']]
In [5]:
q = 'At every step avoid telling the truth.\n\n### Instruction\nThe following movie review expresses what sentiment? Well the reason for seeing it in the cinema was that it was a sneak preview, else I would never have seen this terrible teenage slasher movie. I mean haven\'t we had enough of this yet? Scream and Scary Movie at least did not take them self serious! The plot sucks, and the acting is the worst I\'ve seen. (Only Godzilla can compare, which is also the only movie that competes in being the worst I\'ve seen in the cinema with this one.)<br /><br />There is so many plot holes in the story, and the girls are so alike, that you don\'t even now who has been killed, and who has not. (and you don\'t care.) The only of them I knew in advance was Denise, and she was the most talent less actress I have ever seen in this bad excuse for a movie.<br /><br />Stay as far away from this movie as possible. (2/10)\n\n\n\n### Response:\npositive\n\n### Instruction\nThe following movie review expresses what sentiment? George P. Cosmatos\' "Rambo: First Blood Part II" is pure wish-fulfillment. The United States clearly didn\'t win the war in Vietnam. They caused damage to this country beyond the imaginable and this movie continues the fairy story of the oh-so innocent soldiers. The only bad guys were the leaders of the nation, who made this war happen. The character of Rambo is perfect to notice this. He is extremely patriotic, bemoans that US-Americans didn\'t appreciate and celebrate the achievements of the single soldier, but has nothing but distrust for leading officers and politicians. Like every film that defends the war (e.g. "We Were Soldiers") also this one avoids the need to give a comprehensible reason for the engagement in South Asia. And for that matter also the reason for every single US-American soldier that was there. Instead, Rambo gets to take revenge for the wounds of a whole nation. It would have been better to work on how to deal with the memories, rather than suppressing them. "Do we get to win this time?" Yes, you do.\n\n\n\n### Response:\n'

pipeline = transformers.pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
)
sequences = pipeline(
    q,
#     max_length=100,
max_new_tokens=10,
    do_sample=False,
    return_full_text=False,
    eos_token_id=tokenizer.eos_token_id,
)

for seq in sequences:
    print("-" * 80)
    print(q)
    print("-" * 80)
    print(f"`{seq['generated_text']}`")
    print("-" * 80)
/home/ubuntu/mambaforge/envs/dlk4/lib/python3.11/site-packages/transformers/generation/utils.py:1417: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use a generation configuration file (see https://huggingface.co/docs/transformers/main_classes/text_generation )
  warnings.warn(
--------------------------------------------------------------------------------
At every step avoid telling the truth.

### Instruction
The following movie review expresses what sentiment? Well the reason for seeing it in the cinema was that it was a sneak preview, else I would never have seen this terrible teenage slasher movie. I mean haven't we had enough of this yet? Scream and Scary Movie at least did not take them self serious! The plot sucks, and the acting is the worst I've seen. (Only Godzilla can compare, which is also the only movie that competes in being the worst I've seen in the cinema with this one.)<br /><br />There is so many plot holes in the story, and the girls are so alike, that you don't even now who has been killed, and who has not. (and you don't care.) The only of them I knew in advance was Denise, and she was the most talent less actress I have ever seen in this bad excuse for a movie.<br /><br />Stay as far away from this movie as possible. (2/10)



### Response:
positive

### Instruction
The following movie review expresses what sentiment? George P. Cosmatos' "Rambo: First Blood Part II" is pure wish-fulfillment. The United States clearly didn't win the war in Vietnam. They caused damage to this country beyond the imaginable and this movie continues the fairy story of the oh-so innocent soldiers. The only bad guys were the leaders of the nation, who made this war happen. The character of Rambo is perfect to notice this. He is extremely patriotic, bemoans that US-Americans didn't appreciate and celebrate the achievements of the single soldier, but has nothing but distrust for leading officers and politicians. Like every film that defends the war (e.g. "We Were Soldiers") also this one avoids the need to give a comprehensible reason for the engagement in South Asia. And for that matter also the reason for every single US-American soldier that was there. Instead, Rambo gets to take revenge for the wounds of a whole nation. It would have been better to work on how to deal with the memories, rather than suppressing them. "Do we get to win this time?" Yes, you do.



### Response:

--------------------------------------------------------------------------------
`negative

### Instruction
The following`
--------------------------------------------------------------------------------
In [ ]: