File size: 15,952 Bytes
99ab61a
13ba67a
99ab61a
 
 
13ba67a
203e40c
 
 
 
 
dc82736
 
99ab61a
4731312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13ba67a
99ab61a
 
 
13ba67a
 
 
 
 
 
 
 
 
4731312
13ba67a
 
 
 
 
 
 
 
 
 
 
99ab61a
 
 
 
 
 
3401663
 
99ab61a
 
 
 
 
 
3401663
13ba67a
 
 
 
 
 
 
 
99ab61a
13ba67a
99ab61a
 
 
 
 
4731312
13ba67a
 
 
 
 
 
 
 
 
 
 
4731312
99ab61a
 
 
 
 
3401663
13ba67a
 
 
 
 
 
 
 
99ab61a
 
13ba67a
 
 
 
 
 
99ab61a
 
 
 
 
 
4731312
 
 
99ab61a
4731312
 
3401663
4731312
 
 
3401663
4731312
 
 
3401663
4731312
 
 
3401663
4731312
 
 
3401663
 
 
 
 
4731312
 
 
 
 
99ab61a
 
 
 
 
 
 
 
 
4731312
 
 
 
 
3401663
4731312
 
 
3401663
4731312
 
 
3401663
4731312
 
 
3401663
4731312
 
 
3401663
 
 
 
 
4731312
 
 
 
 
 
 
 
 
99ab61a
 
 
3401663
 
4731312
 
 
 
 
3401663
4731312
 
 
3401663
4731312
 
 
3401663
4731312
 
 
3401663
4731312
 
 
3401663
 
 
 
 
4731312
 
 
 
 
 
 
 
 
99ab61a
 
 
3401663
 
99ab61a
 
 
 
 
 
 
d51c1d7
99ab61a
 
 
 
 
 
203e40c
e09ccdc
99ab61a
 
203e40c
 
 
 
 
 
 
99ab61a
 
203e40c
 
 
 
13ba67a
99ab61a
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
from fasthtml.common import *
from datasets import load_dataset
import datetime
from pbs_data import PBSPublicDataAPIClient
import os
from fasthtml_hf import setup_hf_backup
from fasthtml import FastHTML
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
import atexit

HF_TOKEN = os.environ.get("HF_TOKEN")


custom_css = Style("""
    body {
        font-family: Arial, sans-serif;
        line-height: 1.6;
        color: #333;
        max-width: 800px;
        margin: 0 auto;
        padding: 20px;
        background-color: #f0f0f0;
    }
    h1 {
        color: #2c3e50;
        text-align: center;
        margin-bottom: 30px;
    }
    form {
        background-color: #ffffff;
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    label {
        display: block;
        margin-bottom: 5px;
        font-weight: bold;
        color: #2c3e50;
    }
    select, button {
        width: 100%;
        padding: 10px;
        margin-bottom: 15px;
        border: 1px solid #ddd;
        border-radius: 4px;
        background-color: #fff;
        color: #333;
    }
    button {
        background-color: #3498db;
        color: white;
        font-weight: bold;
        cursor: pointer;
        transition: background-color 0.3s;
    }
    button:hover {
        background-color: #2980b9;
    }
    #results {
        margin-top: 30px;
        background-color: #ffffff;
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    #results h2 {
        color: #2c3e50;
        border-bottom: 2px solid #3498db;
        padding-bottom: 10px;
    }
    #results p {
        margin-bottom: 10px;
        color: #333;
    }
    #results hr {
        border: none;
        border-top: 1px solid #eee;
        margin: 20px 0;
    }
    a {
        color: #3498db;
        text-decoration: none;
    }
    a:hover {
        text-decoration: underline;
    }
""")

DATASET_NAME = "cmcmaster/rheumatology-biologics-dataset"

def load_data():
    try:
        dataset = load_dataset(DATASET_NAME, split="train")
        
        # Create sets for dropdown options
        drugs = set(dataset['drug'])
        brands = set(dataset['brand'])
        formulations = set(dataset['formulation'])
        indications = set(dataset['indication'])
        treatment_phases = set(dataset['treatment_phase'])
        hospital_types = set(dataset['hospital_type'])

        return {
            'combinations': dataset,
            'drugs': sorted(drugs),
            'brands': sorted(brands),
            'formulations': sorted(formulations),
            'indications': sorted(indications),
            'treatment_phases': sorted(treatment_phases),
            'hospital_types': sorted(hospital_types)
        }
    except Exception as e:
        print(f"An error occurred while loading data: {str(e)}")
        return {
            'combinations': [],
            'drugs': [],
            'brands': [],
            'formulations': [],
            'indications': [],
            'treatment_phases': [],
            'hospital_types': []
        }

biologics_data = load_data()

app, rt = fast_app()

def search_biologics(drug, brand, formulation, indication, treatment_phase, hospital_type):
    results = biologics_data['combinations'].filter(
        lambda x: (not drug or x['drug'] == drug) and
                  (not brand or x['brand'] == brand) and
                  (not formulation or x['formulation'] == formulation) and
                  (not indication or x['indication'] == indication) and
                  (not treatment_phase or x['treatment_phase'] == treatment_phase) and
                  (not hospital_type or x['hospital_type'] == hospital_type)
    )
    
    if len(results) == 0:
        return "No results found."
    
    output = ""
    for item in results:
        output += f"""
        <div class="result-item">
            <h2>{item['drug']} ({item['brand']})</h2>
            <p><strong>PBS Code:</strong> <a href="https://www.pbs.gov.au/medicine/item/{item['pbs_code']}" target="_blank">{item['pbs_code']}</a></p>
            <p><strong>Formulation:</strong> {item['formulation']}</p>
            <p><strong>Indication:</strong> {item['indication']}</p>
            <p><strong>Treatment Phase:</strong> {item['treatment_phase']}</p>
            <p><strong>Streamlined Code:</strong> {item['streamlined_code'] or 'N/A'}</p>
            <p><strong>Authority Method:</strong> {item['authority_method']}</p>
            <p><strong>Online Application:</strong> {'Yes' if item['online_application'] else 'No'}</p>
            <p><strong>Hospital Type:</strong> {item['hospital_type']}</p>
            <p><strong>Schedule Year:</strong> {item['schedule_year']}</p>
            <p><strong>Schedule Month:</strong> {item['schedule_month']}</p>
        </div>
        <hr>
        """
    
    return output

def update_options(drug, brand, formulation, indication, treatment_phase, hospital_type):
    filtered = biologics_data['combinations'].filter(
        lambda x: (not drug or x['drug'] == drug) and
                  (not brand or x['brand'] == brand) and
                  (not formulation or x['formulation'] == formulation) and
                  (not indication or x['indication'] == indication) and
                  (not treatment_phase or x['treatment_phase'] == treatment_phase) and
                  (not hospital_type or x['hospital_type'] == hospital_type)
    )
    
    options = {
        'drugs': sorted(set(filtered['drug'])),
        'brands': sorted(set(filtered['brand'])),
        'formulations': sorted(set(filtered['formulation'])),
        'indications': sorted(set(filtered['indication'])),
        'treatment_phases': sorted(set(filtered['treatment_phase'])),
        'hospital_types': sorted(set(filtered['hospital_type']))
    }
    
    return options

@rt('/')
def get():
    return Div(
        custom_css,
        H1("Biologics Prescriber Helper"),
        Form(
            Div(
                Label("Drug:"),
                Select(Option("All", value=""), *[Option(drug, value=drug) for drug in biologics_data['drugs']], name="drug", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Brand:"),
                Select(Option("All", value=""), *[Option(brand, value=brand) for brand in biologics_data['brands']], name="brand", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Formulation:"),
                Select(Option("All", value=""), *[Option(formulation, value=formulation) for formulation in biologics_data['formulations']], name="formulation", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Indication:"),
                Select(Option("All", value=""), *[Option(indication, value=indication) for indication in biologics_data['indications']], name="indication", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Treatment Phase:"),
                Select(Option("All", value=""), *[Option(phase, value=phase) for phase in biologics_data['treatment_phases']], name="treatment_phase", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Hospital Type:"),
                Select(Option("All", value=""), *[Option(ht, value=ht) for ht in biologics_data['hospital_types']], name="hospital_type", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Button("Search", type="submit"),
                Button("Reset", hx_get="/reset", hx_target="#options")
            ),
            hx_post="/search",
            hx_target="#results",
            id="options"
        ),
        Div(id="results")
    )

@rt('/reset')
def get():
    return Div(
        custom_css,
        Form(
            Div(
                Label("Drug:"),
                Select(Option("All", value=""), *[Option(drug, value=drug) for drug in biologics_data['drugs']], name="drug", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Brand:"),
                Select(Option("All", value=""), *[Option(brand, value=brand) for brand in biologics_data['brands']], name="brand", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Formulation:"),
                Select(Option("All", value=""), *[Option(formulation, value=formulation) for formulation in biologics_data['formulations']], name="formulation", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Indication:"),
                Select(Option("All", value=""), *[Option(indication, value=indication) for indication in biologics_data['indications']], name="indication", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Treatment Phase:"),
                Select(Option("All", value=""), *[Option(phase, value=phase) for phase in biologics_data['treatment_phases']], name="treatment_phase", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Hospital Type:"),
                Select(Option("All", value=""), *[Option(ht, value=ht) for ht in biologics_data['hospital_types']], name="hospital_type", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Button("Search", type="submit"),
                Button("Reset", hx_get="/reset", hx_target="#options")
            ),
            hx_post="/search",
            hx_target="#results",
            id="options"
        )
    )

@rt('/update_options')
def get(drug: str = '', brand: str = '', formulation: str = '', indication: str = '', treatment_phase: str = '', hospital_type: str = ''):
    options = update_options(drug, brand, formulation, indication, treatment_phase, hospital_type)
    return Div(
        custom_css,
        Form(
            Div(
                Label("Drug:"),
                Select(Option("All", value=""), *[Option(d, value=d, selected=(d == drug)) for d in options['drugs']], name="drug", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Brand:"),
                Select(Option("All", value=""), *[Option(b, value=b, selected=(b == brand)) for b in options['brands']], name="brand", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Formulation:"),
                Select(Option("All", value=""), *[Option(f, value=f, selected=(f == formulation)) for f in options['formulations']], name="formulation", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Indication:"),
                Select(Option("All", value=""), *[Option(i, value=i, selected=(i == indication)) for i in options['indications']], name="indication", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Treatment Phase:"),
                Select(Option("All", value=""), *[Option(p, value=p, selected=(p == treatment_phase)) for p in options['treatment_phases']], name="treatment_phase", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Label("Hospital Type:"),
                Select(Option("All", value=""), *[Option(ht, value=ht, selected=(ht == hospital_type)) for ht in options['hospital_types']], name="hospital_type", hx_get="/update_options", hx_target="#options", hx_trigger="change", hx_include="[name='drug'],[name='brand'],[name='formulation'],[name='indication'],[name='treatment_phase'],[name='hospital_type']")
            ),
            Div(
                Button("Search", type="submit"),
                Button("Reset", hx_get="/reset", hx_target="#options")
            ),
            hx_post="/search",
            hx_target="#results",
            id="options"
        )
    )

@rt('/search')
def post(drug: str = '', brand: str = '', formulation: str = '', indication: str = '', treatment_phase: str = '', hospital_type: str = ''):
    results = search_biologics(drug, brand, formulation, indication, treatment_phase, hospital_type)
    return results

def update_data():
    print(f"Updating data at {datetime.datetime.now()}")
    client = PBSPublicDataAPIClient("2384af7c667342ceb5a736fe29f1dc6b", rate_limit=0.2)
    try:
        data = client.fetch_rheumatology_biologics_data()
        client.save_data_to_hf(data, HF_TOKEN, DATASET_NAME)
        print("Data updated successfully")
        global biologics_data
        biologics_data = load_data()
    except Exception as e:
        print(f"An error occurred while updating data: {str(e)}")

# Set up the scheduler
update_data()
# Set up the scheduler
scheduler = BackgroundScheduler()
scheduler.add_job(
    func=update_data,
    trigger=IntervalTrigger(hours=24),
    id='update_data',
    name='Update Data',
    replace_existing=True
)
scheduler.start()

# Make sure to shut down the scheduler when the app is terminated

atexit.register(lambda: scheduler.shutdown())

setup_hf_backup(app)
serve()