harshinde commited on
Commit
1570ed8
·
verified ·
1 Parent(s): 31a9afa

Upload src/streamlit_app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +22 -5
src/streamlit_app.py CHANGED
@@ -108,11 +108,28 @@ if uploaded_files:
108
  for uploaded_file in uploaded_files:
109
  st.write(f"Processing file: {uploaded_file.name}")
110
  with st.spinner('Classifying...'):
111
- with h5py.File(uploaded_file, 'r') as hdf:
112
- data = np.array(hdf.get('img'))
113
- data[np.isnan(data)] = 0.000001
114
- channels = config["dataset_config"]["channels"]
115
- image = np.zeros((128, 128, len(channels)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  for i, channel in enumerate(channels):
117
  image[:, :, i] = data[:, :, channel-1]
118
 
 
108
  for uploaded_file in uploaded_files:
109
  st.write(f"Processing file: {uploaded_file.name}")
110
  with st.spinner('Classifying...'):
111
+ # Create a temporary file path
112
+ import tempfile
113
+ import os
114
+
115
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.h5', dir='/app/uploads') as tmp_file:
116
+ # Write the uploaded file to disk
117
+ tmp_file.write(uploaded_file.getvalue())
118
+ tmp_path = tmp_file.name
119
+
120
+ try:
121
+ with h5py.File(tmp_path, 'r') as hdf:
122
+ data = np.array(hdf.get('img'))
123
+ data[np.isnan(data)] = 0.000001
124
+ channels = config["dataset_config"]["channels"]
125
+ image = np.zeros((128, 128, len(channels)))
126
+
127
+ # Clean up the temporary file
128
+ os.unlink(tmp_path)
129
+ except Exception as e:
130
+ st.error(f"Error processing file: {str(e)}")
131
+ if os.path.exists(tmp_path):
132
+ os.unlink(tmp_path)
133
  for i, channel in enumerate(channels):
134
  image[:, :, i] = data[:, :, channel-1]
135