SixpertAI commited on
Commit
5d7d4e2
·
verified ·
1 Parent(s): 82c25a6

Upload examples/vision_example.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. examples/vision_example.py +111 -0
examples/vision_example.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Sixpert K1 - Vision/Multimodal Example
4
+ =======================================
5
+ Demonstrates how to use Sixpert K1's vision capabilities with
6
+ image inputs using llama-cpp-python.
7
+
8
+ Usage:
9
+ python vision_example.py --image ./photo.jpg --prompt "Describe this image in detail"
10
+ """
11
+
12
+ import argparse
13
+ import base64
14
+ import sys
15
+
16
+ try:
17
+ from llama_cpp import Llama
18
+ except ImportError:
19
+ print("Installing llama-cpp-python...")
20
+ import subprocess
21
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "llama-cpp-python"])
22
+ from llama_cpp import Llama
23
+
24
+
25
+ def image_to_base64(image_path: str) -> str:
26
+ """Convert an image file to base64 data URL."""
27
+ with open(image_path, "rb") as f:
28
+ data = base64.b64encode(f.read()).decode("utf-8")
29
+
30
+ # Detect MIME type from file extension
31
+ if image_path.lower().endswith(".png"):
32
+ mime = "image/png"
33
+ elif image_path.lower().endswith(".jpg") or image_path.lower().endswith(".jpeg"):
34
+ mime = "image/jpeg"
35
+ elif image_path.lower().endswith(".webp"):
36
+ mime = "image/webp"
37
+ elif image_path.lower().endswith(".gif"):
38
+ mime = "image/gif"
39
+ else:
40
+ mime = "image/png"
41
+
42
+ return f"data:{mime};base64,{data}"
43
+
44
+
45
+ def analyze_image(
46
+ model_path: str,
47
+ image_path: str,
48
+ prompt: str,
49
+ max_tokens: int = 2048,
50
+ ):
51
+ """Analyze an image using Sixpert K1."""
52
+ print(f"Loading Sixpert K1...")
53
+ llm = Llama(
54
+ model_path=model_path,
55
+ n_ctx=131072,
56
+ n_gpu_layers=-1,
57
+ verbose=False,
58
+ )
59
+
60
+ image_data = image_to_base64(image_path)
61
+
62
+ messages = [
63
+ {
64
+ "role": "user",
65
+ "content": [
66
+ {
67
+ "type": "text",
68
+ "text": prompt,
69
+ },
70
+ {
71
+ "type": "image_url",
72
+ "image_url": {"url": image_data},
73
+ },
74
+ ],
75
+ },
76
+ ]
77
+
78
+ print(f"\nPrompt: {prompt}")
79
+ print(f"Image: {image_path}")
80
+ print("-" * 40)
81
+ print("Analyzing image...")
82
+ print("-" * 40)
83
+
84
+ response = llm.create_chat_completion(
85
+ messages=messages,
86
+ max_tokens=max_tokens,
87
+ temperature=0.7,
88
+ stream=True,
89
+ )
90
+
91
+ for chunk in response:
92
+ delta = chunk["choices"][0]["delta"].get("content", "")
93
+ if delta:
94
+ print(delta, end="", flush=True)
95
+
96
+ print("\n")
97
+
98
+
99
+ def main():
100
+ parser = argparse.ArgumentParser(description="Sixpert K1 Vision Example")
101
+ parser.add_argument("--model", type=str, default="SixpertK1.gguf", help="Path to GGUF model")
102
+ parser.add_argument("--image", type=str, required=True, help="Path to input image")
103
+ parser.add_argument("--prompt", type=str, default="Describe this image in detail.", help="Prompt")
104
+ parser.add_argument("--max-tokens", type=int, default=2048, help="Max tokens")
105
+
106
+ args = parser.parse_args()
107
+ analyze_image(args.model, args.image, args.prompt, args.max_tokens)
108
+
109
+
110
+ if __name__ == "__main__":
111
+ main()