mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-25 07:05:43 -03:00
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:
@@ -17,13 +17,11 @@ from ..config import config
|
|||||||
import time # Add this import at the top
|
import time # Add this import at the top
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
print("Recipe Routes module loaded", file=sys.stderr)
|
|
||||||
|
|
||||||
class RecipeRoutes:
|
class RecipeRoutes:
|
||||||
"""API route handlers for Recipe management"""
|
"""API route handlers for Recipe management"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
print("Initializing RecipeRoutes", file=sys.stderr)
|
|
||||||
self.recipe_scanner = RecipeScanner(LoraScanner())
|
self.recipe_scanner = RecipeScanner(LoraScanner())
|
||||||
self.civitai_client = CivitaiClient()
|
self.civitai_client = CivitaiClient()
|
||||||
|
|
||||||
@@ -33,7 +31,6 @@ class RecipeRoutes:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def setup_routes(cls, app: web.Application):
|
def setup_routes(cls, app: web.Application):
|
||||||
"""Register API routes"""
|
"""Register API routes"""
|
||||||
print("Setting up recipe routes", file=sys.stderr)
|
|
||||||
routes = cls()
|
routes = cls()
|
||||||
app.router.add_get('/api/recipes', routes.get_recipes)
|
app.router.add_get('/api/recipes', routes.get_recipes)
|
||||||
app.router.add_get('/api/recipe/{recipe_id}', routes.get_recipe_detail)
|
app.router.add_get('/api/recipe/{recipe_id}', routes.get_recipe_detail)
|
||||||
@@ -51,38 +48,28 @@ class RecipeRoutes:
|
|||||||
|
|
||||||
# Start cache initialization
|
# Start cache initialization
|
||||||
app.on_startup.append(routes._init_cache)
|
app.on_startup.append(routes._init_cache)
|
||||||
|
|
||||||
print("Recipe routes setup complete", file=sys.stderr)
|
|
||||||
|
|
||||||
async def _init_cache(self, app):
|
async def _init_cache(self, app):
|
||||||
"""Initialize cache on startup"""
|
"""Initialize cache on startup"""
|
||||||
print("Pre-warming recipe cache...", file=sys.stderr)
|
|
||||||
try:
|
try:
|
||||||
# First, ensure the lora scanner is fully initialized
|
# First, ensure the lora scanner is fully initialized
|
||||||
print("Initializing lora scanner...", file=sys.stderr)
|
|
||||||
lora_scanner = self.recipe_scanner._lora_scanner
|
lora_scanner = self.recipe_scanner._lora_scanner
|
||||||
|
|
||||||
# Get lora cache to ensure it's initialized
|
# Get lora cache to ensure it's initialized
|
||||||
lora_cache = await lora_scanner.get_cached_data()
|
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
|
# Verify hash index is built
|
||||||
if hasattr(lora_scanner, '_hash_index'):
|
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
|
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
|
# 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)
|
await self.recipe_scanner.get_cached_data(force_refresh=True)
|
||||||
print("Recipe cache pre-warming complete", file=sys.stderr)
|
|
||||||
except Exception as e:
|
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)
|
logger.error(f"Error pre-warming recipe cache: {e}", exc_info=True)
|
||||||
|
|
||||||
async def get_recipes(self, request: web.Request) -> web.Response:
|
async def get_recipes(self, request: web.Request) -> web.Response:
|
||||||
"""API endpoint for getting paginated recipes"""
|
"""API endpoint for getting paginated recipes"""
|
||||||
try:
|
try:
|
||||||
print("API: GET /api/recipes", file=sys.stderr)
|
|
||||||
# Get query parameters with defaults
|
# Get query parameters with defaults
|
||||||
page = int(request.query.get('page', '1'))
|
page = int(request.query.get('page', '1'))
|
||||||
page_size = int(request.query.get('page_size', '20'))
|
page_size = int(request.query.get('page_size', '20'))
|
||||||
@@ -128,7 +115,6 @@ class RecipeRoutes:
|
|||||||
return web.json_response(result)
|
return web.json_response(result)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error retrieving recipes: {e}", exc_info=True)
|
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)
|
return web.json_response({"error": str(e)}, status=500)
|
||||||
|
|
||||||
async def get_recipe_detail(self, request: web.Request) -> web.Response:
|
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)
|
# Optimize the image (resize and convert to WebP)
|
||||||
optimized_image, extension = ExifUtils.optimize_image(
|
optimized_image, extension = ExifUtils.optimize_image(
|
||||||
image_data=image,
|
image_data=image,
|
||||||
target_width=250,
|
target_width=480,
|
||||||
format='webp',
|
format='webp',
|
||||||
quality=85,
|
quality=85,
|
||||||
preserve_metadata=True
|
preserve_metadata=True
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ class ExifUtils:
|
|||||||
recipe_metadata_marker = f"Recipe metadata: {recipe_metadata_json}"
|
recipe_metadata_marker = f"Recipe metadata: {recipe_metadata_json}"
|
||||||
|
|
||||||
# Append to existing user comment or create new one
|
# 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
|
# Write back to the image
|
||||||
return ExifUtils.update_user_comment(image_path, new_user_comment)
|
return ExifUtils.update_user_comment(image_path, new_user_comment)
|
||||||
@@ -154,6 +154,10 @@ class ExifUtils:
|
|||||||
if recipe_marker_index == -1:
|
if recipe_marker_index == -1:
|
||||||
return user_comment
|
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
|
# Remove the recipe metadata part
|
||||||
# First, find where the metadata ends (next line or end of string)
|
# First, find where the metadata ends (next line or end of string)
|
||||||
next_line_index = user_comment.find("\n", recipe_marker_index)
|
next_line_index = user_comment.find("\n", recipe_marker_index)
|
||||||
|
|||||||
Reference in New Issue
Block a user