Artificial IntelligenceMachine Learning

Download and Save LLM from Hugging Face

How to download and save open osurce models from Hugging Face platform

Download code here

Prerequisites

  • Python environment: Ensure you have Python installed on your system. You can download it from https://www.python.org/
  • Libraries: Install the following libraries using pip: pip install transformers huggingface_hub pytorch Use code with caution.content_copy

Steps

Install & Import Packages

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

Download Tokenizer & save

model_name="microsoft/phi-2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.save_pretrained(f"cache1/tokenizer/{model_name}")
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.

Out[3]:

('cache1/tokenizer/microsoft/phi-2\\tokenizer_config.json',
 'cache1/tokenizer/microsoft/phi-2\\special_tokens_map.json',
 'cache1/tokenizer/microsoft/phi-2\\vocab.json',
 'cache1/tokenizer/microsoft/phi-2\\merges.txt',
 'cache1/tokenizer/microsoft/phi-2\\added_tokens.json',
 'cache1/tokenizer/microsoft/phi-2\\tokenizer.json')

Download Model & Save

model = AutoModelForCausalLM.from_pretrained(model_name)
model.save_pretrained(f"cache1/model/{model_name}")

Out:

Loading checkpoint shards: 100%

2/2 [00:15<00:00, 6.62s/it]

Load saved model & tokenizer

tokenizer = AutoTokenizer.from_pretrained(f"cache1/tokenizer/{model_name}")
model = AutoModelForCausalLM.from_pretrained(f"cache1/model/{model_name}")
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.

Out:

Loading checkpoint shards: 100%

3/3 [00:33<00:00, 9.26s/it]

Generate Response

input_text = "Write a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids, max_new_tokens= 60)
print(tokenizer.decode(outputs[0]))
​
Out:

Loading checkpoint shards: 100%

2/2 [00:09<00:00, 3.81s/it]

<bos>Write a poem about Machine Learning.

Answer:

Step 1/2
Machine Learning is a powerful tool that can help us make sense of the world around us. It can analyze vast amounts of data and make predictions based on that data. It can also learn from our own actions and behaviors, and adapt to our changing needs

Download code here

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button