judebebo32
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.multioutput import MultiOutputRegressor
|
5 |
+
from sklearn.ensemble import GradientBoostingRegressor
|
6 |
+
import pickle
|
7 |
+
|
8 |
+
def create_gui():
|
9 |
+
"""
|
10 |
+
Creates and launches the Gradio user interface for the biomass adsorption prediction.
|
11 |
+
"""
|
12 |
+
# Define the input and output components of the GUI
|
13 |
+
inputs = [
|
14 |
+
gr.Dropdown(choices=["Benzocaine", "Ciprofloxacin", "Citalopram", "Diclofenac", "Dimetridazole",
|
15 |
+
"Floxentine", "Ibuprofen", "Metronidazole", "Nitroimidazole", "Norfloxacin",
|
16 |
+
"Oxytetracycline", "Salicylic Acid", "Sulfadiazine", "Sulfamethazine", "Sulfamethoxazole",
|
17 |
+
"Tetracycline", "Triclosan"],
|
18 |
+
label="Pharmaceutical Type"),
|
19 |
+
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="TemP (K)"),
|
20 |
+
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="Time (min)"),
|
21 |
+
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="PS (mm)"),
|
22 |
+
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="BET (m2/g)"),
|
23 |
+
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="PV (cm3)"),
|
24 |
+
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="C (%)"),
|
25 |
+
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="H (%)"),
|
26 |
+
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="N (%)"),
|
27 |
+
gr.components.Slider(minimum=0, maximum=100, step=0.01, label="O (%)"),
|
28 |
+
]
|
29 |
+
|
30 |
+
outputs = [
|
31 |
+
gr.components.Textbox(label="Qm (mg/g)"),
|
32 |
+
]
|
33 |
+
|
34 |
+
# Define the title and description of the GUI
|
35 |
+
title = "GUI for Pharmaceutical Removal via Biochar Adsorption"
|
36 |
+
description = "This GUI uses machine learning to predict pharmaceutical removal. The app takes ten features and predicts adsorption capacity."
|
37 |
+
|
38 |
+
gr.Interface(fn=biomass_prediction, inputs=inputs, outputs=outputs, title=title, description=description).launch()
|
39 |
+
|
40 |
+
def biomass_prediction(*inputs):
|
41 |
+
"""
|
42 |
+
Predicts properties based on input features of the biomass.
|
43 |
+
|
44 |
+
Parameters:
|
45 |
+
pharm_type (str): Pharmaceutical type.
|
46 |
+
TemP (float): Temperature K of the biomass.
|
47 |
+
Time (float): Minutes for x.
|
48 |
+
PS (float): Pore space in mm.
|
49 |
+
BET (float): Area of powder in m2/g being poured into the solution.
|
50 |
+
PV (float): Pore volume in cm3.
|
51 |
+
C (float): C (%) content of the biomass.
|
52 |
+
H (float): H (%) content of the biomass.
|
53 |
+
N (float): N (%) content of the biomass.
|
54 |
+
O (float): O (%) content of the biomass.
|
55 |
+
|
56 |
+
Returns:
|
57 |
+
float: Absorption capacity
|
58 |
+
"""
|
59 |
+
# Concatenate the inputs into a numpy array
|
60 |
+
input_data = pd.DataFrame([inputs], columns=['pharm type', 'TemP (K)', 'Time (min)', 'PS (mm)', 'BET (m2/g)', 'PV (cm3)', 'C (%)', 'H (%)', 'N (%)', 'O (%)'])
|
61 |
+
|
62 |
+
# Check if values are zero or negative
|
63 |
+
if np.all(input_data <= 0):
|
64 |
+
return 0
|
65 |
+
|
66 |
+
# Load the trained model from the pickled file
|
67 |
+
with open('trained_model.pkl', 'rb') as file:
|
68 |
+
loaded_model = pickle.load(file)
|
69 |
+
|
70 |
+
# Make predictions using the loaded machine learning model
|
71 |
+
prediction = loaded_model.predict(input_data)
|
72 |
+
prediction = np.round(prediction, 2)
|
73 |
+
|
74 |
+
# Extract prediction value and save it to a separate variable
|
75 |
+
output1 = prediction[0][0]
|
76 |
+
|
77 |
+
# Return predicted output values
|
78 |
+
return output1
|
79 |
+
|
80 |
+
if __name__ == '__main__':
|
81 |
+
# Create GUI
|
82 |
+
create_gui()
|