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 +18 -28
handler.py
CHANGED
|
@@ -17,31 +17,6 @@ schema = {
|
|
| 17 |
"required": ["objetivoDistancia", "calentamiento", "drills", "sprints", "enfriamiento"]
|
| 18 |
}
|
| 19 |
|
| 20 |
-
def parse_and_validate_json(output):
|
| 21 |
-
output = output.replace("\n\n", "\n").strip()
|
| 22 |
-
outputs = output.split("\n")
|
| 23 |
-
|
| 24 |
-
for single_output in outputs:
|
| 25 |
-
try:
|
| 26 |
-
corrected_json = single_output.replace('}\n"', '}, "')
|
| 27 |
-
data = json.loads(corrected_json)
|
| 28 |
-
validate(instance=data, schema=schema)
|
| 29 |
-
return True, corrected_json
|
| 30 |
-
except (json.JSONDecodeError, ValidationError):
|
| 31 |
-
continue
|
| 32 |
-
|
| 33 |
-
return False, "No se encontr贸 ning煤n JSON v谩lido seg煤n el esquema requerido."
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
class EndpointHandler():
|
| 46 |
def __init__(self, path=""):
|
| 47 |
self.model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -52,6 +27,21 @@ class EndpointHandler():
|
|
| 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,
|
|
@@ -69,8 +59,8 @@ class EndpointHandler():
|
|
| 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 |
|
|
@@ -88,6 +78,6 @@ class EndpointHandler():
|
|
| 88 |
data_text = json.dumps(input_data, indent=4)
|
| 89 |
filled_template = template.replace("{data}", data_text)
|
| 90 |
|
| 91 |
-
return model_inference_with_validation(filled_template, **parameters)
|
| 92 |
|
| 93 |
|
|
|
|
| 17 |
"required": ["objetivoDistancia", "calentamiento", "drills", "sprints", "enfriamiento"]
|
| 18 |
}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
class EndpointHandler():
|
| 21 |
def __init__(self, path=""):
|
| 22 |
self.model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 27 |
)
|
| 28 |
self.my_tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
| 29 |
|
| 30 |
+
def parse_and_validate_json(output):
|
| 31 |
+
output = output.replace("\n\n", "\n").strip()
|
| 32 |
+
outputs = output.split("\n")
|
| 33 |
+
|
| 34 |
+
for single_output in outputs:
|
| 35 |
+
try:
|
| 36 |
+
corrected_json = single_output.replace('}\n"', '}, "')
|
| 37 |
+
data = json.loads(corrected_json)
|
| 38 |
+
validate(instance=data, schema=schema)
|
| 39 |
+
return True, corrected_json
|
| 40 |
+
except (json.JSONDecodeError, ValidationError):
|
| 41 |
+
continue
|
| 42 |
+
|
| 43 |
+
return False, "No se encontr贸 ning煤n JSON v谩lido seg煤n el esquema requerido."
|
| 44 |
+
|
| 45 |
def model_inference(model, tokenizer, text, **parameters):
|
| 46 |
default_parameters = {
|
| 47 |
"max_new_tokens": 300,
|
|
|
|
| 59 |
|
| 60 |
def model_inference_with_validation(filled_template, **parameters):
|
| 61 |
while True:
|
| 62 |
+
result = self.model_inference(self.model, self.my_tokenizer, filled_template, **parameters)
|
| 63 |
+
is_valid, response_or_error = self.parse_and_validate_json(result)
|
| 64 |
if is_valid:
|
| 65 |
return {'generated_text': [result]}
|
| 66 |
|
|
|
|
| 78 |
data_text = json.dumps(input_data, indent=4)
|
| 79 |
filled_template = template.replace("{data}", data_text)
|
| 80 |
|
| 81 |
+
return self.model_inference_with_validation(filled_template, **parameters)
|
| 82 |
|
| 83 |
|