|
import gradio as gr |
|
import numpy as np |
|
import pandas as pd |
|
from sklearn.multioutput import MultiOutputRegressor |
|
from sklearn.ensemble import GradientBoostingRegressor |
|
import pickle |
|
|
|
def create_gui(): |
|
""" |
|
Creates and launches the Gradio user interface for the biomass adsorption prediction. |
|
""" |
|
|
|
inputs = [ |
|
gr.Dropdown(choices=["Benzocaine", "Ciprofloxacin", "Citalopram", "Diclofenac", "Dimetridazole", |
|
"Floxentine", "Ibuprofen", "Metronidazole", "Nitroimidazole", "Norfloxacin", |
|
"Oxytetracycline", "Salicylic Acid", "Sulfadiazine", "Sulfamethazine", "Sulfamethoxazole", |
|
"Tetracycline", "Triclosan"], |
|
label="Pharmaceutical Type"), |
|
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="TemP (K)"), |
|
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="Time (min)"), |
|
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="PS (mm)"), |
|
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="BET (m2/g)"), |
|
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="PV (cm3)"), |
|
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="C (%)"), |
|
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="H (%)"), |
|
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="N (%)"), |
|
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="O (%)"), |
|
] |
|
|
|
outputs = [ |
|
gr.components.Textbox(label="Qm (mg/g)"), |
|
] |
|
|
|
|
|
title = "GUI for Pharmaceutical Removal via Biochar Adsorption" |
|
description = "This GUI uses machine learning to predict pharmaceutical removal. The app takes ten features and predicts adsorption capacity." |
|
|
|
gr.Interface(fn=biomass_prediction, inputs=inputs, outputs=outputs, title=title, description=description).launch() |
|
|
|
def biomass_prediction(*inputs): |
|
""" |
|
Predicts properties based on input features of the biomass. |
|
|
|
Parameters: |
|
pharm_type (str): Pharmaceutical type. |
|
TemP (float): Temperature K of the biomass. |
|
Time (float): Minutes for x. |
|
PS (float): Pore space in mm. |
|
BET (float): Area of powder in m2/g being poured into the solution. |
|
PV (float): Pore volume in cm3. |
|
C (float): C (%) content of the biomass. |
|
H (float): H (%) content of the biomass. |
|
N (float): N (%) content of the biomass. |
|
O (float): O (%) content of the biomass. |
|
|
|
Returns: |
|
float: Absorption capacity |
|
""" |
|
|
|
input_data = pd.DataFrame([inputs], columns=['pharm type', 'TemP (K)', 'Time (min)', 'PS (mm)', 'BET (m2/g)', 'PV (cm3)', 'C (%)', 'H (%)', 'N (%)', 'O (%)']) |
|
|
|
|
|
if np.all(input_data <= 0): |
|
return 0 |
|
|
|
|
|
with open('trained_model.pkl', 'rb') as file: |
|
loaded_model = pickle.load(file) |
|
|
|
|
|
prediction = loaded_model.predict(input_data) |
|
prediction = np.round(prediction, 2) |
|
|
|
|
|
output1 = prediction[0][0] |
|
|
|
|
|
return output1 |
|
|
|
if __name__ == '__main__': |
|
|
|
create_gui() |