Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prism metric #79

Merged
merged 17 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions jury/metrics/prism/prism_for_language_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

from jury.metrics import LanguageGenerationInstance
from jury.metrics._core import MetricForLanguageGeneration
from jury.metrics._core.utils import download
from jury.utils.io import untar_file

_CITATION = """
@inproceedings{thompson-post-2020-automatic,
Expand Down Expand Up @@ -121,7 +119,7 @@ def model_identifier(self):
if self.scorer is not None:
return self.scorer.identifier()

def _download_model(self):
def _download_model(self, dl_manager):
if self.model_path_or_url is None:
self.model_path_or_url = "http://data.statmt.org/prism/m39v1.tar"

Expand All @@ -131,18 +129,11 @@ def _download_model(self):
self.model_dir = self.model_path_or_url
else:
if not self.model_path_or_url.endswith(".tar"):
raise ValueError("Provided model URL must be a tarfile.")
raise ValueError("Provided model URL must be a tar file.")
model_source = self.model_path_or_url
file_name = os.path.basename(self.model_path_or_url)
model_dir = os.path.join(self.data_dir, file_name.replace(".tar", ""))
if not os.path.isdir(model_dir):
model_dest = os.path.join(self.data_dir, f"prism_model_{file_name}")
print(f"Downloading the model at {self.model_path_or_url} ...")
download(source=model_source, destination=model_dest)
print("Model downloaded.")
untar_file(model_dest, self.data_dir)
os.remove(model_dest)
self.model_dir = model_dir
folder_name = os.path.basename(model_source).replace(".tar", "")
extraction_dir = dl_manager.download_and_extract(model_source)
self.model_dir = os.path.join(extraction_dir, folder_name)

def _download_and_prepare(self, dl_manager) -> None:
"""
Expand All @@ -151,14 +142,12 @@ def _download_and_prepare(self, dl_manager) -> None:
commit on the master branch, in order to keep things stable. See
https://github.com/thompsonb/prism/blob/42e45a46d1c7924e98bceeed2ea81b31efcb6f9d/prism.py
"""
self._download_model()
self._download_model(dl_manager)
prism_source = (
"https://raw.githubusercontent.com/thompsonb/prism/42e45a46d1c7924e98bceeed2ea81b31efcb6f9d/prism.py"
)
prism_dest = os.path.join(self.data_dir, "prism.py")
download(
source=prism_source,
destination=prism_dest,
prism_dest = dl_manager.download(
prism_source,
)
self.external_module_path = prism_dest

Expand Down Expand Up @@ -291,3 +280,9 @@ def _compute_multi_pred_multi_ref(
"segment_scores": segment_scores,
"normalized": normalize,
}


if __name__ == "__main__":
devrimcavusoglu marked this conversation as resolved.
Show resolved Hide resolved
prism = PrismForLanguageGeneration()
res = prism._compute_single_pred_single_ref(predictions=["abc"], references=["def"])
print(res)
5 changes: 0 additions & 5 deletions jury/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,3 @@ def pickle_save(obj: Dict, fp: str, overwrite: bool = True) -> None:

with open(fp, "wb") as pkl:
pickle.dump(obj, pkl)


def untar_file(fp: str, extract_path: str) -> None:
with tarfile.open(fp) as tar_buffer:
tar_buffer.extractall(extract_path)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
datasets>=1.8.0
datasets>=1.18.3
fire>=0.4.0
nltk==3.6.5
numpy>=1.21.0
Expand Down