{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from transformers import pipeline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "275d80538ffc43b198489f7044e31309", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading: 0%| | 0.00/629 [00:00 for this kind of AutoModel: AutoModelForCausalLM.\nModel type should be one of GPTJConfig, RemBertConfig, RoFormerConfig, BigBirdPegasusConfig, GPTNeoConfig, BigBirdConfig, Speech2Text2Config, BlenderbotSmallConfig, BertGenerationConfig, CamembertConfig, XLMRobertaConfig, PegasusConfig, MarianConfig, MBartConfig, MegatronBertConfig, BartConfig, BlenderbotConfig, ReformerConfig, RobertaConfig, BertConfig, OpenAIGPTConfig, GPT2Config, TransfoXLConfig, XLNetConfig, XLMProphetNetConfig, ProphetNetConfig, XLMConfig, CTRLConfig.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [6], line 12\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# tokenizer = AutoTokenizer.from_pretrained(\"BritishLibraryLabs/bl-books-genre\")\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# model = AutoModelForCausalLM.from_pretrained(\"BritishLibraryLabs/bl-books-genre\")\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;66;03m# \"BritishLibraryLabs/bl-books-genre\"\u001b[39;00m\n\u001b[1;32m 7\u001b[0m \n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# tokenizer = AutoTokenizer.from_pretrained(\"gpt2\")\u001b[39;00m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;66;03m# model = AutoModelForCausalLM.from_pretrained(\"gpt2\")\u001b[39;00m\n\u001b[1;32m 11\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdistilbert-base-cased\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 12\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mdistilbert-base-cased\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;66;03m# generator = pipeline('text-generation', model = \"BritishLibraryLabs/bl-books-genre\")\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;66;03m# generator(\"Hello, I'm a language model\", max_length = 30, num_return_sequences=3)\u001b[39;00m\n\u001b[1;32m 17\u001b[0m generator \u001b[38;5;241m=\u001b[39m pipeline(task\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtext-generation\u001b[39m\u001b[38;5;124m\"\u001b[39m, model\u001b[38;5;241m=\u001b[39mmodel, tokenizer\u001b[38;5;241m=\u001b[39mtokenizer)\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/augmented_poetry/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py:420\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[1;32m 418\u001b[0m model_class \u001b[39m=\u001b[39m _get_model_class(config, \u001b[39mcls\u001b[39m\u001b[39m.\u001b[39m_model_mapping)\n\u001b[1;32m 419\u001b[0m \u001b[39mreturn\u001b[39;00m model_class\u001b[39m.\u001b[39mfrom_pretrained(pretrained_model_name_or_path, \u001b[39m*\u001b[39mmodel_args, config\u001b[39m=\u001b[39mconfig, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m--> 420\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m 421\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mUnrecognized configuration class \u001b[39m\u001b[39m{\u001b[39;00mconfig\u001b[39m.\u001b[39m\u001b[39m__class__\u001b[39m\u001b[39m}\u001b[39;00m\u001b[39m for this kind of AutoModel: \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39mcls\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m\u001b[39m}\u001b[39;00m\u001b[39m.\u001b[39m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m\n\u001b[1;32m 422\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mModel type should be one of \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m, \u001b[39m\u001b[39m'\u001b[39m\u001b[39m.\u001b[39mjoin(c\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m \u001b[39mfor\u001b[39;00m c \u001b[39min\u001b[39;00m \u001b[39mcls\u001b[39m\u001b[39m.\u001b[39m_model_mapping\u001b[39m.\u001b[39mkeys())\u001b[39m}\u001b[39;00m\u001b[39m.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 423\u001b[0m )\n", "\u001b[0;31mValueError\u001b[0m: Unrecognized configuration class for this kind of AutoModel: AutoModelForCausalLM.\nModel type should be one of GPTJConfig, RemBertConfig, RoFormerConfig, BigBirdPegasusConfig, GPTNeoConfig, BigBirdConfig, Speech2Text2Config, BlenderbotSmallConfig, BertGenerationConfig, CamembertConfig, XLMRobertaConfig, PegasusConfig, MarianConfig, MBartConfig, MegatronBertConfig, BartConfig, BlenderbotConfig, ReformerConfig, RobertaConfig, BertConfig, OpenAIGPTConfig, GPT2Config, TransfoXLConfig, XLNetConfig, XLMProphetNetConfig, ProphetNetConfig, XLMConfig, CTRLConfig." ] } ], "source": [ "from transformers import pipeline\n", "from transformers import AutoTokenizer, AutoModelForCausalLM\n", "\n", "# tokenizer = AutoTokenizer.from_pretrained(\"BritishLibraryLabs/bl-books-genre\")\n", "# model = AutoModelForCausalLM.from_pretrained(\"BritishLibraryLabs/bl-books-genre\")\n", "# \"BritishLibraryLabs/bl-books-genre\"\n", "\n", "# tokenizer = AutoTokenizer.from_pretrained(\"gpt2\")\n", "# model = AutoModelForCausalLM.from_pretrained(\"gpt2\")\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(\"distilbert-base-cased\")\n", "model = AutoModelForCausalLM.from_pretrained(\"distilbert-base-cased\")\n", "\n", "# generator = pipeline('text-generation', model = \"BritishLibraryLabs/bl-books-genre\")\n", "# generator(\"Hello, I'm a language model\", max_length = 30, num_return_sequences=3)\n", "\n", "generator = pipeline(task=\"text-generation\", model=model, tokenizer=tokenizer)\n", "generator('something to start with', max_length = 30, num_return_sequences=3)\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "If you want to use `RobertaLMHeadModel` as a standalone, add `is_decoder=True.`\n" ] }, { "ename": "ValueError", "evalue": "num_return_sequences has to be 1, but is 3 when doing greedy search.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [11], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m generator \u001b[38;5;241m=\u001b[39m pipeline(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtext-generation\u001b[39m\u001b[38;5;124m'\u001b[39m, model \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mroberta-base\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mgenerator\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHello, I\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mm a language model\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_length\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m30\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnum_return_sequences\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/augmented_poetry/lib/python3.8/site-packages/transformers/pipelines/text_generation.py:150\u001b[0m, in \u001b[0;36mTextGenerationPipeline.__call__\u001b[0;34m(self, text_inputs, **kwargs)\u001b[0m\n\u001b[1;32m 121\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m__call__\u001b[39m(\u001b[39mself\u001b[39m, text_inputs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs):\n\u001b[1;32m 122\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 123\u001b[0m \u001b[39m Complete the prompt(s) given as inputs.\u001b[39;00m\n\u001b[1;32m 124\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[39m -- The token ids of the generated text.\u001b[39;00m\n\u001b[1;32m 149\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 150\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39msuper\u001b[39;49m()\u001b[39m.\u001b[39;49m\u001b[39m__call__\u001b[39;49m(text_inputs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/augmented_poetry/lib/python3.8/site-packages/transformers/pipelines/base.py:915\u001b[0m, in \u001b[0;36mPipeline.__call__\u001b[0;34m(self, inputs, num_workers, *args, **kwargs)\u001b[0m\n\u001b[1;32m 913\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mget_iterator(inputs, num_workers, preprocess_params, forward_params, postprocess_params)\n\u001b[1;32m 914\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m--> 915\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mrun_single(inputs, preprocess_params, forward_params, postprocess_params)\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/augmented_poetry/lib/python3.8/site-packages/transformers/pipelines/base.py:922\u001b[0m, in \u001b[0;36mPipeline.run_single\u001b[0;34m(self, inputs, preprocess_params, forward_params, postprocess_params)\u001b[0m\n\u001b[1;32m 920\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mrun_single\u001b[39m(\u001b[39mself\u001b[39m, inputs, preprocess_params, forward_params, postprocess_params):\n\u001b[1;32m 921\u001b[0m model_inputs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mpreprocess(inputs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mpreprocess_params)\n\u001b[0;32m--> 922\u001b[0m model_outputs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mforward(model_inputs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mforward_params)\n\u001b[1;32m 923\u001b[0m outputs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mpostprocess(model_outputs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mpostprocess_params)\n\u001b[1;32m 924\u001b[0m \u001b[39mreturn\u001b[39;00m outputs\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/augmented_poetry/lib/python3.8/site-packages/transformers/pipelines/base.py:871\u001b[0m, in \u001b[0;36mPipeline.forward\u001b[0;34m(self, model_inputs, **forward_params)\u001b[0m\n\u001b[1;32m 869\u001b[0m \u001b[39mwith\u001b[39;00m torch\u001b[39m.\u001b[39mno_grad():\n\u001b[1;32m 870\u001b[0m model_inputs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_ensure_tensor_on_device(model_inputs, device\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdevice)\n\u001b[0;32m--> 871\u001b[0m model_outputs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_forward(model_inputs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mforward_params)\n\u001b[1;32m 872\u001b[0m model_outputs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_ensure_tensor_on_device(model_outputs, device\u001b[39m=\u001b[39mtorch\u001b[39m.\u001b[39mdevice(\u001b[39m\"\u001b[39m\u001b[39mcpu\u001b[39m\u001b[39m\"\u001b[39m))\n\u001b[1;32m 873\u001b[0m \u001b[39melse\u001b[39;00m:\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/augmented_poetry/lib/python3.8/site-packages/transformers/pipelines/text_generation.py:165\u001b[0m, in \u001b[0;36mTextGenerationPipeline._forward\u001b[0;34m(self, model_inputs, **generate_kwargs)\u001b[0m\n\u001b[1;32m 163\u001b[0m input_ids \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n\u001b[1;32m 164\u001b[0m prompt_text \u001b[39m=\u001b[39m model_inputs\u001b[39m.\u001b[39mpop(\u001b[39m\"\u001b[39m\u001b[39mprompt_text\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m--> 165\u001b[0m generated_sequence \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mmodel\u001b[39m.\u001b[39;49mgenerate(input_ids\u001b[39m=\u001b[39;49minput_ids, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mgenerate_kwargs) \u001b[39m# BS x SL\u001b[39;00m\n\u001b[1;32m 166\u001b[0m \u001b[39mreturn\u001b[39;00m {\u001b[39m\"\u001b[39m\u001b[39mgenerated_sequence\u001b[39m\u001b[39m\"\u001b[39m: generated_sequence, \u001b[39m\"\u001b[39m\u001b[39minput_ids\u001b[39m\u001b[39m\"\u001b[39m: input_ids, \u001b[39m\"\u001b[39m\u001b[39mprompt_text\u001b[39m\u001b[39m\"\u001b[39m: prompt_text}\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/augmented_poetry/lib/python3.8/site-packages/torch/autograd/grad_mode.py:27\u001b[0m, in \u001b[0;36m_DecoratorContextManager.__call__..decorate_context\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[39m@functools\u001b[39m\u001b[39m.\u001b[39mwraps(func)\n\u001b[1;32m 25\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mdecorate_context\u001b[39m(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs):\n\u001b[1;32m 26\u001b[0m \u001b[39mwith\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mclone():\n\u001b[0;32m---> 27\u001b[0m \u001b[39mreturn\u001b[39;00m func(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniforge/base/envs/augmented_poetry/lib/python3.8/site-packages/transformers/generation_utils.py:984\u001b[0m, in \u001b[0;36mGenerationMixin.generate\u001b[0;34m(self, input_ids, max_length, min_length, do_sample, early_stopping, num_beams, temperature, top_k, top_p, repetition_penalty, bad_words_ids, bos_token_id, pad_token_id, eos_token_id, length_penalty, no_repeat_ngram_size, encoder_no_repeat_ngram_size, num_return_sequences, max_time, max_new_tokens, decoder_start_token_id, use_cache, num_beam_groups, diversity_penalty, prefix_allowed_tokens_fn, output_attentions, output_hidden_states, output_scores, return_dict_in_generate, forced_bos_token_id, forced_eos_token_id, remove_invalid_values, synced_gpus, **model_kwargs)\u001b[0m\n\u001b[1;32m 982\u001b[0m \u001b[39mif\u001b[39;00m is_greedy_gen_mode:\n\u001b[1;32m 983\u001b[0m \u001b[39mif\u001b[39;00m num_return_sequences \u001b[39m>\u001b[39m \u001b[39m1\u001b[39m:\n\u001b[0;32m--> 984\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m 985\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mnum_return_sequences has to be 1, but is \u001b[39m\u001b[39m{\u001b[39;00mnum_return_sequences\u001b[39m}\u001b[39;00m\u001b[39m when doing greedy search.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 986\u001b[0m )\n\u001b[1;32m 988\u001b[0m \u001b[39m# greedy search\u001b[39;00m\n\u001b[1;32m 989\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mgreedy_search(\n\u001b[1;32m 990\u001b[0m input_ids,\n\u001b[1;32m 991\u001b[0m logits_processor\u001b[39m=\u001b[39mlogits_processor,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 998\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mmodel_kwargs,\n\u001b[1;32m 999\u001b[0m )\n", "\u001b[0;31mValueError\u001b[0m: num_return_sequences has to be 1, but is 3 when doing greedy search." ] } ], "source": [ "generator = pipeline('text-generation', model = 'roberta-base')\n", "generator(\"Hello, I'm a language model\", max_length = 30, num_return_sequences=3)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'[Illustration: \"I saw there something missing from'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from gutenbergdammit.ziputils import retrieve_one\n", "text = retrieve_one(\"gutenberg-dammit-files-v002.zip\", \"123/12345.txt\")\n", "# text = retrieve_one(\"gutenberg-dammit-files-v002.zip\", \"123/12345.txt\")\n", "text[:50]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Author': ['Franklin Delano Roosevelt'],\n", " 'Author Birth': [1882],\n", " 'Author Death': [1945],\n", " 'Author Given': ['Franklin Delano'],\n", " 'Author Surname': ['Roosevelt'],\n", " 'Copyright Status': ['Not copyrighted in the United States.'],\n", " 'Language': ['English'],\n", " 'LoC Class': ['E740: History: America: Twentieth century'],\n", " 'Num': '104',\n", " 'Subject': ['New Deal, 1933-1939',\n", " 'Presidents -- United States -- Inaugural addresses',\n", " 'United States -- Politics and government -- 1933-1945'],\n", " 'Title': [\"Franklin Delano Roosevelt's First Inaugural Address\"],\n", " 'charset': 'us-ascii',\n", " 'gd-num-padded': '00104',\n", " 'gd-path': '001/00104.txt',\n", " 'href': '/1/0/104/104.zip'}" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from gutenbergdammit.ziputils import loadmetadata\n", "metadata = loadmetadata(\"gutenberg-dammit-files-v002.zip\")\n", "metadata[101]\n", "# ['Essays in the Art of Writing']" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Entertaining Made Easy 108314\n", "Reading Made Easy for Foreigners - Third Reader 209964\n", "The Art of Cookery Made Easy and Refined 262990\n", "Shaving Made Easy\tWhat the Man Who Shaves Ought to Know 44982\n", "Writing and Drawing Made Easy, Amusing and Instructive\tContaining The Whole Alphabet in all the Characters now\tus'd, Both in Printing and Penmanship 10036\n", "Etiquette Made Easy 119372\n" ] } ], "source": [ "from gutenbergdammit.ziputils import searchandretrieve\n", "for info, text in searchandretrieve(\"gutenberg-dammit-files-v002.zip\", {'Title': 'Made Easy'}):\n", " print(info['Title'][0], len(text))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# from gutenbergdammit.ziputils import retrieve_one\n", "# search and retrieve only poetry text\n", "# fine tune with line-by-line" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# from transformers import AutoModel\n", "# model_clpt = \"distilbert-base-uncased\"\n", "# device = torch.device(\"cuda\" id tor)\n", "import torch\n", "torch.cuda.is_available()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From /var/folders/vr/wjfjpzn1755bvptln8g22f9r0000gn/T/ipykernel_91499/3763141526.py:2: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "Use `tf.config.list_physical_devices('GPU')` instead.\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import tensorflow as tf\n", "tf.test.is_gpu_available()\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.config.list_physical_devices('GPU')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.config.list_physical_devices('CPU')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Source data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "curl -O http://static.decontextualize.com/gutenberg-poetry-v001.ndjson.gz" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "import gzip, json\n", "all_lines = []\n", "for line in gzip.open(\"gutenberg-poetry-v001.ndjson.gz\"):\n", " all_lines.append(json.loads(line.strip()))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'s': 'The Song of Hiawatha is based on the legends and stories of', 'gid': '19'}, {'s': 'many North American Indian tribes, but especially those of the', 'gid': '19'}, {'s': 'Ojibway Indians of northern Michigan, Wisconsin, and Minnesota.', 'gid': '19'}, {'s': 'They were collected by Henry Rowe Schoolcraft, the reknowned', 'gid': '19'}, {'s': 'Schoolcraft married Jane, O-bah-bahm-wawa-ge-zhe-go-qua (The', 'gid': '19'}, {'s': 'fur trader, and O-shau-gus-coday-way-qua (The Woman of the Green', 'gid': '19'}, {'s': 'Prairie), who was a daughter of Waub-o-jeeg (The White Fisher),', 'gid': '19'}, {'s': 'who was Chief of the Ojibway tribe at La Pointe, Wisconsin.', 'gid': '19'}, {'s': 'Jane and her mother are credited with having researched,', 'gid': '19'}, {'s': 'authenticated, and compiled much of the material Schoolcraft', 'gid': '19'}]\n" ] } ], "source": [ "import random\n", "random.sample(all_lines, 8)\n", "\n", "print(all_lines[0:10])\n", "\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Author': ['Henry Rider Haggard'],\n", " 'Author Birth': [1856],\n", " 'Author Death': [1925],\n", " 'Author Given': ['Henry Rider'],\n", " 'Author Surname': ['Haggard'],\n", " 'Copyright Status': ['Not copyrighted in the United States.'],\n", " 'Language': ['English'],\n", " 'LoC Class': ['PR: Language and Literatures: English literature'],\n", " 'Num': '2721',\n", " 'Subject': ['Iceland -- Fiction'],\n", " 'Title': ['Eric Brighteyes'],\n", " 'charset': 'iso-8859-1',\n", " 'gd-num-padded': '02721',\n", " 'gd-path': '027/02721.txt',\n", " 'href': '/2/7/2/2721/2721_8.zip'}" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from gutenbergdammit.ziputils import loadmetadata\n", "metadata = loadmetadata(\"gutenberg-dammit-files-v002.zip\")\n", "metadata[2620]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['The Song of Hiawatha is based on the legends and stories of',\n", " 'many North American Indian tribes, but especially those of the',\n", " 'Ojibway Indians of northern Michigan, Wisconsin, and Minnesota.',\n", " 'They were collected by Henry Rowe Schoolcraft, the reknowned',\n", " 'Schoolcraft married Jane, O-bah-bahm-wawa-ge-zhe-go-qua (The',\n", " 'fur trader, and O-shau-gus-coday-way-qua (The Woman of the Green',\n", " 'Prairie), who was a daughter of Waub-o-jeeg (The White Fisher),',\n", " 'who was Chief of the Ojibway tribe at La Pointe, Wisconsin.',\n", " 'Jane and her mother are credited with having researched,',\n", " 'authenticated, and compiled much of the material Schoolcraft']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[line['s'] for line in all_lines[0:10]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.6 ('augmented_poetry')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "00664817f4a09ab74dd392ee5a8d12e3606381c26df296db9ea5c334bb5d1b65" } } }, "nbformat": 4, "nbformat_minor": 2 }