IGUIDEpharmGUI / app.py
jarely's picture
Update app.py
d2d6da6 verified
raw
history blame contribute delete
No virus
3.18 kB
import gradio as gr
import numpy as np
import pandas as pd
from sklearn.ensemble import GradientBoostingRegressor
import pickle
def create_gui():
"""
Creates and launches the Gradio user interface for the biomass adsorption prediction.
"""
# Define the input and output components of the GUI
inputs = [
gr.Dropdown(
["Benzocaine", "Ciprofloxacin", "Citalopram", "Diclofenac", "Dimetridazole", "Floxentine", "Ibuprofen", "Metronidazole", "Nitroimidazole", "Norfloxacin", "Oxytetracycline", "Salicylic Acid", "Sulfadiazine", "Sulfamethazine", "Sulfamethoxazole", "Tetracycline", "Triclosan"], label="Select Pharmaceutical"
),
gr.Slider(minimum=0, maximum=950, step=0.01, label="TemP (K)"),
gr.Slider(minimum=0, maximum=480, step=0.01, label="Time (min)"),
gr.Slider(minimum=0, maximum=250, step=0.01, label="PS (mm)"),
gr.Slider(minimum=0, maximum=2000, step=0.01, label="BET surface area (m2/g)"),
gr.Slider(minimum=0, maximum=1, step=0.01, label="Pore Volume (cm3)"),
gr.Slider(minimum=0, maximum=100, step=0.01, label="C (%)"),
gr.Slider(minimum=0, maximum=100, step=0.01, label="H (%)"),
gr.Slider(minimum=0, maximum=100, step=0.01, label="N (%)"),
gr.Slider(minimum=0, maximum=100, step=0.01, label="O (%)"),
]
outputs = [
gr.Textbox(label="Predicted Maximum Adsorption Capacity, Qm (mg/g)")
]
# Define the title and description of the GUI
title = "GUI for Pharmaceutical Removal via Biochar Adsorption"
description = "This GUI uses machine learning to predict maximum absorption capacity (Qm) for the selected pharmaceutical based on various input parameters."
gr.Interface(fn=predict_qm, inputs=inputs, outputs=outputs, title=title, description=description).launch()
def predict_qm(pharmaceutical, temp, time, ps, bet, pv, c, h, n, o):
"""
Predicts maximum adsorption capacity (Qm) based on input features for the selected pharmaceutical.
Parameters:
pharmaceutical (str): Pharmaceutical type.
TemP (float): Temperature K of the biomass.
Time (float): Time it takes for biochar to remove pharmaceutical in minutes .
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: The predicted maximum adsorption capacity (Qm).
"""
# Concatenate the inputs into a numpy array
input_data = pd.DataFrame([[temp, time, ps, bet, pv, c, h, n, o]], columns=["Temp", "Time", "PS", "BET", "PV", "C", "H", "N", "O"])
# Load the trained model from the pickled file
with open('xgb_best_params.pkl', 'rb') as file:
loaded_model = pickle.load(file)
# Make predictions using the loaded machine learning model
prediction = loaded_model.predict(input_data)
qm = np.round(prediction[0], 2)
return qm
if __name__ == '__main__':
# Create GUI
create_gui()