MooseML commited on
Commit
cdf65e9
·
1 Parent(s): d96a023

moved file uploader outside form and added better csv file upload error handling

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -51,13 +51,13 @@ 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 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
  with st.form("input_form"):
63
  smiles_input = st.text_area(
@@ -65,10 +65,6 @@ with st.form("input_form"):
65
  placeholder="C1=CC=CC=C1, CC(=O)Oc1ccccc1C(=O)O",
66
  height=120
67
  )
68
- uploaded_file = st.file_uploader(
69
- "…or upload a CSV file",
70
- type=["csv"]
71
- )
72
  run_button = st.form_submit_button("Submit")
73
 
74
  smiles_list = []
@@ -78,9 +74,16 @@ if run_button:
78
  # CSV path
79
  if uploaded_file is not None:
80
  try:
81
- uploaded_file.seek(0)
82
- data = uploaded_file.getvalue() # read bytes
83
- df = pd.read_csv(StringIO(data.decode("utf-8")), comment="#")
 
 
 
 
 
 
 
84
 
85
  # choose the SMILES column
86
  if df.shape[1] == 1:
@@ -88,15 +91,15 @@ if run_button:
88
  elif "smiles" in [c.lower() for c in df.columns]:
89
  smiles_col = df[[c for c in df.columns if c.lower() == "smiles"][0]]
90
  else:
91
- st.error("CSV must have a single column or a column named 'SMILES'"
92
- f"Found columns: {', '.join(df.columns)}")
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"Could not read CSV: {e}")
 
100
 
101
  # Textarea path
102
  elif smiles_input.strip():
@@ -151,4 +154,4 @@ if smiles_list:
151
  st.download_button(label="Download Predictions as CSV",
152
  data=result_df.to_csv(index=False).encode('utf-8'),
153
  file_name="homolumo_predictions.csv",
154
- 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
+ # 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(
 
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 = []
 
74
  # CSV path
75
  if uploaded_file is not None:
76
  try:
77
+ # More robust file reading
78
+ content = uploaded_file.read()
79
+ if isinstance(content, bytes):
80
+ content = content.decode('utf-8')
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:
 
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(f"CSV must have a single column or a column named 'SMILES'. Found columns: {', '.join(df.columns)}")
 
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"Could not read CSV: {str(e)}")
102
+ st.error("Error details:", exc_info=True)
103
 
104
  # Textarea path
105
  elif smiles_input.strip():
 
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")