takala/financial_phrasebank
Updated • 9.22k • 257
How to use Mit1208/phi-2-classification-sentiment-merged with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="Mit1208/phi-2-classification-sentiment-merged", trust_remote_code=True)
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe(messages) # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("Mit1208/phi-2-classification-sentiment-merged", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("Mit1208/phi-2-classification-sentiment-merged", trust_remote_code=True)
messages = [
{"role": "user", "content": "Who are you?"},
]
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=40)
print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:]))How to use Mit1208/phi-2-classification-sentiment-merged with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "Mit1208/phi-2-classification-sentiment-merged"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Mit1208/phi-2-classification-sentiment-merged",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker model run hf.co/Mit1208/phi-2-classification-sentiment-merged
How to use Mit1208/phi-2-classification-sentiment-merged with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "Mit1208/phi-2-classification-sentiment-merged" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Mit1208/phi-2-classification-sentiment-merged",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker run --gpus all \
--shm-size 32g \
-p 30000:30000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=<secret>" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server \
--model-path "Mit1208/phi-2-classification-sentiment-merged" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Mit1208/phi-2-classification-sentiment-merged",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'How to use Mit1208/phi-2-classification-sentiment-merged with Docker Model Runner:
docker model run hf.co/Mit1208/phi-2-classification-sentiment-merged
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")