tflite-runtime #3810
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
I'd like to see this as well but the TFLite RT was changed to Lite RT https://developers.googleblog.com/en/tensorflow-lite-is-now-litert/ and the corresponding package on PyPi is now: https://pypi.org/project/ai-edge-litert/ The |
Beta Was this translation helpful? Give feedback.
-
|
On the LiteRT/ InstallAdd it to your deps (the pip name has a hyphen, the import has an underscore): # pyproject.toml
dependencies = [
"flet",
"tflite-runtime",
"numpy",
]All Android ABIs are covered ( Storage & the model file
import os, urllib.request
data_dir = os.getenv("FLET_APP_STORAGE_DATA")
model_path = os.path.join(data_dir, "model.tflite")
if not os.path.exists(model_path):
urllib.request.urlretrieve("https://<your-host>/model.tflite", model_path)Minimal usageAssuming you bundle a import os
import flet as ft
import numpy as np
from tflite_runtime.interpreter import Interpreter
MODEL = os.path.join(os.path.dirname(__file__), "model.tflite") # bundled beside main.py
def main(page: ft.Page):
def infer():
itp = Interpreter(
model_path=MODEL, num_threads=2
) # XNNPACK applied automatically
itp.allocate_tensors()
inp, out = itp.get_input_details()[0], itp.get_output_details()[0]
x = np.ones(inp["shape"], dtype=inp["dtype"]) # ← your real input here
itp.set_tensor(inp["index"], x)
itp.invoke()
y = itp.get_tensor(out["index"])
result.value = (
f"in {list(inp['shape'])} → out {list(y.shape)}\n{np.ravel(y)[:5]}"
)
page.update()
page.add(
ft.SafeArea(result := ft.Text("Running inference…")),
)
page.run_thread(infer) # keep inference off the UI thread
ft.run(main)You'll see the output shape and first values render on screen. Run it off the UI thread
Notes for good results on mobile
|
Beta Was this translation helpful? Give feedback.
tflite-runtime2.21.0 is now supported on both Android and iOS: https://pypi.flet.dev/tflite-runtimetflite-runtimeruns.tflitemodels through the XNNPACK-accelerated CPU path — the portable, dependable baseline that works identically on Android and iOS. Everything runs offline; the only network you need is a one-time model download (or bundle the model — see below). The pinned version is 2.21.0, the last buildable release.On the LiteRT/
ai-edge-litertquestion in the comments: it's the official successor, but it is Bazel-built, so it can't be cross-compiled for mobile today. We therefore target tflite-runtime (the classictf.liteinterpreter), which is buildable for iOS and Android. The…