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:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("BinaryLight1011/Context", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Unsloth Desktop
Download handler.py from BinaryLight1011/Context: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/BinaryLight1011/Context/resolve/main/handler.py
- Command line
-
hf download hf://BinaryLight1011/Context/handler.py
-
curl -L -o handler.py https://huggingface.co/BinaryLight1011/Context/resolve/main/handler.py
1.32 kB
| from unsloth import FastLanguageModel | |
| import torch | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| self.model, self.tokenizer = FastLanguageModel.from_pretrained( | |
| model_name=path, | |
| max_seq_length=2048, | |
| dtype=torch.float16, | |
| load_in_4bit=True, | |
| ) | |
| FastLanguageModel.for_inference(self.model) | |
| def __call__(self, data: dict): | |
| inputs_text = data.pop("inputs", "") | |
| parameters = data.pop("parameters", {}) | |
| # Formata no template do LLaMA 3 | |
| messages = [{"role": "user", "content": inputs_text}] | |
| formatted = self.tokenizer.apply_chat_template( | |
| messages, | |
| tokenize=False, | |
| add_generation_prompt=True | |
| ) | |
| inputs = self.tokenizer( | |
| formatted, | |
| return_tensors="pt" | |
| ).to("cuda") | |
| outputs = self.model.generate( | |
| **inputs, | |
| max_new_tokens=parameters.get("max_new_tokens", 512), | |
| temperature=parameters.get("temperature", 0.7), | |
| do_sample=True, | |
| pad_token_id=self.tokenizer.eos_token_id, | |
| ) | |
| # Retorna só a resposta, sem o prompt | |
| decoded = self.tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return {"generated_text": decoded} |