Zekun Wu commited on
Commit
8cb47d9
1 Parent(s): 595f661
Files changed (2) hide show
  1. pages/4_Benchmarking_Injection.py +26 -0
  2. util/data.py +62 -0
pages/4_Benchmarking_Injection.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from util.data import get_data
4
+
5
+ def check_password():
6
+ def password_entered():
7
+ if password_input == os.getenv('PASSWORD'):
8
+ st.session_state['password_correct'] = True
9
+ else:
10
+ st.error("Incorrect Password, please try again.")
11
+
12
+ password_input = st.text_input("Enter Password:", type="password")
13
+ submit_button = st.button("Submit", on_click=password_entered)
14
+
15
+ if submit_button and not st.session_state.get('password_correct', False):
16
+ st.error("Please enter a valid password to access the demo.")
17
+
18
+
19
+ if not st.session_state.get('password_correct', False):
20
+ check_password()
21
+ else:
22
+ st.sidebar.success("Password Verified. Proceed with the demo.")
23
+
24
+ data = get_data(5)
25
+ st.dataframe(data)
26
+
util/data.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ import pandas as pd
3
+
4
+
5
+ def get_data(sample_size):
6
+ dataset = load_dataset("esnli")
7
+ df = dataset['train'].to_pandas()
8
+
9
+ esnli_train_df = df.dropna(subset=['hypothesis', 'explanation_1'])
10
+
11
+ prompt_template = """You are an advanced AI trained to understand and explain natural language relationships. I will give you a pair of sentences: a premise and a hypothesis. Your task is to determine the relationship between them and provide a detailed explanation of your reasoning process. The possible relationships are "Entailment," "Contradiction," or "Neutral."
12
+
13
+ Instructions:
14
+
15
+ Read the given premise and hypothesis carefully.
16
+
17
+ Identify the relationship between them based on the following definitions:
18
+
19
+ Entailment: The hypothesis logically follows from the premise.
20
+ Contradiction: The hypothesis directly contradicts the premise.
21
+ Neutral: The hypothesis neither logically follows from nor contradicts the premise.
22
+
23
+ Provide the relationship (Entailment, Contradiction, or Neutral).
24
+
25
+ Explain in about ten words your reasoning to justify your conclusion.
26
+
27
+ Example:
28
+
29
+ Premise: "A man is playing a guitar."
30
+ Hypothesis: "A man is making music."
31
+ Relationship: Entailment
32
+ Explanation: Playing guitar inherently involves creating music, fulfilling the hypothesis.
33
+
34
+ Now, try it with the following pair:
35
+
36
+ Premise: "{premise}"
37
+ Hypothesis: "{hypothesis}"
38
+ Relationship:
39
+ """
40
+
41
+ # Generate prompts for the dataset
42
+ def generate_prompts(df):
43
+ prompts = []
44
+ for _, row in df.iterrows():
45
+ prompt = prompt_template.format(premise=row['premise'], hypothesis=row['hypothesis'])
46
+ prompts.append({
47
+ 'question': prompt,
48
+ 'answer': {0: 'Entailment', 1: 'Neutral', 2: 'Contradiction'}[row['label']],
49
+ 'reference_explanation': row['explanation_1']
50
+ })
51
+ return prompts
52
+
53
+ sample_df = esnli_train_df.sample(n=sample_size, random_state=42)
54
+ prompts_data = generate_prompts(sample_df)
55
+
56
+ prompts_df = pd.DataFrame(prompts_data)
57
+
58
+ return prompts_df
59
+
60
+ if __name__ == '__main__':
61
+ sample_size = 5
62
+ print(get_data(sample_size))