Upload entailment.py
Browse files- entailment.py +31 -0
entailment.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
def analyze_entailment(original_sentence, paraphrased_sentences, threshold):
|
5 |
+
# Load the entailment model using pipeline
|
6 |
+
entailment_pipe = pipeline("text-classification", model="ynie/roberta-large-snli_mnli_fever_anli_R1_R2_R3-nli")
|
7 |
+
|
8 |
+
# Function to perform entailment
|
9 |
+
def check_entailment(premise, hypothesis):
|
10 |
+
results = entailment_pipe(f"{premise} [SEP] {hypothesis}", return_all_scores=True)
|
11 |
+
return results[0]
|
12 |
+
|
13 |
+
all_sentences = {}
|
14 |
+
selected_sentences = {}
|
15 |
+
discarded_sentences = {}
|
16 |
+
|
17 |
+
# Check entailment for each paraphrased sentence
|
18 |
+
for paraphrased_sentence in paraphrased_sentences:
|
19 |
+
entailment_results = check_entailment(original_sentence, paraphrased_sentence)
|
20 |
+
entailment_score = next(result['score'] for result in entailment_results if result['label'] == 'entailment')
|
21 |
+
|
22 |
+
all_sentences[paraphrased_sentence] = entailment_score
|
23 |
+
|
24 |
+
if entailment_score >= threshold:
|
25 |
+
selected_sentences[paraphrased_sentence] = entailment_score
|
26 |
+
else:
|
27 |
+
discarded_sentences[paraphrased_sentence] = entailment_score
|
28 |
+
|
29 |
+
return all_sentences, selected_sentences, discarded_sentences
|
30 |
+
|
31 |
+
print(analyze_entailment("I love you", ["You're being loved by me"], 0.7))
|