Refactor logging and improve image optimization in RecipeRoutes and ExifUtils

- Removed print statements for initialization and setup in RecipeRoutes to reduce console clutter and improve logging practices.
- Updated image optimization parameters in RecipeRoutes to enhance image quality by increasing the target width.
- Modified user comment handling in ExifUtils to ensure proper formatting when appending recipe metadata, improving metadata consistency.
This commit is contained in:
Will Miao
2025-03-19 14:50:36 +08:00
parent c3aaef3916
commit f9c54690b0
2 changed files with 6 additions and 16 deletions

View File

@@ -17,13 +17,11 @@ from ..config import config
import time # Add this import at the top
logger = logging.getLogger(__name__)
print("Recipe Routes module loaded", file=sys.stderr)
class RecipeRoutes:
"""API route handlers for Recipe management"""
def __init__(self):
print("Initializing RecipeRoutes", file=sys.stderr)
self.recipe_scanner = RecipeScanner(LoraScanner())
self.civitai_client = CivitaiClient()
@@ -33,7 +31,6 @@ class RecipeRoutes:
@classmethod
def setup_routes(cls, app: web.Application):
"""Register API routes"""
print("Setting up recipe routes", file=sys.stderr)
routes = cls()
app.router.add_get('/api/recipes', routes.get_recipes)
app.router.add_get('/api/recipe/{recipe_id}', routes.get_recipe_detail)
@@ -51,38 +48,28 @@ class RecipeRoutes:
# Start cache initialization
app.on_startup.append(routes._init_cache)
print("Recipe routes setup complete", file=sys.stderr)
async def _init_cache(self, app):
"""Initialize cache on startup"""
print("Pre-warming recipe cache...", file=sys.stderr)
try:
# First, ensure the lora scanner is fully initialized
print("Initializing lora scanner...", file=sys.stderr)
lora_scanner = self.recipe_scanner._lora_scanner
# Get lora cache to ensure it's initialized
lora_cache = await lora_scanner.get_cached_data()
print(f"Lora scanner initialized with {len(lora_cache.raw_data)} loras", file=sys.stderr)
# Verify hash index is built
if hasattr(lora_scanner, '_hash_index'):
hash_index_size = len(lora_scanner._hash_index._hash_to_path) if hasattr(lora_scanner._hash_index, '_hash_to_path') else 0
print(f"Lora hash index contains {hash_index_size} entries", file=sys.stderr)
# Now that lora scanner is initialized, initialize recipe cache
print("Initializing recipe cache...", file=sys.stderr)
await self.recipe_scanner.get_cached_data(force_refresh=True)
print("Recipe cache pre-warming complete", file=sys.stderr)
except Exception as e:
print(f"Error pre-warming recipe cache: {e}", file=sys.stderr)
logger.error(f"Error pre-warming recipe cache: {e}", exc_info=True)
async def get_recipes(self, request: web.Request) -> web.Response:
"""API endpoint for getting paginated recipes"""
try:
print("API: GET /api/recipes", file=sys.stderr)
# Get query parameters with defaults
page = int(request.query.get('page', '1'))
page_size = int(request.query.get('page_size', '20'))
@@ -128,7 +115,6 @@ class RecipeRoutes:
return web.json_response(result)
except Exception as e:
logger.error(f"Error retrieving recipes: {e}", exc_info=True)
print(f"API Error: {e}", file=sys.stderr)
return web.json_response({"error": str(e)}, status=500)
async def get_recipe_detail(self, request: web.Request) -> web.Response:
@@ -412,7 +398,7 @@ class RecipeRoutes:
# Optimize the image (resize and convert to WebP)
optimized_image, extension = ExifUtils.optimize_image(
image_data=image,
target_width=250,
target_width=480,
format='webp',
quality=85,
preserve_metadata=True

View File

@@ -135,7 +135,7 @@ class ExifUtils:
recipe_metadata_marker = f"Recipe metadata: {recipe_metadata_json}"
# Append to existing user comment or create new one
new_user_comment = user_comment + "\n" + recipe_metadata_marker if user_comment else recipe_metadata_marker
new_user_comment = f"{user_comment}, {recipe_metadata_marker}" if user_comment else recipe_metadata_marker
# Write back to the image
return ExifUtils.update_user_comment(image_path, new_user_comment)
@@ -154,6 +154,10 @@ class ExifUtils:
if recipe_marker_index == -1:
return user_comment
# If recipe metadata is not at the start, remove the preceding ", "
if recipe_marker_index >= 2 and user_comment[recipe_marker_index-2:recipe_marker_index] == ", ":
recipe_marker_index -= 2
# Remove the recipe metadata part
# First, find where the metadata ends (next line or end of string)
next_line_index = user_comment.find("\n", recipe_marker_index)