|
| 1 | +import base64 |
| 2 | +import os |
1 | 3 | from typing import List |
2 | 4 |
|
3 | 5 | from dingo.io import Data |
|
10 | 12 | class VLMImageRelevant(BaseOpenAI): |
11 | 13 | prompt = PromptImageRelevant |
12 | 14 |
|
| 15 | + @classmethod |
| 16 | + def _encode_image(cls, image_path: str) -> str: |
| 17 | + """ |
| 18 | + Encode a local image file to base64 data URL format. |
| 19 | + If the input is already a URL, return it as is. |
| 20 | +
|
| 21 | + This method follows Python's standard path resolution: |
| 22 | + - Relative paths are resolved relative to the current working directory |
| 23 | + - Absolute paths are used as-is |
| 24 | + - URLs (http://, https://, data:) are passed through unchanged |
| 25 | +
|
| 26 | + Args: |
| 27 | + image_path: Local file path (absolute or relative) or URL |
| 28 | +
|
| 29 | + Returns: |
| 30 | + Base64 data URL for local files, or original URL for web resources |
| 31 | +
|
| 32 | + Raises: |
| 33 | + FileNotFoundError: If a local file path does not exist |
| 34 | + RuntimeError: If the file cannot be read |
| 35 | + """ |
| 36 | + # Pass through URLs unchanged |
| 37 | + if image_path.startswith(('http://', 'https://', 'data:')): |
| 38 | + return image_path |
| 39 | + |
| 40 | + # Standard file path handling (relative or absolute) |
| 41 | + if not os.path.isfile(image_path): |
| 42 | + raise FileNotFoundError( |
| 43 | + f"Image file not found: '{image_path}'\n" |
| 44 | + f"Current working directory: {os.getcwd()}\n" |
| 45 | + f"Absolute path would be: {os.path.abspath(image_path)}\n" |
| 46 | + f"Ensure the path is correct relative to your current working directory." |
| 47 | + ) |
| 48 | + |
| 49 | + try: |
| 50 | + with open(image_path, "rb") as image_file: |
| 51 | + base64_image = base64.b64encode(image_file.read()).decode('utf-8') |
| 52 | + # Determine MIME type from file extension |
| 53 | + ext = os.path.splitext(image_path)[1].lower() |
| 54 | + mime_type = 'image/jpeg' if ext in ['.jpg', '.jpeg'] else f'image/{ext[1:]}' |
| 55 | + return f"data:{mime_type};base64,{base64_image}" |
| 56 | + except Exception as e: |
| 57 | + raise RuntimeError( |
| 58 | + f"Failed to read image file '{image_path}': {e}" |
| 59 | + ) |
| 60 | + |
13 | 61 | @classmethod |
14 | 62 | def build_messages(cls, input_data: Data) -> List: |
| 63 | + # Encode images if they are local file paths |
| 64 | + image_url_1 = cls._encode_image(input_data.prompt) |
| 65 | + image_url_2 = cls._encode_image(input_data.content) |
| 66 | + |
15 | 67 | messages = [ |
16 | 68 | { |
17 | 69 | "role": "user", |
18 | 70 | "content": [ |
19 | 71 | {"type": "text", "text": cls.prompt.content}, |
20 | | - {"type": "image_url", "image_url": {"url": input_data.prompt}}, |
21 | | - {"type": "image_url", "image_url": {"url": input_data.content}}, |
| 72 | + {"type": "image_url", "image_url": {"url": image_url_1}}, |
| 73 | + {"type": "image_url", "image_url": {"url": image_url_2}}, |
22 | 74 | ], |
23 | 75 | } |
24 | 76 | ] |
|
0 commit comments