-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_models.py
executable file
·102 lines (87 loc) · 3.38 KB
/
generate_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from re import X
from string import punctuation
import requests
import urllib.parse
from pathlib import Path
import numpy as np
import json
import zipfile
import pickle
from punctuator import Punctuator
import gdown
CACHE_PATH = Path.home() / "cache"
CACHE_PATH.mkdir(exist_ok=True)
MODEL_PATH = Path("models")
MODEL_PATH.mkdir(exist_ok=True)
PUNCTUATOR_MODELS = [
{
"url": "http://ltdata1.informatik.uni-hamburg.de/subtitle2go/Model_subs_norm1_filt_5M_tageschau_euparl_h256_lr0.02.pcl",
"tests": [{"input": "hallo ich bin ein testsatz", "expected": "Hallo, ich bin ein testsatz."}]
},
# Rehosted from https://drive.google.com/drive/folders/0B7BsN5f2F1fZQnFsbzJ3TWxxMms?resourcekey=0-6yhuY9FOeITBBWWNdyG2aw
{
"url": "gdrive://1CZ_Os38LjBwyd-jgDMsfpqiWPB6wwVKA",
"name": "Demo-EUROPARL-EN.zip",
"pickle_encoding": "latin-1",
"tests": [
{
"input": "hello this is an example sentence",
"expected": "Hello, this is an example sentence.",
}
],
}
]
def download_model(model):
if model["url"].startswith("gdrive://"):
return download_gdrive_model(model)
else:
return download_http_model(model)
def download_gdrive_model(model):
url_path = urllib.parse.urlparse(model["url"]).netloc
output_model_file_path = MODEL_PATH / model["name"]
input_model_file_path = CACHE_PATH / url_path
if not input_model_file_path.exists():
print("Downloading", url_path)
gdown.download(id=url_path, output=str(input_model_file_path), fuzzy=True)
return input_model_file_path, output_model_file_path
def download_http_model(model):
url_path = urllib.parse.urlparse(model["url"]).path
name = Path(url_path).name
input_model_file_path = CACHE_PATH / name
output_name = Path(url_path).with_suffix(".zip").name
output_model_file_path = MODEL_PATH / output_name
if not input_model_file_path.exists():
print("Downloading", model["url"])
req = requests.get(model["url"])
with open(input_model_file_path, "wb") as f:
f.write(req.content)
return input_model_file_path, output_model_file_path
for model in PUNCTUATOR_MODELS:
input_model_file_path, output_model_file_path = download_model(model)
with open(input_model_file_path, "rb") as f:
if 'pickle_encoding' in model:
u = pickle._Unpickler(f)
u.encoding = model['pickle_encoding']
state = u.load()
else:
state = pickle.load(f)
with zipfile.ZipFile(output_model_file_path, "w") as model_zip:
for k, v in state.items():
if (
(isinstance(v, (list, tuple)))
and v
and np.ndarray in [type(x) for x in v]
):
with model_zip.open(f"{k}.npyl", "w") as f:
for x in v:
np.save(f, x, allow_pickle=False)
else:
with model_zip.open(f"{k}.json", "w") as f:
f.write(json.dumps(v).encode())
if "tests" in model and model["tests"]:
punctuation_model = Punctuator(output_model_file_path)
for test in model["tests"]:
actual = punctuation_model.punctuate(test["input"])
assert (
actual == test["expected"]
), f"'{test['expected']}' expected, got {actual}"