This page looks best with JavaScript enabled

Model and Dataset Operations on HuggingFace

 ·  ☕ 2 min read

HuggingFace offers shared models, datasets, and hosted spaces, giving AI researchers and developers a complete ecosystem. This article explains how to work with HuggingFace models and datasets.

1. Model Operations and Usage

1.1 Custom Storage Directory

1
export HF_HOME=/Volumes/Data/HuggingFace

Otherwise the default is the ~/.cache/huggingface directory.

1.2 Downloading Models

The first method is to click download on the page and save it locally.

https://huggingface.co/LinkSoul/Chinese-Llama-2-7b/tree/main — click the download icon in the file list.

The second method is to download with Git LFS.

After installing git-lfs, run:

1
git lfs install

Clone the model to your local machine:

1
git clone https://huggingface.co/LinkSoul/Chinese-Llama-2-7b

The third method is to download with huggingface-hub:

1
pip install huggingface_hub
1
2
from huggingface_hub import snapshot_download
snapshot_download(repo_id="LinkSoul/Chinese-Llama-2-7b")

The fourth method is online download when using transformers:

1
pip install transformers
1
2
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("LinkSoul/Chinese-Llama-2-7b")

1.3 Operating on Models

  • Load a model
1
2
3
from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained("LinkSoul/Chinese-Llama-2-7b")
  • Save a model
1
2
3
4
from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained("LinkSoul/Chinese-Llama-2-7b")
model.save_pretrained("/Volumes/Data/HuggingFace/Chinese-Llama-2-7b-v2")

1.4 Using Models

  • Install dependencies
1
pip install transformers torch
  • Use the model
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from transformers import EncoderDecoderModel, AutoTokenizer

model_id = "raynardj/wenyanwen-chinese-translate-to-ancient"
model = EncoderDecoderModel.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)

def chat(text):
  input_ids = tokenizer.encode(text, return_tensors='pt')
  output = model.generate(input_ids, max_length=40)
  return tokenizer.decode(output[0], skip_special_tokens=True)

chat("你好")
1
汝 好

2. Dataset Operations and Usage

2.1 Downloading Datasets

  • Install datasets
1
pip install datasets
  • Download a dataset

Start Ipython:

1
ipython
1
2
In [1]: import datasets
In [2]: remote_datasets = datasets.load_dataset("fka/awesome-chatgpt-prompts")

At this point the dataset is downloaded into the $HF_HOME/datasets directory. As with models, datasets can also be downloaded from the web page or with Git LFS; we will not repeat that here.

  • Inspect the dataset collection
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
tree -L 2 $HF_HOME/datasets

/Volumes/Data/HuggingFace/datasets
├── _Volumes_Data_HuggingFace_datasets_fka___awesome-chatgpt-prompts_default-18237255be23cc62_0.0.0_eea64c71ca8b46dd3f537ed218fc9bf495d5707789152eb2764f5c78fa66d59d.lock
├── downloads
│   ├── 7528ed6bf521cf4a58ed283bfa5ba864e12c7203ad53ea3495ba45326e30768a
│   ├── 7528ed6bf521cf4a58ed283bfa5ba864e12c7203ad53ea3495ba45326e30768a.json
│   ├── 7528ed6bf521cf4a58ed283bfa5ba864e12c7203ad53ea3495ba45326e30768a.lock
│   ├── f41fd13f9d4e803c35d9543c56b1d887676f17d84d10e3a428ad1e46bcce6c78.8fbabec58cee4e6f69e20f509619af34f2b4ed0052c2c39ca0d73a47e1035a8b
│   ├── f41fd13f9d4e803c35d9543c56b1d887676f17d84d10e3a428ad1e46bcce6c78.8fbabec58cee4e6f69e20f509619af34f2b4ed0052c2c39ca0d73a47e1035a8b.json
│   └── f41fd13f9d4e803c35d9543c56b1d887676f17d84d10e3a428ad1e46bcce6c78.8fbabec58cee4e6f69e20f509619af34f2b4ed0052c2c39ca0d73a47e1035a8b.lock
└── fka___awesome-chatgpt-prompts
    └── default-18237255be23cc62

As you can see, the stored directory is not fka/awesome-chatgpt-prompts. You cannot use datasets.load_from_disk("fka/awesome-chatgpt-prompts") to load the dataset; load_from_disk is meant for datasets downloaded directly or via Git LFS.

2.2 Operating on Datasets

  • View the dataset
1
2
3
4
5
6
7
8
In [3]: remote_datasets

DatasetDict({
    train: Dataset({
        features: ['act', 'prompt'],
        num_rows: 153
    })
})

As you can see, there are 153 records in total, stored in two fields, act and prompt.

  • View the data
1
2
3
In [4]: remote_datasets["train"][0]
Out[4]: {'act': 'Linux Terminal',
 'prompt': 'I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd'}
  • Select data at random
1
2
3
4
5
6
In [5]: remote_datasets["train"].select(range(10))
Out[5]:
Dataset({
    features: ['act', 'prompt'],
    num_rows: 10
})
  • Rename a column
1
2
3
4
5
6
7
8
9
In [5]: new_datasets = remote_datasets.rename_column("act", "actor")
In [6]: new_datasets
Out[6]:
DatasetDict({
    train: Dataset({
        features: ['actor', 'prompt'],
        num_rows: 153
    })
})
  • Filter data with filter
1
2
3
4
5
6
7
8
In [7]: new_datasets.filter(lambda x: "Linux" in x["actor"])
Out[7]:
DatasetDict({
    train: Dataset({
        features: ['actor', 'prompt'],
        num_rows: 1
    })
})
  • Process data with map
1
2
3
4
In [8]: new_datasets.map(lambda x: {"actor": x["actor"].upper(), "prompt": x["prompt"]})["train"][0]
Out[8]:
{'actor': 'LINUX TERMINAL',
 'prompt': 'I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd'}
  • Sort with sort
1
2
3
4
In [9]: new_datasets.sort("actor")["train"][0]
Out[9]:
{'actor': 'AI Assisted Doctor',
 'prompt': 'I want you to act as an AI assisted doctor. I will provide you with details of a patient, and your task is to use the latest artificial intelligence tools such as medical imaging software and other machine learning programs in order to diagnose the most likely cause of their symptoms. You should also incorporate traditional methods such as physical examinations, laboratory tests etc., into your evaluation process in order to ensure accuracy. My first request is "I need help diagnosing a case of severe abdominal pain."'}
  • Shuffle
1
2
3
4
In [10]: new_datasets.shuffle(seed=42)["train"][0]
Out[10]:
{'actor': 'Tech Reviewer:',
 'prompt': 'I want you to act as a tech reviewer. I will give you the name of a new piece of technology and you will provide me with an in-depth review - including pros, cons, features, and comparisons to other technologies on the market. My first suggestion request is "I am reviewing iPhone 11 Pro Max".'}
  • Select data at random
1
2
3
4
5
6
In [11]: new_datasets.shuffle(seed=42)["train"].select(range(10))
Out[11]:
Dataset({
    features: ['actor', 'prompt'],
    num_rows: 10
})
  • Export the dataset
1
new_datasets.save_to_disk("fka_awesome-chatgpt-prompts_2")

The data will be saved to fka_awesome-chatgpt-prompts_2 under the Ipython working directory.


微信公众号
WRITTEN BY
微信公众号