Spaces:
Running
Running
removed duplicate form
Browse files
app.py
CHANGED
@@ -51,61 +51,60 @@ This app predicts the HOMO-LUMO energy gap for molecules using a trained Graph N
|
|
51 |
**Instructions:**
|
52 |
- Enter a **single SMILES** string or **comma-separated list** in the box below.
|
53 |
- Or **upload a CSV file** containing a single column of SMILES strings.
|
54 |
-
- **Note**: If you've uploaded a CSV and want to switch to typing SMILES, please click the
|
55 |
- SMILES format should look like: `CC(=O)Oc1ccccc1C(=O)O` (for aspirin).
|
56 |
- The app will display predictions and molecule images (up to 10 shown at once).
|
57 |
""")
|
58 |
|
59 |
-
# File uploader outside the form (this is important for Hugging Face Spaces compatibility)
|
60 |
-
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
61 |
|
62 |
-
with st.form("input_form"):
|
63 |
-
smiles_input = st.text_area(
|
64 |
-
"Enter SMILES string(s)",
|
65 |
-
placeholder="C1=CC=CC=C1, CC(=O)Oc1ccccc1C(=O)O",
|
66 |
-
height=120
|
67 |
-
)
|
68 |
-
run_button = st.form_submit_button("Submit")
|
69 |
|
|
|
|
|
70 |
smiles_list = []
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
try:
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
# Debug information
|
83 |
-
st.write(f"File size: {len(content)} bytes")
|
84 |
-
|
85 |
-
df = pd.read_csv(StringIO(content), comment="#")
|
86 |
-
st.write(f"CSV loaded with {df.shape[0]} rows and {df.shape[1]} columns")
|
87 |
-
|
88 |
-
# choose the SMILES column
|
89 |
if df.shape[1] == 1:
|
90 |
smiles_col = df.iloc[:, 0]
|
91 |
elif "smiles" in [c.lower() for c in df.columns]:
|
92 |
smiles_col = df[[c for c in df.columns if c.lower() == "smiles"][0]]
|
93 |
else:
|
94 |
-
st.error(
|
|
|
|
|
|
|
95 |
smiles_col = None
|
96 |
|
97 |
if smiles_col is not None:
|
98 |
smiles_list = smiles_col.dropna().astype(str).tolist()
|
99 |
-
st.success(f"{len(smiles_list)} SMILES loaded from CSV")
|
100 |
except Exception as e:
|
101 |
-
st.error(f"
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
-
# Textarea path
|
105 |
-
elif smiles_input.strip():
|
106 |
-
raw_input = smiles_input.replace("\n", ",")
|
107 |
-
smiles_list = [s.strip() for s in raw_input.split(",") if s.strip()]
|
108 |
-
st.success(f"{len(smiles_list)} SMILES parsed from text")
|
109 |
|
110 |
# Run Inference
|
111 |
if smiles_list:
|
@@ -154,4 +153,4 @@ if smiles_list:
|
|
154 |
st.download_button(label="Download Predictions as CSV",
|
155 |
data=result_df.to_csv(index=False).encode('utf-8'),
|
156 |
file_name="homolumo_predictions.csv",
|
157 |
-
mime="text/csv")
|
|
|
51 |
**Instructions:**
|
52 |
- Enter a **single SMILES** string or **comma-separated list** in the box below.
|
53 |
- Or **upload a CSV file** containing a single column of SMILES strings.
|
54 |
+
- **Note**: If you've uploaded a CSV and want to switch to typing SMILES, please click the “X” next to the uploaded file to clear it.
|
55 |
- SMILES format should look like: `CC(=O)Oc1ccccc1C(=O)O` (for aspirin).
|
56 |
- The app will display predictions and molecule images (up to 10 shown at once).
|
57 |
""")
|
58 |
|
|
|
|
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
|
62 |
+
# Single input form
|
63 |
smiles_list = []
|
64 |
|
65 |
+
with st.form("smiles_or_csv"):
|
66 |
+
smiles_text = st.text_area(
|
67 |
+
"SMILES (comma or line-separated)",
|
68 |
+
placeholder="C1=CC=CC=C1\nCC(=O)Oc1ccccc1C(=O)O",
|
69 |
+
height=120,
|
70 |
+
)
|
71 |
+
csv_file = st.file_uploader(
|
72 |
+
"…or upload a one-column CSV",
|
73 |
+
type=["csv"],
|
74 |
+
)
|
75 |
+
run = st.form_submit_button("Run Prediction")
|
76 |
+
|
77 |
+
if run:
|
78 |
+
if csv_file is not None:
|
79 |
try:
|
80 |
+
csv_file.seek(0)
|
81 |
+
df = pd.read_csv(StringIO(csv_file.getvalue().decode("utf‑8")), comment="#")
|
82 |
+
|
83 |
+
# pick SMILES column
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
if df.shape[1] == 1:
|
85 |
smiles_col = df.iloc[:, 0]
|
86 |
elif "smiles" in [c.lower() for c in df.columns]:
|
87 |
smiles_col = df[[c for c in df.columns if c.lower() == "smiles"][0]]
|
88 |
else:
|
89 |
+
st.error(
|
90 |
+
"CSV must have a single column **or** a column named 'SMILES'. "
|
91 |
+
f"Found columns: {', '.join(df.columns)}"
|
92 |
+
)
|
93 |
smiles_col = None
|
94 |
|
95 |
if smiles_col is not None:
|
96 |
smiles_list = smiles_col.dropna().astype(str).tolist()
|
97 |
+
st.success(f"{len(smiles_list)} SMILES loaded from CSV.")
|
98 |
except Exception as e:
|
99 |
+
st.error(f"CSV read error: {e}")
|
100 |
+
|
101 |
+
elif smiles_text.strip():
|
102 |
+
raw = smiles_text.replace("\n", ",")
|
103 |
+
smiles_list = [s.strip() for s in raw.split(",") if s.strip()]
|
104 |
+
st.success(f"{len(smiles_list)} SMILES parsed from textbox.")
|
105 |
+
else:
|
106 |
+
st.warning("Please paste SMILES or upload a CSV before pressing *Run*.")
|
107 |
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
# Run Inference
|
110 |
if smiles_list:
|
|
|
153 |
st.download_button(label="Download Predictions as CSV",
|
154 |
data=result_df.to_csv(index=False).encode('utf-8'),
|
155 |
file_name="homolumo_predictions.csv",
|
156 |
+
mime="text/csv")
|