Downloads · 30 days
24
2% of all-time downloads
Mit1208/phi-2-classification-sentiment-merged
phi-2-classification-sentiment-merged is a text generation model from Mit1208. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as mit.
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
Downloads · 30 days
24
2% of all-time downloads
All-time downloads
1.6K
Public
Parameters
2.8B
5.6 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors5.6 GB · 100%
From the Hugging Face model README
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
https://github.com/mit1280/fined-tuning/blob/main/phi_2_classification_fine_tune.ipynb
The following hyperparameters were used during training:
!pip install -q transformers==4.37.2 accelerate==0.27.0
import re
from transformers import AutoTokenizer, AutoModelForCausalLM, StoppingCriteria
import torch
tokenizer = AutoTokenizer.from_pretrained("Mit1208/phi-2-classification-sentiment-merged")
model = AutoModelForCausalLM.from_pretrained("Mit1208/phi-2-classification-sentiment-merged", device_map="auto", trust_remote_code=True).eval()
class EosListStoppingCriteria(StoppingCriteria):
def __init__(self, eos_sequence = tokenizer.encode("<|im_end|>")):
self.eos_sequence = eos_sequence
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
last_ids = input_ids[:,-len(self.eos_sequence):].tolist()
return self.eos_sequence in last_ids
inf_conv = [{'from': 'human',
'value': "Text: In sales volume , Coca-Cola 's market share has decreased by 2.2 % to 24.2 % ."},
{'from': 'phi', 'value': "I've read this text."},
{'from': 'human',
'value': 'Please determine the sentiment of the given text and choose from the options: Positive, Negative, Neutral, or Cannot be determined.'}]
# need to load because model doesn't has classifer head.
id2label = {0: 'negative', 1: 'neutral', 2: 'positive'}
inference_text = tokenizer.apply_chat_template(inf_conv, tokenize=False) + '<|im_start|>phi:\n'
inputs = tokenizer(inference_text, return_tensors="pt", return_attention_mask=False).to('cuda')
outputs = model.generate(inputs["input_ids"], max_new_tokens=1024, pad_token_id= tokenizer.eos_token_id,
stopping_criteria = [EosListStoppingCriteria()])
text = tokenizer.batch_decode(outputs)[0]
answer = text.split("<|im_start|>phi:")[-1].replace("<|im_end|>", "").replace(".", "")
sentiment_label = re.search(r'(\d)', answer)
sentiment_score = int(sentiment_label.group(1))
if sentiment_score:
print(id2label.get(sentiment_score, "none"))
else:
print("none")