Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,157 +23,15 @@ except Exception as e:
|
|
| 23 |
model = None
|
| 24 |
tokenizer = None
|
| 25 |
|
| 26 |
-
# --- Data for the Reels ---
|
| 27 |
FOI_COMBINATIONS = [
|
| 28 |
{"authority": "Borders NHS Board", "keywords": "whistleblowing guidance, wrongdoing, public body"},
|
| 29 |
{"authority": "Borders NHS Board", "keywords": "ethical support, clinical triage, minutes"},
|
| 30 |
{"authority": "Buckinghamshire Fire and Rescue Service", "keywords": "fire service, fire officers, membership"},
|
| 31 |
-
{"authority": "Ballymoney Borough Council", "keywords": "parking enforcement, employee responsibility, parking services"},
|
| 32 |
-
{"authority": "British Council", "keywords": "British Council funding, festival, correspondence"},
|
| 33 |
-
{"authority": "Cardiff and Vale University Health Board", "keywords": "drugs, therapeutic, meetings"},
|
| 34 |
-
{"authority": "Hastings Borough Council", "keywords": "local authority care, protection orders, adopted children"},
|
| 35 |
-
{"authority": "Sevenoaks District Council", "keywords": "residential property, council tax, postcode"},
|
| 36 |
-
{"authority": "The Marine Society and Sea Cadets", "keywords": "training, sea cadets, years in office"},
|
| 37 |
-
{"authority": "West Yorkshire Police Authority", "keywords": "professional standards, official duties, chief constable"},
|
| 38 |
-
{"authority": "Royal College of Veterinary Surgeons", "keywords": "veterinary practices, veterinary hospitals, contact details"},
|
| 39 |
-
{"authority": "South Tyneside Metropolitan Borough Council", "keywords": "commercial customers, waste collection, waste producers"},
|
| 40 |
-
{"authority": "University of Surrey", "keywords": "university car parks, parking tickets, parking enforcement"},
|
| 41 |
-
{"authority": "King's College Hospital NHS Trust", "keywords": "annual spend, medical equipment, procurement"},
|
| 42 |
-
{"authority": "Great Ormond Street Hospital", "keywords": "charity donations, research funding, financial year"},
|
| 43 |
-
{"authority": "Public Health England", "keywords": "vaccination costs, public health campaigns, effectiveness"},
|
| 44 |
-
{"authority": "NHS Digital", "keywords": "IT spending, system outages, cyber security"},
|
| 45 |
-
{"authority": "Cornwall Council", "keywords": "adult social care, spending, assessments"},
|
| 46 |
-
{"authority": "Hertfordshire County Council", "keywords": "school transport, route costs, contractor payments"},
|
| 47 |
-
{"authority": "Wiltshire Council", "keywords": "highway maintenance, pothole repairs, annual spend"},
|
| 48 |
-
{"authority": "Surrey County Council", "keywords": "council tax, collection rates, arrears"},
|
| 49 |
-
{"authority": "Ofgem", "keywords": "energy company, penalties, investigations"},
|
| 50 |
-
{"authority": "Financial Conduct Authority", "keywords": "enforcement action, fines imposed, complaint statistics"},
|
| 51 |
-
{"authority": "Competition and Markets Authority", "keywords": "merger investigations, market studies, legal costs"},
|
| 52 |
-
{"authority": "Health and Safety Executive", "keywords": "workplace accidents, enforcement notices, prosecution costs"},
|
| 53 |
-
{"authority": "Network Rail", "keywords": "delay compensation, signal failures, maintenance costs"},
|
| 54 |
-
{"authority": "Highways England", "keywords": "road maintenance, contractor spending, traffic flow"},
|
| 55 |
-
{"authority": "Civil Aviation Authority", "keywords": "airline complaints, enforcement action, safety investigations"},
|
| 56 |
-
{"authority": "HM Revenue and Customs", "keywords": "tax investigations, recovery rates, compliance costs"},
|
| 57 |
-
{"authority": "Department for Work and Pensions", "keywords": "benefit sanctions, appeal outcomes, administrative costs"},
|
| 58 |
-
{"authority": "Nottinghamshire County Council", "keywords": "adult social care, budget allocation, waiting lists"},
|
| 59 |
-
{"authority": "Staffordshire County Council", "keywords": "school meals, contract costs, nutritional standards"},
|
| 60 |
-
{"authority": "Derbyshire County Council", "keywords": "library services, closure consultations, usage statistics"},
|
| 61 |
-
{"authority": "East Sussex County Council", "keywords": "children services, safeguarding, staffing costs"},
|
| 62 |
-
{"authority": "Northumberland County Council", "keywords": "waste collection, recycling rates, contractor performance"},
|
| 63 |
-
{"authority": "Royal Devon NHS Trust", "keywords": "patient complaints, response times, resolution outcomes"},
|
| 64 |
-
{"authority": "Sheffield Teaching Hospitals", "keywords": "medical equipment, procurement costs, maintenance contracts"},
|
| 65 |
-
{"authority": "Bradford Teaching Hospitals", "keywords": "staff training, external courses, annual expenditure"},
|
| 66 |
-
{"authority": "Northampton General Hospital", "keywords": "parking charges, revenue generated, patient feedback"},
|
| 67 |
-
{"authority": "Environment Agency", "keywords": "flood defenses, maintenance costs, effectiveness assessments"},
|
| 68 |
-
{"authority": "Natural England", "keywords": "habitat protection, designation costs, landowner compensation"},
|
| 69 |
-
{"authority": "Marine Management Organisation", "keywords": "fishing licenses, enforcement costs, violation penalties"},
|
| 70 |
-
{"authority": "Forestry Commission", "keywords": "timber sales, revenue targets, environmental impact"},
|
| 71 |
-
{"authority": "Welsh Government", "keywords": "language services, translation costs, usage statistics"},
|
| 72 |
-
{"authority": "Scottish Government", "keywords": "ferry subsidies, route profitability, passenger numbers"},
|
| 73 |
-
{"authority": "Northern Ireland Executive", "keywords": "cross-border initiatives, funding allocation, outcomes"},
|
| 74 |
-
{"authority": "Office for National Statistics", "keywords": "census costs, data collection, contractor payments"},
|
| 75 |
-
{"authority": "Land Registry", "keywords": "property registrations, processing times, fee income"},
|
| 76 |
-
{"authority": "Companies House", "keywords": "late filing penalties, collection rates, enforcement action"},
|
| 77 |
-
{"authority": "Intellectual Property Office", "keywords": "patent applications, processing costs, appeal outcomes"},
|
| 78 |
-
{"authority": "InGen Safety Authority", "keywords": "containment breaches, insurance costs, visitor incidents"},
|
| 79 |
-
{"authority": "Natural England", "keywords": "Bigfoot sightings, habitat, evidence"},
|
| 80 |
-
{"authority": "The Royal Parks", "keywords": "squirrel uprising, acorn stockpiles, intelligence"},
|
| 81 |
-
{"authority": "Midsomer Constabulary", "keywords": "unusually high murder rate, statistics, investigations"},
|
| 82 |
-
{"authority": "Scottish Government", "keywords": "Loch Ness Monster, research, funding"},
|
| 83 |
-
{"authority": "Westminster City Council", "keywords": "noise complaints, nightlife, enforcement"},
|
| 84 |
-
{"authority": "Network Rail", "keywords": "delays, compensation, signal failures"},
|
| 85 |
-
{"authority": "Gloucestershire County Council", "keywords": "adult social care, assessment costs, service provision"},
|
| 86 |
-
{"authority": "Worcestershire County Council", "keywords": "school transport, route planning, contractor performance"},
|
| 87 |
-
{"authority": "Leicestershire County Council", "keywords": "highway maintenance, annual spend, emergency repairs"},
|
| 88 |
-
{"authority": "Warwickshire County Council", "keywords": "library services, usage statistics, closure consultations"},
|
| 89 |
-
{"authority": "Nottinghamshire County Council", "keywords": "children services, safeguarding referrals, staff costs"},
|
| 90 |
-
{"authority": "Derbyshire County Council", "keywords": "waste disposal, recycling rates, collection contracts"},
|
| 91 |
-
{"authority": "Staffordshire County Council", "keywords": "care homes, inspection reports, quality ratings"},
|
| 92 |
-
{"authority": "Shropshire Council", "keywords": "planning applications, approval rates, enforcement action"},
|
| 93 |
-
{"authority": "Herefordshire Council", "keywords": "council tax, collection rates, arrears management"},
|
| 94 |
-
{"authority": "Cheshire East Council", "keywords": "parking enforcement, penalty notices, appeal outcomes"},
|
| 95 |
-
{"authority": "Royal Liverpool University Hospital", "keywords": "patient complaints, response times, resolution outcomes"},
|
| 96 |
-
{"authority": "Sheffield Teaching Hospitals NHS Trust", "keywords": "medical equipment, procurement costs, maintenance contracts"},
|
| 97 |
-
{"authority": "Leeds Teaching Hospitals NHS Trust", "keywords": "agency staff, temporary costs, recruitment spend"},
|
| 98 |
-
{"authority": "Newcastle upon Tyne Hospitals NHS Trust", "keywords": "cancelled operations, reasons given, patient impact"},
|
| 99 |
-
{"authority": "University Hospitals Birmingham", "keywords": "waiting times, treatment delays, performance targets"},
|
| 100 |
-
{"authority": "Guy's and St Thomas' NHS Trust", "keywords": "infection control, outbreak incidents, prevention costs"},
|
| 101 |
-
{"authority": "King's College Hospital NHS Trust", "keywords": "emergency admissions, capacity management, staffing levels"},
|
| 102 |
-
{"authority": "Royal Free NHS Trust", "keywords": "clinical trials, research funding, patient consent"},
|
| 103 |
-
{"authority": "Imperial College Healthcare NHS Trust", "keywords": "private patient, income generated, capacity allocation"},
|
| 104 |
-
{"authority": "Barts Health NHS Trust", "keywords": "financial deficit, recovery plans, cost reduction"},
|
| 105 |
-
{"authority": "West Yorkshire Police", "keywords": "response times, emergency calls, performance standards"},
|
| 106 |
-
{"authority": "South Yorkshire Police", "keywords": "crime statistics, detection rates, resource allocation"},
|
| 107 |
-
{"authority": "North Yorkshire Police", "keywords": "rural policing, coverage areas, response costs"},
|
| 108 |
-
{"authority": "Humberside Police", "keywords": "traffic enforcement, speed cameras, revenue generated"},
|
| 109 |
-
{"authority": "Lincolnshire Police", "keywords": "domestic violence, incident reports, support services"},
|
| 110 |
-
{"authority": "Nottinghamshire Police", "keywords": "stop and search, statistics recorded, outcome data"},
|
| 111 |
-
{"authority": "Derbyshire Police", "keywords": "drug seizures, investigation costs, conviction rates"},
|
| 112 |
-
{"authority": "Leicestershire Police", "keywords": "neighbourhood policing, community engagement, effectiveness measures"},
|
| 113 |
-
{"authority": "Warwickshire Police", "keywords": "police stations, closure plans, public consultations"},
|
| 114 |
-
{"authority": "West Mercia Police", "keywords": "cybercrime, investigation resources, training costs"},
|
| 115 |
-
{"authority": "Environment Agency", "keywords": "flood defenses, maintenance spending, effectiveness assessments"},
|
| 116 |
-
{"authority": "Natural England", "keywords": "habitat designations, compensation payments, landowner agreements"},
|
| 117 |
-
{"authority": "Historic England", "keywords": "listed buildings, consent applications, enforcement cases"},
|
| 118 |
-
{"authority": "Highways England", "keywords": "motorway maintenance, contractor payments, performance indicators"},
|
| 119 |
-
{"authority": "Network Rail", "keywords": "signal failures, delay minutes, compensation costs"},
|
| 120 |
-
{"authority": "Civil Aviation Authority", "keywords": "airline complaints, resolution times, enforcement action"},
|
| 121 |
-
{"authority": "Maritime and Coastguard Agency", "keywords": "rescue operations, helicopter costs, response times"},
|
| 122 |
-
{"authority": "Driver and Vehicle Standards Agency", "keywords": "MOT failures, test center, compliance rates"},
|
| 123 |
-
{"authority": "Vehicle Certification Agency", "keywords": "emissions testing, approval procedures, manufacturer compliance"},
|
| 124 |
-
{"authority": "Planning Inspectorate", "keywords": "appeal decisions, processing times, outcome statistics"},
|
| 125 |
-
{"authority": "Health and Safety Executive", "keywords": "workplace accidents, investigation costs, prosecution outcomes"},
|
| 126 |
-
{"authority": "Care Quality Commission", "keywords": "inspection reports, rating changes, enforcement action"},
|
| 127 |
-
{"authority": "Ofgem", "keywords": "energy company, penalties imposed, consumer complaints"},
|
| 128 |
-
{"authority": "Ofwat", "keywords": "water companies, price reviews, performance monitoring"},
|
| 129 |
-
{"authority": "Ofcom", "keywords": "broadcasting complaints, investigation procedures, penalty decisions"},
|
| 130 |
-
{"authority": "Financial Conduct Authority", "keywords": "financial services, enforcement cases, penalty amounts"},
|
| 131 |
-
{"authority": "Competition and Markets Authority", "keywords": "merger investigations, market studies, intervention costs"},
|
| 132 |
-
{"authority": "Gambling Commission", "keywords": "license suspensions, operator penalties, compliance monitoring"},
|
| 133 |
-
{"authority": "Information Commissioner's Office", "keywords": "data breaches, penalty notices, investigation outcomes"},
|
| 134 |
-
{"authority": "Electoral Commission", "keywords": "campaign spending, compliance checks, penalty procedures"},
|
| 135 |
-
{"authority": "HM Revenue and Customs", "keywords": "tax investigations, recovery amounts, compliance costs"},
|
| 136 |
-
{"authority": "HM Treasury", "keywords": "departmental budgets, spending reviews, efficiency targets"},
|
| 137 |
-
{"authority": "Cabinet Office", "keywords": "government consultancy, external advisors, procurement costs"},
|
| 138 |
-
{"authority": "Ministry of Justice", "keywords": "court delays, case backlogs, administrative costs"},
|
| 139 |
-
{"authority": "Home Office", "keywords": "immigration appeals, processing times, detention costs"},
|
| 140 |
-
{"authority": "Department for Transport", "keywords": "railway subsidies, franchise performance, punctuality targets"},
|
| 141 |
-
{"authority": "Department for Education", "keywords": "academy conversions, funding allocations, performance data"},
|
| 142 |
-
{"authority": "Department for Work and Pensions", "keywords": "benefit sanctions, appeal success, administrative costs"},
|
| 143 |
-
{"authority": "Department of Health", "keywords": "NHS funding, allocation formulas, performance targets"},
|
| 144 |
-
{"authority": "Department for Environment", "keywords": "environmental fines, pollution incidents, enforcement costs"},
|
| 145 |
-
{"authority": "London Fire Brigade", "keywords": "response times, equipment costs, staffing levels"},
|
| 146 |
-
{"authority": "West Midlands Fire Service", "keywords": "false alarms, call reduction, prevention costs"},
|
| 147 |
-
{"authority": "Greater Manchester Fire Service", "keywords": "building inspections, safety compliance, enforcement action"},
|
| 148 |
-
{"authority": "South Yorkshire Fire Service", "keywords": "emergency calls, resource deployment, performance metrics"},
|
| 149 |
-
{"authority": "Merseyside Fire Service", "keywords": "community safety, education programs, effectiveness measures"},
|
| 150 |
-
{"authority": "Essex Fire Service", "keywords": "rescue operations, specialist equipment, training costs"},
|
| 151 |
-
{"authority": "Kent Fire Service", "keywords": "station closures, service changes, public consultation"},
|
| 152 |
-
{"authority": "Surrey Fire Service", "keywords": "road accidents, rescue costs, casualty statistics"},
|
| 153 |
-
{"authority": "Hampshire Fire Service", "keywords": "wildfire incidents, prevention measures, resource allocation"},
|
| 154 |
-
{"authority": "Devon Fire Service", "keywords": "coastal rescues, equipment maintenance, operational costs"},
|
| 155 |
-
{"authority": "London Ambulance Service", "keywords": "response targets, performance data, resource pressures"},
|
| 156 |
-
{"authority": "West Midlands Ambulance", "keywords": "call volumes, crew availability, overtime costs"},
|
| 157 |
-
{"authority": "Yorkshire Ambulance Service", "keywords": "hospital handovers, delay times, capacity issues"},
|
| 158 |
-
{"authority": "North West Ambulance Service", "keywords": "emergency calls, triage procedures, response priorities"},
|
| 159 |
-
{"authority": "South Central Ambulance", "keywords": "patient transport, contract costs, service quality"},
|
| 160 |
-
{"authority": "East Midlands Ambulance", "keywords": "staff training, competency assessments, certification costs"},
|
| 161 |
-
{"authority": "South East Coast Ambulance", "keywords": "vehicle maintenance, fleet costs, replacement schedules"},
|
| 162 |
-
{"authority": "East of England Ambulance", "keywords": "clinical governance, audit results, quality improvements"},
|
| 163 |
-
{"authority": "South Western Ambulance", "keywords": "rural coverage, response challenges, resource deployment"},
|
| 164 |
-
{"authority": "Welsh Ambulance Service", "keywords": "cross-border operations, coordination costs, service agreements"},
|
| 165 |
-
{"authority": "Bristol City Council", "keywords": "cycling infrastructure, usage data, maintenance costs"},
|
| 166 |
-
{"authority": "Brighton and Hove Council", "keywords": "seafront management, event licensing, revenue generation"},
|
| 167 |
-
{"authority": "Cambridge City Council", "keywords": "student housing, planning enforcement, compliance monitoring"},
|
| 168 |
-
{"authority": "Canterbury City Council", "keywords": "heritage sites, conservation costs, visitor management"},
|
| 169 |
-
{"authority": "Chester West Council", "keywords": "tourism promotion, marketing spend, economic impact"},
|
| 170 |
-
{"authority": "Colchester Borough Council", "keywords": "business rates, collection performance, appeals process"},
|
| 171 |
-
{"authority": "Durham County Council", "keywords": "mining subsidence, compensation claims, repair costs"},
|
| 172 |
-
{"authority": "Exeter City Council", "keywords": "flood management, defense systems, emergency planning"},
|
| 173 |
-
{"authority": "Gloucester City Council", "keywords": "regeneration projects, funding sources, completion rates"},
|
| 174 |
{"authority": "Lancaster City Council", "keywords": "coastal erosion, protection measures, maintenance spending"},
|
| 175 |
]
|
| 176 |
|
|
|
|
| 177 |
ALL_AUTHORITIES_FOR_SPIN = list(set([item["authority"] for item in FOI_COMBINATIONS]))
|
| 178 |
ALL_KEYWORDS_FOR_SPIN = list(set(kw.strip() for item in FOI_COMBINATIONS for kw in item["keywords"].split(',')))
|
| 179 |
|
|
@@ -188,22 +46,50 @@ def wrap_in_letter(authority: str, body: str) -> str:
|
|
| 188 |
)
|
| 189 |
return template
|
| 190 |
|
| 191 |
-
# ---
|
|
|
|
| 192 |
@spaces.GPU
|
| 193 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
if not model or not tokenizer:
|
| 195 |
-
|
|
|
|
| 196 |
|
| 197 |
keywords = [kw for kw in [kw1, kw2, kw3] if kw]
|
| 198 |
keyword_string = ", ".join(keywords)
|
| 199 |
prompt = (
|
| 200 |
"You are an expert at writing formal Freedom of Information requests to UK public authorities. "
|
| 201 |
-
f"Generate the request text (without greeting or signature) for {
|
| 202 |
)
|
| 203 |
|
| 204 |
try:
|
| 205 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 206 |
-
|
| 207 |
generation_params = {
|
| 208 |
"max_new_tokens": 250,
|
| 209 |
"do_sample": True,
|
|
@@ -213,9 +99,7 @@ def generate_request_local(authority, kw1, kw2, kw3):
|
|
| 213 |
"repetition_penalty": 1.1,
|
| 214 |
"eos_token_id": tokenizer.eos_token_id
|
| 215 |
}
|
| 216 |
-
|
| 217 |
output_sequences = model.generate(**inputs, **generation_params)
|
| 218 |
-
|
| 219 |
generated_text = tokenizer.decode(
|
| 220 |
output_sequences[0][len(inputs["input_ids"][0]):],
|
| 221 |
skip_special_tokens=True
|
|
@@ -224,43 +108,17 @@ def generate_request_local(authority, kw1, kw2, kw3):
|
|
| 224 |
if generated_text.startswith('.\n'):
|
| 225 |
generated_text = generated_text[2:]
|
| 226 |
|
| 227 |
-
|
| 228 |
|
| 229 |
except Exception as e:
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
# --- Gradio UI and Spinning Logic ---
|
| 233 |
-
def spin_the_reels():
|
| 234 |
-
spin_duration = 2.0
|
| 235 |
-
spin_interval = 0.05
|
| 236 |
-
start_time = time.time()
|
| 237 |
-
while time.time() - start_time < spin_duration:
|
| 238 |
-
yield (
|
| 239 |
-
random.choice(ALL_AUTHORITIES_FOR_SPIN),
|
| 240 |
-
random.choice(ALL_KEYWORDS_FOR_SPIN),
|
| 241 |
-
random.choice(ALL_KEYWORDS_FOR_SPIN),
|
| 242 |
-
random.choice(ALL_KEYWORDS_FOR_SPIN),
|
| 243 |
-
"Spinning..."
|
| 244 |
-
)
|
| 245 |
-
time.sleep(spin_interval)
|
| 246 |
-
|
| 247 |
-
final_combination = random.choice(FOI_COMBINATIONS)
|
| 248 |
-
final_authority = final_combination["authority"]
|
| 249 |
-
|
| 250 |
-
keywords_list = [k.strip() for k in final_combination["keywords"].split(',')]
|
| 251 |
-
keywords_list += [''] * (3 - len(keywords_list))
|
| 252 |
-
kw1, kw2, kw3 = keywords_list[:3]
|
| 253 |
|
|
|
|
| 254 |
yield (
|
| 255 |
final_authority, kw1, kw2, kw3,
|
| 256 |
-
|
| 257 |
)
|
| 258 |
|
| 259 |
-
generated_request = generate_request_local(final_authority, kw1, kw2, kw3)
|
| 260 |
-
yield (
|
| 261 |
-
final_authority, kw1, kw2, kw3,
|
| 262 |
-
generated_request
|
| 263 |
-
)
|
| 264 |
|
| 265 |
# --- CSS for Styling ---
|
| 266 |
reels_css = """
|
|
@@ -320,10 +178,10 @@ with gr.Blocks(css=reels_css, theme=gr.themes.Soft()) as demo:
|
|
| 320 |
)
|
| 321 |
|
| 322 |
pull_button.click(
|
| 323 |
-
fn=
|
| 324 |
inputs=[],
|
| 325 |
outputs=[reel1, reel2, reel3, reel4, output_request]
|
| 326 |
)
|
| 327 |
|
| 328 |
if __name__ == "__main__":
|
| 329 |
-
demo.launch()
|
|
|
|
| 23 |
model = None
|
| 24 |
tokenizer = None
|
| 25 |
|
| 26 |
+
# --- Data for the Reels (Truncated for brevity) ---
|
| 27 |
FOI_COMBINATIONS = [
|
| 28 |
{"authority": "Borders NHS Board", "keywords": "whistleblowing guidance, wrongdoing, public body"},
|
| 29 |
{"authority": "Borders NHS Board", "keywords": "ethical support, clinical triage, minutes"},
|
| 30 |
{"authority": "Buckinghamshire Fire and Rescue Service", "keywords": "fire service, fire officers, membership"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
{"authority": "Lancaster City Council", "keywords": "coastal erosion, protection measures, maintenance spending"},
|
| 32 |
]
|
| 33 |
|
| 34 |
+
|
| 35 |
ALL_AUTHORITIES_FOR_SPIN = list(set([item["authority"] for item in FOI_COMBINATIONS]))
|
| 36 |
ALL_KEYWORDS_FOR_SPIN = list(set(kw.strip() for item in FOI_COMBINATIONS for kw in item["keywords"].split(',')))
|
| 37 |
|
|
|
|
| 46 |
)
|
| 47 |
return template
|
| 48 |
|
| 49 |
+
# --- Gradio UI and Spinning Logic ---
|
| 50 |
+
# --- Main Backend Function for Gradio ---
|
| 51 |
@spaces.GPU
|
| 52 |
+
def spin_and_generate():
|
| 53 |
+
# 1. Spin the reels for visual effect
|
| 54 |
+
spin_duration = 2.0
|
| 55 |
+
spin_interval = 0.05
|
| 56 |
+
start_time = time.time()
|
| 57 |
+
while time.time() - start_time < spin_duration:
|
| 58 |
+
yield (
|
| 59 |
+
random.choice(ALL_AUTHORITIES_FOR_SPIN),
|
| 60 |
+
random.choice(ALL_KEYWORDS_FOR_SPIN),
|
| 61 |
+
random.choice(ALL_KEYWORDS_FOR_SPIN),
|
| 62 |
+
random.choice(ALL_KEYWORDS_FOR_SPIN),
|
| 63 |
+
"Spinning..."
|
| 64 |
+
)
|
| 65 |
+
time.sleep(spin_interval)
|
| 66 |
+
|
| 67 |
+
# 2. Select the final combination and update UI
|
| 68 |
+
final_combination = random.choice(FOI_COMBINATIONS)
|
| 69 |
+
final_authority = final_combination["authority"]
|
| 70 |
+
keywords_list = [k.strip() for k in final_combination["keywords"].split(',')]
|
| 71 |
+
keywords_list += [''] * (3 - len(keywords_list))
|
| 72 |
+
kw1, kw2, kw3 = keywords_list[:3]
|
| 73 |
+
|
| 74 |
+
yield (
|
| 75 |
+
final_authority, kw1, kw2, kw3,
|
| 76 |
+
f"Generating request for {final_authority}...\nPlease wait, this may take a moment."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# 3. Generate the request using the model
|
| 80 |
if not model or not tokenizer:
|
| 81 |
+
yield (final_authority, kw1, kw2, kw3, "Error: Model is not loaded. Check Space logs.")
|
| 82 |
+
return
|
| 83 |
|
| 84 |
keywords = [kw for kw in [kw1, kw2, kw3] if kw]
|
| 85 |
keyword_string = ", ".join(keywords)
|
| 86 |
prompt = (
|
| 87 |
"You are an expert at writing formal Freedom of Information requests to UK public authorities. "
|
| 88 |
+
f"Generate the request text (without greeting or signature) for {final_authority}, using these keywords: {keyword_string}."
|
| 89 |
)
|
| 90 |
|
| 91 |
try:
|
| 92 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
| 93 |
generation_params = {
|
| 94 |
"max_new_tokens": 250,
|
| 95 |
"do_sample": True,
|
|
|
|
| 99 |
"repetition_penalty": 1.1,
|
| 100 |
"eos_token_id": tokenizer.eos_token_id
|
| 101 |
}
|
|
|
|
| 102 |
output_sequences = model.generate(**inputs, **generation_params)
|
|
|
|
| 103 |
generated_text = tokenizer.decode(
|
| 104 |
output_sequences[0][len(inputs["input_ids"][0]):],
|
| 105 |
skip_special_tokens=True
|
|
|
|
| 108 |
if generated_text.startswith('.\n'):
|
| 109 |
generated_text = generated_text[2:]
|
| 110 |
|
| 111 |
+
final_request = wrap_in_letter(final_authority, generated_text)
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
+
final_request = f"An error occurred during text generation: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
# 4. Yield the final result to the UI
|
| 117 |
yield (
|
| 118 |
final_authority, kw1, kw2, kw3,
|
| 119 |
+
final_request
|
| 120 |
)
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# --- CSS for Styling ---
|
| 124 |
reels_css = """
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
pull_button.click(
|
| 181 |
+
fn=spin_and_generate, # Changed to the new combined function
|
| 182 |
inputs=[],
|
| 183 |
outputs=[reel1, reel2, reel3, reel4, output_request]
|
| 184 |
)
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
+
demo.launch()
|