HMC83 commited on
Commit
67dd194
·
verified ·
1 Parent(s): eb841ad

raw output version for testing

Browse files
Files changed (1) hide show
  1. app.py +48 -165
app.py CHANGED
@@ -3,11 +3,10 @@ import os
3
  import random
4
  import time
5
  import torch
6
- import re
7
  from transformers import AutoTokenizer, AutoModelForCausalLM
8
  import spaces
9
 
10
- MODEL_ID = "HMC83/Wightgar-650M-RequestWriter-DPO"
11
 
12
  # --- Load Model and Tokenizer ---
13
  print("Loading model and tokenizer...")
@@ -27,43 +26,34 @@ except Exception as e:
27
  # --- Data for the Reels ---
28
  # A list of authority and keyword combinations.
29
  FOI_COMBINATIONS = [
30
- {"authority": "Borders NHS Board", "keywords": "ethical support, clinical triage, minutes"},
31
- {"authority": "British Council", "keywords": "British Council funding, festival, correspondence"},
32
- {"authority": "Cardiff and Vale University Health Board", "keywords": "drugs, therapeutic, meetings"},
33
- {"authority": "Sevenoaks District Council", "keywords": "residential property, council tax, postcode"},
34
- {"authority": "The Marine Society and Sea Cadets", "keywords": "training, sea cadets, years in office"},
35
- {"authority": "West Yorkshire Police Authority", "keywords": "professional standards, official duties, chief constable"},
36
  {"authority": "South Tyneside Metropolitan Borough Council", "keywords": "commercial customers, waste collection, waste producers"},
37
  {"authority": "University of Surrey", "keywords": "university car parks, parking tickets, parking enforcement"},
38
- {"authority": "King's College Hospital NHS Trust", "keywords": "annual spend, medical equipment, procurement"},
39
  {"authority": "Great Ormond Street Hospital", "keywords": "charity donations, research funding, financial year"},
40
  {"authority": "Public Health England", "keywords": "vaccination costs, public health campaigns, effectiveness"},
41
  {"authority": "NHS Digital", "keywords": "IT spending, system outages, cyber security"},
42
  {"authority": "Cornwall Council", "keywords": "adult social care, spending, assessments"},
43
  {"authority": "Hertfordshire County Council", "keywords": "school transport, route costs, contractor payments"},
44
  {"authority": "Wiltshire Council", "keywords": "highway maintenance, pothole repairs, annual spend"},
45
- {"authority": "Surrey County Council", "keywords": "council tax, collection rates, arrears"},
46
- {"authority": "Ofgem", "keywords": "energy company, penalties, investigations"},
47
  {"authority": "Financial Conduct Authority", "keywords": "enforcement action, fines imposed, complaint statistics"},
48
  {"authority": "Competition and Markets Authority", "keywords": "merger investigations, market studies, legal costs"},
49
  {"authority": "Health and Safety Executive", "keywords": "workplace accidents, enforcement notices, prosecution costs"},
50
- {"authority": "Network Rail", "keywords": "delay compensation, signal failures, maintenance costs"},
51
- {"authority": "Highways England", "keywords": "road maintenance, contractor spending, traffic flow"},
52
  {"authority": "Civil Aviation Authority", "keywords": "airline complaints, enforcement action, safety investigations"},
53
  {"authority": "HM Revenue and Customs", "keywords": "tax investigations, recovery rates, compliance costs"},
54
  {"authority": "Department for Work and Pensions", "keywords": "benefit sanctions, appeal outcomes, administrative costs"},
55
  {"authority": "Nottinghamshire County Council", "keywords": "adult social care, budget allocation, waiting lists"},
56
  {"authority": "Staffordshire County Council", "keywords": "school meals, contract costs, nutritional standards"},
 
 
57
  {"authority": "Northumberland County Council", "keywords": "waste collection, recycling rates, contractor performance"},
58
- {"authority": "Royal Devon NHS Trust", "keywords": "patient complaints, response times, resolution outcomes"},
59
- {"authority": "Sheffield Teaching Hospitals", "keywords": "medical equipment, procurement costs, maintenance contracts"},
60
- {"authority": "Bradford Teaching Hospitals", "keywords": "staff training, external courses, annual expenditure"},
61
- {"authority": "Northampton General Hospital", "keywords": "parking charges, revenue generated, patient feedback"},
62
  {"authority": "Environment Agency", "keywords": "flood defenses, maintenance costs, effectiveness assessments"},
 
63
  {"authority": "Marine Management Organisation", "keywords": "fishing licenses, enforcement costs, violation penalties"},
64
  {"authority": "Forestry Commission", "keywords": "timber sales, revenue targets, environmental impact"},
65
  {"authority": "Welsh Government", "keywords": "language services, translation costs, usage statistics"},
66
  {"authority": "Scottish Government", "keywords": "ferry subsidies, route profitability, passenger numbers"},
 
 
 
67
  {"authority": "Companies House", "keywords": "late filing penalties, collection rates, enforcement action"},
68
  {"authority": "Intellectual Property Office", "keywords": "patent applications, processing costs, appeal outcomes"},
69
  {"authority": "InGen Safety Authority", "keywords": "containment breaches, insurance costs, visitor incidents"},
@@ -76,122 +66,42 @@ FOI_COMBINATIONS = [
76
  {"authority": "Worcestershire County Council", "keywords": "school transport, route planning, contractor performance"},
77
  {"authority": "Leicestershire County Council", "keywords": "highway maintenance, annual spend, emergency repairs"},
78
  {"authority": "Warwickshire County Council", "keywords": "library services, usage statistics, closure consultations"},
 
79
  {"authority": "Derbyshire County Council", "keywords": "waste disposal, recycling rates, collection contracts"},
80
  {"authority": "Staffordshire County Council", "keywords": "care homes, inspection reports, quality ratings"},
81
  {"authority": "Shropshire Council", "keywords": "planning applications, approval rates, enforcement action"},
 
82
  {"authority": "Cheshire East Council", "keywords": "parking enforcement, penalty notices, appeal outcomes"},
83
  {"authority": "Royal Liverpool University Hospital", "keywords": "patient complaints, response times, resolution outcomes"},
 
84
  {"authority": "Leeds Teaching Hospitals NHS Trust", "keywords": "agency staff, temporary costs, recruitment spend"},
85
  {"authority": "Newcastle upon Tyne Hospitals NHS Trust", "keywords": "cancelled operations, reasons given, patient impact"},
86
  {"authority": "University Hospitals Birmingham", "keywords": "waiting times, treatment delays, performance targets"},
87
  {"authority": "Guy's and St Thomas' NHS Trust", "keywords": "infection control, outbreak incidents, prevention costs"},
88
  {"authority": "King's College Hospital NHS Trust", "keywords": "emergency admissions, capacity management, staffing levels"},
89
  {"authority": "Royal Free NHS Trust", "keywords": "clinical trials, research funding, patient consent"},
 
90
  {"authority": "Barts Health NHS Trust", "keywords": "financial deficit, recovery plans, cost reduction"},
91
  {"authority": "West Yorkshire Police", "keywords": "response times, emergency calls, performance standards"},
92
  {"authority": "South Yorkshire Police", "keywords": "crime statistics, detection rates, resource allocation"},
93
  {"authority": "North Yorkshire Police", "keywords": "rural policing, coverage areas, response costs"},
94
  {"authority": "Humberside Police", "keywords": "traffic enforcement, speed cameras, revenue generated"},
 
95
  {"authority": "Nottinghamshire Police", "keywords": "stop and search, statistics recorded, outcome data"},
96
  {"authority": "Derbyshire Police", "keywords": "drug seizures, investigation costs, conviction rates"},
 
97
  {"authority": "Warwickshire Police", "keywords": "police stations, closure plans, public consultations"},
98
  {"authority": "West Mercia Police", "keywords": "cybercrime, investigation resources, training costs"},
99
- {"authority": "Environment Agency", "keywords": "flood defenses, maintenance spending, effectiveness assessments"},
100
- {"authority": "Natural England", "keywords": "habitat designations, compensation payments, landowner agreements"},
101
- {"authority": "Historic England", "keywords": "listed buildings, consent applications, enforcement cases"},
102
- {"authority": "Highways England", "keywords": "motorway maintenance, contractor payments, performance indicators"},
103
- {"authority": "Network Rail", "keywords": "signal failures, delay minutes, compensation costs"},
104
- {"authority": "Civil Aviation Authority", "keywords": "airline complaints, resolution times, enforcement action"},
105
- {"authority": "Maritime and Coastguard Agency", "keywords": "rescue operations, helicopter costs, response times"},
106
- {"authority": "Driver and Vehicle Standards Agency", "keywords": "MOT failures, test center, compliance rates"},
107
- {"authority": "Planning Inspectorate", "keywords": "appeal decisions, processing times, outcome statistics"},
108
- {"authority": "Health and Safety Executive", "keywords": "workplace accidents, investigation costs, prosecution outcomes"},
109
- {"authority": "Care Quality Commission", "keywords": "inspection reports, rating changes, enforcement action"},
110
- {"authority": "Ofwat", "keywords": "water companies, price reviews, performance monitoring"},
111
- {"authority": "Ofcom", "keywords": "broadcasting complaints, investigation procedures, penalty decisions"},
112
- {"authority": "Competition and Markets Authority", "keywords": "merger investigations, market studies, intervention costs"},
113
- {"authority": "Gambling Commission", "keywords": "license suspensions, operator penalties, compliance monitoring"},
114
- {"authority": "Information Commissioner's Office", "keywords": "data breaches, penalty notices, investigation outcomes"},
115
- {"authority": "Electoral Commission", "keywords": "campaign spending, compliance checks, penalty procedures"},
116
- {"authority": "HM Revenue and Customs", "keywords": "tax investigations, recovery amounts, compliance costs"},
117
- {"authority": "HM Treasury", "keywords": "departmental budgets, spending reviews, efficiency targets"},
118
- {"authority": "Cabinet Office", "keywords": "government consultancy, external advisors, procurement costs"},
119
- {"authority": "Ministry of Justice", "keywords": "court delays, case backlogs, administrative costs"},
120
- {"authority": "Home Office", "keywords": "immigration appeals, processing times, detention costs"},
121
- {"authority": "Department for Transport", "keywords": "railway subsidies, franchise performance, punctuality targets"},
122
- {"authority": "Department for Education", "keywords": "academy conversions, funding allocations, performance data"},
123
- {"authority": "Department for Work and Pensions", "keywords": "benefit sanctions, appeal success, administrative costs"},
124
- {"authority": "Department of Health", "keywords": "NHS funding, allocation formulas, performance targets"},
125
- {"authority": "Department for Environment", "keywords": "environmental fines, pollution incidents, enforcement costs"},
126
- {"authority": "London Fire Brigade", "keywords": "response times, equipment costs, staffing levels"},
127
- {"authority": "West Midlands Fire Service", "keywords": "false alarms, call reduction, prevention costs"},
128
- {"authority": "Greater Manchester Fire Service", "keywords": "building inspections, safety compliance, enforcement action"},
129
- {"authority": "South Yorkshire Fire Service", "keywords": "emergency calls, resource deployment, performance metrics"},
130
- {"authority": "Merseyside Fire Service", "keywords": "community safety, education programs, effectiveness measures"},
131
- {"authority": "Essex Fire Service", "keywords": "rescue operations, specialist equipment, training costs"},
132
- {"authority": "Kent Fire Service", "keywords": "station closures, service changes, public consultation"},
133
- {"authority": "Surrey Fire Service", "keywords": "road accidents, rescue costs, casualty statistics"},
134
- {"authority": "Hampshire Fire Service", "keywords": "wildfire incidents, prevention measures, resource allocation"},
135
- {"authority": "Devon Fire Service", "keywords": "coastal rescues, equipment maintenance, operational costs"},
136
- {"authority": "London Ambulance Service", "keywords": "response targets, performance data, resource pressures"},
137
- {"authority": "West Midlands Ambulance", "keywords": "call volumes, crew availability, overtime costs"},
138
- {"authority": "Yorkshire Ambulance Service", "keywords": "hospital handovers, delay times, capacity issues"},
139
- {"authority": "North West Ambulance Service", "keywords": "emergency calls, triage procedures, response priorities"},
140
- {"authority": "South Central Ambulance", "keywords": "patient transport, contract costs, service quality"},
141
- {"authority": "East Midlands Ambulance", "keywords": "staff training, competency assessments, certification costs"},
142
- {"authority": "South East Coast Ambulance", "keywords": "vehicle maintenance, fleet costs, replacement schedules"},
143
- {"authority": "East of England Ambulance", "keywords": "clinical governance, audit results, quality improvements"},
144
- {"authority": "South Western Ambulance", "keywords": "rural coverage, response challenges, resource deployment"},
145
- {"authority": "Welsh Ambulance Service", "keywords": "cross-border operations, coordination costs, service agreements"},
146
- {"authority": "Bristol City Council", "keywords": "cycling infrastructure, usage data, maintenance costs"},
147
- {"authority": "Brighton and Hove Council", "keywords": "seafront management, event licensing, revenue generation"},
148
- {"authority": "Cambridge City Council", "keywords": "student housing, planning enforcement, compliance monitoring"},
149
- {"authority": "Canterbury City Council", "keywords": "heritage sites, conservation costs, visitor management"},
150
- {"authority": "Chester West Council", "keywords": "tourism promotion, marketing spend, economic impact"},
151
- {"authority": "Exeter City Council", "keywords": "flood management, defense systems, emergency planning"},
152
- {"authority": "Gloucester City Council", "keywords": "regeneration projects, funding sources, completion rates"},
153
- {"authority": "Lancaster City Council", "keywords": "coastal erosion, protection measures, maintenance spending"},
154
  ]
155
 
156
  # Create lists for the spinning animation from the combinations above
157
  ALL_AUTHORITIES_FOR_SPIN = list(set([item["authority"] for item in FOI_COMBINATIONS]))
158
  ALL_KEYWORDS_FOR_SPIN = list(set(kw.strip() for item in FOI_COMBINATIONS for kw in item["keywords"].split(',')))
159
 
160
- # --- Helper: clean model output into a numbered list starting at "1." ---
161
- def clean_and_validate_output(text: str):
162
- """
163
- Cleans the model's output. It finds the first numbered list if it exists,
164
- otherwise it takes the whole text. It also removes common signature lines.
165
- """
166
- body = text.strip()
167
-
168
- # Try to find the start of a numbered list (e.g., "1.", " 1. ")
169
- match = re.search(r'(?m)^\s*1\.\s.*', body, re.DOTALL)
170
- if match:
171
- body = match.group(0) # Keep everything from the first "1." onward
172
-
173
- # Remove common signature lines at the end (best-effort)
174
- body = re.sub(r'(?im)^\s*(yours.*|kind regards.*|regards.*|sincerely.*)$', '', body).strip()
175
-
176
- # The new logic: as long as the body isn't empty, we consider it "valid" enough to use.
177
- is_valid = len(body) > 10 # Check if there's a reasonable amount of text
178
- return body, is_valid
179
-
180
- # --- Helper: wrap content in the FOI letter template ---
181
- def wrap_in_letter(authority: str, body: str) -> str:
182
- body = body.strip()
183
- template = (
184
- f"Dear {authority}\n\n"
185
- "Please provide me with a copy of the following information:\n\n"
186
- f"{body}\n\n"
187
- "Yours faithfully,"
188
- )
189
- return template
190
-
191
  # --- Backend Function for Local Inference ---
192
  @spaces.GPU
193
  def generate_request_local(authority, kw1, kw2, kw3):
194
- """Generates a request using the locally loaded transformer model, with validation and retry logic."""
195
  if not model or not tokenizer:
196
  return "Error: Model is not loaded. Please check the Space logs for details."
197
 
@@ -199,69 +109,42 @@ def generate_request_local(authority, kw1, kw2, kw3):
199
  keyword_string = ", ".join(keywords)
200
  prompt = (
201
  "You are an expert at writing formal Freedom of Information requests to UK public authorities. "
202
- f"Generate a formal Freedom of Information request to {authority} using these keywords: {keyword_str}"
203
  )
204
 
205
- max_retries = 2
206
- # This will hold the last valid text we generated, even if it wasn't a perfect format
207
- last_cleaned_text = ""
208
-
209
- for attempt in range(max_retries):
210
- try:
211
- # Tokenize the input prompt
212
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
213
-
214
- # Set generation parameters
215
- generation_params = {
216
- "max_new_tokens": 250,
217
- "do_sample": True,
218
- "temperature": 0.25, # Unchanged as requested
219
- "top_k": 50,
220
- "top_p": 0.95,
221
- "repetition_penalty": 1.1,
222
- "streamer": None,
223
- "eos_token_id": tokenizer.eos_token_id
224
- }
225
-
226
- # Generate text sequences
227
- output_sequences = model.generate(**inputs, **generation_params)
228
-
229
- # Decode the generated text
230
- generated_text = tokenizer.decode(
231
- output_sequences[0][len(inputs["input_ids"][0]):],
232
- skip_special_tokens=True
233
- ).strip()
234
-
235
- # --- DEBUGGING LINE ---
236
- # This helps see the raw output in your logs, which is useful for development
237
- print(f"--- RAW MODEL OUTPUT (Attempt {attempt + 1}) ---\n{generated_text}\n--------------------")
238
-
239
-
240
- # Remove artifact if present
241
- if generated_text.startswith('.\n'):
242
- generated_text = generated_text[2:]
243
-
244
- # Clean and validate the output using the new, more forgiving function
245
- cleaned_text, is_valid = clean_and_validate_output(generated_text)
246
- last_cleaned_text = cleaned_text # Store the latest attempt
247
-
248
- if is_valid:
249
- # If the output is good enough, wrap it in the letter and return
250
- return wrap_in_letter(authority, cleaned_text)
251
- else:
252
- print(f"Attempt {attempt + 1}/{max_retries}: Output was too short. Retrying...")
253
-
254
- except Exception as e:
255
- print(f"Error during generation attempt {attempt + 1}/{max_retries}: {e}")
256
- if attempt == max_retries - 1:
257
- return f"An error occurred during text generation: {e}"
258
-
259
- # If all retries failed to produce a valid output, use the last thing we generated.
260
- # If nothing was ever generated, use the fallback message.
261
- if last_cleaned_text:
262
- return wrap_in_letter(authority, last_cleaned_text)
263
- else:
264
- return wrap_in_letter(authority, "1. [Unable to format automatically] Please restate the information requested.\n2. [Optional second point]")
265
 
266
 
267
  # --- Gradio UI and Spinning Logic ---
 
3
  import random
4
  import time
5
  import torch
 
6
  from transformers import AutoTokenizer, AutoModelForCausalLM
7
  import spaces
8
 
9
+ MODEL_ID = "HMC83/Wihtgar-650M-SFT-Requests_2"
10
 
11
  # --- Load Model and Tokenizer ---
12
  print("Loading model and tokenizer...")
 
26
  # --- Data for the Reels ---
27
  # A list of authority and keyword combinations.
28
  FOI_COMBINATIONS = [
 
 
 
 
 
 
29
  {"authority": "South Tyneside Metropolitan Borough Council", "keywords": "commercial customers, waste collection, waste producers"},
30
  {"authority": "University of Surrey", "keywords": "university car parks, parking tickets, parking enforcement"},
 
31
  {"authority": "Great Ormond Street Hospital", "keywords": "charity donations, research funding, financial year"},
32
  {"authority": "Public Health England", "keywords": "vaccination costs, public health campaigns, effectiveness"},
33
  {"authority": "NHS Digital", "keywords": "IT spending, system outages, cyber security"},
34
  {"authority": "Cornwall Council", "keywords": "adult social care, spending, assessments"},
35
  {"authority": "Hertfordshire County Council", "keywords": "school transport, route costs, contractor payments"},
36
  {"authority": "Wiltshire Council", "keywords": "highway maintenance, pothole repairs, annual spend"},
 
 
37
  {"authority": "Financial Conduct Authority", "keywords": "enforcement action, fines imposed, complaint statistics"},
38
  {"authority": "Competition and Markets Authority", "keywords": "merger investigations, market studies, legal costs"},
39
  {"authority": "Health and Safety Executive", "keywords": "workplace accidents, enforcement notices, prosecution costs"},
 
 
40
  {"authority": "Civil Aviation Authority", "keywords": "airline complaints, enforcement action, safety investigations"},
41
  {"authority": "HM Revenue and Customs", "keywords": "tax investigations, recovery rates, compliance costs"},
42
  {"authority": "Department for Work and Pensions", "keywords": "benefit sanctions, appeal outcomes, administrative costs"},
43
  {"authority": "Nottinghamshire County Council", "keywords": "adult social care, budget allocation, waiting lists"},
44
  {"authority": "Staffordshire County Council", "keywords": "school meals, contract costs, nutritional standards"},
45
+ {"authority": "Derbyshire County Council", "keywords": "library services, closure consultations, usage statistics"},
46
+ {"authority": "East Sussex County Council", "keywords": "children services, safeguarding, staffing costs"},
47
  {"authority": "Northumberland County Council", "keywords": "waste collection, recycling rates, contractor performance"},
 
 
 
 
48
  {"authority": "Environment Agency", "keywords": "flood defenses, maintenance costs, effectiveness assessments"},
49
+ {"authority": "Natural England", "keywords": "habitat protection, designation costs, landowner compensation"},
50
  {"authority": "Marine Management Organisation", "keywords": "fishing licenses, enforcement costs, violation penalties"},
51
  {"authority": "Forestry Commission", "keywords": "timber sales, revenue targets, environmental impact"},
52
  {"authority": "Welsh Government", "keywords": "language services, translation costs, usage statistics"},
53
  {"authority": "Scottish Government", "keywords": "ferry subsidies, route profitability, passenger numbers"},
54
+ {"authority": "Northern Ireland Executive", "keywords": "cross-border initiatives, funding allocation, outcomes"},
55
+ {"authority": "Office for National Statistics", "keywords": "census costs, data collection, contractor payments"},
56
+ {"authority": "Land Registry", "keywords": "property registrations, processing times, fee income"},
57
  {"authority": "Companies House", "keywords": "late filing penalties, collection rates, enforcement action"},
58
  {"authority": "Intellectual Property Office", "keywords": "patent applications, processing costs, appeal outcomes"},
59
  {"authority": "InGen Safety Authority", "keywords": "containment breaches, insurance costs, visitor incidents"},
 
66
  {"authority": "Worcestershire County Council", "keywords": "school transport, route planning, contractor performance"},
67
  {"authority": "Leicestershire County Council", "keywords": "highway maintenance, annual spend, emergency repairs"},
68
  {"authority": "Warwickshire County Council", "keywords": "library services, usage statistics, closure consultations"},
69
+ {"authority": "Nottinghamshire County Council", "keywords": "children services, safeguarding referrals, staff costs"},
70
  {"authority": "Derbyshire County Council", "keywords": "waste disposal, recycling rates, collection contracts"},
71
  {"authority": "Staffordshire County Council", "keywords": "care homes, inspection reports, quality ratings"},
72
  {"authority": "Shropshire Council", "keywords": "planning applications, approval rates, enforcement action"},
73
+ {"authority": "Herefordshire Council", "keywords": "council tax, collection rates, arrears management"},
74
  {"authority": "Cheshire East Council", "keywords": "parking enforcement, penalty notices, appeal outcomes"},
75
  {"authority": "Royal Liverpool University Hospital", "keywords": "patient complaints, response times, resolution outcomes"},
76
+ {"authority": "Sheffield Teaching Hospitals NHS Trust", "keywords": "medical equipment, procurement costs, maintenance contracts"},
77
  {"authority": "Leeds Teaching Hospitals NHS Trust", "keywords": "agency staff, temporary costs, recruitment spend"},
78
  {"authority": "Newcastle upon Tyne Hospitals NHS Trust", "keywords": "cancelled operations, reasons given, patient impact"},
79
  {"authority": "University Hospitals Birmingham", "keywords": "waiting times, treatment delays, performance targets"},
80
  {"authority": "Guy's and St Thomas' NHS Trust", "keywords": "infection control, outbreak incidents, prevention costs"},
81
  {"authority": "King's College Hospital NHS Trust", "keywords": "emergency admissions, capacity management, staffing levels"},
82
  {"authority": "Royal Free NHS Trust", "keywords": "clinical trials, research funding, patient consent"},
83
+ {"authority": "Imperial College Healthcare NHS Trust", "keywords": "private patient, income generated, capacity allocation"},
84
  {"authority": "Barts Health NHS Trust", "keywords": "financial deficit, recovery plans, cost reduction"},
85
  {"authority": "West Yorkshire Police", "keywords": "response times, emergency calls, performance standards"},
86
  {"authority": "South Yorkshire Police", "keywords": "crime statistics, detection rates, resource allocation"},
87
  {"authority": "North Yorkshire Police", "keywords": "rural policing, coverage areas, response costs"},
88
  {"authority": "Humberside Police", "keywords": "traffic enforcement, speed cameras, revenue generated"},
89
+ {"authority": "Lincolnshire Police", "keywords": "domestic violence, incident reports, support services"},
90
  {"authority": "Nottinghamshire Police", "keywords": "stop and search, statistics recorded, outcome data"},
91
  {"authority": "Derbyshire Police", "keywords": "drug seizures, investigation costs, conviction rates"},
92
+ {"authority": "Leicestershire Police", "keywords": "neighbourhood policing, community engagement, effectiveness measures"},
93
  {"authority": "Warwickshire Police", "keywords": "police stations, closure plans, public consultations"},
94
  {"authority": "West Mercia Police", "keywords": "cybercrime, investigation resources, training costs"},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  ]
96
 
97
  # Create lists for the spinning animation from the combinations above
98
  ALL_AUTHORITIES_FOR_SPIN = list(set([item["authority"] for item in FOI_COMBINATIONS]))
99
  ALL_KEYWORDS_FOR_SPIN = list(set(kw.strip() for item in FOI_COMBINATIONS for kw in item["keywords"].split(',')))
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # --- Backend Function for Local Inference ---
102
  @spaces.GPU
103
  def generate_request_local(authority, kw1, kw2, kw3):
104
+ """Generates a request using the locally loaded transformer model."""
105
  if not model or not tokenizer:
106
  return "Error: Model is not loaded. Please check the Space logs for details."
107
 
 
109
  keyword_string = ", ".join(keywords)
110
  prompt = (
111
  "You are an expert at writing formal Freedom of Information requests to UK public authorities. "
112
+ f"""Generate a formal Freedom of Information request to {authority} using these keywords: {keyword_string}"""
113
  )
114
 
115
+ try:
116
+ # Tokenize the input prompt
117
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
118
+
119
+ # Set generation parameters
120
+ generation_params = {
121
+ "max_new_tokens": 500,
122
+ "temperature": 0.3,
123
+ "top_p": 0.95,
124
+ "top_k": 50,
125
+ "repetition_penalty": 1.1,
126
+ "do_sample": True,
127
+ "pad_token_id": tokenizer.eos_token_id
128
+ }
129
+
130
+ # Generate text sequences
131
+ output_sequences = model.generate(**inputs, **generation_params)
132
+
133
+ # Decode the generated text
134
+ generated_text = tokenizer.decode(
135
+ output_sequences[0][len(inputs["input_ids"][0]):],
136
+ skip_special_tokens=True
137
+ ).strip()
138
+
139
+ # Remove artifact if present
140
+ if generated_text.startswith('.\n'):
141
+ generated_text = generated_text[2:]
142
+
143
+ return generated_text
144
+
145
+ except Exception as e:
146
+ print(f"Error during generation: {e}")
147
+ return f"An error occurred during text generation: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
 
150
  # --- Gradio UI and Spinning Logic ---