Instructions to use BinaryLight1011/Context with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use BinaryLight1011/Context with Transformers:
# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("BinaryLight1011/Context", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Unsloth Studio
How to use BinaryLight1011/Context with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for BinaryLight1011/Context to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for BinaryLight1011/Context to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for BinaryLight1011/Context to start chatting
Load model with FastModel
pip install unsloth from unsloth import FastModel model, tokenizer = FastModel.from_pretrained( model_name="BinaryLight1011/Context", max_seq_length=2048, )
Jefferson de Azevedo commited on
Create handler.py
Browse files- handler.py +41 -0
handler.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from unsloth import FastLanguageModel
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
class EndpointHandler:
|
| 5 |
+
def __init__(self, path=""):
|
| 6 |
+
self.model, self.tokenizer = FastLanguageModel.from_pretrained(
|
| 7 |
+
model_name=path,
|
| 8 |
+
max_seq_length=2048,
|
| 9 |
+
dtype=torch.float16,
|
| 10 |
+
load_in_4bit=True,
|
| 11 |
+
)
|
| 12 |
+
FastLanguageModel.for_inference(self.model)
|
| 13 |
+
|
| 14 |
+
def __call__(self, data: dict):
|
| 15 |
+
inputs_text = data.pop("inputs", "")
|
| 16 |
+
parameters = data.pop("parameters", {})
|
| 17 |
+
|
| 18 |
+
# Formata no template do LLaMA 3
|
| 19 |
+
messages = [{"role": "user", "content": inputs_text}]
|
| 20 |
+
formatted = self.tokenizer.apply_chat_template(
|
| 21 |
+
messages,
|
| 22 |
+
tokenize=False,
|
| 23 |
+
add_generation_prompt=True
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
inputs = self.tokenizer(
|
| 27 |
+
formatted,
|
| 28 |
+
return_tensors="pt"
|
| 29 |
+
).to("cuda")
|
| 30 |
+
|
| 31 |
+
outputs = self.model.generate(
|
| 32 |
+
**inputs,
|
| 33 |
+
max_new_tokens=parameters.get("max_new_tokens", 512),
|
| 34 |
+
temperature=parameters.get("temperature", 0.7),
|
| 35 |
+
do_sample=True,
|
| 36 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Retorna s贸 a resposta, sem o prompt
|
| 40 |
+
decoded = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
+
return {"generated_text": decoded}
|