drewThomasson commited on
Commit
ae020c7
1 Parent(s): 204a060

Create download_missing_booknlp_models.py

Browse files
Files changed (1) hide show
  1. download_missing_booknlp_models.py +63 -0
download_missing_booknlp_models.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import zipfile
4
+ from pathlib import Path
5
+ from tqdm import tqdm
6
+
7
+ def download_and_extract():
8
+ url = "https://huggingface.co/drewThomasson/Booknlp_models_Backup/resolve/main/booknlp_models.zip?download=true"
9
+ file_name = "booknlp_models.zip"
10
+ home_dir = str(Path.home())
11
+ zip_path = os.path.join(home_dir, file_name)
12
+ models_folder = os.path.join(home_dir, "booknlp_models")
13
+
14
+ print("Downloading the missing booknlp files...")
15
+ response = requests.get(url, stream=True)
16
+ total_size = int(response.headers.get('content-length', 0))
17
+
18
+ with open(zip_path, 'wb') as file, tqdm(
19
+ desc=file_name,
20
+ total=total_size,
21
+ unit='iB',
22
+ unit_scale=True,
23
+ unit_divisor=1024,
24
+ ) as progress_bar:
25
+ for data in response.iter_content(chunk_size=1024):
26
+ size = file.write(data)
27
+ progress_bar.update(size)
28
+
29
+ print("Extracting required files...")
30
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
31
+ for file in zip_ref.namelist():
32
+ if file.startswith('booknlp_models/') and file.split('/')[-1] in required_files:
33
+ zip_ref.extract(file, home_dir)
34
+
35
+ print("Deleting the zip file...")
36
+ os.remove(zip_path)
37
+
38
+ print("Process completed successfully!")
39
+
40
+ # List of required files
41
+ required_files = [
42
+ "coref_google_bert_uncased_L-12_H-768_A-12-v1.0.model",
43
+ "speaker_google_bert_uncased_L-12_H-768_A-12-v1.0.1.model",
44
+ "entities_google_bert_uncased_L-6_H-768_A-12-v1.0.model"
45
+ ]
46
+
47
+ # Get the user's home directory
48
+ home_dir = str(Path.home())
49
+ models_folder = os.path.join(home_dir, "booknlp_models")
50
+
51
+ # Check if the folder exists, if not create it
52
+ if not os.path.exists(models_folder):
53
+ os.makedirs(models_folder)
54
+
55
+ # Check for missing files
56
+ missing_files = [file for file in required_files if not os.path.exists(os.path.join(models_folder, file))]
57
+
58
+ if missing_files:
59
+ print(f"The following files are missing: {', '.join(missing_files)}")
60
+ print("Downloading and extracting required files...")
61
+ download_and_extract()
62
+ else:
63
+ print("All required booknlp files are present. No action needed.")