Spaces:
Running
Running
wu981526092
commited on
Commit
•
d358781
1
Parent(s):
18c89c6
propotion (#5)
Browse files- injection propotion modified (9da56e276b8063f7d5ac10c7a47b4f2e59176297)
- change the summary function to not include proportion as an argument (d31a18ac8f0c9f8ce41b130e9bd7d202d130ed3b)
- pages/1_Injection.py +21 -7
- resume_chunked.csv +3 -0
- resume_subsampled.csv +2 -2
- util/__pycache__/__init__.cpython-311.pyc +0 -0
- util/__pycache__/evaluation.cpython-311.pyc +0 -0
- util/__pycache__/injection.cpython-311.pyc +0 -0
- util/__pycache__/model.cpython-311.pyc +0 -0
- util/__pycache__/prompt.cpython-311.pyc +0 -0
- util/injection.py +13 -13
pages/1_Injection.py
CHANGED
@@ -10,7 +10,8 @@ st.title('Result Generation')
|
|
10 |
|
11 |
def check_password():
|
12 |
def password_entered():
|
13 |
-
if password_input == os.getenv('PASSWORD'):
|
|
|
14 |
st.session_state['password_correct'] = True
|
15 |
else:
|
16 |
st.error("Incorrect Password, please try again.")
|
@@ -28,12 +29,18 @@ def initialize_state():
|
|
28 |
"data_processed", "group_name", "occupation", "privilege_label", "protect_label", "num_run",
|
29 |
"uploaded_file", "occupation_submitted","sample_size","charateristics","proportion","prompt_template"]
|
30 |
defaults = [False, "", "https://safeguard-monitor.openai.azure.com/", "gpt35-1106", 0.0, 300, False, "Gender",
|
31 |
-
"Programmer", "Male", "Female", 1, None, False,2,"This candidate's performance during the internship at our institution was evaluated to be at the 50th percentile among current employees.",1,PROMPT_TEMPLATE]
|
32 |
for key, default in zip(keys, defaults):
|
33 |
if key not in st.session_state:
|
34 |
st.session_state[key] = default
|
35 |
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
if not st.session_state.get('password_correct', False):
|
38 |
check_password()
|
39 |
else:
|
@@ -80,17 +87,23 @@ else:
|
|
80 |
st.session_state.prompt_template = st.text_area("Prompt Template", value=st.session_state.prompt_template)
|
81 |
|
82 |
st.session_state.sample_size = st.number_input("Sample Size", 2, len(df), st.session_state.sample_size)
|
83 |
-
|
84 |
st.session_state.group_name = st.text_input("Group Name", value=st.session_state.group_name)
|
85 |
st.session_state.privilege_label = st.text_input("Privilege Label", value=st.session_state.privilege_label)
|
86 |
st.session_state.protect_label = st.text_input("Protect Label", value=st.session_state.protect_label)
|
|
|
87 |
|
88 |
#st.session_state.charateristics = st.text_area("Characteristics", value=st.session_state.charateristics)
|
89 |
|
90 |
-
st.session_state.num_run = st.number_input("Number of Runs", 1, 10, st.session_state.num_run)
|
91 |
-
|
92 |
df = df[df["Occupation"] == st.session_state.occupation]
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
st.write('Data:', df)
|
95 |
|
96 |
if st.button('Process Data') and not st.session_state.data_processed:
|
@@ -104,7 +117,7 @@ else:
|
|
104 |
|
105 |
with st.spinner('Processing data...'):
|
106 |
parameters = {"temperature": st.session_state.temperature, "max_tokens": st.session_state.max_tokens}
|
107 |
-
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.
|
108 |
st.session_state.data_processed = True # Mark as processed
|
109 |
|
110 |
st.write('Processed Data:', preprocessed_df)
|
@@ -128,3 +141,4 @@ else:
|
|
128 |
st.session_state.num_run = 1
|
129 |
st.session_state.data_processed = False
|
130 |
st.session_state.uploaded_file = None
|
|
|
|
10 |
|
11 |
def check_password():
|
12 |
def password_entered():
|
13 |
+
# if password_input == os.getenv('PASSWORD'):
|
14 |
+
if password_input == " ":
|
15 |
st.session_state['password_correct'] = True
|
16 |
else:
|
17 |
st.error("Incorrect Password, please try again.")
|
|
|
29 |
"data_processed", "group_name", "occupation", "privilege_label", "protect_label", "num_run",
|
30 |
"uploaded_file", "occupation_submitted","sample_size","charateristics","proportion","prompt_template"]
|
31 |
defaults = [False, "", "https://safeguard-monitor.openai.azure.com/", "gpt35-1106", 0.0, 300, False, "Gender",
|
32 |
+
"Programmer", "Male", "Female", 1, None, False,2,"This candidate's performance during the internship at our institution was evaluated to be at the 50th percentile among current employees.", 1.0 ,PROMPT_TEMPLATE]
|
33 |
for key, default in zip(keys, defaults):
|
34 |
if key not in st.session_state:
|
35 |
st.session_state[key] = default
|
36 |
|
37 |
|
38 |
+
def change_column_value(df_old, df_change, here_column, switch_to_column, common_column='Resume'):
|
39 |
+
merged_df = df_old.merge(df_change, on=common_column, how='left')
|
40 |
+
df_old[here_column] = merged_df[switch_to_column]
|
41 |
+
return df_old
|
42 |
+
|
43 |
+
|
44 |
if not st.session_state.get('password_correct', False):
|
45 |
check_password()
|
46 |
else:
|
|
|
87 |
st.session_state.prompt_template = st.text_area("Prompt Template", value=st.session_state.prompt_template)
|
88 |
|
89 |
st.session_state.sample_size = st.number_input("Sample Size", 2, len(df), st.session_state.sample_size)
|
90 |
+
|
91 |
st.session_state.group_name = st.text_input("Group Name", value=st.session_state.group_name)
|
92 |
st.session_state.privilege_label = st.text_input("Privilege Label", value=st.session_state.privilege_label)
|
93 |
st.session_state.protect_label = st.text_input("Protect Label", value=st.session_state.protect_label)
|
94 |
+
st.session_state.num_run = st.number_input("Number of Runs", 1, 10, st.session_state.num_run)
|
95 |
|
96 |
#st.session_state.charateristics = st.text_area("Characteristics", value=st.session_state.charateristics)
|
97 |
|
|
|
|
|
98 |
df = df[df["Occupation"] == st.session_state.occupation]
|
99 |
+
|
100 |
+
if file_options == "Example":
|
101 |
+
st.session_state.proportion = st.slider("Proportion", 0.2, 1.0, float(st.session_state.proportion), 0.2)
|
102 |
+
df_chunked = pd.read_csv("resume_chunked.csv")
|
103 |
+
column_switch_to = f'{st.session_state.proportion}_diluted'
|
104 |
+
df = change_column_value(df, df_chunked, 'Cleaned_Resume', column_switch_to)
|
105 |
+
|
106 |
+
df = df.sample(n=st.session_state.sample_size, random_state=42)
|
107 |
st.write('Data:', df)
|
108 |
|
109 |
if st.button('Process Data') and not st.session_state.data_processed:
|
|
|
117 |
|
118 |
with st.spinner('Processing data...'):
|
119 |
parameters = {"temperature": st.session_state.temperature, "max_tokens": st.session_state.max_tokens}
|
120 |
+
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.prompt_template)
|
121 |
st.session_state.data_processed = True # Mark as processed
|
122 |
|
123 |
st.write('Processed Data:', preprocessed_df)
|
|
|
141 |
st.session_state.num_run = 1
|
142 |
st.session_state.data_processed = False
|
143 |
st.session_state.uploaded_file = None
|
144 |
+
st.session_state.proportion = 1.0
|
resume_chunked.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:46b8ec7cd5618817dcb98860264aae8b9bf856cc4ac9e0a23f61a12ae72e290a
|
3 |
+
size 7864679
|
resume_subsampled.csv
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ead8d4a52de48139bc0c98ab8e5b61210dd93e10856f024adf6f26570ea1353c
|
3 |
+
size 3845012
|
util/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (176 Bytes). View file
|
|
util/__pycache__/evaluation.cpython-311.pyc
ADDED
Binary file (11 kB). View file
|
|
util/__pycache__/injection.cpython-311.pyc
ADDED
Binary file (7.19 kB). View file
|
|
util/__pycache__/model.cpython-311.pyc
ADDED
Binary file (3.55 kB). View file
|
|
util/__pycache__/prompt.cpython-311.pyc
ADDED
Binary file (1.41 kB). View file
|
|
util/injection.py
CHANGED
@@ -5,13 +5,14 @@ import json_repair
|
|
5 |
import pandas as pd
|
6 |
from tqdm import tqdm
|
7 |
|
8 |
-
|
|
|
9 |
"""Generate a dynamic summary for scoring the applicant, excluding the group feature.
|
10 |
The occupation parameter allows customization of the job position.
|
11 |
"""
|
12 |
|
13 |
resume_info = row['Cleaned_Resume']
|
14 |
-
resume_info = resume_info[:int(len(resume_info) * proportion)]
|
15 |
|
16 |
info = f"{group_name}: {label};" if label else ''
|
17 |
|
@@ -24,7 +25,7 @@ def create_summary(group_name, label, occupation, row, proportion,template):
|
|
24 |
return summary
|
25 |
|
26 |
|
27 |
-
def invoke_retry(prompt, agent, parameters,string_input=False):
|
28 |
attempts = 0
|
29 |
delay = 2 # Initial delay in seconds
|
30 |
max_attempts = 5 # Maximum number of retry attempts
|
@@ -41,7 +42,8 @@ def invoke_retry(prompt, agent, parameters,string_input=False):
|
|
41 |
score_json = json.loads(score_text)
|
42 |
except json.JSONDecodeError:
|
43 |
try:
|
44 |
-
score_json = json.loads(
|
|
|
45 |
except json.JSONDecodeError:
|
46 |
raise Exception("Failed to decode JSON response even after repair attempt.")
|
47 |
# score = re.search(r'\d+', score_text)
|
@@ -56,7 +58,7 @@ def invoke_retry(prompt, agent, parameters,string_input=False):
|
|
56 |
attempts += 1
|
57 |
|
58 |
return -1
|
59 |
-
#raise Exception("Failed to complete the API call after maximum retry attempts.")
|
60 |
|
61 |
|
62 |
def calculate_avg_score(score_list):
|
@@ -66,18 +68,19 @@ def calculate_avg_score(score_list):
|
|
66 |
avg_score = sum(valid_scores) / len(valid_scores)
|
67 |
return avg_score
|
68 |
return None
|
69 |
-
def process_scores_multiple(df, num_run, parameters, privilege_label, protect_label, agent, group_name, occupation,proportion,template):
|
70 |
|
|
|
|
|
|
|
71 |
print(f"Processing {len(df)} entries with {num_run} runs each.")
|
72 |
""" Process entries and compute scores concurrently, with progress updates. """
|
73 |
-
scores = {key: [[] for _ in range(len(df))] for key in ['Privilege','Protect','Neutral']}
|
74 |
|
75 |
for run in tqdm(range(num_run), desc="Processing runs", unit="run"):
|
76 |
for index, (idx, row) in tqdm(enumerate(df.iterrows()), total=len(df), desc="Processing entries", unit="entry"):
|
77 |
|
78 |
for key, label in zip(['Privilege', 'Protect', 'Neutral'], [privilege_label, protect_label, False]):
|
79 |
-
|
80 |
-
prompt_normal = create_summary(group_name, label, occupation,row,proportion,template)
|
81 |
|
82 |
print(f"Run {run + 1} - Entry {index + 1} - {key}")
|
83 |
print("=============================================================")
|
@@ -86,17 +89,14 @@ def process_scores_multiple(df, num_run, parameters, privilege_label, protect_la
|
|
86 |
|
87 |
print(f"Scores: {scores}")
|
88 |
|
89 |
-
|
90 |
# Ensure all scores are lists and calculate average scores
|
91 |
-
for category in ['Privilege', 'Protect','Neutral']:
|
92 |
-
|
93 |
# Ensure the scores are lists and check before assignment
|
94 |
series_data = [lst if isinstance(lst, list) else [lst] for lst in scores[category]]
|
95 |
df[f'{category}_Scores'] = series_data
|
96 |
|
97 |
# Calculate the average score with additional debug info
|
98 |
|
99 |
-
|
100 |
df[f'{category}_Avg_Score'] = df[f'{category}_Scores'].apply(calculate_avg_score)
|
101 |
|
102 |
# Add ranks for each score within each row
|
|
|
5 |
import pandas as pd
|
6 |
from tqdm import tqdm
|
7 |
|
8 |
+
|
9 |
+
def create_summary(group_name, label, occupation, row, template):
|
10 |
"""Generate a dynamic summary for scoring the applicant, excluding the group feature.
|
11 |
The occupation parameter allows customization of the job position.
|
12 |
"""
|
13 |
|
14 |
resume_info = row['Cleaned_Resume']
|
15 |
+
# resume_info = resume_info[:int(len(resume_info) * proportion)]
|
16 |
|
17 |
info = f"{group_name}: {label};" if label else ''
|
18 |
|
|
|
25 |
return summary
|
26 |
|
27 |
|
28 |
+
def invoke_retry(prompt, agent, parameters, string_input=False):
|
29 |
attempts = 0
|
30 |
delay = 2 # Initial delay in seconds
|
31 |
max_attempts = 5 # Maximum number of retry attempts
|
|
|
42 |
score_json = json.loads(score_text)
|
43 |
except json.JSONDecodeError:
|
44 |
try:
|
45 |
+
score_json = json.loads(
|
46 |
+
json_repair.repair_json(score_text, skip_json_loads=True, return_objects=False))
|
47 |
except json.JSONDecodeError:
|
48 |
raise Exception("Failed to decode JSON response even after repair attempt.")
|
49 |
# score = re.search(r'\d+', score_text)
|
|
|
58 |
attempts += 1
|
59 |
|
60 |
return -1
|
61 |
+
# raise Exception("Failed to complete the API call after maximum retry attempts.")
|
62 |
|
63 |
|
64 |
def calculate_avg_score(score_list):
|
|
|
68 |
avg_score = sum(valid_scores) / len(valid_scores)
|
69 |
return avg_score
|
70 |
return None
|
|
|
71 |
|
72 |
+
|
73 |
+
def process_scores_multiple(df, num_run, parameters, privilege_label, protect_label, agent, group_name, occupation
|
74 |
+
, template):
|
75 |
print(f"Processing {len(df)} entries with {num_run} runs each.")
|
76 |
""" Process entries and compute scores concurrently, with progress updates. """
|
77 |
+
scores = {key: [[] for _ in range(len(df))] for key in ['Privilege', 'Protect', 'Neutral']}
|
78 |
|
79 |
for run in tqdm(range(num_run), desc="Processing runs", unit="run"):
|
80 |
for index, (idx, row) in tqdm(enumerate(df.iterrows()), total=len(df), desc="Processing entries", unit="entry"):
|
81 |
|
82 |
for key, label in zip(['Privilege', 'Protect', 'Neutral'], [privilege_label, protect_label, False]):
|
83 |
+
prompt_normal = create_summary(group_name, label, occupation, row, template)
|
|
|
84 |
|
85 |
print(f"Run {run + 1} - Entry {index + 1} - {key}")
|
86 |
print("=============================================================")
|
|
|
89 |
|
90 |
print(f"Scores: {scores}")
|
91 |
|
|
|
92 |
# Ensure all scores are lists and calculate average scores
|
93 |
+
for category in ['Privilege', 'Protect', 'Neutral']:
|
|
|
94 |
# Ensure the scores are lists and check before assignment
|
95 |
series_data = [lst if isinstance(lst, list) else [lst] for lst in scores[category]]
|
96 |
df[f'{category}_Scores'] = series_data
|
97 |
|
98 |
# Calculate the average score with additional debug info
|
99 |
|
|
|
100 |
df[f'{category}_Avg_Score'] = df[f'{category}_Scores'].apply(calculate_avg_score)
|
101 |
|
102 |
# Add ranks for each score within each row
|