hbhzm commited on
Commit
8037081
·
verified ·
1 Parent(s): cd13638

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()