Spaces:
Running
Running
File size: 7,499 Bytes
ed37502 | 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 | """Image catalog service — stores and queries image metadata."""
from __future__ import annotations
import hashlib
import json
import logging
import uuid
from datetime import datetime
from pathlib import Path
from typing import Any
from sqlalchemy import select, func
from sqlalchemy.ext.asyncio import AsyncSession
from content_engine.config import settings
from content_engine.models.database import Image, catalog_session_factory
logger = logging.getLogger(__name__)
class CatalogService:
"""Manages the image catalog: inserting, querying, and organizing generated images."""
async def insert_image(
self,
*,
file_path: str,
image_bytes: bytes | None = None,
character_id: str | None = None,
template_id: str | None = None,
content_rating: str = "sfw",
batch_id: str | None = None,
positive_prompt: str | None = None,
negative_prompt: str | None = None,
checkpoint: str | None = None,
loras: list[dict[str, Any]] | None = None,
seed: int | None = None,
steps: int | None = None,
cfg: float | None = None,
sampler: str | None = None,
scheduler: str | None = None,
width: int | None = None,
height: int | None = None,
generation_backend: str | None = None,
comfyui_prompt_id: str | None = None,
generation_time_seconds: float | None = None,
variables: dict[str, str] | None = None,
) -> str:
"""Insert a new image record into the catalog. Returns the image ID."""
image_id = str(uuid.uuid4())
variables = variables or {}
# Compute file hash if bytes provided
file_hash = None
file_size = None
if image_bytes:
file_hash = hashlib.sha256(image_bytes).hexdigest()
file_size = len(image_bytes)
record = Image(
id=image_id,
batch_id=batch_id,
character_id=character_id,
template_id=template_id,
content_rating=content_rating,
positive_prompt=positive_prompt,
negative_prompt=negative_prompt,
checkpoint=checkpoint,
loras_json=json.dumps(loras) if loras else None,
seed=seed,
steps=steps,
cfg=cfg,
sampler=sampler,
scheduler=scheduler,
width=width,
height=height,
pose=variables.get("pose"),
outfit=variables.get("outfit"),
emotion=variables.get("emotion"),
camera_angle=variables.get("camera_angle"),
lighting=variables.get("lighting"),
scene=variables.get("scene"),
file_path=file_path,
file_hash=file_hash,
file_size=file_size,
generation_backend=generation_backend,
comfyui_prompt_id=comfyui_prompt_id,
generation_time_seconds=generation_time_seconds,
)
async with catalog_session_factory() as session:
session.add(record)
await session.commit()
logger.info("Cataloged image %s at %s", image_id, file_path)
return image_id
async def get_image(self, image_id: str) -> Image | None:
"""Get a single image by ID."""
async with catalog_session_factory() as session:
return await session.get(Image, image_id)
async def search(
self,
*,
character_id: str | None = None,
content_rating: str | None = None,
template_id: str | None = None,
is_approved: bool | None = None,
is_published: bool | None = None,
pose: str | None = None,
outfit: str | None = None,
emotion: str | None = None,
limit: int = 50,
offset: int = 0,
) -> list[Image]:
"""Search images with filters."""
stmt = select(Image)
if character_id is not None:
stmt = stmt.where(Image.character_id == character_id)
if content_rating is not None:
stmt = stmt.where(Image.content_rating == content_rating)
if template_id is not None:
stmt = stmt.where(Image.template_id == template_id)
if is_approved is not None:
stmt = stmt.where(Image.is_approved == is_approved)
if is_published is not None:
stmt = stmt.where(Image.is_published == is_published)
if pose is not None:
stmt = stmt.where(Image.pose == pose)
if outfit is not None:
stmt = stmt.where(Image.outfit == outfit)
if emotion is not None:
stmt = stmt.where(Image.emotion == emotion)
stmt = stmt.order_by(Image.created_at.desc()).limit(limit).offset(offset)
async with catalog_session_factory() as session:
result = await session.execute(stmt)
return list(result.scalars().all())
async def approve_image(self, image_id: str) -> bool:
"""Mark an image as approved for publishing."""
async with catalog_session_factory() as session:
image = await session.get(Image, image_id)
if not image:
return False
image.is_approved = True
await session.commit()
return True
async def delete_image(self, image_id: str) -> bool:
"""Delete an image record and its file from disk."""
async with catalog_session_factory() as session:
image = await session.get(Image, image_id)
if not image:
return False
file_path = Path(image.file_path)
if file_path.exists():
file_path.unlink()
await session.delete(image)
await session.commit()
return True
async def get_approved_unpublished(
self, character_id: str, content_rating: str | None = None, limit: int = 100
) -> list[Image]:
"""Get approved but unpublished images for a character."""
stmt = (
select(Image)
.where(Image.character_id == character_id)
.where(Image.is_approved == True) # noqa: E712
.where(Image.is_published == False) # noqa: E712
)
if content_rating:
stmt = stmt.where(Image.content_rating == content_rating)
stmt = stmt.order_by(Image.created_at.asc()).limit(limit)
async with catalog_session_factory() as session:
result = await session.execute(stmt)
return list(result.scalars().all())
async def get_total_count(self) -> int:
"""Get total number of images in catalog."""
async with catalog_session_factory() as session:
result = await session.execute(select(func.count(Image.id)))
return result.scalar() or 0
def resolve_output_path(
self,
character_id: str,
content_rating: str,
filename: str,
subfolder: str = "raw",
) -> Path:
"""Resolve the output file path for a generated image.
Structure: output/{character_id}/{rating}/{subfolder}/{year-month}/{filename}
"""
now = datetime.now()
date_folder = now.strftime("%Y-%m")
path = (
settings.paths.output_dir
/ character_id
/ content_rating
/ subfolder
/ date_folder
)
path.mkdir(parents=True, exist_ok=True)
return path / filename
|