safetensors #6688
-
|
Add safetensors to support transformers, enabling more convenient tokenization, encoding and decoding during ONNX inference. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Install
dependencies = [
"flet",
"safetensors",
"numpy",
]ExampleSave a couple of tensors, reload them via import os
import flet as ft
import numpy as np
from safetensors.numpy import save_file
from safetensors import safe_open
def main(page: ft.Page):
path = os.path.join(os.environ.get("FLET_APP_STORAGE_TEMP", "."), "w.safetensors")
save_file(
{
"emb": np.arange(6, dtype=np.float32).reshape(2, 3),
"bias": np.ones(3, dtype=np.float32),
},
path,
)
rows = []
with safe_open(path, framework="numpy") as f: # mmap; one tensor at a time
for k in f.keys():
t = f.get_tensor(k)
rows.append(ft.Text(f"{k}: shape {list(t.shape)}, sum {t.sum():.1f}"))
page.add(*rows)
ft.run(main)On device this shows Recommendations
|
Beta Was this translation helpful? Give feedback.
safetensors0.8.0 is now supported on Android and iOS: https://pypi.flet.dev/safetensorssafetensorsloads and saves model weights/embeddings as a safe, zero-copy file format — none of pickle's arbitrary-code-execution risk, and fast mmap reads that pull one tensor at a time.Install
safetensorsitself has no required dependencies, but the usable mobile path needsnumpy, so add both:Example
Save a couple of tensors, reload them via
safe_open, and show the result on screen. Note the file goes into Flet's storage: the app bundle is read-only on device, so write toFLET_APP_STORAGE_TEMP/_DATA, not the working directory.