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 +26 -22
handler.py
CHANGED
|
@@ -35,28 +35,10 @@ def parse_and_validate_json(output):
|
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"top_p": 0.99,
|
| 43 |
-
"top_k": 20,
|
| 44 |
-
"repetition_penalty": 1.2
|
| 45 |
-
}
|
| 46 |
-
default_parameters.update(parameters)
|
| 47 |
-
inputs = tokenizer(text, return_tensors="pt").to("cuda:0")
|
| 48 |
-
inputs_length = len(inputs["input_ids"][0])
|
| 49 |
-
with torch.inference_mode():
|
| 50 |
-
outputs = model.generate(**inputs, **default_parameters)
|
| 51 |
-
return tokenizer.decode(outputs[0][inputs_length:], skip_special_tokens=True)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def model_inference_with_validation(filled_template, **parameters):
|
| 55 |
-
while True:
|
| 56 |
-
result = model_inference(self.model, self.my_tokenizer, filled_template, **parameters)
|
| 57 |
-
is_valid, response_or_error = parse_and_validate_json(result)
|
| 58 |
-
if is_valid:
|
| 59 |
-
return {'generated_text': [result]}
|
| 60 |
|
| 61 |
|
| 62 |
|
|
@@ -70,6 +52,28 @@ class EndpointHandler():
|
|
| 70 |
)
|
| 71 |
self.my_tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
| 74 |
input_data = data.pop("inputs", data)
|
| 75 |
parameters = data.pop("parameters", {})
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
|
|
|
|
| 52 |
)
|
| 53 |
self.my_tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
| 54 |
|
| 55 |
+
def model_inference(model, tokenizer, text, **parameters):
|
| 56 |
+
default_parameters = {
|
| 57 |
+
"max_new_tokens": 300,
|
| 58 |
+
"temperature": 0.5,
|
| 59 |
+
"top_p": 0.99,
|
| 60 |
+
"top_k": 20,
|
| 61 |
+
"repetition_penalty": 1.2
|
| 62 |
+
}
|
| 63 |
+
default_parameters.update(parameters)
|
| 64 |
+
inputs = tokenizer(text, return_tensors="pt").to("cuda:0")
|
| 65 |
+
inputs_length = len(inputs["input_ids"][0])
|
| 66 |
+
with torch.inference_mode():
|
| 67 |
+
outputs = model.generate(**inputs, **default_parameters)
|
| 68 |
+
return tokenizer.decode(outputs[0][inputs_length:], skip_special_tokens=True)
|
| 69 |
+
|
| 70 |
+
def model_inference_with_validation(filled_template, **parameters):
|
| 71 |
+
while True:
|
| 72 |
+
result = model_inference(self.model, self.my_tokenizer, filled_template, **parameters)
|
| 73 |
+
is_valid, response_or_error = parse_and_validate_json(result)
|
| 74 |
+
if is_valid:
|
| 75 |
+
return {'generated_text': [result]}
|
| 76 |
+
|
| 77 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
| 78 |
input_data = data.pop("inputs", data)
|
| 79 |
parameters = data.pop("parameters", {})
|