Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import numpy as np
|
|
| 10 |
from PIL import Image
|
| 11 |
import torch
|
| 12 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
|
|
@@ -22,6 +23,32 @@ from Utils.area import pixel_to_sqft, process_and_overlay_image
|
|
| 22 |
from Utils.convert import read_pansharpened_rgb
|
| 23 |
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
BASE_DIR = os.getcwd()
|
| 26 |
|
| 27 |
# Define subdirectories
|
|
@@ -174,11 +201,12 @@ def upload_page():
|
|
| 174 |
|
| 175 |
with open(filepath, "wb") as f:
|
| 176 |
f.write(bytes_data)
|
|
|
|
| 177 |
|
| 178 |
-
st.success(f"Image saved to {filepath}")
|
| 179 |
|
| 180 |
-
# Save image to Hugging Face repo
|
| 181 |
-
|
| 182 |
|
| 183 |
# Check if the uploaded file is a GeoTIFF
|
| 184 |
if file_extension in ['.tiff', '.tif']:
|
|
@@ -217,8 +245,8 @@ def upload_page():
|
|
| 217 |
cv2.imwrite(mask_filepath, full_mask)
|
| 218 |
st.session_state.mask_filename = mask_filename
|
| 219 |
|
| 220 |
-
# Save mask to Hugging Face repo
|
| 221 |
-
|
| 222 |
|
| 223 |
# Log image details
|
| 224 |
log_image_details(timestamp, converted_filename, mask_filename)
|
|
|
|
| 10 |
from PIL import Image
|
| 11 |
import torch
|
| 12 |
from huggingface_hub import hf_hub_download
|
| 13 |
+
from huggingface_hub import HfApi
|
| 14 |
|
| 15 |
|
| 16 |
|
|
|
|
| 23 |
from Utils.convert import read_pansharpened_rgb
|
| 24 |
|
| 25 |
|
| 26 |
+
hf_api = HfApi()
|
| 27 |
+
HF_TOKEN = st.secrets.get("HF_TOKEN")
|
| 28 |
+
if not HF_TOKEN:
|
| 29 |
+
st.error("HF_TOKEN not found.")
|
| 30 |
+
st.stop()
|
| 31 |
+
|
| 32 |
+
REPO_ID = "Pavan2k4/Building_area"
|
| 33 |
+
REPO_TYPE = "space"
|
| 34 |
+
|
| 35 |
+
# save to dir func
|
| 36 |
+
def save_to_hf_repo(local_path, repo_path):
|
| 37 |
+
try:
|
| 38 |
+
hf_api.upload_file(
|
| 39 |
+
path_or_fileobj=local_path,
|
| 40 |
+
path_in_repo=repo_path,
|
| 41 |
+
repo_id=REPO_ID,
|
| 42 |
+
repo_type=REPO_TYPE,
|
| 43 |
+
token=HF_TOKEN
|
| 44 |
+
)
|
| 45 |
+
st.success(f"File uploaded successfully to {repo_path}")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
st.error(f"Error uploading file: {str(e)}")
|
| 48 |
+
st.error("Detailed error information:")
|
| 49 |
+
st.exception(e)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
BASE_DIR = os.getcwd()
|
| 53 |
|
| 54 |
# Define subdirectories
|
|
|
|
| 201 |
|
| 202 |
with open(filepath, "wb") as f:
|
| 203 |
f.write(bytes_data)
|
| 204 |
+
|
| 205 |
|
| 206 |
+
#st.success(f"Image saved to {filepath}")
|
| 207 |
|
| 208 |
+
# Save image to Hugging Face repo----------------------------------------------------------------------------------------------------------------------------------
|
| 209 |
+
save_to_hf_repo(filepath, f'uploaded_images/{filename}')
|
| 210 |
|
| 211 |
# Check if the uploaded file is a GeoTIFF
|
| 212 |
if file_extension in ['.tiff', '.tif']:
|
|
|
|
| 245 |
cv2.imwrite(mask_filepath, full_mask)
|
| 246 |
st.session_state.mask_filename = mask_filename
|
| 247 |
|
| 248 |
+
# Save mask to Hugging Face repo---------------------------------------------------------------------------------------------
|
| 249 |
+
save_to_hf_repo(mask_filepath, f'generated_masks/{st.session_state.mask_filename}')
|
| 250 |
|
| 251 |
# Log image details
|
| 252 |
log_image_details(timestamp, converted_filename, mask_filename)
|