-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add nlp score metrics --------- Co-authored-by: Felipe Adachi <[email protected]>
- Loading branch information
1 parent
8ea1175
commit 263fab7
Showing
6 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from whylogs.experimental.core.metrics.udf_metric import register_metric_udf | ||
import evaluate | ||
from . import LangKitConfig | ||
from logging import getLogger | ||
|
||
lang_config = LangKitConfig() | ||
_corpus = lang_config.reference_corpus | ||
_scores = lang_config.nlp_scores | ||
_rouge_type = "rouge1" | ||
|
||
diagnostic_logger = getLogger(__name__) | ||
|
||
|
||
def register_score_udfs(): | ||
if _corpus: | ||
for score in _scores: | ||
if "bleu" in score: | ||
bleu = evaluate.load("bleu") | ||
|
||
@register_metric_udf(col_name=lang_config.response_column) | ||
def bleu_score(text: str) -> float: | ||
return bleu.compute(predictions=[text], references=[_corpus])[ | ||
"bleu" | ||
] | ||
|
||
if "rouge" in score: | ||
rouge = evaluate.load("rouge") | ||
|
||
@register_metric_udf(col_name=lang_config.response_column) | ||
def rouge_score(text: str) -> float: | ||
return rouge.compute( | ||
predictions=[text], | ||
references=[_corpus], | ||
rouge_types=[_rouge_type], | ||
)[_rouge_type] | ||
|
||
if "meteor" in score: | ||
meteor = evaluate.load("meteor") | ||
|
||
@register_metric_udf(col_name=lang_config.response_column) | ||
def meteor_score(text: str) -> float: | ||
return meteor.compute(predictions=[text], references=[_corpus])[ | ||
"meteor" | ||
] | ||
|
||
else: | ||
diagnostic_logger.warning( | ||
"No reference corpus provided for NLP scores. Skipping NLP scores." | ||
) | ||
|
||
|
||
def init(corpus=None, scores=[], rouge_type=None): | ||
global _corpus | ||
global _scores | ||
global _rouge_type | ||
if corpus: | ||
_corpus = corpus | ||
if scores: | ||
_scores = scores | ||
if rouge_type: | ||
_rouge_type = rouge_type | ||
|
||
register_score_udfs() | ||
|
||
|
||
init() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import whylogs as why | ||
import pytest | ||
|
||
|
||
@pytest.mark.load | ||
def test_bleu_score(): | ||
from langkit import nlp_scores # noqa | ||
from whylogs.experimental.core.udf_schema import udf_schema | ||
|
||
nlp_scores.init( | ||
scores=["bleu"], corpus="The quick brown fox jumps over the lazy dog" | ||
) | ||
text_schema = udf_schema() | ||
profile = why.log( | ||
{"response": "The quick dog jumps over the lazy brown fox"}, schema=text_schema | ||
).profile() | ||
max_score = ( | ||
profile.view() | ||
.get_column("response") | ||
.get_metrics()[-1] | ||
.to_summary_dict()["bleu_score:distribution/max"] | ||
) | ||
assert round(max_score, 2) == 0.42 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters