Update storage 0.1.1
Browse files- modules/constants.py +8 -2
- modules/storage.md +3 -1
- modules/storage.py +10 -4
modules/constants.py
CHANGED
@@ -27,7 +27,11 @@ model_extensions = {".glb", ".gltf", ".obj", ".ply"}
|
|
27 |
model_extensions_list = list(model_extensions)
|
28 |
image_extensions = {".png", ".jpg", ".jpeg", ".webp"}
|
29 |
image_extensions_list = list(image_extensions)
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
default_slider_images = [
|
33 |
"images/slider/beeuty_545jlbh1_v12_alpha96_300dpi.png",
|
@@ -37,7 +41,9 @@ default_slider_images = [
|
|
37 |
default_model_3d = "models/beeuty_545jlbh1_300dpi.glb"
|
38 |
|
39 |
# Constants for URL shortener
|
40 |
-
HF_REPO_ID = "
|
|
|
|
|
41 |
SHORTENER_JSON_FILE = "shortener.json" # The name of your JSON file in the repo
|
42 |
|
43 |
# Open Graph Defaults
|
|
|
27 |
model_extensions_list = list(model_extensions)
|
28 |
image_extensions = {".png", ".jpg", ".jpeg", ".webp"}
|
29 |
image_extensions_list = list(image_extensions)
|
30 |
+
audio_extensions = {".mp3", ".wav", ".ogg", ".flac", ".aac"}
|
31 |
+
audio_extensions_list = list(audio_extensions)
|
32 |
+
video_extensions = {".mp4"}
|
33 |
+
video_extensions_list = list(video_extensions)
|
34 |
+
upload_file_types = model_extensions_list + image_extensions_list + audio_extensions_list + video_extensions_list
|
35 |
|
36 |
default_slider_images = [
|
37 |
"images/slider/beeuty_545jlbh1_v12_alpha96_300dpi.png",
|
|
|
41 |
default_model_3d = "models/beeuty_545jlbh1_300dpi.glb"
|
42 |
|
43 |
# Constants for URL shortener
|
44 |
+
HF_REPO_ID = os.getenv("HF_REPO_ID")
|
45 |
+
if not HF_REPO_ID:
|
46 |
+
HF_REPO_ID = "Surn/Storage" # Replace with your Hugging Face repository ID
|
47 |
SHORTENER_JSON_FILE = "shortener.json" # The name of your JSON file in the repo
|
48 |
|
49 |
# Open Graph Defaults
|
modules/storage.md
CHANGED
@@ -4,6 +4,8 @@ The `storage.py` module provides helper functions for:
|
|
4 |
- Generating permalinks for 3D viewer projects.
|
5 |
- Uploading files in batches to a Hugging Face repository.
|
6 |
- Managing URL shortening by storing (short URL, full URL) pairs in a JSON file on the repository.
|
|
|
|
|
7 |
|
8 |
## Key Functions
|
9 |
|
@@ -53,7 +55,7 @@ files_for_permalink = [
|
|
53 |
"local/path/to/heightmap.png",
|
54 |
"local/path/to/image.png"
|
55 |
]
|
56 |
-
repo_id = "Surn/Storage" # Make sure this is defined, e.g., from constants
|
57 |
folder_name = "my_new_model_with_permalink"
|
58 |
|
59 |
upload_result = upload_files_to_repo(
|
|
|
4 |
- Generating permalinks for 3D viewer projects.
|
5 |
- Uploading files in batches to a Hugging Face repository.
|
6 |
- Managing URL shortening by storing (short URL, full URL) pairs in a JSON file on the repository.
|
7 |
+
- Retrieving full URLs from short URL IDs and vice versa.
|
8 |
+
- Handle specific file types for 3D models, images, video and audio.
|
9 |
|
10 |
## Key Functions
|
11 |
|
|
|
55 |
"local/path/to/heightmap.png",
|
56 |
"local/path/to/image.png"
|
57 |
]
|
58 |
+
repo_id = "Surn/Storage" # Make sure this is defined, e.g., from constants or environment variables
|
59 |
folder_name = "my_new_model_with_permalink"
|
60 |
|
61 |
upload_result = upload_files_to_repo(
|
modules/storage.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# modules/storage.py
|
2 |
-
__version__ = "0.1.
|
3 |
import os
|
4 |
import urllib.parse
|
5 |
import tempfile
|
@@ -8,7 +8,7 @@ import json
|
|
8 |
import base64
|
9 |
from huggingface_hub import login, upload_folder, hf_hub_download, HfApi
|
10 |
from huggingface_hub.utils import RepositoryNotFoundError, EntryNotFoundError
|
11 |
-
from modules.constants import HF_API_TOKEN, upload_file_types, model_extensions, image_extensions, HF_REPO_ID, SHORTENER_JSON_FILE
|
12 |
from typing import Any, Dict, List, Tuple, Union
|
13 |
|
14 |
# see storage.md for detailed information about the storage module and its functions.
|
@@ -21,6 +21,8 @@ def generate_permalink(valid_files, base_url_external, permalink_viewer_url="sur
|
|
21 |
"""
|
22 |
model_link = None
|
23 |
images_links = []
|
|
|
|
|
24 |
for f in valid_files:
|
25 |
filename = os.path.basename(f)
|
26 |
ext = os.path.splitext(filename)[1].lower()
|
@@ -29,6 +31,10 @@ def generate_permalink(valid_files, base_url_external, permalink_viewer_url="sur
|
|
29 |
model_link = f"{base_url_external}/{filename}"
|
30 |
elif ext in image_extensions:
|
31 |
images_links.append(f"{base_url_external}/{filename}")
|
|
|
|
|
|
|
|
|
32 |
if model_link and len(images_links) == 2:
|
33 |
# Construct a permalink to the viewer project with query parameters.
|
34 |
permalink_viewer_url = f"https://{permalink_viewer_url}/"
|
@@ -212,7 +218,7 @@ def _upload_json_to_repo(data, repo_id, json_file_name, repo_type="dataset"):
|
|
212 |
temp_dir_for_json = os.getenv("TMPDIR", tempfile.gettempdir())
|
213 |
os.makedirs(temp_dir_for_json, exist_ok=True)
|
214 |
|
215 |
-
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json", dir=temp_dir_for_json) as tmp_file:
|
216 |
json.dump(data, tmp_file, indent=2)
|
217 |
tmp_file_path = tmp_file.name
|
218 |
|
@@ -251,7 +257,7 @@ def _find_url_in_json(data, short_url=None, full_url=None):
|
|
251 |
|
252 |
def _add_url_to_json(data, short_url, full_url):
|
253 |
"""Adds a new short_url/full_url pair to the data. Returns updated data."""
|
254 |
-
if data is None:
|
255 |
data = []
|
256 |
data.append({"short_url": short_url, "full_url": full_url})
|
257 |
return data
|
|
|
1 |
# modules/storage.py
|
2 |
+
__version__ = "0.1.1" # Added version
|
3 |
import os
|
4 |
import urllib.parse
|
5 |
import tempfile
|
|
|
8 |
import base64
|
9 |
from huggingface_hub import login, upload_folder, hf_hub_download, HfApi
|
10 |
from huggingface_hub.utils import RepositoryNotFoundError, EntryNotFoundError
|
11 |
+
from modules.constants import HF_API_TOKEN, upload_file_types, model_extensions, image_extensions, audio_extensions, video_extensions, HF_REPO_ID, SHORTENER_JSON_FILE
|
12 |
from typing import Any, Dict, List, Tuple, Union
|
13 |
|
14 |
# see storage.md for detailed information about the storage module and its functions.
|
|
|
21 |
"""
|
22 |
model_link = None
|
23 |
images_links = []
|
24 |
+
audio_links = []
|
25 |
+
video_links = []
|
26 |
for f in valid_files:
|
27 |
filename = os.path.basename(f)
|
28 |
ext = os.path.splitext(filename)[1].lower()
|
|
|
31 |
model_link = f"{base_url_external}/{filename}"
|
32 |
elif ext in image_extensions:
|
33 |
images_links.append(f"{base_url_external}/{filename}")
|
34 |
+
elif ext in audio_extensions:
|
35 |
+
audio_links.append(f"{base_url_external}/{filename}")
|
36 |
+
elif ext in video_extensions:
|
37 |
+
video_links.append(f"{base_url_external}/{filename}")
|
38 |
if model_link and len(images_links) == 2:
|
39 |
# Construct a permalink to the viewer project with query parameters.
|
40 |
permalink_viewer_url = f"https://{permalink_viewer_url}/"
|
|
|
218 |
temp_dir_for_json = os.getenv("TMPDIR", tempfile.gettempdir())
|
219 |
os.makedirs(temp_dir_for_json, exist_ok=True)
|
220 |
|
221 |
+
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json", dir=temp_dir_for_json) as tmp_file:
|
222 |
json.dump(data, tmp_file, indent=2)
|
223 |
tmp_file_path = tmp_file.name
|
224 |
|
|
|
257 |
|
258 |
def _add_url_to_json(data, short_url, full_url):
|
259 |
"""Adds a new short_url/full_url pair to the data. Returns updated data."""
|
260 |
+
if data is None:
|
261 |
data = []
|
262 |
data.append({"short_url": short_url, "full_url": full_url})
|
263 |
return data
|