Pavan2k4 commited on
Commit
4cf96ef
·
verified ·
1 Parent(s): dad7111

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -28
app.py CHANGED
@@ -72,18 +72,22 @@ def refine_mask(mask, blur_kernel=5, threshold_value=127, morph_kernel_size=3, m
72
 
73
  # save to dir func
74
  def save_to_hf_repo(local_path, repo_path):
 
 
 
 
75
  try:
76
- hf_api.upload_file(
77
- path_or_fileobj=local_path,
78
- path_in_repo=repo_path,
79
- repo_id=REPO_ID,
80
- repo_type=REPO_TYPE,
81
- token=HF_TOKEN
82
- )
 
83
  st.success(f"File uploaded successfully to {repo_path}")
84
  except Exception as e:
85
- st.error(f"Uploaded")
86
-
87
  st.exception(e)
88
 
89
 
@@ -228,18 +232,8 @@ def upload_page():
228
  filepath = os.path.join(UPLOAD_DIR, filename)
229
  converted_filepath = os.path.join(UPLOAD_DIR, converted_filename)
230
 
231
-
232
-
233
-
234
- #st.success(f"Image saved to {filepath}")
235
-
236
- # Save image to Hugging Face repo----------------------------------------------------------------------------------------------------------------------------------
237
-
238
- try:
239
- image_repo_path = f"images/{converted_filename}"
240
- save_to_hf_repo(converted_filepath, image_repo_path)
241
- except Exception as e:
242
- st.error(f"Error saving image to Hugging Face repo: {str(e)}")
243
 
244
  # Check if the uploaded file is a GeoTIFF
245
  if file_extension in ['.tiff', '.tif']:
@@ -250,6 +244,14 @@ def upload_page():
250
  img = Image.open(converted_filepath)
251
  else:
252
  img = Image.open(filepath)
 
 
 
 
 
 
 
 
253
 
254
  st.image(img, caption='Uploaded Image', use_column_width=True)
255
  st.success(f'Image processed and saved as {converted_filename}')
@@ -272,22 +274,43 @@ def upload_page():
272
  prediction = predict(img_transformed)
273
  full_mask = (prediction > 0.5).astype(np.uint8) * 255
274
 
275
-
276
-
277
- # Save the full mask---------------------------------------------------------------------------------------------------
278
  full_mask = refine_mask(full_mask)
279
  mask_filename = f"mask_{timestamp}.png"
280
  mask_filepath = os.path.join(MASK_DIR, mask_filename)
281
  cv2.imwrite(mask_filepath, full_mask)
282
  st.session_state.mask_filename = mask_filename
283
 
284
-
285
  try:
286
- mask_repo_path = f"masks/{mask_filename}"
287
- save_to_hf_repo(mask_filepath, mask_repo_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  except Exception as e:
289
  st.error(f"Error saving mask to Hugging Face repo: {str(e)}")
290
-
291
 
292
  # Log image details
293
  log_image_details(timestamp, converted_filename, mask_filename)
 
72
 
73
  # save to dir func
74
  def save_to_hf_repo(local_path, repo_path):
75
+ if not os.path.isfile(local_path):
76
+ st.error(f"File not found at {local_path}")
77
+ return
78
+
79
  try:
80
+ with open(local_path, 'rb') as f:
81
+ hf_api.upload_file(
82
+ path_or_fileobj=f,
83
+ path_in_repo=repo_path,
84
+ repo_id=REPO_ID,
85
+ repo_type=REPO_TYPE,
86
+ token=HF_TOKEN
87
+ )
88
  st.success(f"File uploaded successfully to {repo_path}")
89
  except Exception as e:
90
+ st.error(f"Error during upload: {str(e)}")
 
91
  st.exception(e)
92
 
93
 
 
232
  filepath = os.path.join(UPLOAD_DIR, filename)
233
  converted_filepath = os.path.join(UPLOAD_DIR, converted_filename)
234
 
235
+ with open(filepath, "wb") as f:
236
+ f.write(bytes_data)
 
 
 
 
 
 
 
 
 
 
237
 
238
  # Check if the uploaded file is a GeoTIFF
239
  if file_extension in ['.tiff', '.tif']:
 
244
  img = Image.open(converted_filepath)
245
  else:
246
  img = Image.open(filepath)
247
+ img.save(converted_filepath)
248
+
249
+ if os.path.exists(converted_filepath):
250
+ st.success(f"Image saved successfully: {converted_filepath}")
251
+ file_size = os.path.getsize(converted_filepath)
252
+ st.write(f"File size: {file_size} bytes")
253
+ else:
254
+ st.error(f"Failed to save image: {converted_filepath}")
255
 
256
  st.image(img, caption='Uploaded Image', use_column_width=True)
257
  st.success(f'Image processed and saved as {converted_filename}')
 
274
  prediction = predict(img_transformed)
275
  full_mask = (prediction > 0.5).astype(np.uint8) * 255
276
 
277
+ # Save the full mask
 
 
278
  full_mask = refine_mask(full_mask)
279
  mask_filename = f"mask_{timestamp}.png"
280
  mask_filepath = os.path.join(MASK_DIR, mask_filename)
281
  cv2.imwrite(mask_filepath, full_mask)
282
  st.session_state.mask_filename = mask_filename
283
 
284
+ # Upload to Hugging Face repo
285
  try:
286
+ with open(converted_filepath, 'rb') as f:
287
+ image_repo_path = f"images/{converted_filename}"
288
+ hf_api.upload_file(
289
+ path_or_fileobj=f,
290
+ path_in_repo=image_repo_path,
291
+ repo_id=REPO_ID,
292
+ repo_type=REPO_TYPE,
293
+ token=HF_TOKEN
294
+ )
295
+ st.success(f"Image uploaded successfully to {image_repo_path}")
296
+ except Exception as e:
297
+ st.error(f"Error saving image to Hugging Face repo: {str(e)}")
298
+ st.exception(e)
299
+
300
+ try:
301
+ with open(mask_filepath, 'rb') as f:
302
+ mask_repo_path = f"masks/{mask_filename}"
303
+ hf_api.upload_file(
304
+ path_or_fileobj=f,
305
+ path_in_repo=mask_repo_path,
306
+ repo_id=REPO_ID,
307
+ repo_type=REPO_TYPE,
308
+ token=HF_TOKEN
309
+ )
310
+ st.success(f"Mask uploaded successfully to {mask_repo_path}")
311
  except Exception as e:
312
  st.error(f"Error saving mask to Hugging Face repo: {str(e)}")
313
+ st.exception(e)
314
 
315
  # Log image details
316
  log_image_details(timestamp, converted_filename, mask_filename)