File size: 22,208 Bytes
422d8d6
 
 
 
 
 
 
84153bd
2cd9a3f
422d8d6
 
 
 
 
 
 
 
 
 
 
660c863
422d8d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75e8671
 
 
422d8d6
 
 
 
 
 
 
 
 
 
 
 
 
157a7b8
6e19b58
157a7b8
6e19b58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422d8d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26c83a9
df5431f
422d8d6
 
 
fd2aa28
422d8d6
 
bffd473
422d8d6
7d1040b
 
 
 
 
 
 
 
 
 
 
 
 
 
a945af8
7d1040b
a945af8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bd2bb4
 
7fadff1
2bd2bb4
1661c3c
 
a945af8
66d50c7
7d1040b
a945af8
 
2b1ed6e
 
7d1040b
 
 
 
422d8d6
9d60b86
422d8d6
 
 
 
 
 
15319f1
422d8d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25d5b5a
bed6220
 
25d5b5a
e0fb5c7
3a27459
e4caabe
 
9700103
f000ad4
422d8d6
 
7721b2f
422d8d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7721b2f
422d8d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d60b86
5f6d56a
1ae2960
21a8d5b
 
708573b
 
422d8d6
 
 
a917498
1d35a54
721ce67
4dc908a
721ce67
4dc908a
1d35a54
721ce67
4dc908a
721ce67
1d35a54
721ce67
 
422d8d6
 
7721b2f
422d8d6
 
 
 
 
7721b2f
1d35a54
85e9aef
1d35a54
422d8d6
 
 
5efaf8a
422d8d6
 
9d60b86
7721b2f
1d35a54
9700103
1d35a54
422d8d6
 
 
 
 
 
 
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import GPy
import GPyOpt
import pickle
import tensorflow as tf
import numpy as np
import pandas as pd
from preprocessing_utils import encode_categorical, scale_numerical, fill_nans
import os
import gradio as gr

# Load access tokens
WRITE_TOKEN = os.environ.get("WRITE_PER") # write

# Logs repo path
dataset_url = "https://huggingface.co/datasets/sandl/upload_alloy_hardness"
dataset_path = "logs_alloy_hardness.csv"


# Input parameters
model_path = "model_coatings.h5"
model = tf.keras.models.load_model(model_path)

df_columns = ['Binder', 'NMs_Type', 'Primary_Size (nm)', 'NM-Shape', 'Substrate',
              'Microorganism ', 'Duration (h)', 'Washing_cycles', 'Reduction_%',
              'Concetration (µg/mL)', 'NPs_Synthesis_method', 'Application method\n',
              'Evalutation_Standard', 'Evalutation_Method', 'Durability test',
              'Washing_Detergent', 'Washing_Temp']

targets = ["Reduction_%"]
numerical_columns = [#'Fabric diameter for antibacterial evaluation\n(cm)',
                     'Primary_Size (nm)', 'Duration (h)', 'Washing_cycles', 'Reduction_%',
                     'Concetration (µg/mL)']

categorical_columns = [column for column in df_columns if column not in numerical_columns]

numerical_columns.remove(targets[0])

for column in targets:
    df_columns.remove(column)

# Unpickle files

with open("one_hot_scaler.pickle", "rb") as file:
    unpickler = pickle.Unpickler(file)
    one_hot_scaler = unpickler.load()

with open("minmax_scaler_targets.pickle", "rb") as file:
    unpickler = pickle.Unpickler(file)
    minmax_scaler_targets = unpickler.load()

with open("minmax_scaler_inputs.pickle", "rb") as file:
    unpickler = pickle.Unpickler(file)
    minmax_scaler_inputs = unpickler.load()

with open("one_hot_scaler.pickle", "rb") as file:
    unpickler = pickle.Unpickler(file)
    one_hot_scaler = unpickler.load()

test_data_columns = ['Binder_ADA',
 'Binder_Alginates',
 'Binder_Anatase',
 'Binder_Butane tetracarboxylic',
 'Binder_CDA',
 'Binder_CF4 plasma',
 'Binder_CTAB',
 'Binder_Carboxylic acid ',
 'Binder_Carboxymethyl chitosan (CMCTS)',
 'Binder_Cellulase',
 'Binder_Chitosan',
 'Binder_Citric acid ',
 'Binder_Copper phosphide',
 'Binder_Date seed extract',
 'Binder_Dendrimer',
 'Binder_HSDA',
 'Binder_HY',
 'Binder_Mesosilver',
 'Binder_Multi-amino compound (RSD-NH2)',
 'Binder_NIDA',
 'Binder_Nano-clay',
 'Binder_Organosilicon',
 'Binder_PEG',
 'Binder_PS-b-PAA',
 'Binder_PUBK (hydrophilic aliphatic polyester-urethanes)',
 'Binder_Poly(quaternary ammonium salt-epoxy)',
 'Binder_Printofix® Binder MTB EG liquid',
 'Binder_Rutile',
 'Binder_SDS',
 'Binder_Seaweed',
 'Binder_Silane ',
 'Binder_Silica',
 'Binder_Silpure',
 'Binder_Sodium citrate',
 'Binder_Starch',
 'Binder_TX-100',
 'Binder_Thioglycolic acid (TGA)',
 'Binder_hexadecyltrimethoxysilane(HDTMS)',
 'Binder_hexamethyltriethylenetetramine',
 'Binder_poly-hydroxy-amino methyl silicone',
 'Binder_polyamide network polymer (PNP)',
 'NMs_Type_Ag',
 'NMs_Type_Au',
 'NMs_Type_CS',
 'NMs_Type_Ce',
 'NMs_Type_Ce_ZnO',
 'NMs_Type_Co',
 'NMs_Type_CuO',
 'NMs_Type_CuO_TiO2',
 'NMs_Type_Fe3O4',
 'NMs_Type_Fe3O4_ZnO',
 'NMs_Type_Mn',
 'NMs_Type_SA_TSA',
 'NMs_Type_SiO2_Ag_Cu',
 'NMs_Type_TiO2',
 'NMs_Type_ZnO',
 'NMs_Type_ZnO_Cs',
 'NMs_Type_ZrO2',
 'NM-Shape_Crystalline',
 'NM-Shape_Disc',
 'NM-Shape_Ellipsoidal',
 'NM-Shape_Hexagonal',
 'NM-Shape_Hierarchical',
 'NM-Shape_Irregular',
 'NM-Shape_Nanotube',
 'NM-Shape_Nanowire',
 'NM-Shape_Polygonal',
 'NM-Shape_Prism',
 'NM-Shape_Rod ',
 'NM-Shape_Spherical',
 'NM-Shape_rectangle',
 'Substrate_Bamboo',
 'Substrate_Cotton',
 'Substrate_Cotton_Polyester',
 'Substrate_Denim',
 'Substrate_PET',
 'Substrate_Polyamide',
 'Substrate_Polyester',
 'Substrate_Silk',
 'Substrate_Viscose',
 'Substrate_Wool',
 'Substrate_Wool_Polyester',
 'Substrate_cotton',
 'Microorganism _Aci_baumannii',
 'Microorganism _Alt_brassicicola',
 'Microorganism _Asp_niger',
 'Microorganism _Bac_subtilis',
 'Microorganism _C_albicans',
 'Microorganism _E_coli',
 'Microorganism _Enter_faecalis',
 'Microorganism _Fus_oxysporum',
 'Microorganism _K_aerogens',
 'Microorganism _Kle_pneumoniae',
 'Microorganism _MRSA',
 'Microorganism _Mi_canis',
 'Microorganism _Pse_aeruginosa',
 'Microorganism _S_aureus',
 'Microorganism _S_epidermis',
 'Microorganism _S_pyogenes',
 'Microorganism _Sal_typhimurium',
 'Microorganism _Tric_mentagraphytes',
 'NPs_Synthesis_method_Bio synthesis',
 'NPs_Synthesis_method_Biosythesis ',
 'NPs_Synthesis_method_Degradation',
 'NPs_Synthesis_method_Dip_coated_Temp curated_Ultrasound',
 'NPs_Synthesis_method_Not_applicable',
 'NPs_Synthesis_method_Photochemical Reduction',
 'NPs_Synthesis_method_Supplied',
 'NPs_Synthesis_method_Wet chemical reduced',
 'NPs_Synthesis_method_Wet chemistry',
 'NPs_Synthesis_method_biosynthesis-green',
 'NPs_Synthesis_method_ex situ synthesis',
 'NPs_Synthesis_method_fungal process (biosynthesis)_green synthesis',
 'NPs_Synthesis_method_green synthesis',
 'NPs_Synthesis_method_in situ',
 'NPs_Synthesis_method_in situ  synthesis',
 'NPs_Synthesis_method_in situ biosythesis',
 'NPs_Synthesis_method_in situ desposition (alkalization and deposition)',
 'NPs_Synthesis_method_in situ microwave irradiation',
 'NPs_Synthesis_method_in situ reduction',
 'NPs_Synthesis_method_in situ sol gel immersion',
 'NPs_Synthesis_method_in situ sol–gel method',
 'NPs_Synthesis_method_in situ synthesis',
 'NPs_Synthesis_method_in situ synthesized',
 'NPs_Synthesis_method_in situ ultrasound irradiation',
 'NPs_Synthesis_method_ionic gelation',
 'NPs_Synthesis_method_nebulize',
 'NPs_Synthesis_method_reducing',
 'NPs_Synthesis_method_reduction in situ',
 'NPs_Synthesis_method_reduction of celluloce in viscose',
 'NPs_Synthesis_method_reverse micellar cores',
 'NPs_Synthesis_method_sol gel',
 'NPs_Synthesis_method_sol-gel',
 'NPs_Synthesis_method_sonication',
 'NPs_Synthesis_method_sonochemical',
 'NPs_Synthesis_method_ultrasound irradiation',
 'NPs_Synthesis_method_wet chemical method',
 'NPs_Synthesis_method_wet chemistry',
 'Application method\n_ exhaustion and Pad_squeeze_dry',
 'Application method\n_Dip coating',
 'Application method\n_Dip coating and shaking',
 'Application method\n_Dip padding and microwave irradiation',
 'Application method\n_Dip-coating and Ultrasound irradiation',
 'Application method\n_Dip_coating',
 'Application method\n_Exhaust dyeing',
 'Application method\n_Grafting Wet chemical ',
 'Application method\n_Immersion',
 'Application method\n_In situ Immersion',
 'Application method\n_In situ dip-coating',
 'Application method\n_Mist',
 'Application method\n_Pad-Dry-Cure ',
 'Application method\n_Pad-Dry-Cure and Dip coating',
 'Application method\n_Pad-dry-cure',
 'Application method\n_Padding',
 'Application method\n_Pre-alkalization/sorption',
 'Application method\n_Sonochemical',
 'Application method\n_Sonochemical throwingstones',
 'Application method\n_Sonochemical/Roll to roll ',
 'Application method\n_Sonochemical/Ultrasonic irradiation',
 'Application method\n_Sonochemical/ultrasonic transducer',
 'Application method\n_Sorption',
 'Application method\n_Top-coating with Pericoat',
 'Application method\n_Ultrasonic irradiation',
 'Application method\n_Ultrasonic-mediated dip coating',
 'Application method\n_Ultrasound irradiation',
 'Application method\n_Wet-on-wet padding',
 'Application method\n_Wetting-Immersion',
 'Application method\n_Wetting-Immersion or spraying',
 'Application method\n_Wetting-Spraying',
 'Application method\n_direct multi-layer coating with a socalled\nair blade',
 'Application method\n_pad-dry-cure',
 'Application method\n_plasma jet',
 'Application method\n_ultrasonic ',
 'Evalutation_Standard_AATCC_100',
 'Evalutation_Standard_AATCC_147',
 'Evalutation_Standard_AATCC_147_ISO_20645',
 'Evalutation_Standard_AATCC_30',
 'Evalutation_Standard_ASTME_2149',
 'Evalutation_Standard_ASTM_2180',
 'Evalutation_Standard_GB_T_20944_AATCC_61',
 'Evalutation_Standard_ISO_20645',
 'Evalutation_Standard_ISO_20743',
 'Evalutation_Method_Agar_diffusion',
 'Evalutation_Method_Dyn_shake',
 'Durability test_ Memeret shaker',
 'Durability test_AATCC 124',
 'Durability test_AATCC 61',
 'Durability test_AATCC standard wash machine',
 'Durability test_Boiled',
 'Durability test_GB/T 20944.3-2008(China)',
 'Durability test_Hand washes',
 'Durability test_Home laundering machine',
 'Durability test_Home laundry washing',
 'Durability test_Home/commercial laundering',
 'Durability test_IS 687:1979',
 'Durability test_ISO 105 CO3-1982',
 'Durability test_ISO 105-C014:1989',
 'Durability test_ISO 105-C06: 2010',
 'Durability test_ISO 105-C06:1994',
 'Durability test_ISO 105-C10:2006',
 'Durability test_ISO 105-CO6-1M',
 'Durability test_ISO 105-CO6-1M ',
 'Durability test_ISO 6330 : 2000',
 'Durability test_Industrial washing machine ISO standards',
 'Durability test_Not_applicable',
 'Durability test_Ordinary washing machine',
 'Durability test_PNEN ISO 6330:2002/A1:2011',
 'Durability test_Repeated washing',
 'Durability test_UV transmission',
 'Durability test_Ultrasonic cleaner',
 'Durability test_Ultrasound bath',
 'Durability test_Washed in a bath',
 'Durability test_Washed in bath',
 'Durability test_laundering cycles',
 'Durability test_laundry cycle',
 'Durability test_laundry regimes used in hospitals',
 'Durability test_vigorous magnetic stirring',
 'Washing_Detergent_AATCC Standard Detergent WOB',
 'Washing_Detergent_AATCC WOB standard detergent',
 'Washing_Detergent_AATCC standard detergent WOB',
 'Washing_Detergent_AATCC standards specified detergent WOB',
 'Washing_Detergent_Anionic detergent',
 'Washing_Detergent_Commercial detergent',
 'Washing_Detergent_Deionized water',
 'Washing_Detergent_Distilled water',
 'Washing_Detergent_IS-I neutral soap',
 'Washing_Detergent_Na2CO3/commercial detergent',
 'Washing_Detergent_Neutral soap solution',
 'Washing_Detergent_Non-ionic detergent, Triton X-100',
 'Washing_Detergent_Nonionic detergent',
 'Washing_Detergent_Nonionic washing agent Felosan RG-N',
 'Washing_Detergent_Not_applicable',
 'Washing_Detergent_Ordinary detergent',
 'Washing_Detergent_SDC standard detergent-Sodium carbonate',
 'Washing_Detergent_Soap',
 'Washing_Detergent_Soap detergent',
 'Washing_Detergent_Sodium carbonate',
 'Washing_Detergent_Sodium carbonate and soap',
 'Washing_Detergent_Standard detergent',
 'Washing_Detergent_Tap and deionized water',
 'Washing_Detergent_Tap water',
 'Washing_Detergent_Triton-X, non-ionic detergent',
 'Washing_Detergent_nonionic detergent',
 'Washing_Detergent_sodium dodecanesulphonate',
 'Washing_Detergent_“Li Bai” washing powder',
 'Washing_Temp_25',
 'Washing_Temp_40',
 'Washing_Temp_49',
 'Washing_Temp_50',
 'Washing_Temp_60',
 'Washing_Temp_75',
 'Washing_Temp_83',
 'Washing_Temp_92',
 'Washing_Temp_95',
 'Washing_Temp_Not_applicable',
 'Washing_Temp_Room_Temp',
 'Washing_Temp_Warm water',
 'Washing_Temp_machine set with warm\nwater',
 'Washing_Temp_warm water',
 'Primary_Size (nm)',
 'Duration (h)',
 'Washing_cycles',
 'Concetration (µg/mL)']
    



def write_logs(message, message_type="Prediction"):
    """
    Write logs
    """
    #with Repository(local_dir="data", clone_from=dataset_url, use_auth_token=WRITE_TOKEN).commit(commit_message="from private", blocking=False):
     #   with open(dataset_path, "a") as csvfile:
      #          writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
       #         writer.writerow(
        #            {"name": message_type, "message": message, "time": str(datetime.now())}
         #       )
    return 

def fit_outputs_constraints(X, antimicrobial_activity_target, request: gr.Request):
    reduction_target = 100 - int(antimicrobial_activity_target)
    reduction_target_df = pd.DataFrame({'Reduction_%':[reduction_target]})
    reduction_target_df = scale_numerical(reduction_target_df, ['Reduction_%'], scaler=minmax_scaler_targets, fit=False)
    predictions = model.predict(X)[0]   
    error = np.sqrt(np.square(predictions[0]-reduction_target_df))
    return error

def predict_inverse(antimicrobial_activity_target, substrate, microorganism, num_washing_cycles, request: gr.Request):

    ### Define space and constrains

    dimensionality_dict = {}
    one_hot_mapping = {}
    for c in categorical_columns:
        dimensionality_dict[c] = 0
        one_hot_mapping[c] = []
    for c in categorical_columns:
        for t in test_data_columns:
            if c in t:
                dimensionality_dict[c]+=1 
                one_hot_mapping[c].append(t)

    domain = []

    constrained_columns = ['Substrate', 'Washing_cycles', 'Microorganism ']

    ### Add input domain
    for df_column in df_columns:
        if df_column == "Substrate":
            for one_hot_column in one_hot_mapping[df_column]:
                if one_hot_column == substrate:
                    domain.append({'name': str(one_hot_column), 'type': 'categorical', 'domain': (1.0, 1.0)})
                else:
                    domain.append({'name': str(one_hot_column), 'type': 'categorical', 'domain': (0.0, 0.0)})
        elif df_column == 'Microorganism ':
            for one_hot_column in one_hot_mapping[df_column]:
                if one_hot_column == microorganism:
                    domain.append({'name': str(one_hot_column), 'type': 'categorical', 'domain': (1.0, 1.0)})
                else:
                    domain.append({'name': str(one_hot_column), 'type': 'categorical', 'domain': (0.0, 0.0)})
        elif df_column == 'Washing_cycles':
            washing_cycles_target_df = pd.DataFrame([[0]*len(numerical_columns)], columns=numerical_columns)
            washing_cycles_target_df['Washing_cycles'].iloc[0] = int(num_washing_cycles)
            washing_cycles_target_df = scale_numerical(washing_cycles_target_df, numerical_columns, scaler=minmax_scaler_inputs, fit=False)
            domain.append({'name': str(df_column), 'type': 'continuous', 'domain': (washing_cycles_target_df["Washing_cycles"].iloc[0],
                                                                                    washing_cycles_target_df["Washing_cycles"].iloc[0])})
        elif df_column in numerical_columns:
            domain.append({'name': str(df_column), 'type': 'continuous', 'domain': (0.0,1.)})
        else:
            domain.append({'name': str(df_column), 'type': 'categorical', 'domain': (0,1),
                           'dimensionality': dimensionality_dict[df_column]})
    print("Domain is ", domain)
    print(len(domain))

    # Constraints 
    constraints = []

    def fit_outputs(x):
        return fit_outputs_constraints(x, antimicrobial_activity_target, request)
    opt = GPyOpt.methods.BayesianOptimization(f = fit_outputs,            # function to optimize       
                                              domain = domain,        # box-constraints of the problem
                                              constraints = constraints,
                                              acquisition_type ='LCB',       # LCB acquisition
                                              acquisition_weight = 0.1)   # Exploration exploitation
    # it may take a few seconds
    opt.run_optimization(max_iter=10)
    opt.plot_convergence()

    opt = GPyOpt.methods.BayesianOptimization(f = fit_outputs,            # function to optimize       
                                              domain = domain,        # box-constraints of the problem
                                              constraints = constraints,
                                              acquisition_type ='LCB',       # LCB acquisition
                                              acquisition_weight = 0.1)   # Exploration exploitation

    x_best = opt.X[np.argmin(opt.Y)]
    best_params = dict(zip(
        [el['name'] for el in domain],
        [[x] for x in x_best]))
    optimized_x = pd.DataFrame.from_dict(best_params)
    optimized_x[numerical_columns] = minmax_scaler_inputs.inverse_transform(optimized_x[numerical_columns])

    for column in optimized_x.columns:
        if column in one_hot_mapping:
            optimized_x.loc[0, column] = one_hot_mapping[column][int(optimized_x.loc[0, column])]

    optimal_concentration = optimized_x['Concetration (µg/mL)'].iloc[0] if optimized_x['Concetration (µg/mL)'].iloc[0] > 0 else 11.2

    return (optimized_x['Binder'].iloc[0], optimized_x['NMs_Type'].iloc[0], np.round(optimized_x['Primary_Size (nm)'].iloc[0], 1),
            optimized_x['NM-Shape'].iloc[0], np.round(optimized_x['Concetration (µg/mL)'].iloc[0], 1) if optimized_x['Concetration (µg/mL)'].iloc[0] else 0.1,
            optimized_x['NPs_Synthesis_method'].iloc[0], optimized_x['Application method\n'].iloc[0], 
            optimized_x['Washing_Detergent'].iloc[0], optimized_x['Washing_Temp'].iloc[0])


example_inputs = [80, "Substrate_Bamboo", "Microorganism _Alt_brassicicola", 50]

css_styling = """#submit {background: #1eccd8} 
#submit:hover {background: #a2f1f6} 
.output-image, .input-image, .image-preview {height: 250px !important}
.output-plot {height: 250px !important}"""

light_theme_colors = gr.themes.Color(c50="#e4f3fa", # Dataframe background cell content - light mode only
                                c100="#e4f3fa", # Top corner of clear button in light mode + markdown text in dark mode
                                c200="#a1c6db", # Component borders
                                c300="#FFFFFF", # 
                                c400="#e4f3fa", # Footer text
                                c500="#0c1538", # Text of component headers in light mode only
                                c600="#a1c6db", # Top corner of button in dark mode
                                c700="#475383", # Button text in light mode + component borders in dark mode
                                c800="#0c1538", # Markdown text in light mode
                                c900="#a1c6db", # Background of dataframe - dark mode
                                c950="#0c1538") # Background in dark mode only
# secondary color used for highlight box content when typing in light mode, and download option in dark mode
# primary color used for login button in dark mode
osium_theme = gr.themes.Default(primary_hue="cyan", secondary_hue="cyan", neutral_hue=light_theme_colors)
page_title = "Recommendation of optimal parameters to fulfill coating antimicrobial activity requirement and constraints"
favicon_path = "osiumai_favicon.ico"
logo_path  = "osiumai_logo.jpg"
html = f"""<html> <link rel="icon" type="image/x-icon" href="file={favicon_path}">
<img src='file={logo_path}' alt='Osium AI logo' width='200' height='100'> </html>"""


with gr.Blocks(css=css_styling, title=page_title, theme=osium_theme) as demo:
    #gr.HTML(html)
    gr.Markdown("# <p style='text-align: center;'>Get optimal textile coating recommendation to fufill your target antimicrobial activity requirement</p>")
    gr.Markdown("Recommendation of optimal parameters to fulfill textile coating antimicrobial activity requirement")
    with gr.Row():
        clear_button = gr.Button("Clear")
        prediction_button = gr.Button("Predict", elem_id="submit")
    with gr.Row():
        with gr.Column():
            gr.Markdown("### The target antimicrobial activity of your textile coating")
            antimicrobial_activity_target = gr.Number(label="Enter the minimum acceptable antimicrobial activity for your textile coating")
            gr.Markdown("### Your constraints")
            substrate = gr.Dropdown(label="Your substrate", choices=[c for c in test_data_columns if c.startswith("Substrate")])
            microorganism = gr.Dropdown(label="Microorganism", choices=[c for c in test_data_columns if c.startswith("Microorganism")])
            num_washing_cycles = gr.Number(label="Your number of washing cycles")
            
        with gr.Column():
            with gr.Row():
                with gr.Column():
                    # gr.Markdown("### Optimal conditions")
                    gr.Markdown("### Optimal nanomaterial characteristics")
                    optimal_binder = gr.Textbox(label="Optimal binder")
                    optimal_nms_type = gr.Textbox(label="Optimal nanomaterial type")
                    optimal_primary_size = gr.Textbox(label="Optimal primary size (nm)")
                    optimal_nm_shape = gr.Textbox(label="Optimal nanomaterial shape")
                    gr.Markdown("### Optimal nanomaterial application")
                    optimal_concentration = gr.Textbox(label="Optimal concentration (µg/mL)")
                    optimal_nps_synthesis = gr.Textbox(label="Optimal nanomaterial synthesis method")
                    optimal_application_method = gr.Textbox(label="Optimal application method")
                    gr.Markdown("### Optimal washing conditions")
                    optimal_washing_detergent = gr.Textbox(label="Optimal washing detergent")
                    optimal_washing_temperature = gr.Textbox(label="Optimal washing temperature")

    with gr.Row():
        gr.Examples([example_inputs], [antimicrobial_activity_target, substrate, microorganism, num_washing_cycles])
            
            

    prediction_button.click(
        fn=predict_inverse,
        inputs=[antimicrobial_activity_target, substrate, microorganism, num_washing_cycles],
        outputs=[optimal_binder, optimal_nms_type, optimal_primary_size, optimal_nm_shape, 
                optimal_concentration, optimal_nps_synthesis, optimal_application_method, 
                optimal_washing_detergent, optimal_washing_temperature],
        show_progress=True,
    )
    clear_button.click(
        lambda x: [gr.update(value=None)] * 14,
        [],
        [
            antimicrobial_activity_target,
            substrate, microorganism, num_washing_cycles,
            optimal_binder, optimal_nms_type, optimal_primary_size, optimal_nm_shape, 
            optimal_concentration, optimal_nps_synthesis, optimal_application_method, 
            optimal_washing_detergent, optimal_washing_temperature,
        ],
    )


if __name__ == "__main__":
    demo.queue(concurrency_count=2)
    demo.launch()