我有内存中的文本,json 格式,我正在尝试直接从内存中的文本加载数据集(HuggingFace)。
如果我将其保存到文件中 - 我可以使用 Huggingface load_dataset 加载数据集:
from datasets import load_dataset
dataset = load_dataset('json', data_files='my_file.json')
另请参阅:https://huggingface.co/docs/datasets/v1.11.0/loading_datasets.html#from-local-files
我可以直接从内存中的文本加载数据集而不将其保存到文件中吗?
从 json 构建一个字典,然后自己构建数据集对象:
import json
import datasets
the_json_string = "..." # you define this obviously
the_dict = json.loads(the_json_string) # loads builds a dict from a string
dataset_object = datasets.Dataset.from_dict(the_dict)
查看
datasets.Dataset.from_dict
的文档,了解具体如何实现此功能: