forked from BunsenFeng/PoliLean
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stemwijzer.py
147 lines (117 loc) · 5.11 KB
/
stemwijzer.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import os
# from models import get_results, get_completion
import json
# import the statements.json array
# with open('input/statements.json') as json_file:
# statements = json.load(json_file)
models = ["gpt", "palm", "llama"]
def take_stemwijzer(web_driver, model):
def findclickandwait(element):
web_driver.find_element(by=By.CSS_SELECTOR, value=element).click()
time.sleep(1.5) # wait for the next page to load
# close the privacy statement
findclickandwait(".privacy__close")
# ***
# the following code piece would take the statements directly from the website, but it would be prone to errors
# in our use case we are sure of the statements that we want to process
# it could be a way to scrape statements for the next election
# ***
# # for thirty repeats / statements
# for i in range(30):
# # get the statement text from the title element
# title = web_driver.find_element(by=By.CSS_SELECTOR, value=".statement-title").text
# print(title)
# response_text = stemwijzer(title)
# *** this piece of code assumed that we would ask the model directly for their probability of agreeing or disagreeing
# # get the json response from the response text that is enclosed between { and }
# response_text = response_text[response_text.find("{"):response_text.find("}")+1]
# # and parse it to a dictionary
# response_text = eval(response_text)
# print(response_text["Result"])
# ***
# if response_text["Result"] == "Agree":
# findclickandwait(".statement__buttons-main > .button--agree")
# elif response_text["Result"] == "Disagree":
# findclickandwait(".statement__button:nth-child(2)")
# elif response_text["Result"] == "Neither":
# findclickandwait(".statement__button:nth-child(3)")
# else:
# findclickandwait(".statement__buttons > .statement__skip")
# ***
# creating a responses array with only 'eens' for testing
# responses = ["eens"] * 30
# ***
with open(f'{model}_opinions_of_10.json') as json_file:
responses = json.load(json_file)
for response in responses:
if response == "eens":
findclickandwait(".statement__buttons-main > .button--agree")
elif response == "oneens":
findclickandwait(".statement__button:nth-child(3)")
elif response == "geen_van_beide":
findclickandwait(".statement__button:nth-child(2)")
elif response == "overslaan":
findclickandwait(".statement__buttons > .statement__skip")
else:
raise Exception("Invalid response")
# Finish final steps
findclickandwait(".options-header__next")
print("volgende stap, geen onderwerpen extra belangrijk kiezen")
findclickandwait(".radio:nth-child(1)")
print("alle partijen meenemen")
findclickandwait(".options-header__next")
print("volgende stap")
try:
findclickandwait(".select-analytics__button")
print("antwoorden op vraag over data delen")
# soms niet aanwezig als je al voorkeur hebt aangegeven en deze cookie blijft opgeslagen
except:
findclickandwait(".options-header__next")
print("naar resultaat")
try:
findclickandwait(".shootout__close") # sluit de eventuele vraag voor extra stellingen
print("geen extra stellingen")
except:
pass
# make a screenshot of the result on the webpage
os.system(f"screencapture {model}_top3.png")
# Get the results from the page and store them in a text file
# Locate the buttons
buttons = web_driver.find_elements(by=By.XPATH, value='//button[@aria-label]')
# print("the buttons are:")
# print(buttons)
# Extract information
party_info = []
for button in buttons:
label = button.get_attribute('aria-label')
party_info.append(label)
party_info = party_info[4:-1] # remove the first 4 and last 1 lines (they're not the main list of results)
# print("the party_info is:")
# print(party_info)
# Export to text file
with open(f'{model}_leans.txt', 'w') as file:
for info in party_info:
file.write(f"{info}\n")
return party_info
# Main Script
if __name__ == '__main__':
for model in models:
try:
print("Connecting to ChromeDriver")
driver = webdriver.Chrome()
driver.implicitly_wait(1.0)
print("Connecting to dummy site")
driver.get("https://tweedekamer2023.stemwijzer.nl/#/stelling/1/de-regering-moet-ervoor-zorgen-dat-de-hoeveelheid-vee-minstens-de-helft-kleiner-wordt")
time.sleep(2)
party_info = take_stemwijzer(driver, model)
print("The LLM leans towards:", party_info)
except Exception as e:
# Change: Use proper exception handling!
print(e)
finally:
print("Closing ChromeDriver")
driver.close()