Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,199 +1,161 @@
|
|
|
|
|
| 1 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
library_name: transformers
|
| 3 |
-
|
| 4 |
---
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
<!-- Provide a quick summary of what the model is/does. -->
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
## Model Details
|
| 13 |
-
|
| 14 |
-
### Model Description
|
| 15 |
-
|
| 16 |
-
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
-
|
| 18 |
-
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
| 19 |
-
|
| 20 |
-
- **Developed by:** [More Information Needed]
|
| 21 |
-
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
-
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
-
- **Model type:** [More Information Needed]
|
| 24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
-
- **License:** [More Information Needed]
|
| 26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
-
|
| 28 |
-
### Model Sources [optional]
|
| 29 |
-
|
| 30 |
-
<!-- Provide the basic links for the model. -->
|
| 31 |
-
|
| 32 |
-
- **Repository:** [More Information Needed]
|
| 33 |
-
- **Paper [optional]:** [More Information Needed]
|
| 34 |
-
- **Demo [optional]:** [More Information Needed]
|
| 35 |
-
|
| 36 |
-
## Uses
|
| 37 |
-
|
| 38 |
-
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
-
|
| 40 |
-
### Direct Use
|
| 41 |
-
|
| 42 |
-
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
-
|
| 44 |
-
[More Information Needed]
|
| 45 |
-
|
| 46 |
-
### Downstream Use [optional]
|
| 47 |
-
|
| 48 |
-
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
-
|
| 50 |
-
[More Information Needed]
|
| 51 |
-
|
| 52 |
-
### Out-of-Scope Use
|
| 53 |
-
|
| 54 |
-
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 55 |
-
|
| 56 |
-
[More Information Needed]
|
| 57 |
-
|
| 58 |
-
## Bias, Risks, and Limitations
|
| 59 |
-
|
| 60 |
-
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
-
|
| 62 |
-
[More Information Needed]
|
| 63 |
-
|
| 64 |
-
### Recommendations
|
| 65 |
-
|
| 66 |
-
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 67 |
-
|
| 68 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
-
|
| 70 |
-
## How to Get Started with the Model
|
| 71 |
-
|
| 72 |
-
Use the code below to get started with the model.
|
| 73 |
-
|
| 74 |
-
[More Information Needed]
|
| 75 |
-
|
| 76 |
-
## Training Details
|
| 77 |
-
|
| 78 |
-
### Training Data
|
| 79 |
-
|
| 80 |
-
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
|
| 82 |
-
[More Information Needed]
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
| 91 |
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
-
|
| 97 |
-
#### Speeds, Sizes, Times [optional]
|
| 98 |
-
|
| 99 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
-
|
| 101 |
-
[More Information Needed]
|
| 102 |
-
|
| 103 |
-
## Evaluation
|
| 104 |
-
|
| 105 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
-
|
| 107 |
-
### Testing Data, Factors & Metrics
|
| 108 |
-
|
| 109 |
-
#### Testing Data
|
| 110 |
-
|
| 111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
-
|
| 113 |
-
[More Information Needed]
|
| 114 |
-
|
| 115 |
-
#### Factors
|
| 116 |
-
|
| 117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
-
|
| 119 |
-
[More Information Needed]
|
| 120 |
-
|
| 121 |
-
#### Metrics
|
| 122 |
-
|
| 123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
-
|
| 125 |
-
[More Information Needed]
|
| 126 |
-
|
| 127 |
-
### Results
|
| 128 |
-
|
| 129 |
-
[More Information Needed]
|
| 130 |
-
|
| 131 |
-
#### Summary
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
## Model Examination [optional]
|
| 136 |
-
|
| 137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
-
|
| 139 |
-
[More Information Needed]
|
| 140 |
-
|
| 141 |
-
## Environmental Impact
|
| 142 |
-
|
| 143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
-
|
| 145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
-
|
| 147 |
-
- **Hardware Type:** [More Information Needed]
|
| 148 |
-
- **Hours used:** [More Information Needed]
|
| 149 |
-
- **Cloud Provider:** [More Information Needed]
|
| 150 |
-
- **Compute Region:** [More Information Needed]
|
| 151 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
|
| 153 |
-
|
| 154 |
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
| 158 |
|
| 159 |
-
|
| 160 |
|
| 161 |
-
|
| 162 |
|
| 163 |
-
|
| 164 |
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
|
| 175 |
-
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
|
| 183 |
-
##
|
| 184 |
|
| 185 |
-
|
| 186 |
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
-
|
| 190 |
|
| 191 |
-
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
[More Information Needed]
|
|
|
|
| 1 |
+
|
| 2 |
---
|
| 3 |
+
license: mit
|
| 4 |
+
tags:
|
| 5 |
+
- causal-lm
|
| 6 |
+
- instruction-following
|
| 7 |
+
- loRA
|
| 8 |
+
- QLoRA
|
| 9 |
+
- quantized
|
| 10 |
+
language: en
|
| 11 |
library_name: transformers
|
| 12 |
+
base_model: microsoft/phi-2
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# Phi-2 QLoRA Fine-Tuned Model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
+
**Model:** `mishrabp/phi2-custom-response-qlora-adapter`
|
| 19 |
|
| 20 |
+
**Base Model:** [`microsoft/phi-2`](https://huggingface.co/microsoft/phi-2)
|
| 21 |
|
| 22 |
+
**Fine-Tuning Method:** QLoRA (4-bit quantized LoRA)
|
| 23 |
|
| 24 |
+
**Task:** Instruction-following / Customer Support Responses
|
| 25 |
|
| 26 |
+
---
|
| 27 |
|
| 28 |
+
## Model Description
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
This repository contains a **Phi-2 language model fine-tuned using QLoRA** on a synthetic dataset of customer support instructions and responses. The fine-tuning uses **4-bit quantized LoRA adapters** for memory-efficient training and can run on GPU or CPU (slower on CPU).
|
| 31 |
|
| 32 |
+
The model is designed for **instruction-following tasks** like customer support, FAQs, or other dialog generation tasks.
|
| 33 |
|
| 34 |
+
---
|
| 35 |
|
| 36 |
+
## Training Data
|
| 37 |
|
| 38 |
+
The fine-tuning dataset is synthetic, consisting of 3000 instruction-response pairs:
|
| 39 |
|
| 40 |
+
**Example:**
|
| 41 |
|
| 42 |
+
```text
|
| 43 |
+
Instruction: "Customer asks about refund window #1"
|
| 44 |
+
Response: "Our refund window is 30 days from delivery."
|
| 45 |
+
```
|
| 46 |
|
| 47 |
+
Here is the dataset that was used for fine-tunning:
|
| 48 |
+
https://huggingface.co/datasets/mishrabp/customer-support-responses/resolve/main/train.csv
|
| 49 |
|
| 50 |
+
You can replace the dataset with your own CSV/JSON file to train on real-world data.
|
| 51 |
|
| 52 |
+
---
|
| 53 |
|
| 54 |
+
## Intended Use
|
| 55 |
|
| 56 |
+
* Generate responses to instructions in customer support scenarios.
|
| 57 |
+
* Small-scale instruction-following experiments.
|
| 58 |
+
* Educational or research purposes.
|
| 59 |
|
| 60 |
+
---
|
| 61 |
|
| 62 |
+
## How to Use
|
| 63 |
+
|
| 64 |
+
### Load the Fine-Tuned Model
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
import torch
|
| 68 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 69 |
+
from peft import PeftModel
|
| 70 |
+
|
| 71 |
+
# -----------------------------
|
| 72 |
+
# Load fine-tuned model from HF
|
| 73 |
+
# -----------------------------
|
| 74 |
+
model_name = "mishrabp/phi2-custom-response-qlora-adapter"
|
| 75 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 76 |
+
base_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2")
|
| 77 |
+
model = PeftModel.from_pretrained(base_model, model_name)
|
| 78 |
+
|
| 79 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 80 |
+
model.to(device)
|
| 81 |
+
|
| 82 |
+
# -----------------------------
|
| 83 |
+
# Sample evaluation dataset
|
| 84 |
+
# -----------------------------
|
| 85 |
+
eval_data = [
|
| 86 |
+
{"instruction": "Customer asks about refund window", "reference": "Our refund window is 30 days from delivery."},
|
| 87 |
+
{"instruction": "Order arrived late", "reference": "Sorry for the delay. A delivery credit has been applied."},
|
| 88 |
+
{"instruction": "Wrong item received", "reference": "We’ll ship the correct item and provide a return label."},
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
# -----------------------------
|
| 92 |
+
# Evaluation loop
|
| 93 |
+
# -----------------------------
|
| 94 |
+
for i, example in enumerate(eval_data, 1):
|
| 95 |
+
prompt = f"### Instruction:
|
| 96 |
+
{example['instruction']}
|
| 97 |
+
|
| 98 |
+
### Response:"
|
| 99 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 100 |
+
output_ids = model.generate(**inputs, max_new_tokens=50)
|
| 101 |
+
generated = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 102 |
+
|
| 103 |
+
print(f"Example {i}")
|
| 104 |
+
print("Instruction:", example["instruction"])
|
| 105 |
+
print("Generated Response:", generated.split("### Response:")[-1].strip())
|
| 106 |
+
print("Reference Response:", example["reference"])
|
| 107 |
+
print("-" * 50)
|
| 108 |
+
|
| 109 |
+
# -----------------------------
|
| 110 |
+
# Optional: compute simple token-level accuracy or BLEU
|
| 111 |
+
# -----------------------------
|
| 112 |
+
from nltk.translate.bleu_score import sentence_bleu
|
| 113 |
+
|
| 114 |
+
bleu_scores = []
|
| 115 |
+
for example in eval_data:
|
| 116 |
+
prompt = f"### Instruction:
|
| 117 |
+
{example['instruction']}
|
| 118 |
+
|
| 119 |
+
### Response:"
|
| 120 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 121 |
+
output_ids = model.generate(**inputs, max_new_tokens=50)
|
| 122 |
+
generated = tokenizer.decode(output_ids[0], skip_special_tokens=True).split("### Response:")[-1].strip()
|
| 123 |
+
|
| 124 |
+
reference_tokens = example["reference"].split()
|
| 125 |
+
generated_tokens = generated.split()
|
| 126 |
+
bleu = sentence_bleu([reference_tokens], generated_tokens)
|
| 127 |
+
bleu_scores.append(bleu)
|
| 128 |
+
|
| 129 |
+
print("Average BLEU score:", sum(bleu_scores)/len(bleu_scores))
|
| 130 |
+
|
| 131 |
+
```
|
| 132 |
|
| 133 |
+
---
|
| 134 |
|
| 135 |
+
## Training Script
|
| 136 |
|
| 137 |
+
The training script performs the following steps:
|
| 138 |
|
| 139 |
+
1. Loads the **Phi-2 base model**.
|
| 140 |
+
2. Creates a **synthetic dataset** of instruction-response pairs.
|
| 141 |
+
3. Tokenizes and formats the dataset for causal language modeling.
|
| 142 |
+
4. Applies a **LoRA adapter**.
|
| 143 |
+
5. Trains using **QLoRA** if GPU is available, otherwise full-precision LoRA on CPU.
|
| 144 |
+
6. Saves the adapter and tokenizer to `./phi2-qlora`.
|
| 145 |
+
7. Pushes the adapter and tokenizer to Hugging Face Hub.
|
| 146 |
|
| 147 |
+
### Requirements
|
| 148 |
|
| 149 |
+
```bash
|
| 150 |
+
pip install torch transformers peft datasets huggingface_hub python-dotenv
|
| 151 |
+
```
|
| 152 |
|
| 153 |
+
---
|
| 154 |
|
| 155 |
+
## Parameters
|
| 156 |
|
| 157 |
+
* `r=8`, `lora_alpha=16`, `lora_dropout=0.05`
|
| 158 |
+
* `target_modules=["q_proj","v_proj"]` (adjust for different base models)
|
| 159 |
+
* Learning rate: `2e-4`
|
| 160 |
+
* Batch si
|
| 161 |
|
|
|