File size: 8,826 Bytes
c4ef1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9513cca
c4ef1cf
9513cca
c4ef1cf
9513cca
 
 
c4ef1cf
 
 
 
 
 
 
 
 
 
 
 
9513cca
c4ef1cf
9513cca
c4ef1cf
 
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
 
 
 
9513cca
c4ef1cf
 
 
9513cca
c4ef1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
9513cca
c4ef1cf
 
 
 
 
 
 
 
9513cca
 
c4ef1cf
9513cca
c4ef1cf
 
 
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
9513cca
c4ef1cf
 
 
 
 
9513cca
c4ef1cf
 
 
9513cca
c4ef1cf
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
9513cca
c4ef1cf
 
 
 
 
 
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
 
9513cca
 
c4ef1cf
9513cca
c4ef1cf
9513cca
 
c4ef1cf
 
9513cca
c4ef1cf
 
 
9513cca
c4ef1cf
 
9513cca
c4ef1cf
 
9513cca
c4ef1cf
 
 
 
 
9513cca
c4ef1cf
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
 
9513cca
 
c4ef1cf
9513cca
c4ef1cf
9513cca
 
c4ef1cf
 
9513cca
c4ef1cf
 
 
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
9513cca
c4ef1cf
 
 
 
 
 
 
 
9513cca
c4ef1cf
 
 
 
 
9513cca
c4ef1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
Cloudinary Uploader - Upload images to Cloudinary CDN.

Works INDEPENDENTLY of PDF processing and embedding.
Use it if you just need to upload images to a CDN.

Features:
- Retry logic with timeouts
- Batch uploading
- Automatic JPEG optimization

Environment Variables:
- VISUAL_RAG_THREAD_SAFE: Set to "1" to use thread-safe timeouts
  (required for Streamlit, Flask, or other threaded contexts)
"""

import io
import logging
import os
import platform
import signal
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import TimeoutError as FuturesTimeoutError
from typing import Optional

from PIL import Image

logger = logging.getLogger(__name__)

THREAD_SAFE_MODE = os.getenv("VISUAL_RAG_THREAD_SAFE", "").lower() in ("1", "true", "yes")


class CloudinaryUploader:
    """
    Upload images to Cloudinary CDN.

    Works independently - just needs PIL images.

    Args:
        cloud_name: Cloudinary cloud name
        api_key: Cloudinary API key
        api_secret: Cloudinary API secret
        folder: Base folder for uploads
        max_retries: Number of retry attempts
        timeout_seconds: Timeout per upload

    Example:
        >>> uploader = CloudinaryUploader(
        ...     cloud_name="my-cloud",
        ...     api_key="xxx",
        ...     api_secret="yyy",
        ...     folder="my-project",
        ... )
        >>>
        >>> url = uploader.upload(image, "doc_page_1")
        >>> print(url)  # https://res.cloudinary.com/.../doc_page_1.jpg
    """

    def __init__(
        self,
        cloud_name: Optional[str] = None,
        api_key: Optional[str] = None,
        api_secret: Optional[str] = None,
        folder: str = "visual-rag",
        max_retries: int = 3,
        timeout_seconds: int = 30,
        jpeg_quality: int = 95,
    ):
        # Load from environment if not provided
        self.cloud_name = cloud_name or os.getenv("CLOUDINARY_CLOUD_NAME")
        self.api_key = api_key or os.getenv("CLOUDINARY_API_KEY")
        self.api_secret = api_secret or os.getenv("CLOUDINARY_API_SECRET")

        if not all([self.cloud_name, self.api_key, self.api_secret]):
            raise ValueError(
                "Cloudinary credentials required. Set CLOUDINARY_CLOUD_NAME, "
                "CLOUDINARY_API_KEY, CLOUDINARY_API_SECRET environment variables "
                "or pass them as arguments."
            )

        self.folder = folder
        self.max_retries = max_retries
        self.timeout_seconds = timeout_seconds
        self.jpeg_quality = jpeg_quality

        # Check dependency
        try:
            import cloudinary  # noqa
        except ImportError:
            raise ImportError(
                "Cloudinary not installed. "
                "Install with: pip install visual-rag-toolkit[cloudinary]"
            )

        logger.info("☁️ Cloudinary uploader initialized")
        logger.info(f"   Folder: {folder}")

    def upload(
        self,
        image: Image.Image,
        public_id: str,
        subfolder: Optional[str] = None,
    ) -> Optional[str]:
        """
        Upload a single image to Cloudinary.

        Args:
            image: PIL Image to upload
            public_id: Public ID (filename without extension)
            subfolder: Optional subfolder within base folder

        Returns:
            Secure URL of uploaded image, or None if failed
        """
        import cloudinary
        import cloudinary.uploader

        # Prepare buffer
        buffer = io.BytesIO()
        image.save(buffer, format="JPEG", quality=self.jpeg_quality, optimize=True)

        # Configure Cloudinary
        cloudinary.config(
            cloud_name=self.cloud_name,
            api_key=self.api_key,
            api_secret=self.api_secret,
        )

        # Build folder path
        folder_path = self.folder
        if subfolder:
            folder_path = f"{self.folder}/{subfolder}"

        def do_upload():
            buffer.seek(0)
            result = cloudinary.uploader.upload(
                buffer,
                folder=folder_path,
                overwrite=True,
                public_id=public_id,
                resource_type="image",
                timeout=self.timeout_seconds,
            )
            return result["secure_url"]

        # Use thread-safe mode for Streamlit/Flask/threaded contexts
        # Set VISUAL_RAG_THREAD_SAFE=1 to enable
        if THREAD_SAFE_MODE or threading.current_thread() is not threading.main_thread():
            return self._upload_with_thread_timeout(do_upload, public_id)
        else:
            return self._upload_with_signal_timeout(do_upload, public_id)

    def _upload_with_thread_timeout(self, do_upload, public_id: str) -> Optional[str]:
        """Thread-safe upload with ThreadPoolExecutor timeout."""
        for attempt in range(self.max_retries):
            try:
                with ThreadPoolExecutor(max_workers=1) as executor:
                    future = executor.submit(do_upload)
                    return future.result(timeout=self.timeout_seconds)

            except FuturesTimeoutError:
                logger.warning(
                    f"Upload timeout (attempt {attempt + 1}/{self.max_retries}): {public_id}"
                )
                if attempt < self.max_retries - 1:
                    time.sleep(2**attempt)

            except Exception as e:
                logger.warning(f"Upload failed (attempt {attempt + 1}/{self.max_retries}): {e}")
                if attempt < self.max_retries - 1:
                    time.sleep(2**attempt)

        logger.error(f"❌ Upload failed after {self.max_retries} attempts: {public_id}")
        return None

    def _upload_with_signal_timeout(self, do_upload, public_id: str) -> Optional[str]:
        """Signal-based upload timeout (main thread only, Unix/macOS)."""
        use_timeout = platform.system() != "Windows"

        class SignalTimeoutError(Exception):
            pass

        def timeout_handler(signum, frame):
            raise SignalTimeoutError(f"Upload timed out after {self.timeout_seconds}s")

        for attempt in range(self.max_retries):
            try:
                if use_timeout:
                    old_handler = signal.signal(signal.SIGALRM, timeout_handler)
                    signal.alarm(self.timeout_seconds)

                try:
                    return do_upload()
                finally:
                    if use_timeout:
                        signal.alarm(0)
                        signal.signal(signal.SIGALRM, old_handler)

            except SignalTimeoutError:
                logger.warning(
                    f"Upload timeout (attempt {attempt + 1}/{self.max_retries}): {public_id}"
                )
                if attempt < self.max_retries - 1:
                    time.sleep(2**attempt)

            except Exception as e:
                logger.warning(f"Upload failed (attempt {attempt + 1}/{self.max_retries}): {e}")
                if attempt < self.max_retries - 1:
                    time.sleep(2**attempt)

        logger.error(f"❌ Upload failed after {self.max_retries} attempts: {public_id}")
        return None

    def upload_original_and_resized(
        self,
        original_image: Image.Image,
        resized_image: Image.Image,
        base_public_id: str,
    ) -> tuple:
        """
        Upload both original and resized versions.

        Args:
            original_image: Original PDF page image
            resized_image: Resized image for ColPali
            base_public_id: Base public ID (e.g., "doc_page_1")

        Returns:
            Tuple of (original_url, resized_url) - either can be None on failure
        """
        original_url = self.upload(
            original_image,
            base_public_id,
            subfolder="original",
        )

        resized_url = self.upload(
            resized_image,
            base_public_id,
            subfolder="resized",
        )

        return original_url, resized_url

    def upload_original_cropped_and_resized(
        self,
        original_image: Image.Image,
        cropped_image: Optional[Image.Image],
        resized_image: Image.Image,
        base_public_id: str,
    ) -> tuple:
        original_url = self.upload(
            original_image,
            base_public_id,
            subfolder="original",
        )

        cropped_url = None
        if cropped_image is not None:
            cropped_url = self.upload(
                cropped_image,
                base_public_id,
                subfolder="cropped",
            )

        resized_url = self.upload(
            resized_image,
            base_public_id,
            subfolder="resized",
        )

        return original_url, cropped_url, resized_url