-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvision_util.py
More file actions
53 lines (45 loc) · 1.98 KB
/
vision_util.py
File metadata and controls
53 lines (45 loc) · 1.98 KB
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
# File: vision_util.py
import logging
import time
from pathlib import Path
from ollama_client import OllamaClient
class VisionUtil:
def __init__(self, model_name: str, base_url: str):
self.ollama_client = OllamaClient(model=model_name, url=base_url)
self.logger = logging.getLogger('vision_util')
def describe_image(self, image_path: str, prompt: str) -> str:
"""
Describe an image using the Ollama API with retry logic.
Args:
image_path (str): The file path to the image.
prompt (str): The prompt to guide the description.
Returns:
str: The description of the image or a default message if failed.
"""
retries = 3
delays = [2, 4, 8] # Delays in seconds for each retry
for attempt in range(1, retries + 1):
try:
self.logger.debug(
f"Attempt {attempt}: Describing image at path '{image_path}' with prompt:\n{prompt}"
)
response = self.ollama_client.generate_with_image(
prompt=prompt, image_path=image_path
)
description = response.strip()
self.logger.debug(f"Received image description:\n{description}")
return description
except Exception as e:
self.logger.error(
f"Ollama API request failed on attempt {attempt} for image '{image_path}': {e}"
)
if attempt < retries:
delay = delays[attempt - 1]
self.logger.info(f"Retrying in {delay} seconds...")
time.sleep(delay)
else:
self.logger.error(
f"All {retries} attempts failed for image '{image_path}'. Skipping this image."
)
# Return a default description or handle as needed
return "Description unavailable due to an error."