Replace print() with logger calls in llm_utils, random_image_for_visual_search, and rewards_tasks

This commit is contained in:
abhijitgorde5-crypto
2026-09-08 23:02:34 +05:30
parent 049a119e36
commit 03636ac984
4 changed files with 41 additions and 80 deletions
+34 -72
View File
@@ -1,5 +1,6 @@
import io
import json
import logging
import time
from pathlib import Path
from urllib.parse import urlsplit, urlunsplit
@@ -7,6 +8,8 @@ from urllib.parse import urlsplit, urlunsplit
import requests
from PIL import Image
logger = logging.getLogger(__name__)
# ============================================================
# CONFIG
@@ -93,10 +96,7 @@ def wait_after_429(response, attempt):
wait_time = max(5, wait_time)
print(
f"Rate limited. Waiting "
f"{wait_time} seconds..."
)
logger.warning("Rate limited. Waiting %d seconds...", wait_time)
time.sleep(wait_time)
@@ -118,7 +118,7 @@ def download_image(url):
)
except (requests.RequestException, ConnectionResetError, OSError) as e:
print(f"Download failed: {e}")
logger.warning("Download failed: %s", e)
return None
if response.status_code == 429:
@@ -126,13 +126,13 @@ def download_image(url):
return None
if response.status_code == 403:
print("Wikimedia returned 403 Forbidden.")
logger.warning("Wikimedia returned 403 Forbidden.")
return None
try:
response.raise_for_status()
except requests.RequestException as e:
print(f"HTTP error: {e}")
logger.warning("HTTP error: %s", e)
return None
content_type = response.headers.get(
@@ -141,13 +141,11 @@ def download_image(url):
).lower()
if not content_type.startswith("image/"):
print(
f"Not an image: {content_type}"
)
logger.warning("Not an image: %s", content_type)
return None
if not response.content:
print("Downloaded image is empty.")
logger.warning("Downloaded image is empty.")
return None
return response.content
@@ -188,15 +186,13 @@ def convert_to_jpeg(image_data):
return output.getvalue()
except Exception as e:
print(
f"JPEG conversion failed: {e}"
)
logger.warning("JPEG conversion failed: %s", e)
return None
def generate_fallback_image():
"""Generate a synthetic local JPEG image using PIL as a fallback."""
print("[INFO] Generating synthetic local fallback image for visual search...")
logger.info("Generating synthetic local fallback image for visual search...")
from PIL import ImageDraw
import random
@@ -215,7 +211,7 @@ def generate_fallback_image():
jpeg_data = output.getvalue()
OUTPUT_FILE.write_bytes(jpeg_data)
print(f"Saved fallback image to {OUTPUT_FILE.absolute()}")
logger.info("Saved fallback image to %s", OUTPUT_FILE.absolute())
return {"title": "Fallback Synthetic Image", "width": 800, "height": 600}
@@ -233,10 +229,7 @@ def get_random_image():
if attempt > 1:
time.sleep(REQUEST_DELAY)
print(
f"\nAttempt "
f"{attempt}/{MAX_ATTEMPTS}"
)
logger.debug("Attempt %d/%d", attempt, MAX_ATTEMPTS)
params = {
"action": "query",
@@ -263,7 +256,7 @@ def get_random_image():
)
except (requests.RequestException, ConnectionResetError, OSError) as e:
print(f"API request failed: {e}")
logger.warning("API request failed: %s", e)
continue
if response.status_code == 429:
@@ -281,7 +274,7 @@ def get_random_image():
requests.RequestException,
ValueError,
) as e:
print(f"API error: {e}")
logger.warning("API error: %s", e)
continue
pages = (
@@ -291,7 +284,7 @@ def get_random_image():
)
if not pages:
print("No page returned.")
logger.debug("No page returned.")
continue
page = next(
@@ -308,9 +301,7 @@ def get_random_image():
)
if not imageinfo:
print(
"No image information."
)
logger.debug("No image information.")
continue
info = imageinfo[0]
@@ -348,55 +339,39 @@ def get_random_image():
"image/png",
"image/webp",
}:
print(
f"Skipping unsupported type: "
f"{mime}"
)
logger.debug("Skipping unsupported type: %s", mime)
continue
if width < MIN_WIDTH or height < MIN_HEIGHT:
print(
f"Skipping small image: "
f"{width}x{height}"
)
logger.debug("Skipping small image: %dx%d", width, height)
continue
if size > MAX_FILE_SIZE:
print(
f"Skipping large image: "
f"{size / 1024 / 1024:.1f} MB"
)
logger.debug("Skipping large image: %.1f MB", size / 1024 / 1024)
continue
if not thumbnail_url:
print("No thumbnail URL.")
logger.debug("No thumbnail URL.")
continue
print(f"Found: {title}")
print(
f"Size: {width}x{height}"
)
logger.debug("Found: %s (%dx%d)", title, width, height)
image_data = download_image(
thumbnail_url
)
if image_data is None and original_url:
print(
"Trying original..."
)
logger.debug("Thumbnail download failed, trying original URL...")
image_data = download_image(
original_url
)
if image_data is None:
print(
"Couldn't download image."
)
logger.debug("Couldn't download image.")
continue
print("Converting to JPEG...")
logger.debug("Converting to JPEG...")
jpeg_data = convert_to_jpeg(
image_data
@@ -411,9 +386,7 @@ def get_random_image():
)
except OSError as e:
print(
f"Couldn't save image: {e}"
)
logger.warning("Couldn't save image: %s", e)
continue
metadata = {
@@ -456,30 +429,19 @@ def get_random_image():
)
except OSError as e:
print(
f"Warning: couldn't save "
f"metadata: {e}"
)
logger.warning("Couldn't save metadata: %s", e)
print()
print("=" * 50)
print("SUCCESS")
print("=" * 50)
print(
f"Image: "
f"{OUTPUT_FILE.absolute()}"
)
print(
f"Size: "
f"{len(jpeg_data) / 1024:.1f} KB"
)
print(
f"Source: {title}"
logger.info(
"Visual search image saved: %s (%s, %.1f KB, source: %s)",
OUTPUT_FILE.absolute(),
f"{width}x{height}",
len(jpeg_data) / 1024,
title,
)
return metadata
print("[WARNING] Could not download image from Wikimedia Commons. Generating local fallback image.")
logger.warning("Could not download image from Wikimedia Commons. Generating local fallback image.")
return generate_fallback_image()