harshinde commited on
Commit
ce7cc24
·
verified ·
1 Parent(s): e4f89cd

Upload src/streamlit_app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +40 -24
src/streamlit_app.py CHANGED
@@ -103,33 +103,49 @@ if model_option == "Select a single model":
103
 
104
  # Main content
105
  st.header("Upload Data")
106
- uploaded_files = st.file_uploader("Choose .h5 files...", type="h5", accept_multiple_files=True)
 
 
 
 
 
 
 
 
 
 
 
107
  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
- # 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
 
 
103
 
104
  # Main content
105
  st.header("Upload Data")
106
+
107
+ # Initialize session state for error tracking if not exists
108
+ if 'upload_errors' not in st.session_state:
109
+ st.session_state.upload_errors = []
110
+
111
+ uploaded_files = st.file_uploader(
112
+ "Choose .h5 files...",
113
+ type="h5",
114
+ accept_multiple_files=True,
115
+ help="Upload your .h5 files here. Maximum file size is 200MB."
116
+ )
117
+
118
  if uploaded_files:
119
+ for uploaded_file in uploaded_files:
120
+ st.write(f"Processing file: {uploaded_file.name}")
 
 
 
 
121
 
122
+ # Display file details for debugging
123
+ st.write(f"File size: {uploaded_file.size} bytes")
 
 
124
 
125
+ with st.spinner('Classifying...'):
126
+ try:
127
+ # Read the file directly using BytesIO
128
+ import io
129
+ bytes_data = uploaded_file.getvalue()
130
+ bytes_io = io.BytesIO(bytes_data)
131
+
132
+ with h5py.File(bytes_io, 'r') as hdf:
133
+ # Check if 'img' exists in the file
134
+ if 'img' not in hdf:
135
+ st.error(f"Error: 'img' dataset not found in {uploaded_file.name}")
136
+ continue
137
+
138
+ data = np.array(hdf.get('img'))
139
+ data[np.isnan(data)] = 0.000001
140
+ channels = config["dataset_config"]["channels"]
141
+ image = np.zeros((128, 128, len(channels)))
142
+
143
+ except h5py.Error as he:
144
+ st.error(f"H5PY Error processing {uploaded_file.name}: {str(he)}")
145
+ continue
146
+ except Exception as e:
147
+ st.error(f"Error processing {uploaded_file.name}: {str(e)}")
148
+ continue
149
  for i, channel in enumerate(channels):
150
  image[:, :, i] = data[:, :, channel-1]
151