cmcmaster commited on
Commit
13ba67a
·
verified ·
1 Parent(s): f60db99

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +60 -112
main.py CHANGED
@@ -1,11 +1,10 @@
1
  from fasthtml.common import *
2
- import sqlite3
3
- import os
4
  import datetime
5
  from apscheduler.schedulers.background import BackgroundScheduler
6
  from pbs_data import PBSPublicDataAPIClient
7
  import os
8
- from fasthtml_hf import setup_hf_backup # Add this line
9
 
10
  custom_css = Style("""
11
  body {
@@ -83,19 +82,31 @@ custom_css = Style("""
83
  }
84
  """)
85
 
86
- # Database file
87
- DB_FILE = 'rheumatology_biologics_data.db'
88
-
89
- if not os.path.exists(DB_FILE):
90
- print(f"Database file {DB_FILE} does not exist!")
91
 
92
  def load_data():
93
  try:
94
- conn = sqlite3.connect(DB_FILE)
95
- cursor = conn.cursor()
 
 
 
 
 
 
 
96
 
97
- except sqlite3.Error as e:
98
- print(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
99
  return {
100
  'combinations': [],
101
  'drugs': [],
@@ -106,86 +117,38 @@ def load_data():
106
  'hospital_types': []
107
  }
108
 
109
- # Fetch all data
110
- cursor.execute('''SELECT c.pbs_code, d.name as drug, b.name as brand, f.name as formulation,
111
- i.name as indication, tp.name as treatment_phase, c.streamlined_code,
112
- c.online_application, c.authority_method, ht.name as hospital_type
113
- FROM combinations c
114
- JOIN drugs d ON c.drug_id = d.id
115
- JOIN brands b ON c.brand_id = b.id
116
- JOIN formulations f ON c.formulation_id = f.id
117
- JOIN indications i ON c.indication_id = i.id
118
- JOIN treatment_phases tp ON c.treatment_phase_id = tp.id
119
- JOIN hospital_types ht ON c.hospital_type_id = ht.id''')
120
-
121
- data = cursor.fetchall()
122
-
123
- # Fetch distinct values for dropdowns
124
- cursor.execute('SELECT name FROM drugs')
125
- drugs = [row[0] for row in cursor.fetchall()]
126
-
127
- cursor.execute('SELECT name FROM brands')
128
- brands = [row[0] for row in cursor.fetchall()]
129
-
130
- cursor.execute('SELECT name FROM formulations')
131
- formulations = [row[0] for row in cursor.fetchall()]
132
-
133
- cursor.execute('SELECT name FROM indications')
134
- indications = [row[0] for row in cursor.fetchall()]
135
-
136
- cursor.execute('SELECT name FROM treatment_phases')
137
- treatment_phases = [row[0] for row in cursor.fetchall()]
138
-
139
- cursor.execute('SELECT name FROM hospital_types')
140
- hospital_types = [row[0] for row in cursor.fetchall()]
141
-
142
- conn.close()
143
-
144
- return {
145
- 'combinations': data,
146
- 'drugs': drugs,
147
- 'brands': brands,
148
- 'formulations': formulations,
149
- 'indications': indications,
150
- 'treatment_phases': treatment_phases,
151
- 'hospital_types': hospital_types
152
- }
153
-
154
  biologics_data = load_data()
155
 
156
  app, rt = fast_app()
157
 
158
  def search_biologics(drug, brand, formulation, indication, treatment_phase, hospital_type):
159
- field_indices = {
160
- 'drug': 1,
161
- 'brand': 2,
162
- 'formulation': 3,
163
- 'indication': 4,
164
- 'treatment_phase': 5,
165
- 'hospital_type': 9
166
- }
167
-
168
- results = [combo for combo in biologics_data['combinations'] if all(
169
- not value or combo[field_indices[key]] == value
170
- for key, value in {'drug': drug, 'brand': brand, 'formulation': formulation, 'indication': indication, 'treatment_phase': treatment_phase, 'hospital_type': hospital_type}.items()
171
- )]
172
 
173
- if not results:
174
  return "No results found."
175
 
176
  output = ""
177
  for item in results:
178
  output += f"""
179
  <div class="result-item">
180
- <h2>{item[1]} ({item[2]})</h2>
181
- <p><strong>PBS Code:</strong> <a href="https://www.pbs.gov.au/medicine/item/{item[0]}" target="_blank">{item[0]}</a></p>
182
- <p><strong>Formulation:</strong> {item[3]}</p>
183
- <p><strong>Indication:</strong> {item[4]}</p>
184
- <p><strong>Treatment Phase:</strong> {item[5]}</p>
185
- <p><strong>Streamlined Code:</strong> {item[6] or 'N/A'}</p>
186
- <p><strong>Authority Method:</strong> {item[8]}</p>
187
- <p><strong>Online Application:</strong> {'Yes' if item[7] else 'No'}</p>
188
- <p><strong>Hospital Type:</strong> {item[9]}</p>
 
 
189
  </div>
190
  <hr>
191
  """
@@ -193,36 +156,22 @@ def search_biologics(drug, brand, formulation, indication, treatment_phase, hosp
193
  return output
194
 
195
  def update_options(drug, brand, formulation, indication, treatment_phase, hospital_type):
196
- selected = {
197
- 'drug': drug,
198
- 'brand': brand,
199
- 'formulation': formulation,
200
- 'indication': indication,
201
- 'treatment_phase': treatment_phase,
202
- 'hospital_type': hospital_type
203
- }
204
-
205
- field_indices = {
206
- 'drug': 1,
207
- 'brand': 2,
208
- 'formulation': 3,
209
- 'indication': 4,
210
- 'treatment_phase': 5,
211
- 'hospital_type': 9
212
- }
213
-
214
- filtered = [combo for combo in biologics_data['combinations'] if all(
215
- not selected[key] or combo[field_indices[key]] == selected[key]
216
- for key in selected
217
- )]
218
 
219
  options = {
220
- 'drugs': sorted(set(combo[1] for combo in filtered)),
221
- 'brands': sorted(set(combo[2] for combo in filtered)),
222
- 'formulations': sorted(set(combo[3] for combo in filtered)),
223
- 'indications': sorted(set(combo[4] for combo in filtered)),
224
- 'treatment_phases': sorted(set(combo[5] for combo in filtered)),
225
- 'hospital_types': sorted(set(combo[9] for combo in filtered))
226
  }
227
 
228
  return options
@@ -352,13 +301,12 @@ def post(drug: str = '', brand: str = '', formulation: str = '', indication: str
352
  results = search_biologics(drug, brand, formulation, indication, treatment_phase, hospital_type)
353
  return results
354
 
355
-
356
  def update_data():
357
  print(f"Updating data at {datetime.datetime.now()}")
358
  client = PBSPublicDataAPIClient("2384af7c667342ceb5a736fe29f1dc6b", rate_limit=0.2)
359
  try:
360
  data = client.fetch_rheumatology_biologics_data()
361
- client.save_data_to_sqlite(data, DB_FILE)
362
  print("Data updated successfully")
363
  global biologics_data
364
  biologics_data = load_data()
@@ -367,8 +315,8 @@ def update_data():
367
 
368
  # Set up the scheduler
369
  scheduler = BackgroundScheduler()
370
- scheduler.add_job(func=update_data, trigger='interval', minutes=10)
371
  scheduler.start()
372
 
373
- setup_hf_backup(app) # Add this line
374
  serve()
 
1
  from fasthtml.common import *
2
+ from datasets import load_dataset
 
3
  import datetime
4
  from apscheduler.schedulers.background import BackgroundScheduler
5
  from pbs_data import PBSPublicDataAPIClient
6
  import os
7
+ from fasthtml_hf import setup_hf_backup
8
 
9
  custom_css = Style("""
10
  body {
 
82
  }
83
  """)
84
 
85
+ DATASET_NAME = "cmcmaster/rheumatology-biologics-dataset"
 
 
 
 
86
 
87
  def load_data():
88
  try:
89
+ dataset = load_dataset(DATASET_NAME, split="train")
90
+
91
+ # Create sets for dropdown options
92
+ drugs = set(dataset['drug'])
93
+ brands = set(dataset['brand'])
94
+ formulations = set(dataset['formulation'])
95
+ indications = set(dataset['indication'])
96
+ treatment_phases = set(dataset['treatment_phase'])
97
+ hospital_types = set(dataset['hospital_type'])
98
 
99
+ return {
100
+ 'combinations': dataset,
101
+ 'drugs': sorted(drugs),
102
+ 'brands': sorted(brands),
103
+ 'formulations': sorted(formulations),
104
+ 'indications': sorted(indications),
105
+ 'treatment_phases': sorted(treatment_phases),
106
+ 'hospital_types': sorted(hospital_types)
107
+ }
108
+ except Exception as e:
109
+ print(f"An error occurred while loading data: {str(e)}")
110
  return {
111
  'combinations': [],
112
  'drugs': [],
 
117
  'hospital_types': []
118
  }
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  biologics_data = load_data()
121
 
122
  app, rt = fast_app()
123
 
124
  def search_biologics(drug, brand, formulation, indication, treatment_phase, hospital_type):
125
+ results = biologics_data['combinations'].filter(
126
+ lambda x: (not drug or x['drug'] == drug) and
127
+ (not brand or x['brand'] == brand) and
128
+ (not formulation or x['formulation'] == formulation) and
129
+ (not indication or x['indication'] == indication) and
130
+ (not treatment_phase or x['treatment_phase'] == treatment_phase) and
131
+ (not hospital_type or x['hospital_type'] == hospital_type)
132
+ )
 
 
 
 
 
133
 
134
+ if len(results) == 0:
135
  return "No results found."
136
 
137
  output = ""
138
  for item in results:
139
  output += f"""
140
  <div class="result-item">
141
+ <h2>{item['drug']} ({item['brand']})</h2>
142
+ <p><strong>PBS Code:</strong> <a href="https://www.pbs.gov.au/medicine/item/{item['pbs_code']}" target="_blank">{item['pbs_code']}</a></p>
143
+ <p><strong>Formulation:</strong> {item['formulation']}</p>
144
+ <p><strong>Indication:</strong> {item['indication']}</p>
145
+ <p><strong>Treatment Phase:</strong> {item['treatment_phase']}</p>
146
+ <p><strong>Streamlined Code:</strong> {item['streamlined_code'] or 'N/A'}</p>
147
+ <p><strong>Authority Method:</strong> {item['authority_method']}</p>
148
+ <p><strong>Online Application:</strong> {'Yes' if item['online_application'] else 'No'}</p>
149
+ <p><strong>Hospital Type:</strong> {item['hospital_type']}</p>
150
+ <p><strong>Schedule Year:</strong> {item['schedule_year']}</p>
151
+ <p><strong>Schedule Month:</strong> {item['schedule_month']}</p>
152
  </div>
153
  <hr>
154
  """
 
156
  return output
157
 
158
  def update_options(drug, brand, formulation, indication, treatment_phase, hospital_type):
159
+ filtered = biologics_data['combinations'].filter(
160
+ lambda x: (not drug or x['drug'] == drug) and
161
+ (not brand or x['brand'] == brand) and
162
+ (not formulation or x['formulation'] == formulation) and
163
+ (not indication or x['indication'] == indication) and
164
+ (not treatment_phase or x['treatment_phase'] == treatment_phase) and
165
+ (not hospital_type or x['hospital_type'] == hospital_type)
166
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
  options = {
169
+ 'drugs': sorted(set(filtered['drug'])),
170
+ 'brands': sorted(set(filtered['brand'])),
171
+ 'formulations': sorted(set(filtered['formulation'])),
172
+ 'indications': sorted(set(filtered['indication'])),
173
+ 'treatment_phases': sorted(set(filtered['treatment_phase'])),
174
+ 'hospital_types': sorted(set(filtered['hospital_type']))
175
  }
176
 
177
  return options
 
301
  results = search_biologics(drug, brand, formulation, indication, treatment_phase, hospital_type)
302
  return results
303
 
 
304
  def update_data():
305
  print(f"Updating data at {datetime.datetime.now()}")
306
  client = PBSPublicDataAPIClient("2384af7c667342ceb5a736fe29f1dc6b", rate_limit=0.2)
307
  try:
308
  data = client.fetch_rheumatology_biologics_data()
309
+ client.save_data_to_hf(data, DATASET_NAME)
310
  print("Data updated successfully")
311
  global biologics_data
312
  biologics_data = load_data()
 
315
 
316
  # Set up the scheduler
317
  scheduler = BackgroundScheduler()
318
+ scheduler.add_job(func=update_data, trigger='interval', days=1)
319
  scheduler.start()
320
 
321
+ setup_hf_backup(app)
322
  serve()