Spaces:
Runtime error
Runtime error
Commit ·
2d90d0f
1
Parent(s): bc6e9f5
Add code to unzip data in HF space
Browse files- .gitignore +2 -1
- app.py +4 -0
- engine/utils.py +29 -0
.gitignore
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
data/
|
| 2 |
.DS_Store
|
| 3 |
-
__pycache__
|
|
|
|
|
|
| 1 |
data/
|
| 2 |
.DS_Store
|
| 3 |
+
__pycache__
|
| 4 |
+
data.zip
|
app.py
CHANGED
|
@@ -2,17 +2,21 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
from engine.search import ImageSearchModule
|
|
|
|
| 5 |
import os
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
PROJECT_ROOT = Path(__file__).resolve().parent
|
| 9 |
|
|
|
|
| 10 |
def check_dirs():
|
| 11 |
dirs = {
|
| 12 |
"Data": (PROJECT_ROOT / "data"),
|
| 13 |
"Images": (PROJECT_ROOT / "data" / "images"),
|
| 14 |
"Features": (PROJECT_ROOT / "data" / "features")
|
| 15 |
}
|
|
|
|
|
|
|
| 16 |
for dir_name, dir_path in dirs.items():
|
| 17 |
if not dir_path.exists():
|
| 18 |
raise FileNotFoundError(f"{dir_name} directory not found: {dir_path}")
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
from engine.search import ImageSearchModule
|
| 5 |
+
from engine.utils import unzip_file
|
| 6 |
import os
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
PROJECT_ROOT = Path(__file__).resolve().parent
|
| 10 |
|
| 11 |
+
|
| 12 |
def check_dirs():
|
| 13 |
dirs = {
|
| 14 |
"Data": (PROJECT_ROOT / "data"),
|
| 15 |
"Images": (PROJECT_ROOT / "data" / "images"),
|
| 16 |
"Features": (PROJECT_ROOT / "data" / "features")
|
| 17 |
}
|
| 18 |
+
if not dirs['Data'].exists():
|
| 19 |
+
unzip_file(zip_file_path=str(PROJECT_ROOT / "data.zip"), destination_folder=str(PROJECT_ROOT ))
|
| 20 |
for dir_name, dir_path in dirs.items():
|
| 21 |
if not dir_path.exists():
|
| 22 |
raise FileNotFoundError(f"{dir_name} directory not found: {dir_path}")
|
engine/utils.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import zipfile
|
| 2 |
+
import os
|
| 3 |
+
def unzip_file(zip_file_path: str, destination_folder: str) -> None:
|
| 4 |
+
"""Unzips a ZIP file to the specified destination folder.
|
| 5 |
+
|
| 6 |
+
Args:
|
| 7 |
+
zip_file_path: The path to the ZIP file.
|
| 8 |
+
destination_folder: The path to the folder where the contents should be extracted.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
# Check if the ZIP file exists
|
| 13 |
+
if not os.path.isfile(zip_file_path):
|
| 14 |
+
raise FileNotFoundError(f"ZIP file not found: {zip_file_path}")
|
| 15 |
+
|
| 16 |
+
# Check if the destination folder exists and create it if necessary
|
| 17 |
+
os.makedirs(destination_folder, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
# Extract the ZIP file
|
| 20 |
+
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
| 21 |
+
zip_ref.extractall(destination_folder)
|
| 22 |
+
print(f"Successfully unzipped '{zip_file_path}' to '{destination_folder}'")
|
| 23 |
+
|
| 24 |
+
except zipfile.BadZipFile:
|
| 25 |
+
print(f"Error: '{zip_file_path}' is not a valid ZIP file.")
|
| 26 |
+
except FileNotFoundError as e:
|
| 27 |
+
print(e) # Print the error message for FileNotFoundError
|
| 28 |
+
except Exception as e: # Catch any other unexpected errors
|
| 29 |
+
print(f"An unexpected error occurred: {e}")
|