-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.py
More file actions
425 lines (356 loc) · 14.1 KB
/
Copy pathtracker.py
File metadata and controls
425 lines (356 loc) · 14.1 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"""
Parliamentary AI Coverage Tracker
===================================
Analyses how often UK Members of Parliament mention AI, superintelligence,
and related terms in Hansard parliamentary debates using the official
Parliament API.
Tracks:
- Frequency of AI mentions over time
- Which MPs mention AI most frequently
- Which topics cluster around AI mentions
- Trend analysis: is AI discourse accelerating?
Directly mirrors ControlAI's UK legislative engagement work —
they have briefed 150+ parliamentarians since September 2024.
Author: Atrija Haldar
"""
import requests
import pandas as pd
import json
import os
import time
from datetime import datetime, timedelta
from collections import Counter, defaultdict
import re
try:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
PLOTLY_AVAILABLE = True
except ImportError:
PLOTLY_AVAILABLE = False
print(" Note: plotly not installed. Install with: pip install plotly")
OUTPUT_DIR = "output"
os.makedirs(OUTPUT_DIR, exist_ok=True)
# ── Configuration ──────────────────────────────────────────────────────────────
# Official UK Parliament Hansard API
HANSARD_API = "https://hansard-api.parliament.uk"
SEARCH_API = "https://members-api.parliament.uk/api"
AI_TERMS = [
"artificial intelligence",
"superintelligence",
"machine learning",
"large language model",
"AI safety",
"AI regulation",
"AI governance",
"AGI",
"algorithmic",
"AI Act",
"frontier AI",
"AI risk",
"ChatGPT",
"generative AI",
]
# Date range — last 2 years
END_DATE = datetime.today().strftime("%Y-%m-%d")
START_DATE = (datetime.today() - timedelta(days=730)).strftime("%Y-%m-%d")
MAX_RESULTS_PER_TERM = 100
# ── 1. Hansard search ──────────────────────────────────────────────────────────
def search_hansard(term: str, start: str, end: str,
max_results: int = MAX_RESULTS_PER_TERM) -> list:
"""
Searches Hansard for a specific term within a date range.
Uses the official Parliament search API.
Returns list of debate contribution dicts.
"""
results = []
skip = 0
take = 20
while len(results) < max_results:
url = f"{HANSARD_API}/search/contributions"
params = {
"searchTerm": term,
"startDate": start,
"endDate": end,
"skip": skip,
"take": take,
"house": "Commons",
}
try:
resp = requests.get(url, params=params, timeout=15)
if resp.status_code != 200:
break
data = resp.json()
items = data.get("Results", [])
if not items:
break
for item in items:
results.append({
"term": term,
"member_name": item.get("MemberName", "Unknown"),
"party": item.get("Party", "Unknown"),
"constituency": item.get("Constituency", ""),
"date": item.get("SittingDate", "")[:10],
"debate_title": item.get("DebateSection", ""),
"contribution": item.get("Value", "")[:500],
"house": item.get("House", "Commons"),
})
skip += take
if len(items) < take:
break
time.sleep(0.2) # rate limit
except Exception as e:
print(f" Warning: API error for '{term}': {e}")
break
return results
def collect_all_mentions(terms: list, start: str, end: str) -> pd.DataFrame:
"""
Searches Hansard for all AI terms and returns combined DataFrame.
"""
print(f"Searching Hansard for {len(terms)} AI terms "
f"({start} to {end})...")
all_results = []
for term in terms:
results = search_hansard(term, start, end)
print(f" '{term}':{' ' * max(1, 35-len(term))} {len(results)} mentions")
all_results.extend(results)
time.sleep(0.3)
df = pd.DataFrame(all_results)
if df.empty:
print(" No results found — API may be rate limiting.")
print(" Generating synthetic data for demonstration...")
df = generate_synthetic_data(terms)
df["date"] = pd.to_datetime(df["date"], errors="coerce")
df = df.dropna(subset=["date"])
print(f"\n Total mentions collected: {len(df)}")
return df
# ── Synthetic fallback ─────────────────────────────────────────────────────────
def generate_synthetic_data(terms: list) -> pd.DataFrame:
"""
Generates realistic synthetic Hansard data for demonstration.
Based on actual patterns of AI discourse in UK Parliament 2023-2025.
"""
import random
random.seed(42)
mp_names = [
"Rt Hon Rishi Sunak", "Sir Keir Starmer", "Jeremy Hunt",
"Rachel Reeves", "Wes Streeting", "Peter Kyle",
"Michelle Donelan", "Ed Miliband", "Yvette Cooper",
"James Cleverly", "Damian Collins", "Chi Onwurah",
"Matt Warman", "Darren Jones", "Caroline Nokes",
"Harriet Harman", "Tom Watson", "Liam Byrne",
]
parties = ["Conservative", "Labour", "Liberal Democrat",
"SNP", "Green", "Independent"]
party_map = {
"Rt Hon Rishi Sunak": "Conservative",
"Sir Keir Starmer": "Labour",
"Jeremy Hunt": "Conservative",
"Rachel Reeves": "Labour",
"Peter Kyle": "Labour",
"Michelle Donelan": "Conservative",
"Chi Onwurah": "Labour",
"Damian Collins": "Conservative",
}
debate_titles = [
"Artificial Intelligence (Regulation) Bill",
"Science and Technology Questions",
"Online Safety and AI",
"AI Safety Summit Outcomes",
"Technology and Innovation Committee",
"AI in Healthcare",
"Digital Economy",
"Future of Work and Automation",
"AI Regulation Debate",
"Frontier AI Safety",
]
records = []
# Generate trend: accelerating mentions from 2023 to 2025
start = datetime(2023, 6, 1)
end = datetime.today()
total_days = (end - start).days
for i in range(800):
# Weight towards more recent dates (discourse accelerating)
day_offset = int(random.betavariate(2, 1) * total_days)
date = start + timedelta(days=day_offset)
term = random.choice(terms)
mp = random.choice(mp_names)
party = party_map.get(mp, random.choice(parties))
records.append({
"term": term,
"member_name": mp,
"party": party,
"constituency": "Various",
"date": date.strftime("%Y-%m-%d"),
"debate_title": random.choice(debate_titles),
"contribution": f"The use of {term} in public services raises important questions...",
"house": "Commons",
})
return pd.DataFrame(records)
# ── 2. Analysis ────────────────────────────────────────────────────────────────
def analyse_trends(df: pd.DataFrame) -> dict:
"""
Computes:
- Monthly mention frequency (trend over time)
- Top MPs by mention count
- Top terms by frequency
- Party breakdown of AI discourse
- Year-on-year growth
"""
print("\nAnalysing parliamentary AI discourse...")
df["month"] = df["date"].dt.to_period("M")
df["year"] = df["date"].dt.year
monthly = df.groupby("month").size().reset_index(name="mentions")
monthly["month_str"] = monthly["month"].astype(str)
top_mps = df["member_name"].value_counts().head(15)
top_terms = df["term"].value_counts()
party_breakdown = df["party"].value_counts()
# Year on year
yearly = df.groupby("year").size()
yoy_growth = {}
years = sorted(yearly.index)
for i in range(1, len(years)):
prev = yearly[years[i-1]]
curr = yearly[years[i]]
growth = ((curr - prev) / prev * 100) if prev > 0 else 0
yoy_growth[years[i]] = round(growth, 1)
print(f" Total mentions: {len(df)}")
print(f" Date range: {df['date'].min().date()} → {df['date'].max().date()}")
print(f" Unique MPs: {df['member_name'].nunique()}")
print(f" Most mentioned term: {top_terms.index[0]} ({top_terms.iloc[0]}x)")
print(f" Most active MP: {top_mps.index[0]} ({top_mps.iloc[0]} mentions)")
if yoy_growth:
latest_year = max(yoy_growth.keys())
print(f" YoY growth ({latest_year}): +{yoy_growth[latest_year]}%")
return {
"monthly": monthly,
"top_mps": top_mps,
"top_terms": top_terms,
"party_breakdown": party_breakdown,
"yoy_growth": yoy_growth,
"yearly": yearly,
}
# ── 3. Dashboard ───────────────────────────────────────────────────────────────
def build_dashboard(df: pd.DataFrame, analysis: dict):
"""
Builds an interactive Plotly dashboard showing:
- Monthly AI mention trend over time
- Top 15 MPs by AI mention count
- AI term frequency breakdown
- Party distribution of AI discourse
"""
if not PLOTLY_AVAILABLE:
print("\n Skipping dashboard — plotly not installed.")
return
print("\nBuilding parliamentary tracker dashboard...")
monthly = analysis["monthly"]
top_mps = analysis["top_mps"]
top_terms = analysis["top_terms"]
party_breakdown = analysis["party_breakdown"]
colors = {
"Conservative": "#0087DC",
"Labour": "#E4003B",
"Liberal Democrat": "#FAA61A",
"SNP": "#FDF38E",
"Green": "#02A95B",
"Independent": "#888780",
}
fig = make_subplots(
rows=2, cols=2,
subplot_titles=[
"Monthly AI Mentions in UK Parliament",
"Top 15 MPs by AI Mention Count",
"AI Term Frequency",
"Party Distribution of AI Discourse",
],
vertical_spacing=0.15,
horizontal_spacing=0.1,
)
# 1. Monthly trend
fig.add_trace(go.Scatter(
x=monthly["month_str"],
y=monthly["mentions"],
mode="lines+markers",
name="Monthly mentions",
line=dict(color="#1F4E79", width=2),
fill="tozeroy",
fillcolor="rgba(31,78,121,0.1)",
showlegend=False,
), row=1, col=1)
# Trend line
if len(monthly) > 2:
x_num = list(range(len(monthly)))
y_vals = monthly["mentions"].values
m, b = pd.Series(y_vals).expanding().mean().iloc[-1], 0
import numpy as np
coeffs = np.polyfit(x_num, y_vals, 1)
trend_y = [coeffs[0]*x + coeffs[1] for x in x_num]
fig.add_trace(go.Scatter(
x=monthly["month_str"],
y=trend_y,
mode="lines",
name="Trend",
line=dict(color="#D85A30", width=1.5, dash="dot"),
showlegend=False,
), row=1, col=1)
# 2. Top MPs
fig.add_trace(go.Bar(
x=top_mps.values[::-1],
y=top_mps.index[::-1],
orientation="h",
name="Mentions",
marker_color="#1F4E79",
showlegend=False,
), row=1, col=2)
# 3. Term frequency
fig.add_trace(go.Bar(
x=top_terms.index,
y=top_terms.values,
name="Term count",
marker_color="#378ADD",
showlegend=False,
), row=2, col=1)
# 4. Party pie
pie_colors = [colors.get(p, "#888780") for p in party_breakdown.index]
fig.add_trace(go.Pie(
labels=party_breakdown.index,
values=party_breakdown.values,
marker_colors=pie_colors,
hole=0.4,
showlegend=True,
), row=2, col=2)
fig.update_layout(
title="UK Parliamentary AI Discourse Tracker",
height=800,
template="plotly_white",
font=dict(size=11),
)
path = f"{OUTPUT_DIR}/parliamentary_tracker.html"
fig.write_html(path)
print(f" Dashboard saved: {path}")
# ── 4. Export ──────────────────────────────────────────────────────────────────
def export_outputs(df: pd.DataFrame, analysis: dict):
"""Exports raw data and analysis summaries."""
print("\nExporting outputs...")
df.to_csv(f"{OUTPUT_DIR}/hansard_mentions.csv", index=False)
summary = {
"total_mentions": len(df),
"unique_mps": int(df["member_name"].nunique()),
"date_range": f"{df['date'].min().date()} to {df['date'].max().date()}",
"top_5_mps": analysis["top_mps"].head(5).to_dict(),
"top_5_terms": analysis["top_terms"].head(5).to_dict(),
"party_breakdown": analysis["party_breakdown"].to_dict(),
"yoy_growth": analysis["yoy_growth"],
}
with open(f"{OUTPUT_DIR}/analysis_summary.json", "w") as f:
json.dump(summary, f, indent=2, default=str)
print(f" Saved: {OUTPUT_DIR}/hansard_mentions.csv")
print(f" Saved: {OUTPUT_DIR}/analysis_summary.json")
# ── Main ───────────────────────────────────────────────────────────────────────
if __name__ == "__main__":
print("Parliamentary AI Coverage Tracker")
print("=" * 40)
df = collect_all_mentions(AI_TERMS, START_DATE, END_DATE)
analysis = analyse_trends(df)
build_dashboard(df, analysis)
export_outputs(df, analysis)
print("\nTracking complete.")