SixpertAI commited on
Commit
ac71b05
·
verified ·
1 Parent(s): f99d7b6

Upload examples/vision_example.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. examples/vision_example.py +109 -0
examples/vision_example.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Sixpert K2 - Vision/Multimodal Example
4
+ =======================================
5
+ Demonstrates how to use Sixpert K2's vision capabilities with
6
+ image inputs. K2's MoE architecture provides specialized experts
7
+ for visual understanding.
8
+
9
+ Usage:
10
+ python vision_example.py --image ./diagram.png --prompt "Analyze this diagram"
11
+ """
12
+
13
+ import argparse
14
+ import base64
15
+ import sys
16
+
17
+ try:
18
+ from llama_cpp import Llama
19
+ except ImportError:
20
+ print("Installing llama-cpp-python...")
21
+ import subprocess
22
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "llama-cpp-python"])
23
+ from llama_cpp import Llama
24
+
25
+
26
+ def image_to_base64(image_path: str) -> str:
27
+ """Convert an image file to base64 data URL."""
28
+ with open(image_path, "rb") as f:
29
+ data = base64.b64encode(f.read()).decode("utf-8")
30
+
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 = 4096,
50
+ ):
51
+ """Analyze an image using Sixpert K2."""
52
+ print(f"Loading Sixpert K2 (MoE)...")
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": "system",
65
+ "content": "You are Sixpert K2, a deep reasoning engine with advanced multimodal capabilities. Analyze images with exceptional detail and precision.",
66
+ },
67
+ {
68
+ "role": "user",
69
+ "content": [
70
+ {"type": "text", "text": prompt},
71
+ {"type": "image_url", "image_url": {"url": image_data}},
72
+ ],
73
+ },
74
+ ]
75
+
76
+ print(f"\nPrompt: {prompt}")
77
+ print(f"Image: {image_path}")
78
+ print("-" * 40)
79
+ print("Analyzing with MoE vision experts...")
80
+ print("-" * 40)
81
+
82
+ response = llm.create_chat_completion(
83
+ messages=messages,
84
+ max_tokens=max_tokens,
85
+ temperature=0.6,
86
+ stream=True,
87
+ )
88
+
89
+ for chunk in response:
90
+ delta = chunk["choices"][0]["delta"].get("content", "")
91
+ if delta:
92
+ print(delta, end="", flush=True)
93
+
94
+ print("\n")
95
+
96
+
97
+ def main():
98
+ parser = argparse.ArgumentParser(description="Sixpert K2 Vision Example")
99
+ parser.add_argument("--model", type=str, default="SixpertK2.gguf", help="Path to GGUF model")
100
+ parser.add_argument("--image", type=str, required=True, help="Path to input image")
101
+ parser.add_argument("--prompt", type=str, default="Describe this image in detail.", help="Prompt")
102
+ parser.add_argument("--max-tokens", type=int, default=4096, help="Max tokens")
103
+
104
+ args = parser.parse_args()
105
+ analyze_image(args.model, args.image, args.prompt, args.max_tokens)
106
+
107
+
108
+ if __name__ == "__main__":
109
+ main()