Open In Colab

We follow

  • https://docs.argilla.io/en/latest/tutorials/notebooks/labelling-textclassification-sentence-transformers-semantic.html
  • https://github.com/argilla-io/argilla/blob/develop/scripts/load_data.py

but apply to our own dataset and argilla instance.

In [1]:
%pip install argilla datasets==2.8.0 sentence-transformers==2.2.2  -qqq
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.4/2.4 MB 48.2 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 452.9/452.9 kB 46.0 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 86.0/86.0 kB 11.0 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 110.5/110.5 kB 11.9 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 212.5/212.5 kB 23.3 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 134.3/134.3 kB 11.3 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.0/1.0 MB 51.2 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 236.8/236.8 kB 22.5 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.1/7.1 MB 73.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.3/1.3 MB 56.4 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 71.5/71.5 kB 7.9 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 238.1/238.1 kB 23.0 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 114.5/114.5 kB 13.9 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 268.8/268.8 kB 32.0 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 149.6/149.6 kB 19.1 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 69.6/69.6 kB 8.5 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 51.1/51.1 kB 6.3 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.8/7.8 MB 107.8 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 7.7 MB/s eta 0:00:00
  Building wheel for sentence-transformers (setup.py) ... done
In [2]:
import argilla as rg
In [3]:
rg.init(
    api_url="https://utensil-argilla.hf.space/",
    api_key="admin.apikey"
)
/usr/local/lib/python3.10/dist-packages/argilla/client/client.py:152: UserWarning: You're connecting to Argilla Server 1.9.0-dev0 using a different client version (1.8.0).
This may lead to potential compatibility issues during your experience.
To ensure a seamless and optimized connection, we highly recommend aligning your client version with the server version.
  warnings.warn(
In [4]:
from sentence_transformers import SentenceTransformer
from datasets import load_dataset
In [5]:
# Define fast version of sentence transformers
encoder = SentenceTransformer("all-MiniLM-L6-v2")
Downloading (…)e9125/.gitattributes:   0%|          | 0.00/1.18k [00:00<?, ?B/s]
Downloading (…)_Pooling/config.json:   0%|          | 0.00/190 [00:00<?, ?B/s]
Downloading (…)7e55de9125/README.md:   0%|          | 0.00/10.6k [00:00<?, ?B/s]
Downloading (…)55de9125/config.json:   0%|          | 0.00/612 [00:00<?, ?B/s]
Downloading (…)ce_transformers.json:   0%|          | 0.00/116 [00:00<?, ?B/s]
Downloading (…)125/data_config.json:   0%|          | 0.00/39.3k [00:00<?, ?B/s]
Downloading pytorch_model.bin:   0%|          | 0.00/90.9M [00:00<?, ?B/s]
Downloading (…)nce_bert_config.json:   0%|          | 0.00/53.0 [00:00<?, ?B/s]
Downloading (…)cial_tokens_map.json:   0%|          | 0.00/112 [00:00<?, ?B/s]
Downloading (…)e9125/tokenizer.json:   0%|          | 0.00/466k [00:00<?, ?B/s]
Downloading (…)okenizer_config.json:   0%|          | 0.00/350 [00:00<?, ?B/s]
Downloading (…)9125/train_script.py:   0%|          | 0.00/13.2k [00:00<?, ?B/s]
Downloading (…)7e55de9125/vocab.txt:   0%|          | 0.00/232k [00:00<?, ?B/s]
Downloading (…)5de9125/modules.json:   0%|          | 0.00/349 [00:00<?, ?B/s]
In [19]:
from datasets import load_dataset

dataset = load_dataset("QingyiSi/Alpaca-CoT", data_files ="Chain-of-Thought/formatted_cot_data/gsm8k_train.json", split="train")
WARNING:datasets.builder:Using custom data configuration QingyiSi--Alpaca-CoT-2953efcfeb19f105
WARNING:datasets.builder:Found cached dataset json (/root/.cache/huggingface/datasets/QingyiSi___json/QingyiSi--Alpaca-CoT-2953efcfeb19f105/0.0.0/0f7e3662623656454fcd2b650f34e886a7db4b9104504885bd462096cc7a9f51)
In [20]:
dataset.to_pandas().head()
Out[20]:
instruction input output
0 Natalia sold clips to 48 of her friends in Apr... Natalia sold 48 / 2 = 24 clips in May. Natalia...
1 Lizzy: Weng earns $12 an hour for babysitting.... Weng earns 12 / 60 = $0.2 per minute. Working ...
2 Give the step-by-step reasoning process and th... In the beginning, Betty has only 100 / 2 = $50...
3 Question: Julie is reading a 120-page book. Ye... Maila read 12 x 2 = 24 pages today. So she was...
4 James writes a 3-page letter to 2 different fr... He writes each friend 3 * 2 = 6 pages a week. ...
In [21]:
dataset.data.shape
Out[21]:
(7473, 3)
In [22]:
# Encode text field using batched computation
vd = dataset.map(
    lambda batch: {"vectors": encoder.encode(batch["instruction"]
        ),
    },
    batched=True,
    batch_size=32
)
  0%|          | 0/234 [00:00<?, ?ba/s]
In [23]:
vd.data.shape
Out[23]:
(7473, 4)
In [24]:
vd.to_pandas().head()
Out[24]:
instruction input output vectors
0 Natalia sold clips to 48 of her friends in Apr... Natalia sold 48 / 2 = 24 clips in May. Natalia... [0.0087681115, -0.048527658, 0.081025615, -0.0...
1 Lizzy: Weng earns $12 an hour for babysitting.... Weng earns 12 / 60 = $0.2 per minute. Working ... [-0.0025617, 0.084493876, 0.076010056, 0.04024...
2 Give the step-by-step reasoning process and th... In the beginning, Betty has only 100 / 2 = $50... [0.045931183, 0.1536832, -0.0038327056, -0.063...
3 Question: Julie is reading a 120-page book. Ye... Maila read 12 x 2 = 24 pages today. So she was... [0.061020136, 0.06757341, -0.0059414604, -0.02...
4 James writes a 3-page letter to 2 different fr... He writes each friend 3 * 2 = 6 pages a week. ... [0.0141281085, 0.04150687, 0.07270662, -0.0285...
In [25]:
# Turn vectors into a dictionary
vdm = vd.map(
    lambda r: 
     {
        "text": f'USER: {r["instruction"]}\nASSISTANT: {r["output"]}',
        "vectors": {"mini-lm-sentence-transformers": r["vectors"]}
     }
    # remove_columns=vd.column_names
)
  0%|          | 0/7473 [00:00<?, ?ex/s]
In [27]:
dataset_rg = rg.read_datasets(vdm, task="Text2Text")
WARNING:argilla.client.datasets:Following columns are not supported by the Text2TextRecord model and are ignored: ['instruction', 'input', 'output']
In [28]:
# Log the dataset
rg.log(
    dataset_rg,
    name="gsm-8k-test",
    tags={"description": "Testing gsm-8k"},
)
Output()


7473 records logged to https://utensil-argilla.hf.space/datasets/admin/gsm-8k-test
Out[28]:
BulkResponse(dataset='gsm-8k-test', processed=7473, failed=0)
In [47]:
v = vdm.to_pandas().head(1)['vectors'][0]['mini-lm-sentence-transformers']
v
Out[47]:
array([ 8.76811147e-03, -4.85276580e-02,  8.10256153e-02, -2.26558466e-03,
       -4.04507369e-02,  4.98062260e-02,  8.03768262e-02,  6.82035759e-02,
       -7.24245096e-03,  6.23441720e-03,  6.15905784e-02,  3.00756097e-02,
        3.23917135e-03, -3.56388204e-02, -5.26666548e-03,  9.66139659e-02,
       -6.49311393e-02,  3.97711433e-02, -7.81763718e-02, -1.70678906e-02,
       -2.55708005e-02, -1.84912294e-01, -7.04867253e-03, -2.80134450e-03,
        1.21055856e-01,  3.34485136e-02, -6.51538447e-02, -4.38730493e-02,
       -6.70669973e-03, -1.57886930e-02,  1.29787158e-02,  9.46261641e-03,
        1.82289872e-02,  5.01755103e-02, -1.56352092e-02, -6.36283755e-02,
       -1.32085206e-02, -5.46702407e-02,  3.70722637e-03,  3.52124423e-02,
       -1.05476342e-02, -9.15647224e-02, -7.56042749e-02,  1.65473646e-03,
       -2.10455079e-02,  1.05940588e-02, -6.23101965e-02,  1.30693406e-01,
        1.91438198e-02,  2.97926161e-02, -5.40030375e-02,  5.85508794e-02,
       -1.57515168e-01, -1.84658391e-04, -1.38531048e-02, -5.86063713e-02,
       -2.45666038e-02, -1.06479833e-02,  2.60100365e-02,  2.14954484e-02,
        5.34393676e-02, -6.83432221e-02, -6.79798126e-02, -2.84137204e-02,
        1.88998170e-02, -3.27054448e-02, -2.38910597e-02, -1.39383643e-04,
       -3.09031252e-02,  1.90786980e-02,  4.15069088e-02,  8.15688446e-02,
       -2.00472139e-02,  6.88026287e-03,  9.50629823e-03, -3.48923281e-02,
        8.61660019e-02, -2.39894390e-02, -4.90744114e-02, -2.06725374e-02,
       -3.93320508e-02,  6.64086489e-04,  1.58783849e-02,  1.10367369e-02,
       -7.74381310e-02,  4.59574834e-02, -2.88681313e-02,  3.50629762e-02,
        2.48812325e-02,  2.69718412e-02, -9.16993245e-02,  1.39986938e-02,
        3.88188697e-02,  5.01063801e-02,  1.38211688e-02, -3.66736879e-03,
       -8.08107778e-02,  4.31151316e-02,  1.17929913e-01,  1.26365367e-02,
        9.31376684e-03,  7.05228299e-02,  3.04432679e-02, -1.79383680e-02,
       -5.65484352e-02,  3.91498953e-03, -1.27494829e-02, -5.21364436e-03,
       -7.75685161e-02, -2.28064619e-02, -4.57904600e-02,  6.25766022e-03,
        4.60167639e-02, -9.32503864e-03,  4.10420671e-02,  1.60013419e-02,
        1.56276114e-02,  7.75843412e-02,  5.07574975e-02,  3.08171269e-02,
       -1.26047432e-02,  1.31498396e-01,  1.52890375e-02, -1.89122409e-04,
       -9.77964327e-02,  1.33101372e-02, -1.31811369e-02, -8.06712558e-35,
       -3.62042412e-02, -4.01039608e-02,  2.37318445e-02,  3.76849957e-02,
        2.07235310e-02, -2.14410033e-02,  2.28526313e-02, -3.02873855e-03,
       -1.46790212e-02, -3.07900328e-02, -2.58650165e-02, -1.06054269e-01,
        7.86898006e-03, -8.57314244e-02,  2.39466000e-02, -3.13424505e-02,
        9.79893059e-02,  3.45466770e-02,  7.84577709e-03,  7.06806704e-02,
        2.91841123e-02,  2.95803212e-02,  1.96768548e-02,  7.11199194e-02,
       -2.59629879e-02, -1.17908474e-02,  4.25040051e-02,  6.37006611e-02,
        6.51342720e-02,  7.82022160e-03,  2.21183547e-03, -3.19852829e-02,
        3.15304436e-02, -4.17818539e-02,  3.30076776e-02,  6.47614524e-02,
        1.18069641e-01, -2.52201147e-02,  5.62101677e-02, -6.65564928e-03,
       -4.75848019e-02, -7.74295954e-03,  5.67173176e-02, -1.03917317e-02,
       -1.15797631e-01,  3.04536596e-02, -1.84666049e-02,  8.11435953e-02,
        6.63698167e-02,  3.13464031e-02, -1.97230931e-02,  4.60464209e-02,
       -1.70823447e-02,  4.24231775e-02, -1.37718450e-02,  7.48422882e-03,
       -4.49400991e-02, -5.62258922e-02,  2.84257866e-02,  5.52885048e-03,
        2.28445604e-02,  4.72706966e-02,  4.39520590e-02, -2.37044208e-02,
       -5.14908396e-02,  4.98669073e-02,  2.94104889e-02,  4.60110139e-03,
       -2.06038225e-02,  1.22389123e-01, -9.29173529e-02,  6.29264340e-02,
       -8.38771537e-02, -7.35517368e-02,  4.41356674e-02, -6.74400032e-02,
        5.45322634e-02, -7.22953305e-02,  4.21615429e-02,  9.09085050e-02,
       -4.16153073e-02, -1.22325716e-03,  6.05295300e-02,  4.90164198e-02,
       -3.31566506e-03,  2.89744344e-02,  4.08251919e-02, -8.74960888e-03,
        1.29373120e-02,  3.12568545e-02,  1.60560161e-02,  2.35699322e-02,
        4.18388359e-02, -1.32630467e-02,  1.02354595e-02, -1.10134793e-33,
        3.54116112e-02,  3.72039266e-02, -3.28558646e-02,  4.52919863e-02,
        8.65826756e-02,  7.69483820e-02, -6.26853853e-02,  1.60462633e-02,
       -5.34879193e-02,  9.01478622e-03,  3.47595476e-02, -9.38973054e-02,
       -5.34004197e-02,  1.03960317e-02, -6.09315187e-02, -3.05268597e-02,
        3.13471369e-02, -9.63482168e-03,  5.07301185e-03, -3.82963084e-02,
       -8.20474774e-02, -3.05929631e-02,  7.50897601e-02,  1.29101172e-01,
       -4.35712896e-02,  3.74550372e-02,  7.39330575e-02, -4.25359979e-02,
       -1.90514245e-03, -8.14457424e-03,  3.98994237e-02, -9.49942693e-02,
       -1.33318566e-02,  8.24633893e-03,  2.43295711e-02,  8.48984346e-02,
       -1.56631414e-02,  5.62991528e-03,  7.14968666e-02, -5.81957959e-02,
       -5.59045840e-03, -5.17178290e-02,  7.24550635e-02,  3.03000174e-02,
       -4.62108403e-02, -3.70454267e-02,  6.43115267e-02,  2.67343242e-02,
        3.66750360e-02, -1.18874451e-02, -5.70949465e-02, -9.01118107e-03,
       -1.37731969e-01,  3.58180441e-02, -4.29509282e-02, -5.38169360e-03,
       -2.78080292e-02,  6.05350062e-02,  6.19128011e-02,  9.84708779e-03,
       -2.11841557e-02,  1.16136344e-03, -8.31565037e-02, -8.85498896e-02,
       -2.54681204e-02, -7.51398355e-02,  7.43575543e-02, -5.49400412e-02,
       -4.60222550e-02, -3.58609371e-02, -5.23429178e-02,  4.81307693e-02,
       -1.18278936e-02, -4.03171182e-02, -2.11152174e-02,  7.07582384e-02,
       -1.06246918e-01, -3.72095034e-02,  2.26787385e-02, -5.13242036e-02,
       -5.21480367e-02,  5.16740233e-03,  5.87806897e-03,  5.31955473e-02,
       -7.92425312e-03,  3.71847712e-02,  5.69262467e-02,  3.47808041e-02,
       -2.96219327e-02,  7.78562799e-02,  5.13246395e-02,  1.97630916e-02,
        2.64607836e-02, -5.71206305e-03, -4.56360094e-02, -2.68322591e-08,
        5.65887503e-02,  6.40522540e-02, -6.49571419e-03, -1.77617290e-03,
        2.84781400e-02,  7.55349025e-02,  6.46115392e-02,  2.22149715e-02,
        7.57809309e-03,  4.44797464e-02,  2.01221630e-02, -3.02046984e-02,
       -8.79112855e-02,  4.08991016e-02, -4.57425565e-02, -1.06016964e-01,
       -8.51793587e-02,  7.61090815e-02,  2.07329374e-02, -2.80443183e-03,
        3.18390094e-02, -3.70170474e-02,  7.04261437e-02,  3.61589566e-02,
       -6.02237582e-02, -3.55659649e-02, -2.80338340e-02,  6.85308874e-02,
        7.46041015e-02, -1.36446347e-02,  4.59405705e-02, -4.02614772e-02,
       -6.78126663e-02, -4.13187295e-02,  1.78156197e-02, -3.55810151e-02,
       -3.46058942e-02, -1.81127433e-02, -4.97366041e-02,  8.68342724e-03,
        5.12727462e-02, -1.52268395e-01,  3.00468598e-02,  2.98504233e-02,
       -3.32455300e-02, -2.06418559e-02, -4.32278737e-02, -1.35596678e-01,
       -5.01504168e-02, -9.12472829e-02, -1.24169067e-02, -8.47464427e-03,
        5.55751845e-03,  3.19067538e-02,  1.07651521e-02,  1.46903293e-02,
        1.35049699e-02, -3.46617028e-02,  1.92392096e-02, -2.00889166e-02,
       -4.48083095e-02, -9.78459045e-02, -9.34615582e-02,  2.46190354e-02])
In [48]:
v.shape
Out[48]:
(384,)
In [ ]: