-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathgenerate_data_similarity.py
More file actions
78 lines (61 loc) · 2.76 KB
/
Copy pathgenerate_data_similarity.py
File metadata and controls
78 lines (61 loc) · 2.76 KB
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
# Copyright 2025 CVS Health and/or one of its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import json
import asyncio
from uqlm.black_box import BertScorer, BLEURTScorer, CosineScorer, MatchScorer
async def main():
# Load data
current_directory = os.getcwd()
datafile_path = os.path.join("/".join(current_directory.split("/")[:-1]), "scorers/bsdetector_results_file.json")
with open(datafile_path, "r") as f:
data = json.load(f)
responses = data["responses"]
sampled_responses = data["sampled_responses"]
store_results = dict()
store_results.update({"responses": responses, "sampled_responses": sampled_responses})
# 1. Bert Scorer
bert = BertScorer()
bert_result = bert.evaluate(responses=responses, sampled_responses=sampled_responses)
store_results.update(
{
"bert_result": bert_result
# 'F1': F1
}
)
# 2. Bleurt Scorer
bluert = BLEURTScorer()
bluert_result = bluert.evaluate(responses=responses, sampled_responses=sampled_responses)
bluert_scorer_result = []
for i in range(len(responses)):
bluert_scorer_result.append(bluert.bleurt_scorer.score(references=[responses[i]] * len(sampled_responses[i]), candidates=sampled_responses[i]))
store_results.update({"bluert_result": bluert_result, "bluert_score": bluert_scorer_result})
# 3. Cosine Similarity Scorer
cosine = CosineScorer()
cosine_result = cosine.evaluate(responses=responses, sampled_responses=sampled_responses)
embeddings1, embeddings2 = [], []
for i in range(len(responses)):
embeddings1.append(cosine.model.encode([responses[i]] * len(sampled_responses[i])).tolist())
embeddings2.append(cosine.model.encode(sampled_responses[i]).tolist())
store_results.update({"cosine_result": cosine_result, "embeddings1": embeddings1, "embeddings2": embeddings2})
# 4. Exact Match scorer
match = MatchScorer()
match_result = match.evaluate(responses=responses, sampled_responses=sampled_responses)
store_results.update({"match_result": match_result})
# Store results
results_file = "similarity_results_file.json"
with open(results_file, "w") as f:
json.dump(store_results, f)
if __name__ == "__main__":
asyncio.run(main())