File size: 3,033 Bytes
5d7d4e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Sixpert K1 - Vision/Multimodal Example
=======================================
Demonstrates how to use Sixpert K1's vision capabilities with
image inputs using llama-cpp-python.

Usage:
    python vision_example.py --image ./photo.jpg --prompt "Describe this image in detail"
"""

import argparse
import base64
import sys

try:
    from llama_cpp import Llama
except ImportError:
    print("Installing llama-cpp-python...")
    import subprocess
    subprocess.check_call([sys.executable, "-m", "pip", "install", "llama-cpp-python"])
    from llama_cpp import Llama


def image_to_base64(image_path: str) -> str:
    """Convert an image file to base64 data URL."""
    with open(image_path, "rb") as f:
        data = base64.b64encode(f.read()).decode("utf-8")

    # Detect MIME type from file extension
    if image_path.lower().endswith(".png"):
        mime = "image/png"
    elif image_path.lower().endswith(".jpg") or image_path.lower().endswith(".jpeg"):
        mime = "image/jpeg"
    elif image_path.lower().endswith(".webp"):
        mime = "image/webp"
    elif image_path.lower().endswith(".gif"):
        mime = "image/gif"
    else:
        mime = "image/png"

    return f"data:{mime};base64,{data}"


def analyze_image(
    model_path: str,
    image_path: str,
    prompt: str,
    max_tokens: int = 2048,
):
    """Analyze an image using Sixpert K1."""
    print(f"Loading Sixpert K1...")
    llm = Llama(
        model_path=model_path,
        n_ctx=131072,
        n_gpu_layers=-1,
        verbose=False,
    )

    image_data = image_to_base64(image_path)

    messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": prompt,
                },
                {
                    "type": "image_url",
                    "image_url": {"url": image_data},
                },
            ],
        },
    ]

    print(f"\nPrompt: {prompt}")
    print(f"Image: {image_path}")
    print("-" * 40)
    print("Analyzing image...")
    print("-" * 40)

    response = llm.create_chat_completion(
        messages=messages,
        max_tokens=max_tokens,
        temperature=0.7,
        stream=True,
    )

    for chunk in response:
        delta = chunk["choices"][0]["delta"].get("content", "")
        if delta:
            print(delta, end="", flush=True)

    print("\n")


def main():
    parser = argparse.ArgumentParser(description="Sixpert K1 Vision Example")
    parser.add_argument("--model", type=str, default="SixpertK1.gguf", help="Path to GGUF model")
    parser.add_argument("--image", type=str, required=True, help="Path to input image")
    parser.add_argument("--prompt", type=str, default="Describe this image in detail.", help="Prompt")
    parser.add_argument("--max-tokens", type=int, default=2048, help="Max tokens")

    args = parser.parse_args()
    analyze_image(args.model, args.image, args.prompt, args.max_tokens)


if __name__ == "__main__":
    main()