Spaces:
Build error
Build error
Update app.py
Browse files
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
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
try:
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
return f"
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
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 |
-
|
51 |
-
gr.
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
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 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
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()
|