touchy-dankbot 0.1.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.
dankbot/__init__.py ADDED
@@ -0,0 +1,13 @@
1
+ """dankbot — witty one-liners about overheard conversation, via a remote LLM.
2
+
3
+ This is the "responder" half of the parent repo's creep3r art project: it takes
4
+ a snippet of overheard conversation and asks a small LLM (served remotely by
5
+ Ollama) for a witty reply, or the command ``/notfunny`` when it has nothing worth
6
+ saying. See ``docs/plans/initial.md``.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ __version__ = "0.1.1"
12
+
13
+ __all__ = ["__version__"]
@@ -0,0 +1,88 @@
1
+ # jokemark model matrix — the plan for `dankbot jokemark`.
2
+ #
3
+ # Every `[[models]]` entry is one *bank* of runs: a model plus the sampler it is
4
+ # chatted with, run against every line of the packaged overheard corpus. TOML
5
+ # (rather than JSON) so this file can explain itself — see docs/plans/jokemark.md.
6
+ #
7
+ # Per-entry keys (all optional except `model`):
8
+ # model Ollama model name, e.g. "hf.co/org/repo:Q4_K_M" (required)
9
+ # label report heading; must be unique; defaults to the model leaf name
10
+ # temperature sampler temperature; 0.0 is near-greedy (default 0.85)
11
+ # min_p min-p sampling floor, 0..1 (default 0.05)
12
+ # ctx context window in tokens (default 4096)
13
+ # max_tokens reply cap in tokens (default 120)
14
+ # native true → Ollama /api/chat, false → OpenAI /v1 (CLI flag)
15
+ # timeout per-request chat timeout in seconds (CLI flag)
16
+ #
17
+ # `[defaults]` (same knobs, minus model/label) applies to every entry; an entry
18
+ # overrides it. Anything still unset falls back to dankbot.config's DEFAULT_*.
19
+ #
20
+ # To retire a candidate, comment out its `# [[models]]` block and leave the
21
+ # verdict beside it ("# disabled: very unfunny") — the record of having tried it
22
+ # is worth more than a deleted entry.
23
+
24
+ version = 1
25
+
26
+ [defaults]
27
+ temperature = 0.85
28
+ min_p = 0.05
29
+ ctx = 4096
30
+ max_tokens = 120
31
+
32
+ # --- models already evaluated -------------------------------------------------
33
+
34
+ # The disciplined baseline — `DEFAULT_MODEL` until the Rocinante-X run.
35
+ [[models]]
36
+ model = "hf.co/NousResearch/Hermes-3-Llama-3.1-8B-GGUF:latest"
37
+ label = "hermes-3-8b"
38
+
39
+ # The former default's 12B alternative from docs/plans/initial.md. Q8_0 is
40
+ # the biggest quant that still fits beside everything else on the 16 GB Pi 5.
41
+ [[models]]
42
+ model = "hf.co/bartowski/Human-Like-Mistral-Nemo-Instruct-2407-GGUF:Q8_0"
43
+ label = "human-like-nemo-12b"
44
+
45
+ # The two t= variants exercise the parameter axis and answer the sampler
46
+ # question (does heat help or hurt?) with evidence instead of feel.
47
+
48
+ # Near-greedy: the model should be consistent but repeat itself.
49
+ [[models]]
50
+ model = "hf.co/NousResearch/Hermes-3-Llama-3.1-8B-GGUF:latest"
51
+ label = "hermes-3-8b t=0.0"
52
+ temperature = 0.0
53
+
54
+ # Hot: expected to be wilder, at the risk of incoherence.
55
+ [[models]]
56
+ model = "hf.co/bartowski/Human-Like-Mistral-Nemo-Instruct-2407-GGUF:Q8_0"
57
+ label = "human-like-nemo-12b t=1.0"
58
+ temperature = 1.0
59
+
60
+ # --- smaller Q4_K_M candidates ------------------------------------------------
61
+ #
62
+ # Q4_K_M is the quant that fits a 16 GB Pi 5 alongside the rest of the stack.
63
+ # Rocinante won this group; the other three are retired, with their verdicts.
64
+
65
+ # A 12B "Rocinante" merge — **the current DEFAULT_MODEL**: the funniest of the
66
+ # batch in the bake-off, and Q4_K_M keeps it Pi-sized.
67
+ [[models]]
68
+ model = "hf.co/TheDrummer/Rocinante-X-12B-v1-GGUF:Q4_K_M"
69
+ label = "rocinante-x-12b"
70
+
71
+ # Gemma 3 4B tuned on comedy captions: small enough to be fast on the Pi.
72
+ # @geeksville disabled fairly unfunny
73
+ # [[models]]
74
+ # model = "hf.co/mradermacher/gemma3-4b-it-comedy-v2-GGUF:Q4_K_M"
75
+ # label = "gemma3-4b-comedy"
76
+
77
+ # Gemma 4 12B — newer generation, same size class as the Nemo/Rocinante rows.
78
+ # @geeksville disabled very unfunny
79
+ # [[models]]
80
+ # model = "hf.co/unsloth/gemma-4-12b-it-GGUF:Q4_K_M"
81
+ # label = "gemma-4-12b"
82
+
83
+ # Qwen 3.5 9B Claude-merge (uncensored/heretic): the most "personality" of the
84
+ # batch; uncensored models tend to be looser with the persona's tone.
85
+ # @geeksville disabled very unfunny
86
+ # [[models]]
87
+ # model = "hf.co/mradermacher/Qwen3.5-9B-Claude-3xMerge-UNCENSORED-heretic-i1-GGUF:Q4_K_M"
88
+ # label = "qwen3.5-9b-claude-merge"
@@ -0,0 +1,36 @@
1
+ meer loves the new toy
2
+ the bread is done baking
3
+ today was hard at work
4
+ the light is red
5
+ have you heard from mike lately?
6
+ im glad i voted - lets see how this goes
7
+ can we get a table for four please
8
+ the wine list is longer than the menu
9
+ i think the fish is a little undercooked
10
+ did you try the truffle fries yet
11
+ we should split the bill three ways
12
+ she ordered the same thing as me
13
+ the waiter forgot about our appetizers
14
+ do you want dessert or just coffee
15
+ my sister is vegetarian but she eats fish sometimes
16
+ the music in here is way too loud
17
+ you have to try the chocolate cake
18
+ im pretty sure that table is arguing
19
+ lets order another bottle of the red
20
+ that couple has been on their phones all night
21
+ i cant believe the chef came out to say hi
22
+ the bread basket is already empty again
23
+ i thought you said you were cutting back on wine
24
+ our reservation was for seven thirty
25
+ the anniversary dinner went better than expected
26
+ someone at the bar is singing happy birthday
27
+ do you think they know its our first date
28
+ the pasta special sounds amazing
29
+ i asked for no onions three times
30
+ the tip is already included on the bill
31
+ that was the best risotto ive had in years
32
+ lets go somewhere quieter next time
33
+ the kitchen closes in twenty minutes
34
+ pass the salt
35
+ more wine please
36
+ its too loud
dankbot/audio.py ADDED
@@ -0,0 +1,260 @@
1
+ """Microphone capture + speech-to-text — the "audio in" stage.
2
+
3
+ This is the only module that imports ``moonshine_voice`` / ``sounddevice`` (and
4
+ it does so lazily, so importing :mod:`dankbot` — and the test suite — never
5
+ needs them). Everything else talks to the tiny :class:`TranscriptSource` seam
6
+ below, which is what makes the loop testable with no mic, no model and no
7
+ network.
8
+
9
+ Mic: a Seeed reSpeaker XVF3800 USB array in production, addressed by name so it
10
+ is found whatever PortAudio index it lands on. Engine: Moonshine Voice's
11
+ ``MicTranscriber``, which already does VAD, streaming and its own worker-thread
12
+ capture + resampling to 16 kHz — we only configure it and bridge its finished
13
+ lines onto a queue. See ``docs/plans/audioin.md``.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import logging
19
+ import queue
20
+ from collections.abc import Iterable, Iterator
21
+ from dataclasses import dataclass
22
+ from pathlib import Path
23
+ from typing import Protocol, TextIO, runtime_checkable
24
+
25
+ from .server import DankbotError
26
+
27
+ log = logging.getLogger(__name__)
28
+
29
+
30
+ class AudioError(DankbotError):
31
+ """The microphone could not be opened, or the STT engine could not start."""
32
+
33
+
34
+ @dataclass(frozen=True)
35
+ class Utterance:
36
+ """One finished thing a person said, as produced by the STT engine."""
37
+
38
+ text: str
39
+ duration_s: float | None = None
40
+
41
+
42
+ _CLOSED = object()
43
+
44
+
45
+ @runtime_checkable
46
+ class TranscriptSource(Protocol):
47
+ """The audio seam: a blocking stream of finished utterances."""
48
+
49
+ def lines(self) -> Iterator[Utterance]:
50
+ """Block and yield finished utterances until :meth:`close`."""
51
+ ...
52
+
53
+ def close(self) -> None:
54
+ """Stop the source and release the microphone."""
55
+ ...
56
+
57
+
58
+ class FakeTranscriptSource:
59
+ """Replay a scripted list of utterances — tests and ``listen --fake``.
60
+
61
+ Needs no audio hardware and no model, so it drives the whole loop in CI.
62
+ """
63
+
64
+ def __init__(self, utterances: Iterable[str | Utterance]) -> None:
65
+ self._items = [u if isinstance(u, Utterance) else Utterance(str(u)) for u in utterances]
66
+ self.closed = False
67
+
68
+ def lines(self) -> Iterator[Utterance]:
69
+ yield from self._items
70
+
71
+ def close(self) -> None:
72
+ self.closed = True
73
+
74
+
75
+ def read_lines(stream: TextIO) -> list[str]:
76
+ """The non-blank, whitespace-trimmed lines of *stream* (for ``--fake``)."""
77
+ return [stripped for raw in stream if (stripped := raw.strip())]
78
+
79
+
80
+ #: Friendly ``--model-arch`` names -> Moonshine ``ModelArch`` attribute. The
81
+ #: streaming variants are what we want; every English alias maps to one.
82
+ _MOONSHINE_ARCHES = {
83
+ "tiny": "TINY_STREAMING",
84
+ "tiny-streaming": "TINY_STREAMING",
85
+ "tinystreaming": "TINY_STREAMING",
86
+ "base": "BASE_STREAMING",
87
+ "base-streaming": "BASE_STREAMING",
88
+ "basestreaming": "BASE_STREAMING",
89
+ "small": "SMALL_STREAMING",
90
+ "small-streaming": "SMALL_STREAMING",
91
+ "smallstreaming": "SMALL_STREAMING",
92
+ "medium": "MEDIUM_STREAMING",
93
+ "medium-streaming": "MEDIUM_STREAMING",
94
+ "mediumstreaming": "MEDIUM_STREAMING",
95
+ }
96
+
97
+
98
+ def _as_device(value: int | str | None) -> int | str | None:
99
+ """Normalise a PortAudio device spec.
100
+
101
+ ``MicTranscriber.device()`` (and sounddevice) treat a *string* as a
102
+ case-insensitive name substring and an *int* as a device index. The CLI's
103
+ ``--device`` is a string, so ``--device 5`` would otherwise be passed
104
+ through as the name ``"5"`` and latch onto whatever device happened to
105
+ contain a 5 (``hw:5,0`` …). Coerce a bare integer string back to an index.
106
+ """
107
+ if isinstance(value, str):
108
+ text = value.strip()
109
+ if text.isdigit():
110
+ return int(text)
111
+ return value
112
+
113
+
114
+ def resolve_device(hint: str | None, *, device: int | str | None = None) -> int | str | None:
115
+ """Pick a PortAudio capture device.
116
+
117
+ An explicit *device* (index or name substring) always wins. Otherwise the
118
+ first input device whose name contains *hint* (case-insensitive) is used —
119
+ that is how the reSpeaker XVF3800 is found whatever index it gets. ``None``
120
+ means "let PortAudio pick the default".
121
+ """
122
+ if device is not None:
123
+ return _as_device(device)
124
+ if not hint:
125
+ return None
126
+ try:
127
+ import sounddevice as sd
128
+
129
+ for index, info in enumerate(sd.query_devices()):
130
+ if info.get("max_input_channels", 0) > 0 and hint.lower() in info["name"].lower():
131
+ log.info("using input device %d: %s", index, info["name"])
132
+ return index
133
+ except Exception as exc: # noqa: BLE001 - any PortAudio/backend error is fine here
134
+ log.warning("could not query audio devices (%s); using the default", exc)
135
+ return None
136
+ log.warning("no input device matched %r; using PortAudio's default", hint)
137
+ return None
138
+
139
+
140
+ def _close_quietly(mic) -> None:
141
+ """Best-effort release of a ``MicTranscriber`` that failed partway through.
142
+
143
+ ``load()`` opens the model and its capture stream before ``start()`` can
144
+ fail, so a half-built transcriber still holds the microphone; ``close()``
145
+ is safe on a partially constructed one. Never mask the real error.
146
+ """
147
+ try:
148
+ mic.close()
149
+ except Exception as exc: # noqa: BLE001 - cleanup must not raise
150
+ log.debug("mic.close() while cleaning up: %s", exc)
151
+
152
+
153
+ class AudioIn:
154
+ """A :class:`TranscriptSource` backed by the mic and a Moonshine model.
155
+
156
+ ``MicTranscriber`` does the heavy lifting (VAD, streaming, its own capture
157
+ worker, resampling to 16 kHz); we configure it, bridge its ``on_line``
158
+ callback onto a queue (and log its ``on_error`` stream), and hand finished
159
+ lines out through :meth:`lines`.
160
+ """
161
+
162
+ def __init__(
163
+ self,
164
+ *,
165
+ device: int | str | None = None,
166
+ mic_hint: str | None = "respeaker",
167
+ model_arch: str = "small",
168
+ language: str = "en",
169
+ models_dir: str | Path | None = None,
170
+ channels: int | None = None,
171
+ options: dict | None = None,
172
+ ) -> None:
173
+ self._device = resolve_device(mic_hint, device=device)
174
+ self._model_arch = model_arch
175
+ self._language = language
176
+ self._models_dir = models_dir
177
+ self._channels = channels
178
+ self._options = options or {}
179
+ self._queue: queue.Queue = queue.Queue()
180
+ self._mic = None
181
+
182
+ # -- construction ------------------------------------------------------
183
+
184
+ def _build(self):
185
+ """Build, load and start the Moonshine mic transcriber."""
186
+ arch_name = _MOONSHINE_ARCHES.get(self._model_arch.lower().replace("_", "-"))
187
+ if arch_name is None:
188
+ known = ", ".join(sorted(set(_MOONSHINE_ARCHES)))
189
+ raise AudioError(f"unknown model arch {self._model_arch!r} (expected one of: {known})")
190
+
191
+ try:
192
+ from moonshine_voice import MicTranscriber, ModelArch
193
+ except Exception as exc: # pragma: no cover - dependency missing
194
+ raise AudioError(f"speech-to-text engine unavailable: {exc}") from exc
195
+
196
+ mic = MicTranscriber().language(self._language).model_arch(getattr(ModelArch, arch_name))
197
+ if self._device is not None:
198
+ mic.device(self._device)
199
+ if self._channels is not None:
200
+ mic.channels(self._channels)
201
+ if self._models_dir is not None:
202
+ mic.models_from(str(self._models_dir))
203
+ if self._options:
204
+ mic.options(self._options)
205
+ mic.on_line(self._on_line)
206
+ mic.on_error(self._on_error)
207
+ try:
208
+ log.info("loading the %s speech-to-text model…", self._model_arch)
209
+ mic.load()
210
+ mic.start()
211
+ except Exception as exc:
212
+ # load() may already have opened the model and its stream; a failed
213
+ # start() must not leak the microphone for the life of the process.
214
+ _close_quietly(mic)
215
+ raise AudioError(f"could not start the microphone: {exc}") from exc
216
+ return mic
217
+
218
+ def _on_line(self, line) -> None:
219
+ """Called from Moonshine's worker thread for each finished line."""
220
+ text = (getattr(line, "text", "") or "").strip()
221
+ if text:
222
+ self._queue.put(Utterance(text, getattr(line, "duration", None)))
223
+
224
+ def _on_error(self, error) -> None:
225
+ """Called from Moonshine's worker thread when the pipeline raises.
226
+
227
+ The library delivers ``Error`` events (a capture/inference failure
228
+ inside its audio worker) to ``on_error`` and otherwise drops them, so
229
+ attaching this is what keeps a broken stream from failing silently.
230
+ """
231
+ log.warning("speech-to-text error: %s", error)
232
+
233
+ # -- TranscriptSource --------------------------------------------------
234
+
235
+ def lines(self) -> Iterator[Utterance]:
236
+ self._mic = self._build()
237
+ try:
238
+ while True:
239
+ item = self._queue.get()
240
+ if item is _CLOSED:
241
+ return
242
+ yield item
243
+ finally:
244
+ self._teardown()
245
+
246
+ def close(self) -> None:
247
+ # Wake lines() (if running) and stop the mic now, so a ctrl-c that
248
+ # interrupts the loop still releases the device.
249
+ self._queue.put(_CLOSED)
250
+ self._teardown()
251
+
252
+ def _teardown(self) -> None:
253
+ mic, self._mic = self._mic, None
254
+ if mic is None:
255
+ return
256
+ for step in ("stop", "close"):
257
+ try:
258
+ getattr(mic, step)()
259
+ except Exception as exc: # noqa: BLE001 - best-effort cleanup, never fatal
260
+ log.debug("mic.%s() while closing: %s", step, exc)