cffl/Exploring_Intelligent_Writing_Assistance
9
1# ###########################################################################2#3# CLOUDERA APPLIED MACHINE LEARNING PROTOTYPE (AMP)4# (C) Cloudera, Inc. 20225# All rights reserved.6#7# Applicable Open Source License: Apache 2.08#9# NOTE: Cloudera open source products are modular software products10# made up of hundreds of individual components, each of which was11# individually copyrighted. Each Cloudera open source product is a12# collective work under U.S. Copyright Law. Your license to use the13# collective work is as provided in your written agreement with14# Cloudera. Used apart from the collective work, this file is15# licensed for your use pursuant to the open source license16# identified above.17#18# This code is provided to you pursuant a written agreement with19# (i) Cloudera, Inc. or (ii) a third-party authorized to distribute20# this code. If you do not have a written agreement with Cloudera nor21# with an authorized and properly licensed third party, you do not22# have any rights to access nor to use this code.23#24# Absent a written agreement with Cloudera, Inc. (“Cloudera”) to the25# contrary, A) CLOUDERA PROVIDES THIS CODE TO YOU WITHOUT WARRANTIES OF ANY26# KIND; (B) CLOUDERA DISCLAIMS ANY AND ALL EXPRESS AND IMPLIED27# WARRANTIES WITH RESPECT TO THIS CODE, INCLUDING BUT NOT LIMITED TO28# IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND29# FITNESS FOR A PARTICULAR PURPOSE; (C) CLOUDERA IS NOT LIABLE TO YOU,30# AND WILL NOT DEFEND, INDEMNIFY, NOR HOLD YOU HARMLESS FOR ANY CLAIMS31# ARISING FROM OR RELATED TO THE CODE; AND (D)WITH RESPECT TO YOUR EXERCISE32# OF ANY RIGHTS GRANTED TO YOU FOR THE CODE, CLOUDERA IS NOT LIABLE FOR ANY33# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR34# CONSEQUENTIAL DAMAGES INCLUDING, BUT NOT LIMITED TO, DAMAGES35# RELATED TO LOST REVENUE, LOST PROFITS, LOSS OF INCOME, LOSS OF36# BUSINESS ADVANTAGE OR UNAVAILABILITY, OR LOSS OR CORRUPTION OF37# DATA.38#39# ###########################################################################40 41import pytest42import transformers43 44from src.style_transfer import StyleTransfer45from src.style_classification import StyleIntensityClassifier46from src.content_preservation import ContentPreservationScorer47from src.transformer_interpretability import InterpretTransformer48 49 50@pytest.fixture51def subjectivity_example_data():52 examples = [53 """there is an iconic roadhouse, named "spud's roadhouse", which sells fuel and general shop items , has great meals and has accommodation.""",54 "chemical abstracts service (cas), a prominent division of the american chemical society, is the world's leading source of chemical information.",55 "the most serious scandal was the iran-contra affair.",56 "another strikingly elegant four-door saloon for the s3 continental came from james young.",57 "other ambassadors also sent their messages of condolence following her passing.",58 ]59 60 ground_truth = [61 'there is a roadhouse, named "spud\'s roadhouse", which sells fuel and general shop items and has accommodation.',62 "chemical abstracts service (cas), a division of the american chemical society, is a source of chemical information.",63 "one controversy was the iran-contra affair.",64 "another four-door saloon for the s3 continental came from james young.",65 "other ambassadors also sent their messages of condolence following her death.",66 ]67 68 return {"examples": examples, "ground_truth": ground_truth}69 70 71@pytest.fixture72def subjectivity_styletransfer():73 MODEL_PATH = "cffl/bart-base-styletransfer-subjective-to-neutral"74 return StyleTransfer(model_identifier=MODEL_PATH, max_gen_length=200)75 76 77@pytest.fixture78def subjectivity_styleintensityclassifier():79 CLS_MODEL_PATH = "cffl/bert-base-styleclassification-subjective-neutral"80 return StyleIntensityClassifier(model_identifier=CLS_MODEL_PATH)81 82 83@pytest.fixture84def subjectivity_contentpreservationscorer():85 CLS_MODEL_PATH = "cffl/bert-base-styleclassification-subjective-neutral"86 SBERT_MODEL_PATH = "sentence-transformers/all-MiniLM-L6-v2"87 return ContentPreservationScorer(88 cls_model_identifier=CLS_MODEL_PATH, sbert_model_identifier=SBERT_MODEL_PATH89 )90 91 92@pytest.fixture93def subjectivity_interprettransformer():94 CLS_MODEL_PATH = "cffl/bert-base-styleclassification-subjective-neutral"95 return InterpretTransformer(cls_model_identifier=CLS_MODEL_PATH)96 97 98# test class initialization99def test_StyleTransfer_init(subjectivity_styletransfer):100 assert isinstance(101 subjectivity_styletransfer.pipeline,102 transformers.pipelines.text2text_generation.Text2TextGenerationPipeline,103 )104 105 106def test_StyleIntensityClassifier_init(subjectivity_styleintensityclassifier):107 assert isinstance(108 subjectivity_styleintensityclassifier.pipeline,109 transformers.pipelines.text_classification.TextClassificationPipeline,110 )111 112 113def test_ContentPreservationScorer_init(subjectivity_contentpreservationscorer):114 assert isinstance(115 subjectivity_contentpreservationscorer.cls_model,116 transformers.models.bert.modeling_bert.BertForSequenceClassification,117 )118 assert isinstance(119 subjectivity_contentpreservationscorer.sbert_model,120 transformers.models.bert.modeling_bert.BertModel,121 )122 123 124def test_InterpretTransformer_init(subjectivity_interprettransformer):125 assert isinstance(126 subjectivity_interprettransformer.cls_model,127 transformers.models.bert.modeling_bert.BertForSequenceClassification,128 )129 130 131# test class functionality132def test_StyleTransfer_transfer(subjectivity_styletransfer, subjectivity_example_data):133 assert subjectivity_example_data[134 "ground_truth"135 ] == subjectivity_styletransfer.transfer(subjectivity_example_data["examples"])136 137 138def test_StyleIntensityClassifier_calculate_transfer_intensity_fraction(139 subjectivity_styleintensityclassifier, subjectivity_example_data140):141 sti_frac = (142 subjectivity_styleintensityclassifier.calculate_transfer_intensity_fraction(143 input_text=subjectivity_example_data["examples"],144 output_text=subjectivity_example_data["ground_truth"],145 )146 )147 assert sti_frac == [148 0.9891820847234861,149 0.9808499743983614,150 0.8070009460737938,151 0.9913705583756346,152 0.9611679711017459,153 ]154 155 156def test_ContentPreservationScorer_calculate_content_preservation_score(157 subjectivity_contentpreservationscorer, subjectivity_example_data158):159 cps = subjectivity_contentpreservationscorer.calculate_content_preservation_score(160 input_text=subjectivity_example_data["examples"],161 output_text=subjectivity_example_data["ground_truth"],162 mask_type="none",163 )164 assert cps == [0.9369, 0.9856, 0.7328, 0.9718, 0.9709]165 