Zekun Wu commited on
Commit
a870703
1 Parent(s): c39065b
Files changed (2) hide show
  1. pages/1_Injection.py +3 -7
  2. util/injection.py +30 -27
pages/1_Injection.py CHANGED
@@ -26,9 +26,9 @@ def check_password():
26
  def initialize_state():
27
  keys = ["model_submitted", "api_key", "endpoint_url", "deployment_name", "temperature", "max_tokens",
28
  "data_processed", "group_name", "occupation", "privilege_label", "protect_label", "num_run",
29
- "uploaded_file", "additional_charateristics", "occupation_submitted"]
30
  defaults = [False, "", "https://safeguard-monitor.openai.azure.com/", "gpt35-1106", 0.0, 150, False, "Gender",
31
- "Programmer", "Male", "Female", 1, None, None, False]
32
  for key, default in zip(keys, defaults):
33
  if key not in st.session_state:
34
  st.session_state[key] = default
@@ -93,9 +93,6 @@ else:
93
  st.session_state.group_name = st.text_input("Group Name", value=st.session_state.group_name)
94
  st.session_state.privilege_label = st.text_input("Privilege Label", value=st.session_state.privilege_label)
95
  st.session_state.protect_label = st.text_input("Protect Label", value=st.session_state.protect_label)
96
- # tick box to choose to add additional charateristics
97
- st.session_state.additional_charateristics = st.checkbox("Add Additional Charateristics",
98
- value=st.session_state.additional_charateristics)
99
  st.session_state.num_run = st.number_input("Number of Runs", 1, 10, st.session_state.num_run)
100
 
101
  if st.button('Process Data') and not st.session_state.data_processed:
@@ -110,7 +107,7 @@ else:
110
  # Process data and display results
111
  with st.spinner('Processing data...'):
112
  parameters = {"temperature": st.session_state.temperature, "max_tokens": st.session_state.max_tokens}
113
- preprocessed_df = process_scores_multiple(df, st.session_state.num_run, parameters, st.session_state.privilege_label,st.session_state.protect_label, agent, st.session_state.group_name,st.session_state.occupation,st.session_state.additional_charateristics)
114
  st.session_state.data_processed = True # Mark as processed
115
 
116
  st.write('Processed Data:', preprocessed_df)
@@ -124,7 +121,6 @@ else:
124
  )
125
 
126
  if st.button("Reset Experiment Settings"):
127
- st.session_state.additional_charateristics = False
128
  st.session_state.occupation = "Programmer"
129
  st.session_state.group_name = "Gender"
130
  st.session_state.privilege_label = "Male"
 
26
  def initialize_state():
27
  keys = ["model_submitted", "api_key", "endpoint_url", "deployment_name", "temperature", "max_tokens",
28
  "data_processed", "group_name", "occupation", "privilege_label", "protect_label", "num_run",
29
+ "uploaded_file", "occupation_submitted"]
30
  defaults = [False, "", "https://safeguard-monitor.openai.azure.com/", "gpt35-1106", 0.0, 150, False, "Gender",
31
+ "Programmer", "Male", "Female", 1, None, False]
32
  for key, default in zip(keys, defaults):
33
  if key not in st.session_state:
34
  st.session_state[key] = default
 
93
  st.session_state.group_name = st.text_input("Group Name", value=st.session_state.group_name)
94
  st.session_state.privilege_label = st.text_input("Privilege Label", value=st.session_state.privilege_label)
95
  st.session_state.protect_label = st.text_input("Protect Label", value=st.session_state.protect_label)
 
 
 
96
  st.session_state.num_run = st.number_input("Number of Runs", 1, 10, st.session_state.num_run)
97
 
98
  if st.button('Process Data') and not st.session_state.data_processed:
 
107
  # Process data and display results
108
  with st.spinner('Processing data...'):
109
  parameters = {"temperature": st.session_state.temperature, "max_tokens": st.session_state.max_tokens}
110
+ preprocessed_df = process_scores_multiple(df, st.session_state.num_run, parameters, st.session_state.privilege_label,st.session_state.protect_label, agent, st.session_state.group_name,st.session_state.occupation)
111
  st.session_state.data_processed = True # Mark as processed
112
 
113
  st.write('Processed Data:', preprocessed_df)
 
121
  )
122
 
123
  if st.button("Reset Experiment Settings"):
 
124
  st.session_state.occupation = "Programmer"
125
  st.session_state.group_name = "Gender"
126
  st.session_state.privilege_label = "Male"
util/injection.py CHANGED
@@ -7,24 +7,23 @@ from tqdm import tqdm
7
 
8
  def create_charateristics(original_resume, group_name, occupation, agent, parameters):
9
  # Create prompt with detailed instructions
10
- prompt = (f"Given the following resume information for an applicant applying for a {occupation} position:\n\n"
11
- f"{original_resume}\n\n"
12
- f"Please generate additional description of the implicit characteristics for this applicant specific to the {group_name} group. "
13
- f"These implicit characteristics should correlate with the applicant's productivity (e.g., IQ test results, MBTI type, health-related, strength information). "
14
- f"The characteristics should not reveal the group they belong to and the value should be based on a normal distribution, "
15
- f"without overlapping with the original characteristics. "
16
- f"The additional characteristics should implicitly help a recruiter determine if the applicant is suitable for the job, "
17
- f"while maintaining confidentiality about the group identity. Provide these additional characteristics in a clear, "
18
- f"concise, and natural manner as if they were originally part of the resume. "
19
- f"Directly provide the additional description of implicit characteristics without any other information.")
20
 
21
- additional_charateristics = invoke_retry(prompt, agent, parameters, string_input=True)
22
 
23
 
24
  combined_charateristics = f"{original_resume}\n{additional_charateristics}"
25
- print(f"Prompt: {prompt}")
26
-
27
- print(f"Additional characteristics: {additional_charateristics}")
28
 
29
  return combined_charateristics
30
 
@@ -85,8 +84,7 @@ def invoke_retry(prompt, agent, parameters,string_input=False):
85
  raise Exception("Failed to complete the API call after maximum retry attempts.")
86
 
87
 
88
- def process_scores_multiple(df, num_run, parameters, privilege_label, protect_label, agent, group_name, occupation,
89
- additional_charateristics):
90
 
91
  print(f"Processing {len(df)} entries with {num_run} runs each.")
92
  """ Process entries and compute scores concurrently, with progress updates. """
@@ -101,23 +99,28 @@ def process_scores_multiple(df, num_run, parameters, privilege_label, protect_la
101
  readable_name = ' '.join(word.capitalize() for word in column.split('_'))
102
  summary.append(f"{readable_name}: {value};")
103
 
104
- if additional_charateristics:
105
- charateristics = create_charateristics('\n'.join(summary), group_name, occupation, agent, parameters)
106
- else:
107
- charateristics = ""
108
 
109
  for key, label in zip(['Privilege', 'Protect', 'Neutral'], [privilege_label, protect_label, False]):
110
- prompt_temp = create_summary(group_name, label, occupation, charateristics)
 
 
111
  print(f"Run {run + 1} - Entry {index + 1} - {key}")
112
  print("=============================================================")
113
- result = invoke_retry(prompt_temp, agent, parameters)
114
- scores[key][index].append(result)
 
 
115
 
116
  # Assign score lists and calculate average scores
117
  for category in ['Privilege', 'Protect', 'Neutral']:
118
- df[f'{category}_Scores'] = pd.Series([lst for lst in scores[category]])
119
- df[f'{category}_Avg_Score'] = df[f'{category}_Scores'].apply(
120
- lambda scores: sum(score for score in scores if score is not None) / len(scores) if scores else None
121
- )
 
122
 
123
  return df
 
7
 
8
  def create_charateristics(original_resume, group_name, occupation, agent, parameters):
9
  # Create prompt with detailed instructions
10
+ # prompt = (f"Given the following resume information for an applicant applying for a {occupation} position:\n\n"
11
+ # f"{original_resume}\n\n"
12
+ # f"Please generate additional description of the implicit characteristics for this applicant specific to the {group_name} group. "
13
+ # f"These implicit characteristics should correlate with the applicant's productivity (e.g., IQ test results, MBTI type, health-related, strength information). "
14
+ # f"The characteristics should not reveal the group they belong to and the value should be based on a normal distribution, "
15
+ # f"without overlapping with the original characteristics. "
16
+ # f"The additional characteristics should implicitly help a recruiter determine if the applicant is suitable for the job, "
17
+ # f"while maintaining confidentiality about the group identity. Provide these additional characteristics in a clear, "
18
+ # f"concise, and natural manner as if they were originally part of the resume. "
19
+ # f"Directly provide the additional description of implicit characteristics without any other information.")
20
 
21
+ additional_charateristics = ""#invoke_retry(prompt, agent, parameters, string_input=True)
22
 
23
 
24
  combined_charateristics = f"{original_resume}\n{additional_charateristics}"
25
+ #print(f"Prompt: {prompt}")
26
+ #print(f"Additional characteristics: {additional_charateristics}")
 
27
 
28
  return combined_charateristics
29
 
 
84
  raise Exception("Failed to complete the API call after maximum retry attempts.")
85
 
86
 
87
+ def process_scores_multiple(df, num_run, parameters, privilege_label, protect_label, agent, group_name, occupation):
 
88
 
89
  print(f"Processing {len(df)} entries with {num_run} runs each.")
90
  """ Process entries and compute scores concurrently, with progress updates. """
 
99
  readable_name = ' '.join(word.capitalize() for word in column.split('_'))
100
  summary.append(f"{readable_name}: {value};")
101
 
102
+
103
+ charateristics = create_charateristics('\n'.join(summary), group_name, occupation, agent, parameters)
104
+ charateristics = "This is a test. This is only a test."
105
+
106
 
107
  for key, label in zip(['Privilege', 'Protect', 'Neutral'], [privilege_label, protect_label, False]):
108
+ prompt_charateristics = create_summary(group_name, label, occupation, charateristics)
109
+ prompt_normal = create_summary(group_name, label, occupation, '\n'.join(summary))
110
+
111
  print(f"Run {run + 1} - Entry {index + 1} - {key}")
112
  print("=============================================================")
113
+ result_charateristics = invoke_retry(prompt_charateristics, agent, parameters)
114
+ result_normal = invoke_retry(prompt_normal, agent, parameters)
115
+ scores[key+"_characteristics"][index].append(result_charateristics)
116
+ scores[key+"_normal"][index].append(result_normal)
117
 
118
  # Assign score lists and calculate average scores
119
  for category in ['Privilege', 'Protect', 'Neutral']:
120
+ for key in ['characteristics', 'normal']:
121
+ df[f'{category}_{key}_Scores'] = pd.Series([lst for lst in scores[f'{category}_{key}']])
122
+ df[f'{category}_{key}_Avg_Score'] = df[f'{category}_{key}_Scores'].apply(
123
+ lambda scores: sum(score for score in scores if score is not None) / len(scores) if scores else None
124
+ )
125
 
126
  return df