Spaces:
Running
Running
Upload prepare_everything.py
Browse files- prepare_everything.py +71 -0
prepare_everything.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
import sys
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
import requests
|
7 |
+
|
8 |
+
from parltopic.utils.helper import get_main_config
|
9 |
+
|
10 |
+
config = get_main_config()
|
11 |
+
|
12 |
+
|
13 |
+
def download_model(url, save_path):
|
14 |
+
# Send a GET request to the URL
|
15 |
+
response = requests.get(url, stream=True)
|
16 |
+
|
17 |
+
# Check if the request was successful (status code 200)
|
18 |
+
if response.status_code == 200:
|
19 |
+
# Open a file in binary write mode to save the downloaded content
|
20 |
+
with open(save_path, "wb") as f:
|
21 |
+
# Iterate over the response content in chunks and write to the file
|
22 |
+
for chunk in response.iter_content(chunk_size=1024):
|
23 |
+
f.write(chunk)
|
24 |
+
print("Model downloaded successfully!")
|
25 |
+
else:
|
26 |
+
# Print an error message if the request was not successful
|
27 |
+
print(f"Failed to download model. Status code: {response.status_code}")
|
28 |
+
|
29 |
+
|
30 |
+
def set_tokenizers_parallelism(value):
|
31 |
+
"""Set the TOKENIZERS_PARALLELISM environment variable."""
|
32 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "true" if value else "false"
|
33 |
+
print(f"TOKENIZERS_PARALLELISM set to {os.environ['TOKENIZERS_PARALLELISM']}")
|
34 |
+
|
35 |
+
|
36 |
+
def install_requirements():
|
37 |
+
"""Install packages listed in requirements.txt"""
|
38 |
+
try:
|
39 |
+
subprocess.check_call(
|
40 |
+
[sys.executable, "-m", "pip", "install", "-r", "requirements.txt"]
|
41 |
+
)
|
42 |
+
print("All packages from requirements.txt installed successfully.")
|
43 |
+
except subprocess.CalledProcessError as e:
|
44 |
+
print(f"Failed to install packages from requirements.txt: {e}")
|
45 |
+
sys.exit(1)
|
46 |
+
|
47 |
+
|
48 |
+
def install_spacy_model(model_name):
|
49 |
+
"""Install a specific spaCy model"""
|
50 |
+
try:
|
51 |
+
subprocess.check_call([sys.executable, "-m", "spacy", "download", model_name])
|
52 |
+
print(f"spaCy model '{model_name}' installed successfully.")
|
53 |
+
except subprocess.CalledProcessError as e:
|
54 |
+
print(f"Failed to install spaCy model '{model_name}': {e}")
|
55 |
+
sys.exit(1)
|
56 |
+
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
|
60 |
+
for name, path in config["paths"].items():
|
61 |
+
Path(path).mkdir(exist_ok=True)
|
62 |
+
|
63 |
+
download_model(
|
64 |
+
"https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin",
|
65 |
+
Path(config["paths"]["resources"]) / "lid.176.bin",
|
66 |
+
)
|
67 |
+
|
68 |
+
install_spacy_model("de_core_news_lg")
|
69 |
+
install_spacy_model("fr_core_news_lg")
|
70 |
+
install_spacy_model("it_core_news_lg")
|
71 |
+
set_tokenizers_parallelism(True)
|