Text Generation
Transformers
Safetensors
English
mistral
sentiment-analysis
text-classification
reinforcement-learning
conversational
text-generation-inference
Instructions to use microsoft/Sentiment-Analysis-7B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Sentiment-Analysis-7B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/Sentiment-Analysis-7B") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("microsoft/Sentiment-Analysis-7B") model = AutoModelForCausalLM.from_pretrained("microsoft/Sentiment-Analysis-7B", device_map="auto") 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=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/Sentiment-Analysis-7B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Sentiment-Analysis-7B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Sentiment-Analysis-7B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/microsoft/Sentiment-Analysis-7B
- SGLang
How to use microsoft/Sentiment-Analysis-7B with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "microsoft/Sentiment-Analysis-7B" \ --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": "microsoft/Sentiment-Analysis-7B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
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 "microsoft/Sentiment-Analysis-7B" \ --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": "microsoft/Sentiment-Analysis-7B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use microsoft/Sentiment-Analysis-7B with Docker Model Runner:
docker model run hf.co/microsoft/Sentiment-Analysis-7B
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,93 @@
|
|
| 1 |
---
|
|
|
|
| 2 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
language: en
|
| 3 |
license: mit
|
| 4 |
+
library_name: transformers
|
| 5 |
+
pipeline_tag: text-generation
|
| 6 |
+
base_model: mistralai/Mistral-7B-Instruct-v0.2
|
| 7 |
+
tags:
|
| 8 |
+
- sentiment-analysis
|
| 9 |
+
- text-classification
|
| 10 |
+
- reinforcement-learning
|
| 11 |
+
- mistral
|
| 12 |
---
|
| 13 |
+
|
| 14 |
+
### Sentiment-Analysis-7B
|
| 15 |
+
|
| 16 |
+
**Sentiment-Analysis-7B** is a 7B-parameter autoregressive language model fine-tuned for sentiment classification of user messages directed at a service, product, or agent (the "Target"). It is built on top of [*mistralai/Mistral-7B-Instruct-v0.2*](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2) and was fine-tuned with reinforcement learning, using an LLM as a judge, on a small internal training set of ~300 examples. No pre-defined labels were used for training.
|
| 17 |
+
|
| 18 |
+
Given a user message, the model generates a response in the following format: the **first token is the sentiment label** (`Negative`, `Neutral`, or `Positive`), followed by a short explanation of the decision. If only the label is needed, set `max_new_tokens=1`.
|
| 19 |
+
|
| 20 |
+
### Evaluation metrics
|
| 21 |
+
|
| 22 |
+
The evaluation metric used is accuracy. The following compares the base model with the fine-tuned model.
|
| 23 |
+
|
| 24 |
+
| model | accuracy |
|
| 25 |
+
| ---------------------------------- | --------- |
|
| 26 |
+
| mistralai/Mistral-7B-Instruct-v0.2 | 62.0% |
|
| 27 |
+
| **Sentiment-Analysis-7B (ours)** | **72.8%** |
|
| 28 |
+
|
| 29 |
+
### How to use
|
| 30 |
+
|
| 31 |
+
The model is a causal language model and is loaded with `AutoModelForCausalLM`. The sentiment label is always the first generated token, so setting `max_new_tokens=1` returns the label only. Use the system prompt below, which defines the "Target" and the sentiment rules.
|
| 32 |
+
|
| 33 |
+
```python
|
| 34 |
+
import torch
|
| 35 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 36 |
+
|
| 37 |
+
model_id = "microsoft/Sentiment-Analysis-7B"
|
| 38 |
+
|
| 39 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 40 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 41 |
+
model_id, torch_dtype=torch.bfloat16, device_map="auto"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
system_prompt = """Return sentiment of user message as Positive/Neutral/Negative and explanation. Ex: Negative, the user expresses blame (as "you lie"), which conveys a negative tone.
|
| 45 |
+
Definition of "Target": the current service, product, or agent the user is interacting with (including its website, app, API, or support).
|
| 46 |
+
Rules of sentiment:
|
| 47 |
+
* Questions/statements clearly unrelated to Target → Neutral
|
| 48 |
+
* Otherwise, if related to Target:
|
| 49 |
+
* Questions containing complaint/frustration/blame → Negative. Ex, What is the point of writing here if technical support does not respond?
|
| 50 |
+
* Indirect incidents → Neutral. Ex, submit helpdesk ticket: mobile app crashes on startup
|
| 51 |
+
* Statement of any inability/failure → Negative. Ex, I'm having trouble logging in.
|
| 52 |
+
* Failure statement + polite/help request → Neutral. Ex, Login button is not working. Can you help?
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
sample = "I'm having trouble logging in to my account."
|
| 56 |
+
|
| 57 |
+
messages = [
|
| 58 |
+
{"role": "system", "content": system_prompt},
|
| 59 |
+
{"role": "user", "content": sample},
|
| 60 |
+
]
|
| 61 |
+
inputs = tokenizer.apply_chat_template(
|
| 62 |
+
messages,
|
| 63 |
+
tokenize=True,
|
| 64 |
+
return_dict=True,
|
| 65 |
+
return_tensors="pt",
|
| 66 |
+
add_generation_prompt=True,
|
| 67 |
+
).to(model.device)
|
| 68 |
+
|
| 69 |
+
# Label only
|
| 70 |
+
output = model.generate(**inputs, max_new_tokens=1, do_sample=False)
|
| 71 |
+
print(tokenizer.decode(output[0, inputs["input_ids"].shape[-1]:], skip_special_tokens=True))
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
Output:
|
| 75 |
+
|
| 76 |
+
```
|
| 77 |
+
Negative
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
To get the explanation as well, increase `max_new_tokens`:
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
output = model.generate(**inputs, max_new_tokens=128, do_sample=False)
|
| 84 |
+
print(tokenizer.decode(output[0, inputs["input_ids"].shape[-1]:], skip_special_tokens=True))
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
Output:
|
| 88 |
+
|
| 89 |
+
```
|
| 90 |
+
Negative, the user states an inability to log in, which is a failure related to the Target.
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
> Created by Microsoft
|