JamePeng2023 commited on
Commit
930f906
·
verified ·
1 Parent(s): 86ffe39

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +167 -0
README.md CHANGED
@@ -1,3 +1,170 @@
1
  ---
2
  license: apache-2.0
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ base_model:
4
+ - opendatalab/MinerU2.5-Pro-2605-1.2B
5
+ pipeline_tag: image-text-to-text
6
  ---
7
+ The `MinerU2.5-Pro` is now supported in `llama-cpp-python`. This project provides a test GGUF file.
8
+
9
+ `llama-cpp-python`: https://github.com/JamePeng/llama-cpp-python
10
+
11
+ Code example:
12
+ ```python
13
+ from llama_cpp import Llama
14
+ from llama_cpp.llama_chat_format import Qwen25VLChatHandler
15
+ import base64
16
+ import os
17
+
18
+ # Model and multimodal projection paths
19
+ MODEL_PATH = r".\MinerU2.5-Pro-2605-1.2b-BF16.gguf"
20
+ MMPROJ_PATH = r".\mmproj-MinerU2.5-Pro-2605-BF16.gguf"
21
+
22
+ # Initialize the Llama model with vision support
23
+ llm = Llama(
24
+ model_path=MODEL_PATH,
25
+ chat_handler=Qwen25VLChatHandler(
26
+ clip_model_path=MMPROJ_PATH,
27
+ verbose=True
28
+ ),
29
+ n_gpu_layers=-1, # Use all available GPU layers
30
+ n_ctx = 20480, # Context window size
31
+ n_batch=2048,
32
+ verbose=False
33
+ )
34
+
35
+ # Comprehensive MIME type mapping (updated as of 2025)
36
+ # Based on Pillow 10.x+ "Fully Supported" (Read & Write) formats
37
+ # Reference: IANA official media types + common real-world usage
38
+ # See: https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html
39
+ _IMAGE_MIME_TYPES = {
40
+ # Most common formats
41
+ '.png': 'image/png',
42
+ '.jpg': 'image/jpeg',
43
+ '.jpeg': 'image/jpeg',
44
+ '.gif': 'image/gif',
45
+ '.webp': 'image/webp',
46
+
47
+ # Next-generation formats
48
+ '.avif': 'image/avif',
49
+ '.jp2': 'image/jp2',
50
+ '.j2k': 'image/jp2',
51
+ '.jpx': 'image/jp2',
52
+
53
+ # Legacy / Windows formats
54
+ '.bmp': 'image/bmp',
55
+ '.ico': 'image/x-icon',
56
+ '.pcx': 'image/x-pcx',
57
+ '.tga': 'image/x-tga',
58
+ '.icns': 'image/icns',
59
+
60
+ # Professional / Scientific imaging
61
+ '.tif': 'image/tiff',
62
+ '.tiff': 'image/tiff',
63
+ '.eps': 'application/postscript',
64
+ '.dds': 'image/vnd-ms.dds',
65
+ '.dib': 'image/dib',
66
+ '.sgi': 'image/sgi',
67
+
68
+ # Portable Map formats (PPM/PGM/PBM)
69
+ '.pbm': 'image/x-portable-bitmap',
70
+ '.pgm': 'image/x-portable-graymap',
71
+ '.ppm': 'image/x-portable-pixmap',
72
+
73
+ # Miscellaneous / Older formats
74
+ '.xbm': 'image/x-xbitmap',
75
+ '.mpo': 'image/mpo',
76
+ '.msp': 'image/msp',
77
+ '.im': 'image/x-pillow-im',
78
+ '.qoi': 'image/qoi',
79
+ }
80
+
81
+ def image_to_base64_data_uri(
82
+ file_path: str,
83
+ *,
84
+ fallback_mime: str = "application/octet-stream"
85
+ ) -> str:
86
+ """
87
+ Convert a local image file to a base64-encoded data URI with the correct MIME type.
88
+
89
+ Supports 20+ image formats (PNG, JPEG, WebP, AVIF, HEIC, SVG, BMP, ICO, TIFF, etc.).
90
+
91
+ Args:
92
+ file_path: Path to the image file on disk.
93
+ fallback_mime: MIME type used when the file extension is unknown.
94
+
95
+ Returns:
96
+ A valid data URI string (e.g., data:image/webp;base64,...).
97
+
98
+ Raises:
99
+ FileNotFoundError: If the file does not exist.
100
+ OSError: If reading the file fails.
101
+ """
102
+ if not os.path.isfile(file_path):
103
+ raise FileNotFoundError(f"Image file not found: {file_path}")
104
+
105
+ extension = os.path.splitext(file_path)[1].lower()
106
+ mime_type = _IMAGE_MIME_TYPES.get(extension, fallback_mime)
107
+
108
+ if mime_type == fallback_mime:
109
+ print(f"Warning: Unknown extension '{extension}' for '{file_path}'. "
110
+ f"Using fallback MIME type: {fallback_mime}")
111
+
112
+ try:
113
+ with open(file_path, "rb") as img_file:
114
+ encoded_data = base64.b64encode(img_file.read()).decode("utf-8")
115
+ except OSError as e:
116
+ raise OSError(f"Failed to read image file '{file_path}': {e}") from e
117
+
118
+ return f"data:{mime_type};base64,{encoded_data}"
119
+
120
+
121
+ # ========================
122
+ # Main image processing & inference section
123
+ # ========================
124
+
125
+ # 1. List of image paths you want to analyze (supports mixed formats)
126
+ image_paths = [
127
+ r'./book.jpg',
128
+ ]
129
+
130
+ # 2. Container for message content (each image + final text prompt)
131
+ user_content = []
132
+
133
+ # 3. Convert every image to a properly formatted data URI message
134
+ for path in image_paths:
135
+ data_uri = image_to_base64_data_uri(path)
136
+ user_content.append({
137
+ "type": "image_url",
138
+ "image_url": {"url": data_uri}
139
+ })
140
+
141
+ DEFAULT_PROMPTS: dict[str, str] = {
142
+ "table": "\nTable Recognition:",
143
+ "equation": "\nFormula Recognition:",
144
+ "image": "\nImage Analysis:",
145
+ "chart": "\nImage Analysis:",
146
+ "[default]": "\nText Recognition:",
147
+ "[layout]": "\nLayout Detection:",
148
+ "[cross_page_table_merge]": "", # prompt is dynamic, built from table content
149
+ }
150
+
151
+ # 4. Append the text instruction (appears after all images in the message)
152
+ user_content.append({
153
+ "type": "text",
154
+ "text": DEFAULT_PROMPTS['[default]'] # You can change the prompt as needed
155
+ })
156
+
157
+ # 5. Perform chat completion with vision
158
+ response = llm.create_chat_completion(
159
+ messages=[
160
+ {"role": "system", "content": "You are a helpful assistant."},
161
+ {"role": "user", "content": user_content}
162
+ ],
163
+ max_tokens=10240,
164
+ present_penalty=1.0,
165
+ frequency_penalty=0.05
166
+ )
167
+
168
+ # 6. Print the model's reply
169
+ print(response["choices"][0]["message"]["content"])
170
+ ```