File size: 3,480 Bytes
5c5bd15 |
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 |
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.
"""
# Define the input and output components of the GUI
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)"),
]
# Define the title and description of the GUI
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
"""
# Concatenate the inputs into a numpy array
input_data = pd.DataFrame([inputs], columns=['pharm type', 'TemP (K)', 'Time (min)', 'PS (mm)', 'BET (m2/g)', 'PV (cm3)', 'C (%)', 'H (%)', 'N (%)', 'O (%)'])
# Check if values are zero or negative
if np.all(input_data <= 0):
return 0
# Load the trained model from the pickled file
with open('trained_model.pkl', 'rb') as file:
loaded_model = pickle.load(file)
# Make predictions using the loaded machine learning model
prediction = loaded_model.predict(input_data)
prediction = np.round(prediction, 2)
# Extract prediction value and save it to a separate variable
output1 = prediction[0][0]
# Return predicted output values
return output1
if __name__ == '__main__':
# Create GUI
create_gui() |