image retagging
This commit is contained in:
1 parent
bb9832f4ce
commit
1137174398
4 files changed
+65
-19
No files matched your search
@@ -751,7 +751,7 @@ async def upload_images_blob(
|
||||
image_urls = [img.img_url for img in result_images]
|
||||
|
||||
logger.info(f"[image_tagging] START - task_id: {task_id}")
|
||||
await tag_images_if_not_exist(image_urls)
|
||||
await tagging_images(image_urls, clear_old_tags=True)
|
||||
logger.info(f"[image_tagging] Done - task_id: {task_id}")
|
||||
|
||||
total_time = time.perf_counter() - request_start
|
||||
@@ -771,8 +771,9 @@ async def upload_images_blob(
|
||||
)
|
||||
|
||||
|
||||
async def tag_images_if_not_exist(
|
||||
image_urls : list[str]
|
||||
async def tagging_images(
|
||||
image_urls : list[str],
|
||||
clear_old_tags : bool = False
|
||||
) -> None:
|
||||
# 1. 조회
|
||||
async with AsyncSessionLocal() as session:
|
||||
@@ -784,21 +785,24 @@ async def tag_images_if_not_exist(
|
||||
image_tags_query_results = await session.execute(stmt)
|
||||
image_tags = image_tags_query_results.scalars().all()
|
||||
existing_urls = {tag.img_url for tag in image_tags}
|
||||
new_tags = [
|
||||
new_imt = [
|
||||
ImageTag(img_url=url, img_tag=None)
|
||||
for url in image_urls
|
||||
if url not in existing_urls
|
||||
]
|
||||
session.add_all(new_tags)
|
||||
|
||||
null_tags = [tag for tag in image_tags if tag.img_tag is None] + new_tags
|
||||
|
||||
if null_tags:
|
||||
tag_datas = await autotag_images([img.img_url for img in null_tags])
|
||||
|
||||
print(tag_datas)
|
||||
|
||||
for tag, tag_data in zip(null_tags, tag_datas):
|
||||
tag.img_tag = tag_data.model_dump(mode="json")
|
||||
|
||||
if clear_old_tags:
|
||||
for tag in image_tags:
|
||||
tag.img_tag = None
|
||||
session.add_all(new_imt)
|
||||
null_imts = [imt for imt in image_tags if imt.img_tag is None] + new_imt
|
||||
await session.commit()
|
||||
|
||||
if null_imts:
|
||||
tag_datas = await autotag_images([img.img_url for img in null_imts])
|
||||
print(tag_datas)
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
for tag, tag_data in zip(null_imts, tag_datas):
|
||||
tag.img_tag = tag_data.model_dump(mode="json")
|
||||
session.add(tag)
|
||||
await session.commit()
|
||||
Reference in new issue
Block a user