-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_local.py
More file actions
77 lines (65 loc) · 2.34 KB
/
test_local.py
File metadata and controls
77 lines (65 loc) · 2.34 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
import sys
import os
import moondream as md
from PIL import Image
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(endpoint="http://localhost:2020/v1")
# 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)