-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_congress_data.py
executable file
·164 lines (149 loc) · 4.66 KB
/
generate_congress_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
"""
Generates a CSV file called congress_gpt3.csv that contains
contains real facts about all currently-serving US senators
and representatives, plus GPT3-synthesized answers to some configurable
policy questions. (Note that, as the policy answers may not be accurate,
the generated CSV file should used for demo purposes only, not as primary
source material!)
To use this, first download the legislator data at:
https://github.com/unitedstates/congress-legislators/blob/main/legislators-current.yaml
Then copy it into this directory, and run this script to produce the demo CSV.
Change the _QUESTIONS below if you want to add more synthesized questions.
"""
import csv
from datetime import datetime
import json
import openai
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
_QUESTIONS = [
{
"csv_column": "What are your policy positions",
"prompt": "How would [Name], [Office] from [State], describe their policy positions?",
},
{
"csv_column": "What are your positions on education",
"prompt": "How would [Name], [Office] from [State], describe their positions on education in one or two sentences?",
},
]
_OFFICES = {"rep": "representative", "sen": "senator"}
_STATES = {
"AK": "Alaska",
"AL": "Alabama",
"AR": "Arkansas",
"AZ": "Arizona",
"CA": "California",
"CO": "Colorado",
"CT": "Connecticut",
"DC": "District of Columbia",
"DE": "Delaware",
"FL": "Florida",
"GA": "Georgia",
"HI": "Hawaii",
"IA": "Iowa",
"ID": "Idaho",
"IL": "Illinois",
"IN": "Indiana",
"KS": "Kansas",
"KY": "Kentucky",
"LA": "Louisiana",
"MA": "Massachusetts",
"MD": "Maryland",
"ME": "Maine",
"MI": "Michigan",
"MN": "Minnesota",
"MO": "Missouri",
"MS": "Mississippi",
"MT": "Montana",
"NC": "North Carolina",
"ND": "North Dakota",
"NE": "Nebraska",
"NH": "New Hampshire",
"NJ": "New Jersey",
"NM": "New Mexico",
"NV": "Nevada",
"NY": "New York",
"OH": "Ohio",
"OK": "Oklahoma",
"OR": "Oregon",
"PA": "Pennsylvania",
"RI": "Rhode Island",
"SC": "South Carolina",
"SD": "South Dakota",
"TN": "Tennessee",
"TX": "Texas",
"UT": "Utah",
"VA": "Virginia",
"VT": "Vermont",
"WA": "Washington",
"WI": "Wisconsin",
"WV": "West Virginia",
"WY": "Wyoming",
"VI": "Virgin Islands",
"MP": "MP",
"AS": "American Samoa",
"PR": "Puerto Rico",
"GU": "Guam",
}
def run_completion_query(prompt, model="text-davinci-003", num_to_generate=1):
tries = 0
while tries < 3:
try:
response = openai.Completion.create(
model=model,
prompt=prompt,
temperature=0.0,
n=num_to_generate,
stop=["\n\n"],
max_tokens=300,
)
return response
except (openai.error.RateLimitError, openai.error.APIError) as e:
st.write(e)
tries += 1
time.sleep(10)
if __name__ == "__main__":
data = load(open("legislators-current.yaml").read(), Loader=Loader)
csv_output = []
for x in data:
name = x["name"]["official_full"]
gender = x["bio"]["gender"]
birthday = x["bio"]["birthday"]
latest_term = x["terms"][-1]
office = latest_term["type"]
state = latest_term["state"]
party = latest_term["party"]
# Discretize age to decade
d1 = datetime.strptime(birthday, "%Y-%m-%d")
d2 = datetime.now()
age = d2.year - d1.year - ((d2.month, d2.day) < (d1.month, d1.day))
age = str("%d0s" % (age / 10))
csv_output.append(
{
"Name": name,
"Office": _OFFICES[office],
"State": _STATES[state],
"Party": party,
"Gender": gender,
"Age": age,
}
)
# Get GPT-3 answers and output rows
fieldnames = list(csv_output[0].keys()) + [x["csv_column"] for x in _QUESTIONS]
csv_out = csv.DictWriter(open("congress_gpt3.csv", "w"), fieldnames)
csv_out.writeheader()
for x in csv_output:
for q in _QUESTIONS:
prompt = q["prompt"]
for var in ["Name", "Office", "State"]:
prompt = prompt.replace("[" + var + "]", x[var])
print(prompt)
gpt3_result = run_completion_query(prompt + "\n")
print(gpt3_result)
answer = gpt3_result["choices"][0]["text"]
x[q["csv_column"]] = answer
csv_out.writerow(x)