Zekun Wu
commited on
Commit
·
c91dc45
1
Parent(s):
093cdcd
update
Browse files- pages/1_Demo_1.py +77 -101
pages/1_Demo_1.py
CHANGED
@@ -7,30 +7,9 @@ from utils.model import gpt2
|
|
7 |
import os
|
8 |
|
9 |
# Set up the Streamlit interface
|
10 |
-
st.set_page_config(page_title="Gender Bias Analysis", page_icon="🔍", layout="wide")
|
11 |
st.title('Gender Bias Analysis in Text Generation')
|
12 |
|
13 |
-
|
14 |
-
if 'password_correct' not in st.session_state:
|
15 |
-
st.session_state['password_correct'] = False
|
16 |
-
if 'data_size' not in st.session_state:
|
17 |
-
st.session_state['data_size'] = 10
|
18 |
-
if 'bold' not in st.session_state:
|
19 |
-
st.session_state['bold'] = None
|
20 |
-
if 'female_bold' not in st.session_state:
|
21 |
-
st.session_state['female_bold'] = []
|
22 |
-
if 'male_bold' not in st.session_state:
|
23 |
-
st.session_state['male_bold'] = []
|
24 |
-
if 'male_prompts' not in st.session_state:
|
25 |
-
st.session_state['male_prompts'] = []
|
26 |
-
if 'female_prompts' not in st.session_state:
|
27 |
-
st.session_state['female_prompts'] = []
|
28 |
-
if 'male_continuations' not in st.session_state:
|
29 |
-
st.session_state['male_continuations'] = []
|
30 |
-
if 'female_continuations' not in st.session_state:
|
31 |
-
st.session_state['female_continuations'] = []
|
32 |
-
|
33 |
-
# Password protection function
|
34 |
def check_password():
|
35 |
def password_entered():
|
36 |
if password_input == os.getenv('PASSWORD'):
|
@@ -39,83 +18,25 @@ def check_password():
|
|
39 |
st.error("Incorrect Password, please try again.")
|
40 |
|
41 |
password_input = st.text_input("Enter Password:", type="password")
|
42 |
-
st.button("Submit", on_click=password_entered)
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
if st.session_state['bold'] is None:
|
47 |
-
st.session_state['bold'] = load_dataset("AlexaAI/bold", split="train")
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
st.session_state['female_bold'] = sample(
|
52 |
-
[p for p in st.session_state['bold'] if p['category'] == 'American_actresses'], data_size)
|
53 |
-
st.session_state['male_bold'] = sample(
|
54 |
-
[p for p in st.session_state['bold'] if p['category'] == 'American_actors'], data_size)
|
55 |
-
|
56 |
-
# Text generation function
|
57 |
-
def generate_text():
|
58 |
-
GPT2 = gpt2()
|
59 |
-
st.session_state['male_prompts'] = [p['prompts'][0] for p in st.session_state['male_bold']]
|
60 |
-
st.session_state['female_prompts'] = [p['prompts'][0] for p in st.session_state['female_bold']]
|
61 |
-
|
62 |
-
progress_bar = st.progress(0)
|
63 |
-
st.write('Generating text for male prompts...')
|
64 |
-
male_generation = GPT2.text_generation(st.session_state['male_prompts'], pad_token_id=50256, max_length=50,
|
65 |
-
do_sample=False, truncation=True)
|
66 |
-
st.session_state['male_continuations'] = [gen[0]['generated_text'].replace(prompt, '') for gen, prompt in
|
67 |
-
zip(male_generation, st.session_state['male_prompts'])]
|
68 |
-
|
69 |
-
progress_bar.progress(50)
|
70 |
-
|
71 |
-
st.write('Generating text for female prompts...')
|
72 |
-
female_generation = GPT2.text_generation(st.session_state['female_prompts'], pad_token_id=50256,
|
73 |
-
max_length=50, do_sample=False, truncation=True)
|
74 |
-
st.session_state['female_continuations'] = [gen[0]['generated_text'].replace(prompt, '') for gen, prompt in
|
75 |
-
zip(female_generation, st.session_state['female_prompts'])]
|
76 |
-
|
77 |
-
progress_bar.progress(100)
|
78 |
-
st.write('Text generation completed.')
|
79 |
-
|
80 |
-
# Display data samples function
|
81 |
-
def display_samples():
|
82 |
-
st.write("### Male Data Samples")
|
83 |
-
samples_df = pd.DataFrame({
|
84 |
-
'Male Prompt': st.session_state['male_prompts'],
|
85 |
-
'Male Continuation': st.session_state['male_continuations'],
|
86 |
-
})
|
87 |
-
st.dataframe(samples_df)
|
88 |
-
|
89 |
-
st.write("### Female Data Samples")
|
90 |
-
samples_df = pd.DataFrame({
|
91 |
-
'Female Prompt': st.session_state['female_prompts'],
|
92 |
-
'Female Continuation': st.session_state['female_continuations']
|
93 |
-
})
|
94 |
-
st.dataframe(samples_df)
|
95 |
-
|
96 |
-
# Evaluate regard function
|
97 |
-
def evaluate_regard():
|
98 |
-
regard = Regard("compare")
|
99 |
-
st.write('Computing regard results to compare male and female continuations...')
|
100 |
-
|
101 |
-
with st.spinner('Computing regard results...'):
|
102 |
-
regard_results = regard.compute(data=st.session_state['male_continuations'],
|
103 |
-
references=st.session_state['female_continuations'])
|
104 |
-
st.write('**Raw Regard Results:**')
|
105 |
-
st.json(regard_results)
|
106 |
-
|
107 |
-
regard_results_avg = regard.compute(data=st.session_state['male_continuations'],
|
108 |
-
references=st.session_state['female_continuations'],
|
109 |
-
aggregation='average')
|
110 |
-
st.write('**Average Regard Results:**')
|
111 |
-
st.json(regard_results_avg)
|
112 |
-
|
113 |
-
# Main app logic
|
114 |
-
if not st.session_state['password_correct']:
|
115 |
check_password()
|
116 |
else:
|
117 |
st.sidebar.success("Password Verified. Proceed with the demo.")
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
st.subheader('Step 1: Set Data Size')
|
121 |
data_size = st.slider('Select number of samples per category:', min_value=1, max_value=50,
|
@@ -123,17 +44,72 @@ else:
|
|
123 |
st.session_state['data_size'] = data_size
|
124 |
|
125 |
if st.button('Show Data'):
|
126 |
-
|
|
|
|
|
|
|
|
|
127 |
st.write(f'Sampled {data_size} female and male American actors.')
|
128 |
-
|
|
|
129 |
|
130 |
if st.session_state['female_bold'] and st.session_state['male_bold']:
|
131 |
st.subheader('Step 2: Generate Text')
|
|
|
132 |
if st.button('Generate Text'):
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
-
if st.session_state['male_continuations'] and st.session_state['female_continuations']:
|
136 |
-
st.subheader('Step 3: Evaluate')
|
137 |
-
display_samples()
|
138 |
if st.button('Evaluate'):
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import os
|
8 |
|
9 |
# Set up the Streamlit interface
|
|
|
10 |
st.title('Gender Bias Analysis in Text Generation')
|
11 |
|
12 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def check_password():
|
14 |
def password_entered():
|
15 |
if password_input == os.getenv('PASSWORD'):
|
|
|
18 |
st.error("Incorrect Password, please try again.")
|
19 |
|
20 |
password_input = st.text_input("Enter Password:", type="password")
|
21 |
+
submit_button = st.button("Submit", on_click=password_entered)
|
22 |
|
23 |
+
if submit_button and not st.session_state.get('password_correct', False):
|
24 |
+
st.error("Please enter a valid password to access the demo.")
|
|
|
|
|
25 |
|
26 |
+
|
27 |
+
if not st.session_state.get('password_correct', False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
check_password()
|
29 |
else:
|
30 |
st.sidebar.success("Password Verified. Proceed with the demo.")
|
31 |
+
|
32 |
+
if 'data_size' not in st.session_state:
|
33 |
+
st.session_state['data_size'] = 10
|
34 |
+
if 'bold' not in st.session_state:
|
35 |
+
st.session_state['bold'] = load_dataset("AlexaAI/bold", split="train")
|
36 |
+
if 'female_bold' not in st.session_state:
|
37 |
+
st.session_state['female_bold'] = []
|
38 |
+
if 'male_bold' not in st.session_state:
|
39 |
+
st.session_state['male_bold'] = []
|
40 |
|
41 |
st.subheader('Step 1: Set Data Size')
|
42 |
data_size = st.slider('Select number of samples per category:', min_value=1, max_value=50,
|
|
|
44 |
st.session_state['data_size'] = data_size
|
45 |
|
46 |
if st.button('Show Data'):
|
47 |
+
st.session_state['female_bold'] = sample(
|
48 |
+
[p for p in st.session_state['bold'] if p['category'] == 'American_actresses'], data_size)
|
49 |
+
st.session_state['male_bold'] = sample(
|
50 |
+
[p for p in st.session_state['bold'] if p['category'] == 'American_actors'], data_size)
|
51 |
+
|
52 |
st.write(f'Sampled {data_size} female and male American actors.')
|
53 |
+
st.write('**Female Samples:**', pd.DataFrame(st.session_state['female_bold']))
|
54 |
+
st.write('**Male Samples:**', pd.DataFrame(st.session_state['male_bold']))
|
55 |
|
56 |
if st.session_state['female_bold'] and st.session_state['male_bold']:
|
57 |
st.subheader('Step 2: Generate Text')
|
58 |
+
|
59 |
if st.button('Generate Text'):
|
60 |
+
GPT2 = gpt2()
|
61 |
+
st.session_state['male_prompts'] = [p['prompts'][0] for p in st.session_state['male_bold']]
|
62 |
+
st.session_state['female_prompts'] = [p['prompts'][0] for p in st.session_state['female_bold']]
|
63 |
+
|
64 |
+
progress_bar = st.progress(0)
|
65 |
+
|
66 |
+
st.write('Generating text for male prompts...')
|
67 |
+
male_generation = GPT2.text_generation(st.session_state['male_prompts'], pad_token_id=50256, max_length=50,
|
68 |
+
do_sample=False, truncation=True)
|
69 |
+
st.session_state['male_continuations'] = [gen[0]['generated_text'].replace(prompt, '') for gen, prompt in
|
70 |
+
zip(male_generation, st.session_state['male_prompts'])]
|
71 |
+
|
72 |
+
progress_bar.progress(50)
|
73 |
+
|
74 |
+
st.write('Generating text for female prompts...')
|
75 |
+
female_generation = GPT2.text_generation(st.session_state['female_prompts'], pad_token_id=50256,
|
76 |
+
max_length=50, do_sample=False, truncation=True)
|
77 |
+
st.session_state['female_continuations'] = [gen[0]['generated_text'].replace(prompt, '') for gen, prompt in
|
78 |
+
zip(female_generation, st.session_state['female_prompts'])]
|
79 |
+
|
80 |
+
progress_bar.progress(100)
|
81 |
+
st.write('Text generation completed.')
|
82 |
+
|
83 |
+
if st.session_state.get('male_continuations') and st.session_state.get('female_continuations'):
|
84 |
+
st.subheader('Step 3: Sample Generated Texts')
|
85 |
+
|
86 |
+
st.write("Male Data Samples:")
|
87 |
+
samples_df = pd.DataFrame({
|
88 |
+
'Male Prompt': st.session_state['male_prompts'],
|
89 |
+
'Male Continuation': st.session_state['male_continuations'],
|
90 |
+
})
|
91 |
+
st.write(samples_df)
|
92 |
+
|
93 |
+
st.write("Female Data Samples:")
|
94 |
+
samples_df = pd.DataFrame({
|
95 |
+
'Female Prompt': st.session_state['female_prompts'],
|
96 |
+
'Female Continuation': st.session_state['female_continuations']
|
97 |
+
})
|
98 |
+
st.write(samples_df)
|
99 |
|
|
|
|
|
|
|
100 |
if st.button('Evaluate'):
|
101 |
+
st.subheader('Step 4: Regard Results')
|
102 |
+
regard = Regard("compare")
|
103 |
+
st.write('Computing regard results to compare male and female continuations...')
|
104 |
+
|
105 |
+
with st.spinner('Computing regard results...'):
|
106 |
+
regard_results = regard.compute(data=st.session_state['male_continuations'],
|
107 |
+
references=st.session_state['female_continuations'])
|
108 |
+
st.write('**Raw Regard Results:**')
|
109 |
+
st.json(regard_results)
|
110 |
+
|
111 |
+
regard_results_avg = regard.compute(data=st.session_state['male_continuations'],
|
112 |
+
references=st.session_state['female_continuations'],
|
113 |
+
aggregation='average')
|
114 |
+
st.write('**Average Regard Results:**')
|
115 |
+
st.json(regard_results_avg)
|