Repository navigation
Expand file tree
/
Copy pathtest_local.py
More file actions
667 lines (538 loc) · 19.4 KB
/
Copy pathtest_local.py
File metadata and controls
667 lines (538 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
import sys
import os
import asyncio
import threading
from types import SimpleNamespace
import moondream as md
from PIL import Image
from moondream.photon_client import PhotonClient, PhotonStream, _parse_model
def test_model_parser_preserves_registered_repository_ids():
assert _parse_model("Qwen/Qwen3.5-4B") == ("Qwen/Qwen3.5-4B", None)
assert _parse_model("google/gemma-4-E2B-it") == (
"google/gemma-4-E2B-it",
None,
)
def test_model_parser_extracts_explicit_finetune_suffix():
assert _parse_model("moondream3-preview/01HXYZ@1000") == (
"moondream3-preview",
"01HXYZ@1000",
)
assert _parse_model("moondream3-preview/ft_abc@1000") == (
"moondream3-preview",
"ft_abc@1000",
)
def test_model_parser_rejects_malformed_finetune_suffixes():
assert _parse_model("moondream3-preview/01HXYZ") == (
"moondream3-preview/01HXYZ",
None,
)
assert _parse_model("moondream3-preview/01HXYZ@latest") == (
"moondream3-preview/01HXYZ@latest",
None,
)
def test_vl_local_delegates_to_photon(monkeypatch):
captured = {}
def fake_photon(model, *, api_key=None, **runtime_config):
captured.update(
model=model,
api_key=api_key,
runtime_config=runtime_config,
)
return "photon"
monkeypatch.setattr(md, "photon", fake_photon)
result = md.vl(
api_key="key",
local=True,
model="Qwen/Qwen3.5-4B",
max_batch_size=8,
)
assert result == "photon"
assert captured == {
"model": "Qwen/Qwen3.5-4B",
"api_key": "key",
"runtime_config": {"max_batch_size": 8},
}
def test_vl_local_requires_model():
try:
md.vl(local=True)
except TypeError as exc:
assert str(exc) == "vl(local=True) requires model=<model identifier>"
else:
raise AssertionError("local Photon inference accepted no model")
def test_photon_requires_model():
try:
md.photon()
except TypeError as exc:
assert "model" in str(exc)
else:
raise AssertionError("Photon accepted no model")
def test_photon_chat_preserves_model_reasoning_default():
calls = []
class FakeModel:
async def chat(self, **prompt):
calls.append(prompt)
return SimpleNamespace(
output={
"message": {"role": "assistant", "content": "answer"},
"finish_reason": None,
},
finish_reason=None,
)
client = object.__new__(PhotonClient)
client._adapter = None
client._model = FakeModel()
client._run = asyncio.run
result = client.chat([{"role": "user", "content": "question"}])
client.chat(
[{"role": "user", "content": "question"}],
reasoning=False,
)
assert "reasoning" not in calls[0]
assert calls[1]["reasoning"] is False
assert result["finish_reason"] == "stop"
def test_photon_transcribe_passes_options_and_returns_public_output():
calls = []
class FakeModel:
model_id = "openai/whisper-large-v3-turbo"
tasks = ("transcribe",)
def supports(self, task):
return task in self.tasks
async def transcribe(self, **prompt):
calls.append(prompt)
return SimpleNamespace(
output={
"text": "hello world",
"language": "en",
"segments": [],
}
)
client = object.__new__(PhotonClient)
client._adapter = None
client._model = FakeModel()
client._run = asyncio.run
result = client.transcribe(
audio=b"encoded audio",
task="translate",
timestamps="word",
initial_prompt="Moondream, Photon",
settings={"temperature": 0.0},
)
assert result == {
"text": "hello world",
"language": "en",
"segments": [],
}
assert calls == [
{
"audio": b"encoded audio",
"task": "translate",
"timestamps": "word",
"initial_prompt": "Moondream, Photon",
"settings": {"temperature": 0.0},
}
]
def test_photon_synthesize_forwards_prompt_and_audio():
calls = []
class FakeModel:
model_id = "Qwen/Qwen3-TTS-12Hz-0.6B-CustomVoice"
tasks = ("synthesize",)
def supports(self, task):
return task in self.tasks
async def synthesize(self, **prompt):
calls.append(prompt)
return SimpleNamespace(output={"audio": [0.1, 0.2], "sample_rate": 24_000})
async def arun(coro):
return await coro
client = object.__new__(PhotonClient)
client._adapter = None
client._model = FakeModel()
client._run = asyncio.run
client._arun = arun
expected = {"audio": [0.1, 0.2], "sample_rate": 24_000}
assert client.synthesize(text="hello") == expected
assert asyncio.run(client.asynthesize(text="hello")) == expected
assert calls == [{"text": "hello"}, {"text": "hello"}]
def test_photon_invoke_rejects_unadvertised_capability():
class FakeModel:
model_id = "vision-model"
tasks = ("query",)
def supports(self, task):
return task in self.tasks
client = object.__new__(PhotonClient)
client._model = FakeModel()
try:
client.invoke("transcribe", audio=b"audio")
except ValueError as exc:
assert "does not support 'transcribe'" in str(exc)
else:
raise AssertionError("Photon invoked an unadvertised capability")
def test_photon_transcription_stream_preserves_snapshots_and_result():
class FakeCapabilityStream:
def __init__(self):
self._updates = iter(
[
{"text": "hello", "segments": []},
{"text": "hello world", "segments": []},
]
)
def __aiter__(self):
return self
async def __anext__(self):
try:
output = next(self._updates)
except StopIteration:
raise StopAsyncIteration from None
return SimpleNamespace(output=output)
async def result(self):
return SimpleNamespace(
output={"text": "hello world", "segments": []}
)
async def aclose(self):
return None
class FakeModel:
model_id = "openai/whisper-large-v3-turbo"
tasks = ("transcribe",)
def supports(self, task):
return task in self.tasks
async def transcribe(self, **prompt):
assert prompt == {"audio": b"audio", "stream": True}
return FakeCapabilityStream()
loop = asyncio.new_event_loop()
thread = threading.Thread(target=loop.run_forever)
thread.start()
client = object.__new__(PhotonClient)
client._adapter = None
client._loop = loop
client._model = FakeModel()
try:
stream = client.transcribe(audio=b"audio", stream=True)
assert isinstance(stream, PhotonStream)
assert list(stream) == [
{"text": "hello", "segments": []},
{"text": "hello world", "segments": []},
]
assert stream.result() == {"text": "hello world", "segments": []}
finally:
loop.call_soon_threadsafe(loop.stop)
thread.join()
loop.close()
def test_photon_run_returns_single_pass_public_output():
class FakeModel:
async def run(self, task, inputs):
assert task == "embed"
assert inputs == {"text": "hello"}
return SimpleNamespace(output={"embedding": [1.0, 2.0]})
client = object.__new__(PhotonClient)
client._model = FakeModel()
client._run = asyncio.run
assert client.run("embed", {"text": "hello"}) == {
"embedding": [1.0, 2.0]
}
def test_photon_stateful_stream_forwards_chunks_updates_and_close():
sent = []
class FakeModelStream:
def __init__(self):
self._emitted = False
def __aiter__(self):
return self
async def __anext__(self):
if self._emitted:
raise StopAsyncIteration
self._emitted = True
return SimpleNamespace(output={"text": "partial"})
async def send(self, **chunk):
sent.append(chunk)
async def close(self):
return SimpleNamespace(output={"text": "final"})
async def result(self):
return SimpleNamespace(output={"text": "final"})
class FakeModel:
async def stream(self, task, **initial_prompt):
assert task == "listen"
assert initial_prompt == {"sample_rate": 16_000}
return FakeModelStream()
loop = asyncio.new_event_loop()
thread = threading.Thread(target=loop.run_forever)
thread.start()
client = object.__new__(PhotonClient)
client._loop = loop
client._model = FakeModel()
try:
stream = client.stream("listen", sample_rate=16_000)
stream.send(audio=b"pcm")
assert next(stream) == {"text": "partial"}
assert stream.close() == {"text": "final"}
assert sent == [{"audio": b"pcm"}]
finally:
loop.call_soon_threadsafe(loop.stop)
thread.join()
loop.close()
def test_photon_live_audio_bridges_application_and_worker_loops():
source_loops = []
class FakeLiveStream:
def __init__(self, audio):
self._audio = audio.__aiter__()
self._chunks = []
def __aiter__(self):
return self
async def __anext__(self):
try:
chunk = await self._audio.__anext__()
except StopAsyncIteration:
raise StopAsyncIteration from None
self._chunks.append(chunk)
return SimpleNamespace(output={"chunk": chunk})
async def result(self):
return SimpleNamespace(output={"chunks": list(self._chunks)})
async def aclose(self):
closer = getattr(self._audio, "aclose", None)
if closer is not None:
await closer()
class FakeModel:
model_id = "openai/whisper-large-v3-turbo"
tasks = ("transcribe",)
def supports(self, task):
return task in self.tasks
async def transcribe(self, **prompt):
assert prompt["sample_rate"] == 16_000
assert prompt["stream"] is True
return FakeLiveStream(prompt["audio"])
worker_loop = asyncio.new_event_loop()
thread = threading.Thread(target=worker_loop.run_forever)
thread.start()
client = object.__new__(PhotonClient)
client._adapter = None
client._loop = worker_loop
client._model = FakeModel()
async def run():
application_loop = asyncio.get_running_loop()
async def audio_chunks():
source_loops.append(asyncio.get_running_loop())
yield b"first"
await asyncio.sleep(0)
source_loops.append(asyncio.get_running_loop())
yield b"second"
stream = client.transcribe(
audio=audio_chunks(),
sample_rate=16_000,
stream=True,
)
updates = [update async for update in stream]
assert updates == [{"chunk": b"first"}, {"chunk": b"second"}]
assert await stream.aresult() == {"chunks": [b"first", b"second"]}
assert source_loops == [application_loop, application_loop]
try:
asyncio.run(run())
finally:
worker_loop.call_soon_threadsafe(worker_loop.stop)
thread.join()
worker_loop.close()
def test_photon_nonstream_live_audio_uses_async_entrypoint():
source_loops = []
class FakeModel:
model_id = "openai/whisper-large-v3-turbo"
tasks = ("transcribe",)
def supports(self, task):
return task in self.tasks
async def transcribe(self, **prompt):
chunks = [chunk async for chunk in prompt["audio"]]
return SimpleNamespace(output={"chunks": chunks})
worker_loop = asyncio.new_event_loop()
thread = threading.Thread(target=worker_loop.run_forever)
thread.start()
client = object.__new__(PhotonClient)
client._adapter = None
client._loop = worker_loop
client._model = FakeModel()
async def run():
application_loop = asyncio.get_running_loop()
async def audio_chunks():
source_loops.append(asyncio.get_running_loop())
yield b"first"
await asyncio.sleep(0)
source_loops.append(asyncio.get_running_loop())
yield b"second"
try:
client.transcribe(audio=audio_chunks(), stream=False)
except RuntimeError as exc:
assert "await speech.atranscribe" in str(exc)
else:
raise AssertionError("sync live transcription should reject this loop")
result = await client.atranscribe(
audio=audio_chunks(),
stream=False,
)
assert result == {"chunks": [b"first", b"second"]}
assert source_loops == [application_loop, application_loop]
try:
asyncio.run(run())
finally:
worker_loop.call_soon_threadsafe(worker_loop.stop)
thread.join()
worker_loop.close()
def test_photon_stream_fails_after_its_worker_loop_stops():
class FakeStream:
async def __anext__(self):
return SimpleNamespace(output={"text": "unreachable"})
async def result(self):
return SimpleNamespace(output={"text": "unreachable"})
loop = asyncio.new_event_loop()
thread = threading.Thread(target=loop.run_forever)
thread.start()
stream = PhotonStream(FakeStream(), loop)
loop.call_soon_threadsafe(loop.stop)
thread.join()
try:
next(stream)
except RuntimeError as exc:
assert "engine is closed" in str(exc)
else:
raise AssertionError("stream operation waited on a stopped worker")
finally:
loop.close()
def test_photon_stream_detects_worker_stop_after_submission():
class FakeStream:
async def __anext__(self):
return SimpleNamespace(output={"text": "unreachable"})
loop = asyncio.new_event_loop()
worker_entered = threading.Event()
release_worker = threading.Event()
submission_queued = threading.Event()
outcome = []
def block_then_stop():
worker_entered.set()
release_worker.wait()
loop.stop()
thread = threading.Thread(target=loop.run_forever)
thread.start()
loop.call_soon_threadsafe(block_then_stop)
assert worker_entered.wait(timeout=1)
original_call_soon = loop.call_soon_threadsafe
def observed_call_soon(callback, *args, **kwargs):
handle = original_call_soon(callback, *args, **kwargs)
submission_queued.set()
return handle
loop.call_soon_threadsafe = observed_call_soon
stream = PhotonStream(FakeStream(), loop)
def consume():
try:
next(stream)
except BaseException as exc:
outcome.append(exc)
consumer = threading.Thread(target=consume)
consumer.start()
assert submission_queued.wait(timeout=1)
release_worker.set()
thread.join(timeout=1)
consumer.join(timeout=1)
loop.call_soon_threadsafe = original_call_soon
try:
assert not consumer.is_alive()
assert len(outcome) == 1
assert isinstance(outcome[0], RuntimeError)
assert "engine is closed" in str(outcome[0])
finally:
if consumer.is_alive():
rescue = threading.Thread(target=loop.run_forever)
rescue.start()
consumer.join(timeout=1)
original_call_soon(loop.stop)
rescue.join(timeout=1)
loop.close()
def test_photon_stream_propagates_worker_timeout_sync_and_async():
class TimeoutStream:
async def __anext__(self):
raise TimeoutError("operation timeout")
loop = asyncio.new_event_loop()
thread = threading.Thread(target=loop.run_forever)
thread.start()
try:
sync_stream = PhotonStream(TimeoutStream(), loop)
try:
next(sync_stream)
except TimeoutError as exc:
assert str(exc) == "operation timeout"
else:
raise AssertionError("sync stream swallowed the worker timeout")
async def consume():
async_stream = PhotonStream(TimeoutStream(), loop)
try:
await async_stream.__anext__()
except TimeoutError as exc:
assert str(exc) == "operation timeout"
else:
raise AssertionError("async stream swallowed the worker timeout")
asyncio.run(consume())
finally:
loop.call_soon_threadsafe(loop.stop)
thread.join()
loop.close()
def main(image_path: str):
if not os.path.exists(image_path):
print(f"Error: Image not found at path '{image_path}'")
sys.exit(1)
try:
image = Image.open(image_path)
except Exception as e:
print(f"Error loading image: {e}")
sys.exit(1)
# Instantiate the client in local mode.
client = md.vl(local=True, model="moondream3.1-9B-A2B")
# Test the caption method.
try:
print("Starting caption")
caption_output = client.caption(image, settings={"max_tokens": 10})
print("Caption:", caption_output.get("caption"))
print("\n------ done ------\n")
except Exception as e:
print("Caption test failed:", e)
try:
print("Starting caption stream")
for chunk in client.caption(image, length="long", stream=True)["caption"]:
print(chunk, end="", flush=True)
print("\n------ done ------\n")
except Exception as e:
print("Caption stream test failed:", e)
try:
print("start Query")
query_output = client.query(
image, "What's in the image?", settings={"max_tokens": 10}
)
print("Query Answer:", query_output.get("answer"))
print("\n------ done ------\n")
except Exception as e:
print("Query test failed:", e)
try:
print("Starting query stream")
for chunk in client.query(image, "What's in the image?", stream=True)["answer"]:
print(chunk, end="", flush=True)
print("\n------ done ------\n")
except Exception as e:
print("query stream test failed:", e)
# Test the detect method.
try:
print("Starting output")
detect_output = client.detect(image, "item")
print("Detected Objects:", detect_output.get("objects"))
print("\n------ done ------\n")
except Exception as e:
print("Detect test failed:", e)
# Test the point method.
try:
print("Starting Point")
point_output = client.point(image, "person")
print("Points:", point_output.get("points"))
print("\n------ done ------\n")
except Exception as e:
print("Point test failed:", e)
if __name__ == "__main__":
image_path = "moondream/assets/how-to-be-a-people-person-1662995088.jpg"
main(image_path)
def test_photon_device_falls_back_to_cpu(monkeypatch):
import torch
from moondream import photon_client
monkeypatch.setattr(torch.cuda, "is_available", lambda: False)
monkeypatch.setattr(torch.backends.cuda, "is_built", lambda: False)
if hasattr(torch.backends, "mps"):
monkeypatch.setattr(torch.backends.mps, "is_available", lambda: False)
assert photon_client._default_photon_device() == "cpu"