Instructions to use microsoft/phi-2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/phi-2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/phi-2")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2") model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2") - Inference
- Local Apps Settings
- vLLM
How to use microsoft/phi-2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/phi-2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/microsoft/phi-2
- SGLang
How to use microsoft/phi-2 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 "microsoft/phi-2" \ --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": "microsoft/phi-2", "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 "microsoft/phi-2" \ --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": "microsoft/phi-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use microsoft/phi-2 with Docker Model Runner:
docker model run hf.co/microsoft/phi-2
Unrecognized configuration class
ValueError: Unrecognized configuration class <class 'transformers_modules.microsoft.phi-2.d3186761bf5c4409f7679359284066c25ab668ee.configuration_phi.PhiConfig'> for this kind of AutoModel: TFAutoModelForCausalLM.Model type should be one of BertConfig, CamembertConfig, CTRLConfig, GPT2Config, GPT2Config, GPTJConfig, OpenAIGPTConfig, OPTConfig, RemBertConfig, RobertaConfig, RobertaPreLayerNormConfig, RoFormerConfig, TransfoXLConfig, XGLMConfig, XLMConfig, XLMRobertaConfig, XLNetConfig.
Hello @mrizwanse!
Could you please provide the code you are using to load the model?
Regards,
Gustavo.
Importing Python packages
from environs import Env
Importing HuggingFace packages
from transformers import TFAutoModelForCausalLM, AutoTokenizer, pipeline
Importing LangChain packages
from langchain.llms.huggingface_pipeline import HuggingFacePipeline
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
---------------------------------------------------------------------------
Read environment variables from .env file
env = Env()
env.read_env(".env")
HuggingFace Hub API token
HUGGINGFACEHUB_API_TOKEN = env.str("HUGGINGFACEHUB_API_TOKEN")
---------------------------------------------------------------------------
1. Image to Text (Image Captioning)
def img2txt(img_url):
image_to_text = pipeline(
task="image-to-text", model="Salesforce/blip-image-captioning-base"
)
text = image_to_text(img_url)[0]["generated_text"]
# print("\n\n", text)
return text
2. LLM to generate story
def generate_story(scenario):
template = """
You are a story teller;
You can generate a short story based on a simple narrative, the story should be no more than 100 words;
CONTEXT: {scenario}
STORY:
"""
prompt = PromptTemplate(template=template, input_variables=["scenario"])
model_id = "microsoft/phi-2"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = TFAutoModelForCausalLM.from_pretrained(
model_id, trust_remote_code=True
)
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=70,
)
story_llm = HuggingFacePipeline(pipeline=pipe)
llm_chain = LLMChain(prompt=prompt, llm=story_llm, verbose=True)
story = llm_chain.run(scenario)
print("\n\n", story)
return story
generated_text = img2txt("images/rizwan.jpg")
generated_story = generate_story(generated_text)
Python
- 3.11.4
Python pip
- 23.3.1
TensorFlow
- 2.15.0
Trransformers
- 4.35.2
Environs
- 9.5.0
PIL
- 10.1.0
LangChain
- 0.0.345
There is an ongoing PR which should mitigate such an issue: https://github.com/huggingface/transformers/pull/28163.
We will use it to update Phi-2's code.
I solved the problem with setting trust_remote_code=False.
I solved the problem with setting
trust_remote_code=False.
tell me how to use this code. where to enter it? I also encountered the error florence2