103 MB
5,154 files
Updated 1 day ago
NameSize
character_select
wave
README.md2.01 kB
xet
README.md

Hugging Face Persistent Storage Example

This Space uses Hugging Face Persistent Storage mounted at /data.


Folder Structure

/data
├── uploads/
├── videos/
├── images/
└── cache/

List Files

from pathlib import Path

BASE_DIR = Path("/data")

for path in BASE_DIR.rglob("*"):
    print(path)

List Only Files

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

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

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

from pathlib import Path

file_path = Path("/data/uploads/example.txt")

if file_path.exists():
    file_path.unlink()

Save Temporary File

import tempfile

with tempfile.NamedTemporaryFile(
    suffix=".mp4",
    dir="/tmp",
    delete=False
) as f:

    temp_path = f.name

print(temp_path)

Gradio Download Example

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

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
Total size
103 MB
Files
5,154
Last updated
Jun 9
Pre-warmed CDN
US EU US EU

Contributors