pythonhere 0.3.1__py3-none-any.whl → 0.4.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pythonhere/.agents/skills/pythonhere/SKILL.md +5 -1
- pythonhere/.agents/skills/pythonhere/references/litert.md +121 -0
- pythonhere/.agents/skills/pythonhere/references/tasks.md +58 -0
- pythonhere/main.py +3 -0
- pythonhere/ml_here/__init__.py +17 -0
- pythonhere/ml_here/huggingface.py +998 -0
- pythonhere/ml_here/storage.py +145 -0
- pythonhere/server_here.py +1 -0
- pythonhere/tasks_here.py +1292 -0
- pythonhere/ui_here/connection_address_here.kv +40 -0
- pythonhere/version_here.py +1 -1
- {pythonhere-0.3.1.dist-info → pythonhere-0.4.1.dist-info}/METADATA +6 -2
- {pythonhere-0.3.1.dist-info → pythonhere-0.4.1.dist-info}/RECORD +16 -10
- {pythonhere-0.3.1.dist-info → pythonhere-0.4.1.dist-info}/WHEEL +0 -0
- {pythonhere-0.3.1.dist-info → pythonhere-0.4.1.dist-info}/licenses/LICENSE +0 -0
- {pythonhere-0.3.1.dist-info → pythonhere-0.4.1.dist-info}/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: pythonhere
|
|
3
|
-
description: "Build, inspect, and debug PythonHere applications by executing Python through the `there` CLI in an already-running Kivy/Python-for-Android process. Use when a task targets a PythonHere device or `there.env` connection and involves live Kivy UI, KV language, Android APIs, Pyjnius, permissions, installed Android packages, media/files, Plyer device features, BLE with able, or
|
|
3
|
+
description: "Build, inspect, and debug PythonHere applications by executing Python through the `there` CLI in an already-running Kivy/Python-for-Android process. Use when a task targets a PythonHere device or `there.env` connection and involves live Kivy UI, KV language, Android APIs, Pyjnius, permissions, installed Android packages, media/files, Plyer device features, BLE with able, MIDI with midistream, durable on-device task runs, or on-device AI with LiteRT-LM and `.litertlm` models."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Operate PythonHere with `there`
|
|
@@ -40,6 +40,10 @@ when the task depends on it broadly or requires its exact current contents.
|
|
|
40
40
|
recording, camera, file selection, GPS, battery, or sensors.
|
|
41
41
|
- Consult [able.md](references/able.md) for Bluetooth Low Energy work.
|
|
42
42
|
- Consult [midi.md](references/midi.md) for MIDI or synthesizer work.
|
|
43
|
+
- Consult [litert.md](references/litert.md) for PythonHere's ML model helpers
|
|
44
|
+
and LiteRT-LM usage.
|
|
45
|
+
- Consult [tasks.md](references/tasks.md) for durable sequential work that must
|
|
46
|
+
continue after a client disconnect or recover after PythonHere restarts.
|
|
43
47
|
|
|
44
48
|
Combine references when a feature genuinely needs details from several
|
|
45
49
|
concerns; for example, an Android gallery may need permission, MediaStore, and
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# ML model helpers for LiteRT
|
|
2
|
+
|
|
3
|
+
PythonHere provides `ml_here` for acquiring and locating ML model files. It
|
|
4
|
+
does not wrap LiteRT-LM inference.
|
|
5
|
+
|
|
6
|
+
All public model helpers return paths as strings (a list of strings for
|
|
7
|
+
`discover_models()`), ready to pass to `litert_lm.Engine()`.
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from ml_here import (
|
|
11
|
+
discover_models,
|
|
12
|
+
download_hf_model,
|
|
13
|
+
model_path,
|
|
14
|
+
models_directory,
|
|
15
|
+
require_model,
|
|
16
|
+
)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
- `models_directory(create=False)` returns PythonHere's model directory. On
|
|
20
|
+
Android this is the app-specific external files directory under `models`; on
|
|
21
|
+
Linux it is `~/.local/share/pythonhere/models`.
|
|
22
|
+
- `model_path(repo_id, filename)` returns the namespaced path for a model. It
|
|
23
|
+
does not create directories and rejects unsafe path components.
|
|
24
|
+
- `require_model(repo_id, filename)` returns that path when the model is
|
|
25
|
+
installed and raises `FileNotFoundError` otherwise.
|
|
26
|
+
- `discover_models(extra_directories=(), extensions=".litertlm")` recursively
|
|
27
|
+
returns sorted, unique LiteRT-LM model paths from managed storage and any
|
|
28
|
+
additional directories. Pass another extension or iterable to select other
|
|
29
|
+
formats, or `extensions=None` to include every completed stored artifact.
|
|
30
|
+
- `download_hf_model(repo_id, filename, *, revision="main", token=None,
|
|
31
|
+
destination=None, progress=None, cancelled=None, overwrite=False,
|
|
32
|
+
expected_sha256=None)` downloads a Hugging Face model file of any format. By
|
|
33
|
+
default, it stores the file at `model_path(repo_id, filename)`.
|
|
34
|
+
|
|
35
|
+
Choose by intent: use `require_model()` to load a known installed model, the
|
|
36
|
+
path returned by `download_hf_model()` after a download, and
|
|
37
|
+
`discover_models()` only to inventory models whose identities are not already
|
|
38
|
+
known. Use `model_path()` when only the expected location is needed.
|
|
39
|
+
|
|
40
|
+
Downloads are resumable across restarts and make the final file visible only
|
|
41
|
+
after the download completes. Cancellation preserves the partial download for
|
|
42
|
+
resuming. Pass a token for private or gated models. The `progress` callback
|
|
43
|
+
receives the following interface; use it directly rather than redefining it:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
class DownloadProgress:
|
|
47
|
+
repo_id: str
|
|
48
|
+
filename: str
|
|
49
|
+
destination: str
|
|
50
|
+
downloaded: int
|
|
51
|
+
total: int | None
|
|
52
|
+
elapsed: float
|
|
53
|
+
speed_bps: float
|
|
54
|
+
fraction: float | None
|
|
55
|
+
percent: float | None
|
|
56
|
+
speed_mbps: float
|
|
57
|
+
remaining: int | None
|
|
58
|
+
eta: float | None
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Use `litert_lm` directly to load and run the returned model path.
|
|
62
|
+
|
|
63
|
+
## Run an installed model
|
|
64
|
+
|
|
65
|
+
Run model loading and inference on a worker (`--worker`) to keep Kivy
|
|
66
|
+
responsive. In the app runtime, import model helpers from `ml_here`.
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import litert_lm
|
|
70
|
+
from ml_here import require_model
|
|
71
|
+
|
|
72
|
+
model_file = require_model(
|
|
73
|
+
"litert-community/SmolLM2-135M-Instruct",
|
|
74
|
+
"SmolLM2_135M_Instruct.litertlm",
|
|
75
|
+
)
|
|
76
|
+
engine = litert_lm.Engine(model_file)
|
|
77
|
+
try:
|
|
78
|
+
with engine.create_conversation(max_output_tokens=128) as conversation:
|
|
79
|
+
response = conversation.send_message(
|
|
80
|
+
"What is the Kivy framework? Answer in one short paragraph."
|
|
81
|
+
)
|
|
82
|
+
text = "".join(
|
|
83
|
+
part["text"]
|
|
84
|
+
for part in response["content"]
|
|
85
|
+
if part.get("type") == "text"
|
|
86
|
+
)
|
|
87
|
+
print(text)
|
|
88
|
+
finally:
|
|
89
|
+
engine.close()
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The conversation closes before the engine, including when inference raises.
|
|
93
|
+
For an interactive chat, keep the engine loaded and reuse the conversation
|
|
94
|
+
for follow-up messages; close both when the chat is finished.
|
|
95
|
+
|
|
96
|
+
`litert_lm.Engine(model_file)` uses CPU by default. For a model and device
|
|
97
|
+
that support GPU inference, replace the engine construction above with:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
engine = litert_lm.Engine(model_file, backend=litert_lm.Backend.GPU())
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Download first when needed
|
|
104
|
+
|
|
105
|
+
If the model is not installed, download it on a worker before inference:
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from ml_here import download_hf_model
|
|
109
|
+
|
|
110
|
+
model_file = download_hf_model(
|
|
111
|
+
"litert-community/SmolLM2-135M-Instruct",
|
|
112
|
+
"SmolLM2_135M_Instruct.litertlm",
|
|
113
|
+
)
|
|
114
|
+
print(model_file)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
When downloading and loading in the same script, pass the returned
|
|
118
|
+
`model_file` directly to `litert_lm.Engine()`.
|
|
119
|
+
|
|
120
|
+
See the [upstream Python API](https://github.com/google-ai-edge/LiteRT-LM/tree/main/python)
|
|
121
|
+
for streaming, sampling, and multimodal inference.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Durable task runs
|
|
2
|
+
|
|
3
|
+
Use `TaskRunner` for sequential work that must continue after the client
|
|
4
|
+
disconnects, retain progress across a PythonHere restart, or retry individual
|
|
5
|
+
items.
|
|
6
|
+
|
|
7
|
+
## Start a run
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from tasks_here import TaskRunner
|
|
11
|
+
|
|
12
|
+
runner = TaskRunner.create("data-import", max_attempts=3)
|
|
13
|
+
runner.add([
|
|
14
|
+
{"id": "part-1", "path": "one.json"},
|
|
15
|
+
{"id": "part-2", "path": "two.json"},
|
|
16
|
+
])
|
|
17
|
+
runner.start(execute_task)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Tasks and results must contain plain JSON-compatible values. Every task needs a
|
|
21
|
+
unique nonempty string `id`. `start()` returns promptly and processes one task
|
|
22
|
+
at a time. Preserve the runner in a named global when later commands need it.
|
|
23
|
+
|
|
24
|
+
Inspect `runner.status()`, `runner.results()`, `runner.get(id)`, or
|
|
25
|
+
`runner.failed()` through normal `there get` calls. `pause()` finishes the
|
|
26
|
+
current item and stops before claiming another.
|
|
27
|
+
|
|
28
|
+
## Wait and inspect the outcome
|
|
29
|
+
|
|
30
|
+
After starting the runner, wait in bounded intervals through the CLI:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
there --json --timeout 60 get --worker \
|
|
34
|
+
"{'stopped': runner.wait(timeout=50), 'status': runner.status()}"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Here, `--worker` keeps the wait off the app's event loop.
|
|
38
|
+
Keep the CLI timeout longer than the wait timeout to allow time for connection
|
|
39
|
+
and response. If `stopped` is `false`, tasks continue running; repeat the command
|
|
40
|
+
to wait again. A `true` value only means the worker is no longer running (or was
|
|
41
|
+
never started), not that every task succeeded. Inspect `status.state`,
|
|
42
|
+
`status.failed`, and `status.worker_error`. A `completed` state includes tasks
|
|
43
|
+
that exhausted their retries; inspect `runner.failed()` for their errors.
|
|
44
|
+
|
|
45
|
+
## Resume after restart
|
|
46
|
+
|
|
47
|
+
After PythonHere restarts, reopen the named run, recreate resources and the
|
|
48
|
+
executor callable, then restart execution:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
runner = TaskRunner.open("data-import")
|
|
52
|
+
runner.start(execute_task)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Execution is at least once: an item interrupted by process death can run again.
|
|
56
|
+
Make irreversible executor side effects idempotent or check their durable state
|
|
57
|
+
before applying them. The executor runs in a background thread; marshal Kivy UI
|
|
58
|
+
access to the main thread according to `kivy-runtime.md`.
|
pythonhere/main.py
CHANGED
|
@@ -26,6 +26,7 @@ from kivy.app import App
|
|
|
26
26
|
from kivy.clock import Clock
|
|
27
27
|
from kivy.config import Config, ConfigParser
|
|
28
28
|
from kivy.logger import Logger
|
|
29
|
+
from kivy.properties import StringProperty # pylint: disable=no-name-in-module
|
|
29
30
|
from patches_here import monkeypatch_kivy
|
|
30
31
|
from server_here import run_ssh_server
|
|
31
32
|
from window_here import reset_window_environment
|
|
@@ -42,6 +43,8 @@ def configure_logging():
|
|
|
42
43
|
class PythonHereApp(App):
|
|
43
44
|
"""PythonHere main app."""
|
|
44
45
|
|
|
46
|
+
ssh_host_key_sha256 = StringProperty("")
|
|
47
|
+
|
|
45
48
|
def __init__(self):
|
|
46
49
|
super().__init__()
|
|
47
50
|
self.server_task: asyncio.Task | None = None
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Acquire, store, index, and discover ML model files for PythonHere."""
|
|
2
|
+
|
|
3
|
+
from .huggingface import download_hf_model
|
|
4
|
+
from .storage import (
|
|
5
|
+
discover_models,
|
|
6
|
+
model_path,
|
|
7
|
+
models_directory,
|
|
8
|
+
require_model,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__all__ = (
|
|
12
|
+
"discover_models",
|
|
13
|
+
"download_hf_model",
|
|
14
|
+
"model_path",
|
|
15
|
+
"models_directory",
|
|
16
|
+
"require_model",
|
|
17
|
+
)
|