Instructions to use Britania/m2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Britania/m2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Britania/m2") 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("Britania/m2") model = AutoModelForCausalLM.from_pretrained("Britania/m2", 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 Britania/m2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Britania/m2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Britania/m2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/Britania/m2
- SGLang
How to use Britania/m2 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 "Britania/m2" \ --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": "Britania/m2", "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 "Britania/m2" \ --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": "Britania/m2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use Britania/m2 with Docker Model Runner:
docker model run hf.co/Britania/m2
Update handler.py
Browse files- handler.py +22 -38
handler.py
CHANGED
|
@@ -1,52 +1,36 @@
|
|
| 1 |
-
from
|
| 2 |
-
import
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 4 |
|
| 5 |
class EndpointHandler():
|
| 6 |
-
def __init__(self):
|
| 7 |
-
#
|
| 8 |
-
self.
|
| 9 |
self.bnb_config = BitsAndBytesConfig(
|
| 10 |
load_in_4bit=True,
|
| 11 |
bnb_4bit_quant_type="nf4",
|
| 12 |
bnb_4bit_compute_dtype=torch.float16,
|
| 13 |
)
|
| 14 |
-
self.
|
| 15 |
-
self.
|
| 16 |
quantization_config=self.bnb_config,
|
| 17 |
trust_remote_code=True,
|
| 18 |
torch_dtype=torch.bfloat16,
|
| 19 |
-
|
| 20 |
)
|
| 21 |
-
self.
|
| 22 |
-
|
| 23 |
-
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 24 |
-
"""
|
| 25 |
-
data args:
|
| 26 |
-
inputs (:obj: `str`): Cadena de texto de entrada
|
| 27 |
-
kwargs: Otros argumentos opcionales que puedas necesitar
|
| 28 |
-
Return:
|
| 29 |
-
A :obj:`list` | `dict`: ser谩 serializado y devuelto
|
| 30 |
-
"""
|
| 31 |
-
input_text = data.get("inputs", "") # Obtener el texto de entrada de los datos
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
max_new_tokens = 1600
|
| 35 |
temperature = 0.000001
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
generated_text = self.model_inference(input_text, max_new_tokens, temperature)
|
| 39 |
-
|
| 40 |
-
return [{"generated_text": generated_text}]
|
| 41 |
-
|
| 42 |
-
def model_inference(self, text: str, max_new_tokens: int, temperature: float) -> str:
|
| 43 |
-
inputs = self.tokenizer(text, return_tensors="pt").to(self.model.device)
|
| 44 |
-
inputs_length = len(inputs["input_ids"][0])
|
| 45 |
-
|
| 46 |
-
with torch.inference_mode():
|
| 47 |
-
outputs = self.model.generate(**inputs,
|
| 48 |
-
max_new_tokens=max_new_tokens,
|
| 49 |
-
temperature=temperature)
|
| 50 |
-
|
| 51 |
-
return self.tokenizer.decode(outputs[0][inputs_length:], skip_special_tokens=True)
|
| 52 |
-
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
from typing import Dict, Any, List
|
|
|
|
| 3 |
|
| 4 |
class EndpointHandler():
|
| 5 |
+
def __init__(self, path=""):
|
| 6 |
+
# Carga el modelo y el tokenizer
|
| 7 |
+
self.my_model_name = "Britania/AthelticRoutinesModel"
|
| 8 |
self.bnb_config = BitsAndBytesConfig(
|
| 9 |
load_in_4bit=True,
|
| 10 |
bnb_4bit_quant_type="nf4",
|
| 11 |
bnb_4bit_compute_dtype=torch.float16,
|
| 12 |
)
|
| 13 |
+
self.fine_tuned_model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
self.my_model_name,
|
| 15 |
quantization_config=self.bnb_config,
|
| 16 |
trust_remote_code=True,
|
| 17 |
torch_dtype=torch.bfloat16,
|
| 18 |
+
device_map="auto"
|
| 19 |
)
|
| 20 |
+
self.my_tokenizer = AutoTokenizer.from_pretrained(self.my_model_name, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 23 |
+
# Procesa la entrada y llama al modelo para generar texto
|
| 24 |
+
input_data = data["input"]
|
| 25 |
+
template = """
|
| 26 |
+
[INST] <<SYS>>
|
| 27 |
+
A continuaci贸n se presentan par谩metros fisiol贸gicos y ambientales capturados por los sensores de un smartwatch utilizado por un atleta. Bas谩ndote en estos datos y en tu conocimiento de las rutinas de entrenamiento para atletas, crea una rutina diaria de entrenamiento personalizada para el atleta.
|
| 28 |
+
<</SYS>>
|
| 29 |
+
{data} [/INST]
|
| 30 |
+
""".strip()
|
| 31 |
+
data_text = json.dumps(input_data, indent=4)
|
| 32 |
+
filled_template = template.replace("{data}", data_text)
|
| 33 |
max_new_tokens = 1600
|
| 34 |
temperature = 0.000001
|
| 35 |
+
result = model_inference(self.fine_tuned_model, self.my_tokenizer, filled_template, max_new_tokens, temperature)
|
| 36 |
+
return [{"generated_text": result}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|