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 +9 -24
handler.py
CHANGED
|
@@ -3,19 +3,11 @@ from typing import Dict, Any, List
|
|
| 3 |
import torch
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
def model_inference(model, tokenizer, text,
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
max_new_tokens=max_new_tokens,
|
| 12 |
-
temperature=temperature,
|
| 13 |
-
top_p=top_p,
|
| 14 |
-
top_k=top_k,
|
| 15 |
-
repetition_penalty=repetition_penalty)
|
| 16 |
-
return tokenizer.decode(outputs[0][inputs_length:], skip_special_tokens=True)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
class EndpointHandler():
|
| 21 |
def __init__(self, path=""):
|
|
@@ -27,24 +19,17 @@ class EndpointHandler():
|
|
| 27 |
)
|
| 28 |
self.my_tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
| 29 |
|
| 30 |
-
def __call__(self, data):
|
| 31 |
-
print(data)
|
| 32 |
input_data = data.pop("inputs", data)
|
|
|
|
| 33 |
template = """
|
| 34 |
-
[INST] <<SYS>>
|
| 35 |
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.
|
| 36 |
<</SYS>>
|
| 37 |
{data} [/INST]
|
| 38 |
""".strip()
|
| 39 |
-
|
| 40 |
data_text = json.dumps(input_data, indent=4)
|
| 41 |
filled_template = template.replace("{data}", data_text)
|
| 42 |
-
|
| 43 |
-
print(filled_template)
|
| 44 |
-
max_new_tokens = 1600
|
| 45 |
-
temperature = 0.5
|
| 46 |
|
| 47 |
-
result = model_inference(self.model, self.my_tokenizer, filled_template, max_new_tokens, temperature)
|
| 48 |
-
print("output:")
|
| 49 |
-
print(result)
|
| 50 |
return {'generated_text': [result]}
|
|
|
|
| 3 |
import torch
|
| 4 |
import json
|
| 5 |
|
| 6 |
+
def model_inference(model, tokenizer, text, **kwargs):
|
| 7 |
+
inputs = tokenizer(text, return_tensors="pt").to("cuda:0")
|
| 8 |
+
with torch.inference_mode():
|
| 9 |
+
outputs = model.generate(**inputs, **kwargs)
|
| 10 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class EndpointHandler():
|
| 13 |
def __init__(self, path=""):
|
|
|
|
| 19 |
)
|
| 20 |
self.my_tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
| 21 |
|
| 22 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
|
|
|
| 23 |
input_data = data.pop("inputs", data)
|
| 24 |
+
parameters = data.pop("parameters", {})
|
| 25 |
template = """
|
| 26 |
+
<s>[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 |
+
result = model_inference(self.model, self.my_tokenizer, filled_template, **parameters)
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
return {'generated_text': [result]}
|