File size: 560 Bytes
6d4bcdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import os
import uuid
import torch
from gradio.utils import get_upload_folder

def save_tensor_to_file(tensor, prefix="tensor"):
    upload_dir = get_upload_folder()
    os.makedirs(upload_dir, exist_ok=True)
    path = os.path.join(upload_dir, f"{prefix}_{uuid.uuid4().hex}.pt")
    torch.save(tensor, path)
    return path

def load_tensor_from_file(path, map_location=None):
    # Use weights_only=True for security and to suppress FutureWarning (only tensors are loaded in this app)
    return torch.load(path, map_location=map_location, weights_only=True)