jarely commited on
Commit
d2d6da6
1 Parent(s): f011963

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -27
app.py CHANGED
@@ -1,7 +1,6 @@
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
 
@@ -12,13 +11,13 @@ def create_gui():
12
  # Define the input and output components of the GUI
13
  inputs = [
14
  gr.Dropdown(
15
- ["Benzocaine", "Ciprofloxacin", "Citalopram", "Diclofenac", "Dimetridazole", "Floxentine", "Ibuprofen", "Metronidazole", "Nitroimidazole", "Norfloxacin", "Oxytetracycline", "Salicylic Acid", "Sulfadiazine", "Sulfamethazine", "Sulfamethoxazole", "Tetracycline", "Triclosan"], label="Pharmaceutical Type"
16
  ),
17
  gr.Slider(minimum=0, maximum=950, step=0.01, label="TemP (K)"),
18
  gr.Slider(minimum=0, maximum=480, step=0.01, label="Time (min)"),
19
  gr.Slider(minimum=0, maximum=250, step=0.01, label="PS (mm)"),
20
- gr.Slider(minimum=0, maximum=2000, step=0.01, label="BET (m2/g)"),
21
- gr.Slider(minimum=0, maximum=1, step=0.01, label="PV (cm3)"),
22
  gr.Slider(minimum=0, maximum=100, step=0.01, label="C (%)"),
23
  gr.Slider(minimum=0, maximum=100, step=0.01, label="H (%)"),
24
  gr.Slider(minimum=0, maximum=100, step=0.01, label="N (%)"),
@@ -26,21 +25,21 @@ def create_gui():
26
  ]
27
 
28
  outputs = [
29
- gr.Textbox(label="Qm (mg/g)")
30
  ]
31
 
32
  # Define the title and description of the GUI
33
  title = "GUI for Pharmaceutical Removal via Biochar Adsorption"
34
- description = "This GUI uses machine learning to predict pharmaceutical removal. The app takes ten features and predicts adsorption capacity."
35
 
36
- gr.Interface(fn=biomass_prediction, inputs=inputs, outputs=outputs, title=title, description=description).launch()
37
 
38
- def biomass_prediction(pharm_type, temp, time, ps, bet, pv, c, h, n, o):
39
  """
40
- Predicts properties based on input features of the biomass.
41
 
42
  Parameters:
43
- pharm_type (str): Pharmaceutical type.
44
  TemP (float): Temperature K of the biomass.
45
  Time (float): Time it takes for biochar to remove pharmaceutical in minutes .
46
  PS (float): Pore space in mm.
@@ -52,32 +51,21 @@ def biomass_prediction(pharm_type, temp, time, ps, bet, pv, c, h, n, o):
52
  O (float): O (%) content of the biomass.
53
 
54
  Returns:
55
- float: Absorption capacity
56
  """
57
 
58
  # Concatenate the inputs into a numpy array
59
- input_data = pd.DataFrame([[pharm_type, temp, time, ps, bet, pv, c, h, n, o]],
60
- 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
- try:
68
- with open('xgb_best_params.pkl', 'rb') as file:
69
  loaded_model = pickle.load(file)
70
- except FileNotFoundError:
71
- return "Model file not found"
72
  # Make predictions using the loaded machine learning model
73
  prediction = loaded_model.predict(input_data)
74
- prediction = np.round(prediction, 2)
75
 
76
- # Extract prediction value and save it to a separate variable
77
- output1 = prediction[0][0]
78
-
79
- # Return predicted output values
80
- return output1
81
 
82
  if __name__ == '__main__':
83
  # Create GUI
 
1
  import gradio as gr
2
  import numpy as np
3
  import pandas as pd
 
4
  from sklearn.ensemble import GradientBoostingRegressor
5
  import pickle
6
 
 
11
  # Define the input and output components of the GUI
12
  inputs = [
13
  gr.Dropdown(
14
+ ["Benzocaine", "Ciprofloxacin", "Citalopram", "Diclofenac", "Dimetridazole", "Floxentine", "Ibuprofen", "Metronidazole", "Nitroimidazole", "Norfloxacin", "Oxytetracycline", "Salicylic Acid", "Sulfadiazine", "Sulfamethazine", "Sulfamethoxazole", "Tetracycline", "Triclosan"], label="Select Pharmaceutical"
15
  ),
16
  gr.Slider(minimum=0, maximum=950, step=0.01, label="TemP (K)"),
17
  gr.Slider(minimum=0, maximum=480, step=0.01, label="Time (min)"),
18
  gr.Slider(minimum=0, maximum=250, step=0.01, label="PS (mm)"),
19
+ gr.Slider(minimum=0, maximum=2000, step=0.01, label="BET surface area (m2/g)"),
20
+ gr.Slider(minimum=0, maximum=1, step=0.01, label="Pore Volume (cm3)"),
21
  gr.Slider(minimum=0, maximum=100, step=0.01, label="C (%)"),
22
  gr.Slider(minimum=0, maximum=100, step=0.01, label="H (%)"),
23
  gr.Slider(minimum=0, maximum=100, step=0.01, label="N (%)"),
 
25
  ]
26
 
27
  outputs = [
28
+ gr.Textbox(label="Predicted Maximum Adsorption Capacity, Qm (mg/g)")
29
  ]
30
 
31
  # Define the title and description of the GUI
32
  title = "GUI for Pharmaceutical Removal via Biochar Adsorption"
33
+ description = "This GUI uses machine learning to predict maximum absorption capacity (Qm) for the selected pharmaceutical based on various input parameters."
34
 
35
+ gr.Interface(fn=predict_qm, inputs=inputs, outputs=outputs, title=title, description=description).launch()
36
 
37
+ def predict_qm(pharmaceutical, temp, time, ps, bet, pv, c, h, n, o):
38
  """
39
+ Predicts maximum adsorption capacity (Qm) based on input features for the selected pharmaceutical.
40
 
41
  Parameters:
42
+ pharmaceutical (str): Pharmaceutical type.
43
  TemP (float): Temperature K of the biomass.
44
  Time (float): Time it takes for biochar to remove pharmaceutical in minutes .
45
  PS (float): Pore space in mm.
 
51
  O (float): O (%) content of the biomass.
52
 
53
  Returns:
54
+ float: The predicted maximum adsorption capacity (Qm).
55
  """
56
 
57
  # Concatenate the inputs into a numpy array
58
+ input_data = pd.DataFrame([[temp, time, ps, bet, pv, c, h, n, o]], columns=["Temp", "Time", "PS", "BET", "PV", "C", "H", "N", "O"])
 
 
 
 
 
59
 
60
  # Load the trained model from the pickled file
61
+ with open('xgb_best_params.pkl', 'rb') as file:
 
62
  loaded_model = pickle.load(file)
63
+
 
64
  # Make predictions using the loaded machine learning model
65
  prediction = loaded_model.predict(input_data)
66
+ qm = np.round(prediction[0], 2)
67
 
68
+ return qm
 
 
 
 
69
 
70
  if __name__ == '__main__':
71
  # Create GUI