|
--- |
|
language: |
|
- zh |
|
license: apache-2.0 |
|
library_name: transformers |
|
tags: |
|
- trl |
|
- sft |
|
datasets: |
|
- Mike0307/alpaca-en-zhtw |
|
pipeline_tag: text-generation |
|
--- |
|
|
|
|
|
## Download the model |
|
|
|
The base-model *microsoft/Phi-3-mini-4k-instruct* relies on development-version transformers: |
|
```bash |
|
pip uninstall -y transformers && pip install git+https://github.com/huggingface/transformers |
|
``` |
|
|
|
```python |
|
import torch |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
model_path = "Mike0307/Phi-3-mini-4k-instruct-chinese-lora" |
|
base_model = AutoModelForCausalLM.from_pretrained( |
|
model_path, |
|
device_map="mps", # FIX mps if not MacOS |
|
torch_dtype=torch.float32, |
|
trust_remote_code=True, |
|
) |
|
tokenizer = AutoTokenizer.from_pretrained(model_path) |
|
``` |
|
|
|
## Example of inference |
|
|
|
```python |
|
input_text = "<|user|>將這五種動物分成兩組。\n老虎、鯊魚、大象、鯨魚、袋鼠 <|end|>\n<|assistant|>" |
|
inputs = tokenizer(input_text, return_tensors="pt").to(torch.device("mps")) # FIX mps if not MacOS |
|
|
|
outputs = base_model.generate( |
|
**inputs, |
|
temperature = 0.0, |
|
max_length = 500, |
|
do_sample = False |
|
) |
|
|
|
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True, predict_with_generate=True) |
|
print(generated_text) |
|
``` |