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()