andreamena commited on
Commit
88e9ed9
·
verified ·
1 Parent(s): 77373b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -76,20 +76,30 @@ def predict_ui(app_choice, smiles):
76
 
77
  def batch_predict(app_choice, file_obj):
78
  try:
79
- # Robust CSV reading: skip bad lines and try multiple encodings
80
- try:
81
- df = pd.read_csv(file_obj.name, encoding='utf-8', on_bad_lines='skip')
82
- except UnicodeDecodeError:
83
- df = pd.read_csv(file_obj.name, encoding='latin1', on_bad_lines='skip')
 
 
 
 
 
 
 
 
84
 
 
85
  s_col = next((c for c in df.columns if 'smiles' in c.lower()), None)
86
  if not s_col:
87
  err_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
88
- with open(err_path, "w") as f: f.write("Error: CSV must have a 'SMILES' column.")
89
  return None, err_path
90
 
91
  results = []
92
  for s in df[s_col]:
 
93
  feats = get_mff_features(str(s))
94
  if feats is not None:
95
  if app_choice == "Structural (Strength)":
@@ -106,6 +116,7 @@ def batch_predict(app_choice, file_obj):
106
  output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".csv").name
107
  df.to_csv(output_path, index=False)
108
  return df.head(10), output_path
 
109
  except Exception as e:
110
  err_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
111
  with open(err_path, "w") as f: f.write(f"Error processing file: {str(e)}")
 
76
 
77
  def batch_predict(app_choice, file_obj):
78
  try:
79
+ filename = file_obj.name
80
+ # --- NEW: Support for both CSV and Excel ---
81
+ if filename.endswith('.csv'):
82
+ try:
83
+ df = pd.read_csv(filename, encoding='utf-8', on_bad_lines='skip')
84
+ except UnicodeDecodeError:
85
+ df = pd.read_csv(filename, encoding='latin1', on_bad_lines='skip')
86
+ elif filename.endswith(('.xls', '.xlsx')):
87
+ df = pd.read_excel(filename)
88
+ else:
89
+ err_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
90
+ with open(err_path, "w") as f: f.write("Unsupported file format. Please upload a .csv or .xlsx file.")
91
+ return None, err_path
92
 
93
+ # Find the column containing SMILES (case-insensitive)
94
  s_col = next((c for c in df.columns if 'smiles' in c.lower()), None)
95
  if not s_col:
96
  err_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
97
+ with open(err_path, "w") as f: f.write("Error: File must have a 'SMILES' column.")
98
  return None, err_path
99
 
100
  results = []
101
  for s in df[s_col]:
102
+ # This utility handles both *CC* (p-SMILES) and CCC (SMILES)
103
  feats = get_mff_features(str(s))
104
  if feats is not None:
105
  if app_choice == "Structural (Strength)":
 
116
  output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".csv").name
117
  df.to_csv(output_path, index=False)
118
  return df.head(10), output_path
119
+
120
  except Exception as e:
121
  err_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
122
  with open(err_path, "w") as f: f.write(f"Error processing file: {str(e)}")