Repository: flairNLP/flair

Issue:
PermissionError when downloading and copying models
I am on Windows 10, python 3.6.  Today I installed flair with pip, and have been going through the documentation. 

Whenever the flair package downloads a model and tries to remove it from temp, I get the following PermissionError, which my guess is a Window's specific thing.

```
2019-02-21 11:28:27,309 https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/de-wiki-fasttext-300d-1M.vectors.npy not found in cache, downloading to C:\Users\EXTERN~1\AppData\Local\Temp\2\tmp5uq0mv3m

100%|██████████| 1199998928/1199998928 [00:23<00:00, 50211190.58B/s]

2019-02-21 11:28:51,427 copying C:\Users\EXTERN~1\AppData\Local\Temp\2\tmp5uq0mv3m to cache at C:\Users\external-dsvm-1\.flair\embeddings\de-wiki-fasttext-300d-1M.vectors.npy
2019-02-21 11:29:04,958 removing temp file C:\Users\EXTERN~1\AppData\Local\Temp\2\tmp5uq0mv3m

---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
<ipython-input-12-a2ec2b8c11d7> in <module>
----> 1 german_embedding = WordEmbeddings('de')

C:\anaconda\envs\tf_gpu\lib\site-packages\flair\embeddings.py in __init__(self, embeddings)
    185         # two-letter language code wiki embeddings
    186         elif len(embeddings.lower()) == 2:
--> 187             cached_path(f'{embeddings_path_v4}{embeddings}-wiki-fasttext-300d-1M.vectors.npy', cache_dir=cache_dir)
    188             embeddings = cached_path(f'{embeddings_path_v4}{embeddings}-wiki-fasttext-300d-1M', cache_dir=cache_dir)
    189 

C:\anaconda\envs\tf_gpu\lib\site-packages\flair\file_utils.py in cached_path(url_or_filename, cache_dir)
     72     if parsed.scheme in ('http', 'https'):
     73         # URL, so get it from the cache (downloading if necessary)
---> 74         return get_from_cache(url_or_filename, dataset_cache)
     75     elif parsed.scheme == '' and Path(url_or_filename).exists():
     76         # File, and it exists.

C:\anaconda\envs\tf_gpu\lib\site-packages\flair\file_utils.py in get_from_cache(url, cache_dir)
    128         shutil.copyfile(temp_filename, str(cache_path))
    129         logger.info("removing temp file %s", temp_filename)
--> 130         os.remove(temp_filename)
    131 
    132     return cache_path

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\EXTERN~1\\AppData\\Local\\Temp\\2\\tmp5uq0mv3m'
```


Discussion:
Hm that is strange, and I haven't heard about this on Windows machines yet. Is this maybe a permissions issue?
I always had this issue on Windows, but not on Linux.
Could you try replacing the line 

```python
os.remove(temp_filename)
```
with 
```python
shutil.rmtree(temp_filename)
```

to see if the error is still thrown?

Resolving patch:
diff --git a/flair/file_utils.py b/flair/file_utils.py
--- a/flair/file_utils.py
+++ b/flair/file_utils.py
@@ -123,7 +123,7 @@ def get_from_cache(url: str, cache_dir: Path = None) -> Path:
     if not cache_path.exists():
         # Download to temporary file, then copy to cache dir once finished.
         # Otherwise you get corrupt cache entries if the download gets interrupted.
-        _, temp_filename = tempfile.mkstemp()
+        fd, temp_filename = tempfile.mkstemp()
         logger.info("%s not found in cache, downloading to %s", url, temp_filename)
 
         # GET file object
@@ -142,6 +142,7 @@ def get_from_cache(url: str, cache_dir: Path = None) -> Path:
         logger.info("copying %s to cache at %s", temp_filename, cache_path)
         shutil.copyfile(temp_filename, str(cache_path))
         logger.info("removing temp file %s", temp_filename)
+        os.close(fd)
         os.remove(temp_filename)
 
     return cache_path


Test patch: