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 +14 -9
handler.py
CHANGED
|
@@ -4,8 +4,7 @@ import torch
|
|
| 4 |
import json
|
| 5 |
|
| 6 |
def model_inference(model, tokenizer, text, max_new_tokens, temperature, top_p=0.99, top_k=20, repetition_penalty=1.2):
|
| 7 |
-
|
| 8 |
-
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 9 |
inputs_length = len(inputs["input_ids"][0])
|
| 10 |
with torch.inference_mode():
|
| 11 |
outputs = model.generate(**inputs,
|
|
@@ -20,17 +19,16 @@ def model_inference(model, tokenizer, text, max_new_tokens, temperature, top_p=0
|
|
| 20 |
|
| 21 |
class EndpointHandler():
|
| 22 |
def __init__(self, path=""):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
self.fine_tuned_model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
-
self.my_model_name,
|
| 27 |
trust_remote_code=True,
|
| 28 |
torch_dtype=torch.bfloat16,
|
| 29 |
device_map="auto"
|
| 30 |
)
|
| 31 |
-
self.my_tokenizer = AutoTokenizer.from_pretrained(
|
| 32 |
|
| 33 |
def __call__(self, data):
|
|
|
|
| 34 |
input_data = data
|
| 35 |
template = """
|
| 36 |
[INST] <<SYS>>
|
|
@@ -38,9 +36,16 @@ class EndpointHandler():
|
|
| 38 |
<</SYS>>
|
| 39 |
{data} [/INST]
|
| 40 |
""".strip()
|
|
|
|
| 41 |
data_text = json.dumps(input_data, indent=4)
|
| 42 |
filled_template = template.replace("{data}", data_text)
|
|
|
|
|
|
|
| 43 |
max_new_tokens = 1600
|
| 44 |
-
temperature = 0.
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return [{"generated_text": result}]
|
|
|
|
| 4 |
import json
|
| 5 |
|
| 6 |
def model_inference(model, tokenizer, text, max_new_tokens, temperature, top_p=0.99, top_k=20, repetition_penalty=1.2):
|
| 7 |
+
inputs = tokenizer(text, return_tensors="pt").to("cuda:0")
|
|
|
|
| 8 |
inputs_length = len(inputs["input_ids"][0])
|
| 9 |
with torch.inference_mode():
|
| 10 |
outputs = model.generate(**inputs,
|
|
|
|
| 19 |
|
| 20 |
class EndpointHandler():
|
| 21 |
def __init__(self, path=""):
|
| 22 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
path,
|
|
|
|
|
|
|
| 24 |
trust_remote_code=True,
|
| 25 |
torch_dtype=torch.bfloat16,
|
| 26 |
device_map="auto"
|
| 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
|
| 33 |
template = """
|
| 34 |
[INST] <<SYS>>
|
|
|
|
| 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 |
+
print("input:")
|
| 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 |
+
|
| 51 |
return [{"generated_text": result}]
|