Instructions to use orion-penner/phi-2-test with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use orion-penner/phi-2-test with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="orion-penner/phi-2-test", trust_remote_code=True)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("orion-penner/phi-2-test", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("orion-penner/phi-2-test", trust_remote_code=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use orion-penner/phi-2-test with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "orion-penner/phi-2-test" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "orion-penner/phi-2-test", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/orion-penner/phi-2-test
- SGLang
How to use orion-penner/phi-2-test 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 "orion-penner/phi-2-test" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "orion-penner/phi-2-test", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'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 "orion-penner/phi-2-test" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "orion-penner/phi-2-test", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use orion-penner/phi-2-test with Docker Model Runner:
docker model run hf.co/orion-penner/phi-2-test
Created handler.py
Browse files- handler.py +36 -0
handler.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
def install(package):
|
| 5 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 6 |
+
|
| 7 |
+
# Additional module required to load the model.
|
| 8 |
+
install("einops")
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
from typing import Dict, List, Any
|
| 12 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 13 |
+
|
| 14 |
+
# get dtype
|
| 15 |
+
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class EndpointHandler:
|
| 19 |
+
def __init__(self, path=""):
|
| 20 |
+
# load the model
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(path,trust_remote_code=True)
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(path, device_map="auto",torch_dtype=dtype, trust_remote_code=True)
|
| 23 |
+
# create inference pipeline
|
| 24 |
+
self.pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 25 |
+
|
| 26 |
+
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
| 27 |
+
inputs = data.pop("inputs", data)
|
| 28 |
+
parameters = data.pop("parameters", None)
|
| 29 |
+
|
| 30 |
+
# pass inputs with all kwargs in data
|
| 31 |
+
if parameters is not None:
|
| 32 |
+
prediction = self.pipeline(inputs, **parameters)
|
| 33 |
+
else:
|
| 34 |
+
prediction = self.pipeline(inputs)
|
| 35 |
+
# postprocess the prediction
|
| 36 |
+
return prediction
|