hbhzm commited on
Commit
a9bbd1e
·
verified ·
1 Parent(s): 1afebd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -57
app.py CHANGED
@@ -1,72 +1,84 @@
1
  import gradio as gr
 
2
  import subprocess
3
- import pandas as pd
4
  import uuid
5
- import os
6
 
7
- def run_prediction(data_path, model_path, prediction_type="classification", output_path="results.csv"):
8
- command = [
9
- "python", model_path,
10
- "--data_path", data_path,
11
- "--checkpoint_path", model_path.replace("predict.py", "../models/fusion_model.pt" if "mod" in model_path else "../models/vanilla_model.pt"),
12
- "--prediction_type", prediction_type,
13
- "--save_dir", ".",
14
- "--preds_path", output_path
15
- ]
16
- subprocess.run(command, check=True)
17
- return pd.read_csv(output_path)
 
 
 
 
 
 
 
 
18
 
19
- def predict_from_smiles(smiles, model_choice, dataset_choice):
20
- temp_id = uuid.uuid4().hex
21
- temp_csv = f"data/temp_{temp_id}.csv"
22
- out_csv = f"data/output_{temp_id}.csv"
23
- df = pd.DataFrame([{"smiles": smiles, "compound_name": "molecule"}])
24
- df.to_csv(temp_csv, index=False)
 
 
 
25
 
26
- model_path = "chemprop_mod/predict.py" if model_choice == "Fusion (GNN + Transformer)" else "chemprop/predict.py"
27
-
 
 
 
 
 
28
  try:
29
- predictions = run_prediction(temp_csv, model_path, output_path=out_csv)
30
- return predictions
31
- except subprocess.CalledProcessError as e:
32
- return f"Error: {str(e)}"
 
 
33
 
34
- def predict_from_file(file_obj, model_choice, dataset_choice):
35
- temp_id = uuid.uuid4().hex
36
- file_path = f"data/uploaded_{temp_id}.csv"
37
- out_csv = f"data/output_file_{temp_id}.csv"
38
-
39
- with open(file_path, "wb") as f:
40
- f.write(file_obj.read())
41
 
42
- model_path = "chemprop_mod/predict.py" if model_choice == "Fusion (GNN + Transformer)" else "chemprop/predict.py"
 
 
 
 
43
 
44
- try:
45
- predictions = run_prediction(file_path, model_path, output_path=out_csv)
46
- return predictions
47
- except subprocess.CalledProcessError as e:
48
- return f"Error: {str(e)}"
49
 
50
- with gr.Blocks() as demo:
51
- gr.Markdown("## 🧪 Drug Property Prediction with Fusion Models")
52
- gr.Markdown("Choose prediction input type and compare Chemprop vs Fusion model")
 
 
 
 
 
 
 
53
 
54
- with gr.Tab("Single SMILES"):
55
- with gr.Row():
56
- smiles_input = gr.Textbox(label="Enter SMILES string")
57
- model_select = gr.Radio(["Vanilla Chemprop", "Fusion (GNN + Transformer)"], label="Model")
58
- dataset_select = gr.Dropdown(["BBBP", "BACE"], label="Dataset")
59
- predict_button = gr.Button("Predict")
60
- result_output = gr.Dataframe(label="Prediction Result")
61
- predict_button.click(fn=predict_from_smiles, inputs=[smiles_input, model_select, dataset_select], outputs=result_output)
62
 
63
- with gr.Tab("Upload File"):
64
- with gr.Row():
65
- file_input = gr.File(label="Upload CSV File")
66
- model_select_file = gr.Radio(["Vanilla Chemprop", "Fusion (GNN + Transformer)"], label="Model")
67
- dataset_select_file = gr.Dropdown(["BBBP", "BACE"], label="Dataset")
68
- predict_button_file = gr.Button("Predict")
69
- result_output_file = gr.Dataframe(label="Prediction Result")
70
- predict_button_file.click(fn=predict_from_file, inputs=[file_input, model_select_file, dataset_select_file], outputs=result_output_file)
71
 
72
  demo.launch()
 
1
  import gradio as gr
2
+ import os
3
  import subprocess
 
4
  import uuid
5
+ import pandas as pd
6
 
7
+ # Helper to save SMILES string to a temporary CSV
8
+ def save_smiles_to_csv(smiles: str, temp_dir="temp_inputs"):
9
+ os.makedirs(temp_dir, exist_ok=True)
10
+ file_path = os.path.join(temp_dir, f"{uuid.uuid4().hex}.csv")
11
+ df = pd.DataFrame({"smiles": [smiles]})
12
+ df.to_csv(file_path, index=False)
13
+ return file_path
14
+
15
+ # Core prediction logic
16
+ def predict(model_version, dataset_name, input_type, file=None, smiles=None):
17
+ # Set paths
18
+ if model_version == "Vanilla Chemprop":
19
+ model_dir = "chemprop"
20
+ model_path = f"model_weight/{dataset_name}/best_unbalanced.pt"
21
+ script_path = "chemprop/chemprop/cli/predict.py"
22
+ else:
23
+ model_dir = "chemprop_update"
24
+ model_path = f"model_weight/{dataset_name}/best_bert_fusion.pt"
25
+ script_path = "chemprop_update/chemprop/cli/predict.py"
26
 
27
+ # Prepare input file
28
+ if input_type == "Upload CSV":
29
+ if file is None:
30
+ return "Please upload a CSV file."
31
+ input_path = file.name
32
+ else:
33
+ if not smiles:
34
+ return "Please enter a SMILES string."
35
+ input_path = save_smiles_to_csv(smiles)
36
 
37
+ # Run prediction command
38
+ cmd = [
39
+ "python", script_path,
40
+ "--test-path", input_path,
41
+ "--model-paths", model_path,
42
+ "--smiles-columns", "smiles"
43
+ ]
44
  try:
45
+ result = subprocess.run(cmd, capture_output=True, text=True)
46
+ if result.returncode != 0:
47
+ return f"Error:\n{result.stderr}"
48
+ return f"Prediction Output:\n{result.stdout}"
49
+ except Exception as e:
50
+ return f"Execution Failed: {str(e)}"
51
 
52
+ # Gradio UI setup
53
+ with gr.Blocks() as demo:
54
+ gr.Markdown("## 🧪 Molecular Property Prediction using Chemprop and Transformers")
 
 
 
 
55
 
56
+ model_version = gr.Radio(
57
+ ["Vanilla Chemprop", "Updated Fusion Model"],
58
+ label="Select Model Version"
59
+ )
60
+ dataset_name = gr.Radio(["BBBP", "ClinTox"], label="Select Dataset")
61
 
62
+ input_type = gr.Radio(["Upload CSV", "Single SMILES"], label="Input Type")
 
 
 
 
63
 
64
+ file_input = gr.File(file_types=[".csv"], label="Upload CSV", visible=True)
65
+ smiles_input = gr.Textbox(label="Enter SMILES string", visible=False)
66
+
67
+ def toggle_inputs(choice):
68
+ return {
69
+ file_input: gr.update(visible=(choice == "Upload CSV")),
70
+ smiles_input: gr.update(visible=(choice == "Single SMILES"))
71
+ }
72
+
73
+ input_type.change(toggle_inputs, input_type, [file_input, smiles_input])
74
 
75
+ predict_button = gr.Button("Predict")
76
+ output = gr.Textbox(label="Output")
 
 
 
 
 
 
77
 
78
+ predict_button.click(
79
+ fn=predict,
80
+ inputs=[model_version, dataset_name, input_type, file_input, smiles_input],
81
+ outputs=output
82
+ )
 
 
 
83
 
84
  demo.launch()