dvgodoy / PyTorchStepByStep

Official repository of my book: "Deep Learning with PyTorch Step-by-Step: A Beginner's Guide"
https://pytorchstepbystep.com
MIT License
866 stars 332 forks source link

[Chapter 11] Import error; ModuleNotFoundError: No module named 'flair' #41

Closed petrov826 closed 1 year ago

petrov826 commented 1 year ago

Hello.

On Google Colab, I installed necessary packages for chapter11 using below pip commands and tried to import them. But I got an error below.

pip commands

!pip install gensim==3.8.3
!pip install allennlp==0.9.0
!pip install flair==0.8.0.post1 # uses PyTorch 1.7.1
!pip install torchvision==0.8.2
# HuggingFace
!pip install transformers==4.5.1
!pip install datasets==1.6.0

import code

import os
import json
import errno
import requests
import numpy as np
from copy import deepcopy
from operator import itemgetter

import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, TensorDataset, Dataset

from data_generation.nlp import ALICE_URL, WIZARD_URL, download_text
from stepbystep.v4 import StepByStep
# These are the classes we built in Chapter 10
from seq2seq import *

import nltk
from nltk.tokenize import sent_tokenize

import gensim
from gensim import corpora, downloader
from gensim.parsing.preprocessing import *
from gensim.utils import simple_preprocess
from gensim.models import Word2Vec

from flair.data import Sentence
from flair.embeddings import ELMoEmbeddings, WordEmbeddings, \
    TransformerWordEmbeddings, TransformerDocumentEmbeddings

from datasets import load_dataset, Split
from transformers import (
    DataCollatorForLanguageModeling,
    BertModel, BertTokenizer, BertForSequenceClassification,
    DistilBertModel, DistilBertTokenizer,
    DistilBertForSequenceClassification,
    AutoModelForSequenceClassification,
    AutoModel, AutoTokenizer, AutoModelForCausalLM,
    Trainer, TrainingArguments, pipeline, TextClassificationPipeline
)
from transformers.pipelines import SUPPORTED_TASKS

Error

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
[<ipython-input-4-d2e06031a8c5>](https://localhost:8080/#) in <cell line: 29>()
     27 from gensim.models import Word2Vec
     28 
---> 29 from flair.data import Sentence
     30 from flair.embeddings import ELMoEmbeddings, WordEmbeddings, \
     31     TransformerWordEmbeddings, TransformerDocumentEmbeddings

ModuleNotFoundError: No module named 'flair'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
Here’s the installation log. Collecting gensim==3.8.3 Downloading gensim-3.8.3.tar.gz (23.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 23.4/23.4 MB 31.6 MB/s eta 0:00:00 Preparing metadata (setup.py) ... done Requirement already satisfied: numpy>=1.11.3 in /usr/local/lib/python3.10/dist-packages (from gensim==3.8.3) (1.23.5) Requirement already satisfied: scipy>=0.18.1 in /usr/local/lib/python3.10/dist-packages (from gensim==3.8.3) (1.10.1) Requirement already satisfied: six>=1.5.0 in /usr/local/lib/python3.10/dist-packages (from gensim==3.8.3) (1.16.0) Requirement already satisfied: smart_open>=1.8.1 in /usr/local/lib/python3.10/dist-packages (from gensim==3.8.3) (6.3.0) Building wheels for collected packages: gensim error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. Building wheel for gensim (setup.py) ... error ERROR: Failed building wheel for gensim Running setup.py clean for gensim Failed to build gensim ERROR: Could not build wheels for gensim, which is required to install pyproject.toml-based projects Collecting allennlp==0.9.0 Downloading allennlp-0.9.0-py3-none-any.whl (7.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.6/7.6 MB 16.4 MB/s eta 0:00:00 Requirement already satisfied: torch>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from allennlp==0.9.0) (2.0.1+cu118) Collecting overrides (from allennlp==0.9.0) Downloading overrides-7.4.0-py3-none-any.whl (17 kB) Requirement already satisfied: nltk in /usr/local/lib/python3.10/dist-packages (from allennlp==0.9.0) (3.8.1) Collecting spacy<2.2,>=2.1.0 (from allennlp==0.9.0) Downloading spacy-2.1.9.tar.gz (30.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 30.7/30.7 MB 38.4 MB/s eta 0:00:00 error: subprocess-exited-with-error × pip subprocess to install build dependencies did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. Installing build dependencies ... error error: subprocess-exited-with-error × pip subprocess to install build dependencies did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. Collecting flair==0.8.0.post1 Downloading flair-0.8.0.post1-py3-none-any.whl (284 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 284.8/284.8 kB 4.8 MB/s eta 0:00:00 Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.10/dist-packages (from flair==0.8.0.post1) (2.8.2) INFO: pip is looking at multiple versions of flair to determine which version is compatible with other requirements. This could take a while. ERROR: Could not find a version that satisfies the requirement torch<=1.7.1,>=1.5.0 (from flair) (from versions: 1.11.0, 1.12.0, 1.12.1, 1.13.0, 1.13.1, 2.0.0, 2.0.1) ERROR: No matching distribution found for torch<=1.7.1,>=1.5.0 ERROR: Could not find a version that satisfies the requirement torchvision==0.8.2 (from versions: 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.2.0, 0.2.1, 0.2.2, 0.2.2.post2, 0.2.2.post3, 0.12.0, 0.13.0, 0.13.1, 0.14.0, 0.14.1, 0.15.0, 0.15.1, 0.15.2) ERROR: No matching distribution found for torchvision==0.8.2 Collecting transformers==4.5.1 Downloading transformers-4.5.1-py3-none-any.whl (2.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 8.9 MB/s eta 0:00:00 Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers==4.5.1) (3.12.2) Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from transformers==4.5.1) (1.23.5) Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from transformers==4.5.1) (23.1) Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers==4.5.1) (2023.6.3) Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers==4.5.1) (2.31.0) Collecting sacremoses (from transformers==4.5.1) Downloading sacremoses-0.0.53.tar.gz (880 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 880.6/880.6 kB 14.0 MB/s eta 0:00:00 Preparing metadata (setup.py) ... done Collecting tokenizers<0.11,>=0.10.1 (from transformers==4.5.1) Downloading tokenizers-0.10.3.tar.gz (212 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 212.7/212.7 kB 15.8 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers==4.5.1) (4.66.1) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers==4.5.1) (3.2.0) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->transformers==4.5.1) (3.4) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers==4.5.1) (2.0.4) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->transformers==4.5.1) (2023.7.22) Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from sacremoses->transformers==4.5.1) (1.16.0) Requirement already satisfied: click in /usr/local/lib/python3.10/dist-packages (from sacremoses->transformers==4.5.1) (8.1.7) Requirement already satisfied: joblib in /usr/local/lib/python3.10/dist-packages (from sacremoses->transformers==4.5.1) (1.3.2) Building wheels for collected packages: tokenizers, sacremoses error: subprocess-exited-with-error × Building wheel for tokenizers (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. Building wheel for tokenizers (pyproject.toml) ... error ERROR: Failed building wheel for tokenizers Building wheel for sacremoses (setup.py) ... done Created wheel for sacremoses: filename=sacremoses-0.0.53-py3-none-any.whl size=895241 sha256=3fcf65ccb41363bbf5a10c5d759cebc935827085d5004ebeffef4ef3feeb8f31 Stored in directory: /root/.cache/pip/wheels/00/24/97/a2ea5324f36bc626e1ea0267f33db6aa80d157ee977e9e42fb Successfully built sacremoses Failed to build tokenizers ERROR: Could not build wheels for tokenizers, which is required to install pyproject.toml-based projects Collecting datasets==1.6.0 Downloading datasets-1.6.0-py3-none-any.whl (202 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 202.2/202.2 kB 3.5 MB/s eta 0:00:00 Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from datasets==1.6.0) (1.23.5) Requirement already satisfied: pyarrow>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from datasets==1.6.0) (9.0.0) Collecting dill (from datasets==1.6.0) Downloading dill-0.3.7-py3-none-any.whl (115 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 115.3/115.3 kB 7.0 MB/s eta 0:00:00 Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from datasets==1.6.0) (1.5.3) Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from datasets==1.6.0) (2.31.0) Collecting tqdm<4.50.0,>=4.27 (from datasets==1.6.0) Downloading tqdm-4.49.0-py2.py3-none-any.whl (69 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 69.8/69.8 kB 7.5 MB/s eta 0:00:00 Collecting xxhash (from datasets==1.6.0) Downloading xxhash-3.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (194 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 194.1/194.1 kB 8.1 MB/s eta 0:00:00 Collecting multiprocess (from datasets==1.6.0) Downloading multiprocess-0.70.15-py310-none-any.whl (134 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 134.8/134.8 kB 6.1 MB/s eta 0:00:00 Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from datasets==1.6.0) (2023.6.0) Collecting huggingface-hub<0.1.0 (from datasets==1.6.0) Downloading huggingface_hub-0.0.19-py3-none-any.whl (56 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.9/56.9 kB 6.6 MB/s eta 0:00:00 Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from datasets==1.6.0) (23.1) Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<0.1.0->datasets==1.6.0) (3.12.2) Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<0.1.0->datasets==1.6.0) (6.0.1) Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<0.1.0->datasets==1.6.0) (4.7.1) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->datasets==1.6.0) (3.2.0) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->datasets==1.6.0) (3.4) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->datasets==1.6.0) (2.0.4) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->datasets==1.6.0) (2023.7.22) Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets==1.6.0) (2.8.2) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets==1.6.0) (2023.3) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.1->pandas->datasets==1.6.0) (1.16.0) Installing collected packages: xxhash, tqdm, dill, multiprocess, huggingface-hub, datasets Attempting uninstall: tqdm Found existing installation: tqdm 4.66.1 Uninstalling tqdm-4.66.1: Successfully uninstalled tqdm-4.66.1 Successfully installed datasets-1.6.0 dill-0.3.7 huggingface-hub-0.0.19 multiprocess-0.70.15 tqdm-4.49.0 xxhash-3.3.0

It seems that a few things have changed since last update. According to the AllenNLP’s official repo, the library is now in maintenance mode. PyTorch 1.7.1 used flair==0.8.0.post1 is no longer available via pip.

I want to work with these fancy packages, torchtext, fastText or something, but I have few idea to solve this dependency problem. Could you give me some help?

petrov826 commented 1 year ago

I found that using !pip install gensim flair torchvision transformers datasets -U will almost solve this issue. But this pip command will NOT install allennlp so we can't use flair.embeddings.ELMoEmbeddings. And we have to upgrade gensim 3.x to 4.0. There will be some API changes like len(glove.vocab) now should be len(glove.key_to_index) and so on. I hope this information helps.

dvgodoy commented 1 year ago

Hi @petrov826

Thank you very much for pointing this out. You're spot on on the changes you proposed :-) I've just pushed the updated notebook with more up-to-date versions of the libraries. Unfortunately, allennlp had to go, and we lost ELMO embeddings. They were interesting for historical purposes but they hardly had any practical use these days, so it's not a big loss, I guess. In every cell that was changed, I commented the original code and placed its updated replacement right after. Everything should work now.

Thanks again for supporting my work and for helping me maintain it :-)

Best, Daniel

petrov826 commented 1 year ago

Thank you @dvgodoy!

I ran the notebook you pushed on Google Colab and everything worked fine. I miss ELMO but it still lives in your book and so many blog post. I'll read carefully the ELMO section on chapter11. Any way, I'll keep on learning and dive into the NLP world with my new friend Transformer😎.

Thanks again😄