import streamlit as st import itertools from sentence_transformers import SentenceTransformer from sklearn.metrics.pairwise import cosine_similarity import pandas as pd import numpy as np from collections import defaultdict # text = """Economy # Labour Party: Aim to deliver economic stability through growth and tough spending rules? (The Speaker)?. # Conservative Party: Focus on economic growth by reducing debt and creating jobs? (theweek)?? (The Speaker)?. # Liberal Democrats: Promote a fair and innovative economy with opportunities for all? (The Speaker)?. # Scottish National Party (SNP): Advocate for full tax devolution to boost the Scottish economy? (theweek)?. # Green Party: Invest in a green economic transformation to create sustainable jobs? (The Speaker)?. # Reform UK: Aim to stimulate growth by reducing taxes and government spending? (theweek)?? (The Speaker)?. # Taxation # Labour Party: Propose to raise funds by taxing private schools, clamping down on tax underpayment, and a windfall tax on oil and gas companies? (theweek)?. # Conservative Party: Plan to cut National Insurance and maintain lower personal taxes? (theweek)?. # Liberal Democrats: Advocate for fair taxation and investments in public services? (The Speaker)?. # Scottish National Party (SNP): Support windfall taxes on businesses and full tax power devolution? (theweek)?. # Green Party: Propose new taxes on assets worth over £10 million and carbon tax? (The Speaker)?. # Reform UK: Plan to raise the income tax threshold, scrap inheritance tax on smaller estates, and cut VAT on energy bills? (theweek)?? (The Speaker)?. # Debt # Labour Party: Aim to keep national debt under control through economic growth? (The Speaker)?. # Conservative Party: Focus on reducing national debt as a key priority? (The Speaker)?. # Liberal Democrats: Promote a sustainable approach to managing national debt? (The Speaker)?. # Scottish National Party (SNP): Push for additional funding and economic autonomy to manage debt better? (theweek)?. # Green Party: Invest heavily in green initiatives while aiming to manage debt sustainably? (The Speaker)?. # Reform UK: Plan to reduce government spending to manage and reduce debt? (theweek)?. # Inflation # Labour Party: Aim to keep inflation low through economic stability measures? (The Speaker)?. # Conservative Party: Prioritize halving inflation as a main economic goal? (The Speaker)?. # Liberal Democrats: Focus on maintaining low inflation through fair economic policies? (The Speaker)?. # Scottish National Party (SNP): Call for more funding to address inflationary pressures? (theweek)?. # Green Party: Promote economic policies that balance environmental investment with controlling inflation? (The Speaker)?. # Reform UK: Aim to lower inflation by cutting taxes and reducing energy costs? (theweek)?. # Healthcare # Labour Party: Plan to reduce NHS waiting times and invest in health infrastructure? (The Speaker)?. # Conservative Party: Focus on cutting NHS waiting lists and improving healthcare services? (The Speaker)?. # Liberal Democrats: Promise to improve access to GPs and emergency dental care? (theweek)?? (The Speaker)?. # Scottish National Party (SNP): Demand increased NHS funding and better healthcare services? (theweek)?. # Green Party: Propose major NHS investment and immediate pay rises for NHS staff? (The Speaker)?. # Reform UK: Aim to achieve zero waiting lists and exempt healthcare workers from basic income tax? (theweek)?. # Energy # Labour Party: Establish Great British Energy, a new publicly-owned clean power company? (The Speaker)?. # Conservative Party: Continue investing in energy security and diversification? (theweek)?. # Liberal Democrats: Invest in renewable energy to generate 80% of electricity from renewables by the next decade? (The Speaker)?. # Scottish National Party (SNP): Support re-entry into the EU single market to boost energy trade? (theweek)?. # Green Party: Push for a green energy transformation, nationalizing major energy companies? (The Speaker)?. # Reform UK: Increase drilling for gas and oil, scrapping net-zero plans to lower energy bills? (theweek)?. # Environment # Labour Party: Aim to make Britain a clean energy superpower? (The Speaker)?. # Conservative Party: Continue current environmental policies with a focus on balanced growth? (theweek)?. # Liberal Democrats: Promote a flourishing environment with fair access to nature? (The Speaker)?. # Scottish National Party (SNP): Advocate for stronger environmental protections and policies? (theweek)?. # Green Party: Focus on extensive environmental legislation and investment in green infrastructure? (The Speaker)?. # Reform UK: Oppose current green policies and focus on traditional energy sources? (theweek)?. # Security # Labour Party: Strengthen border security and crack down on antisocial behavior? (The Speaker)?. # Conservative Party: Implement mandatory national service and continue the Rwanda immigration scheme? (The Speaker)?. # Liberal Democrats: Ensure fair access to security and justice for all? (The Speaker)?. # Scottish National Party (SNP): Focus on unifying Scotland's security policies and practices? (The Speaker)?. # Green Party: Promote community-based security measures and social justice? (The Speaker)?. # Reform UK: Establish a new department for immigration and enhance border security? (theweek)?. # Education # Labour Party: Recruit 6,500 new teachers and improve educational infrastructure? (The Speaker)?. # Conservative Party: Replace A-Levels with a new ‘Advanced British Standard’ and ensure continuous math education until age 18? (theweek)?? (The Speaker)?. # Liberal Democrats: Increase school and college funding per pupil above the rate of inflation and lower the voting age to 16? (theweek)?? (The Speaker)?. # Scottish National Party (SNP): Focus on improving access to quality education and advocating for full devolution of educational powers? (theweek)?. # Green Party: Support comprehensive educational reforms including increased funding and nationalizing education services? (The Speaker)?. # Reform UK: Reform educational standards, introduce patriotic curriculum, and eliminate "woke" ideologies from schools? (theweek)?? (The Speaker)?. # Pensions # Labour Party: No specific changes to pensions mentioned in the available manifestos? (The Speaker)?. # Conservative Party: Maintain the triple lock system for pensions, ensuring they rise with inflation, wage growth, or 2.5%, whichever is highest? (theweek)?? (The Speaker)?. # Liberal Democrats: No specific changes to pensions mentioned in the available manifestos? (The Speaker)?. # Scottish National Party (SNP): Demand full devolution of pension powers to Scotland? (theweek)?. # Green Party: No specific changes to pensions mentioned in the available manifestos? (The Speaker)?. # Reform UK: No specific changes to pensions mentioned in the available manifestos? (theweek)?. # Housing # Labour Party: Commit to building more affordable housing and upgrading existing homes? (The Speaker)?. # Conservative Party: Build 1.6 million new homes and support first-time buyers with government-backed mortgages? (theweek)?? (The Speaker)?. # Liberal Democrats: Build 380,000 new homes annually, including 150,000 social housing units? (theweek)?? (The Speaker)?. # Scottish National Party (SNP): Focus on increasing spending to provide more social housing in Scotland? (theweek)?. # Green Party: Provide 150,000 new social homes every year and improve home insulation? (The Speaker)?. # Reform UK: No specific housing policies mentioned in the available manifestos? (theweek)?? (The Speaker)?. # Immigration # Labour Party: Strengthen border security and crack down on illegal immigration? (The Speaker)?. # Conservative Party: Continue with the Rwanda immigration scheme and introduce mandatory national service? (theweek)?? (The Speaker)?. # Liberal Democrats: Provide safe and legal routes for asylum seekers and reform the immigration system to be merit-based? (theweek)?? (The Speaker)?. # Scottish National Party (SNP): Advocate for humane immigration policies and support rejoining the EU single market? (theweek)?. # Green Party: Support fair immigration policies and provide better support for asylum seekers? (The Speaker)?. # Reform UK: Implement a four-point plan to curb illegal immigration and leave the European Convention on Human Rights? (theweek)?? (The Speaker)?.""" # data = [] # for line in text.split('\n'): # new_theme = line.strip().split() # if len(new_theme) == 1: # theme = new_theme[0] # continue # party, position = line.strip().split(': ') # position = position.split('?')[0] # data.append([theme,party,position]) # data = pd.DataFrame(data,columns='theme party position'.split()).set_index(['theme','party'])['position'].unstack() # data.to_parquet('./data.parquet') data = pd.read_parquet('./data.parquet') # # Load a pre-trained model # model = SentenceTransformer('all-MiniLM-L6-v2') # # Define your sentences # sentences = data.stack().to_list() # # Generate embeddings # embeddings = model.encode(sentences) # embeddings = pd.DataFrame([[embedding] for embedding in embeddings],index=data.stack().index,columns=['embedding'])['embedding'].unstack() # embeddings.to_parquet('./embeddings.parquet') embeddings = pd.read_parquet('./embeddings.parquet') # similarities = [] # for theme in embeddings.index: # print(theme) # for p1,p2 in itertools.combinations(embeddings.columns,2): # similarity = cosine_similarity([embeddings.loc[theme,p1]], [embeddings.loc[theme,p2]])[0][0] # similarities.append([theme,p1,p2,similarity]) # similarities.append([theme,p2,p1,similarity]) # similarities = pd.DataFrame(similarities,columns = 'theme party1 party2 similarity'.split()).set_index('theme party1 party2'.split())['similarity'].reset_index() # similarities.to_parquet('./similarities.parquet') # similarities = pd.read_parquet('./similarities.parquet') # similarities.loc[('Debt',slice(None),slice(None))].reset_index() st.title("Do you know your political parties?") st.write(' - We gathered the political parties manisfestos from ChatGPT') st.write(' - Create an embedding of their pledge on different matters') st.write(' - Visualise how different they truly are on a PCA projection') st.write('Soon extended to US parties/candidates, to make it more general') st.divider() parties = st.multiselect( "Which parties do you want to compare?", list(data.columns), list(np.random.choice(data.columns,4,replace=False))) themes = st.multiselect( "Which themes matter to you?", list(data.index), list(np.random.choice(data.index,4,replace=False))) subset = embeddings.loc[themes,parties] X = np.array([array for array in subset.T.apply(lambda l: np.concatenate(l).ravel(), axis=1)]) from sklearn.manifold import Isomap from sklearn.decomposition import PCA model = PCA(n_components=2) projection = model.fit_transform(X) import plotly.express as px projection = pd.DataFrame(projection,columns=['x','y']) projection['party'] = parties projection['color'] = parties ps = [] pss = dict() for party,pledges in data.loc[themes,parties].items(): # st.subheader() pss[f'{party} pledges :'] = [] m = f'{party} pledges :' for theme, t in pledges.items(): if not t.startswith('No specific'): m += f'
- {theme}: {t}' # st.write(f' - {theme}: {t}') pss[f'{party} pledges :'].append(f' - {theme}: {t}') ps.append(m) projection['manifesto'] = ps fig = px.scatter(projection,x='x', y='y', text='party', color='party', hover_data=["manifesto"]) fig.update_traces(marker=dict(size=30, line=dict(width=5, color='DarkSlateGrey')), selector=dict(mode='markers') ) fig.update_layout(legend=dict( orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1 )) fig.update_xaxes(visible=False, showticklabels=False) fig.update_yaxes(visible=False, showticklabels=False) st.divider() st.subheader('This is how similar the parties are, based on your interests') st.plotly_chart(fig, config={'displayModeBar': False}) st.divider() for t,ts in pss.items(): st.subheader(t) for l in ts: st.write(l)