Buckets:
| # Hugging Face Persistent Storage Example | |
| This Space uses Hugging Face Persistent Storage mounted at `/data`. | |
| --- | |
| # Folder Structure | |
| ```text | |
| /data | |
| ├── uploads/ | |
| ├── videos/ | |
| ├── images/ | |
| └── cache/ | |
| ``` | |
| --- | |
| # List Files | |
| ```python | |
| from pathlib import Path | |
| BASE_DIR = Path("/data") | |
| for path in BASE_DIR.rglob("*"): | |
| print(path) | |
| ``` | |
| --- | |
| # List Only Files | |
| ```python | |
| from pathlib import Path | |
| UPLOAD_DIR = Path("/data/uploads") | |
| files = [p.name for p in UPLOAD_DIR.iterdir() if p.is_file()] | |
| print(files) | |
| ``` | |
| --- | |
| # Upload File | |
| ```python | |
| import shutil | |
| from pathlib import Path | |
| UPLOAD_DIR = Path("/data/uploads") | |
| UPLOAD_DIR.mkdir(parents=True, exist_ok=True) | |
| def upload_file(uploaded_file): | |
| dst = UPLOAD_DIR / uploaded_file.name | |
| shutil.copy(uploaded_file.name, dst) | |
| return str(dst) | |
| ``` | |
| --- | |
| # Download File | |
| ```python | |
| from pathlib import Path | |
| file_path = Path("/data/uploads/example.txt") | |
| with open(file_path, "rb") as f: | |
| binary_data = f.read() | |
| print(len(binary_data)) | |
| ``` | |
| --- | |
| # Delete File | |
| ```python | |
| from pathlib import Path | |
| file_path = Path("/data/uploads/example.txt") | |
| if file_path.exists(): | |
| file_path.unlink() | |
| ``` | |
| --- | |
| # Save Temporary File | |
| ```python | |
| import tempfile | |
| with tempfile.NamedTemporaryFile( | |
| suffix=".mp4", | |
| dir="/tmp", | |
| delete=False | |
| ) as f: | |
| temp_path = f.name | |
| print(temp_path) | |
| ``` | |
| --- | |
| # Gradio Download Example | |
| ```python | |
| import gradio as gr | |
| def get_file(): | |
| return "/data/uploads/example.txt" | |
| demo = gr.Interface( | |
| fn=get_file, | |
| inputs=[], | |
| outputs=gr.File() | |
| ) | |
| demo.launch() | |
| ``` | |
| --- | |
| # Cache Configuration | |
| ```python | |
| import os | |
| os.environ["HF_HOME"] = "/data/cache" | |
| os.environ["TRANSFORMERS_CACHE"] = "/data/cache" | |
| ``` | |
| --- | |
| # Notes | |
| - `/data` is persistent | |
| - `/tmp` is temporary | |
| - `/tmp` is cleared on restart | |
| - `/data` survives rebuild/restart |
Xet Storage Details
- Size:
- 2.01 kB
- Xet hash:
- ef28654ab431e7b2377267d9c20fe1532fe4fe12a3467c7eaed1762d842ca05d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.