Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NSFW tag support #773

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Promise.resolve().then(async () => {
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Infinite Image Browsing</title>
<script type="module" crossorigin src="/infinite_image_browsing/fe-static/assets/index-870cca06.js"></script>
<link rel="stylesheet" href="/infinite_image_browsing/fe-static/assets/index-c05ac913.css">
<script type="module" crossorigin src="/infinite_image_browsing/fe-static/assets/index-ac57907e.js"></script>
<link rel="stylesheet" href="/infinite_image_browsing/fe-static/assets/index-b0193fc9.css">
</head>

<body>
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Pillow
pillow-avif-plugin
imageio
av
lxml
lxml
ifnude
7 changes: 5 additions & 2 deletions scripts/iib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,10 @@ async def delete_extra_path(extra_path: ExtraPathModel):
f"{db_api_base}/rebuild_index",
dependencies=[Depends(verify_secret), Depends(write_permission_required)],
)
async def rebuild_index():
async def rebuild_index(request: Request):
payload = await request.json()
update_extra_paths(conn = DataBase.get_conn())
rebuild_image_index(search_dirs = get_img_search_dirs() + mem["extra_paths"])

rebuild_image_index(search_dirs = get_img_search_dirs() + mem["extra_paths"], paylaod=payload)
return {"detail": "Index rebuilt successfully"}

6 changes: 6 additions & 0 deletions scripts/iib/db/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ def create_table(cls, conn):
except sqlite3.OperationalError as e:
pass

cur.execute(
"""INSERT OR IGNORE INTO tag(name, score, type, count, color)
VALUES ("nsfw", 0, "custom", 0, "rgb(216, 27, 67)");
"""
)


class ImageTag:
def __init__(self, image_id: int, tag_id: int):
Expand Down
21 changes: 15 additions & 6 deletions scripts/iib/db/update_image_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from scripts.iib.logger import logger
from scripts.iib.parsers.index import parse_image_info
from scripts.iib.plugin import plugin_inst_map
from ifnude import detect

# 定义一个函数来获取图片文件的EXIF数据
def get_exif_data(file_path):
Expand All @@ -28,7 +29,7 @@ def get_exif_data(file_path):
return ImageGenerationInfo()


def update_image_data(search_dirs: List[str], is_rebuild = False):
def update_image_data(search_dirs: List[str], is_rebuild = False, detect_nsfw = False):
conn = DataBase.get_conn()
tag_incr_count_rec: Dict[int, int] = {}

Expand All @@ -53,7 +54,7 @@ def process_folder(folder_path: str):
if os.path.isdir(file_path):
process_folder(file_path)
elif is_valid_media_path(file_path):
build_single_img_idx(conn, file_path, is_rebuild, safe_save_img_tag)
build_single_img_idx(conn, file_path, is_rebuild, safe_save_img_tag, detect_nsfw)
# neg暂时跳过感觉个没人会搜索这个
except Exception as e:
logger.error("Tag generation failed. Skipping this file. file:%s error: %s", file_path, e)
Expand Down Expand Up @@ -96,7 +97,7 @@ def safe_save_img_tag(img_tag: ImageTag):
tag.save(conn)
conn.commit()

def rebuild_image_index(search_dirs: List[str]):
def rebuild_image_index(search_dirs: List[str], paylaod):
conn = DataBase.get_conn()
with closing(conn.cursor()) as cur:
cur.execute(
Expand All @@ -108,7 +109,7 @@ def rebuild_image_index(search_dirs: List[str]):
)
cur.execute("""DELETE FROM tag WHERE tag.type <> 'custom'""")
conn.commit()
update_image_data(search_dirs=search_dirs, is_rebuild=True)
update_image_data(search_dirs=search_dirs, is_rebuild=True, detect_nsfw=paylaod.get("autoDetectNsfwContent", False))


def get_extra_meta_keys_from_plugins(source_identifier: str):
Expand All @@ -120,9 +121,10 @@ def get_extra_meta_keys_from_plugins(source_identifier: str):
logger.error("get_extra_meta_keys_from_plugins %s", e)
return []

def build_single_img_idx(conn, file_path, is_rebuild, safe_save_img_tag):
def build_single_img_idx(conn, file_path, is_rebuild, safe_save_img_tag, detect_nsfw=False):
img = DbImg.get(conn, file_path)
parsed_params = None

if is_rebuild:
info = get_exif_data(file_path)
parsed_params = info.params
Expand Down Expand Up @@ -160,10 +162,17 @@ def build_single_img_idx(conn, file_path, is_rebuild, safe_save_img_tag):
conn,
str(meta.get("Size-1", 0)) + " * " + str(meta.get("Size-2", 0)),
type="size",
)
)
safe_save_img_tag(ImageTag(img.id, size_tag.id))
media_type_tag = Tag.get_or_create(conn, "Image" if is_image_file(file_path) else "Video", 'Media Type')
safe_save_img_tag(ImageTag(img.id, media_type_tag.id))

if detect_nsfw:
isNsfw = len(detect(file_path)) > 0
if(isNsfw):
nsfw_tag = Tag.get_or_create(conn, "nsfw", "custom")
safe_save_img_tag(ImageTag(img.id, nsfw_tag.id))

keys = [
"Model",
"Sampler",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions vue/dist/assets/FileItem-0443a788.js

This file was deleted.

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions vue/dist/assets/FileItem-e3b6a812.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions vue/dist/assets/ImgSliPagePane-5abae1a7.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vue/dist/assets/ImgSliPagePane-5f5e28b7.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.img-sli-container[data-v-ec71de83]{position:relative;overflow-y:auto;height:calc(100vh - 40px)}
1 change: 0 additions & 1 deletion vue/dist/assets/ImgSliPagePane-868b21f8.css

This file was deleted.

1 change: 0 additions & 1 deletion vue/dist/assets/ImgSliPagePane-8904be39.js

This file was deleted.

1 change: 1 addition & 0 deletions vue/dist/assets/MatchedImageGrid-2b7a400a.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vue/dist/assets/MatchedImageGrid-5ebcb029.js

This file was deleted.

Loading