Skip to content
153 changes: 130 additions & 23 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,102 @@
import shutil
import json
import mimetypes
import shutil

datapath = os.path.join(os.path.dirname(__file__), 'promptImages')


def update_libraries_file():
lib_path = os.path.join(datapath, "promptGallery_libraries.json")
txt_folder = os.path.join(datapath, "txt")
yaml_folder = os.path.join(datapath, "yaml")
# Load libraries JSON
with open(lib_path, 'r') as f:
lib = json.load(f)
libraries = lib.get("libraries", [])

# Get all txt files in txt folder
txt_files = [f for f in os.listdir(txt_folder) if os.path.isfile(os.path.join(txt_folder, f))]
# Get all yaml files in yaml folder
yaml_files = []
if os.path.exists(yaml_folder):
yaml_files = [f for f in os.listdir(yaml_folder) if os.path.isfile(os.path.join(yaml_folder, f)) and f.lower().endswith('.yaml')]

# Get set of existing library names for quick lookup (now without extension)
existing_names = set(library.get("name") for library in libraries)

updated = False
for txt_file in txt_files:
name_without_ext = os.path.splitext(txt_file)[0]
if name_without_ext not in existing_names:
new_library = {
"name": name_without_ext,
"filetype": "csv",
"type": name_without_ext,
"skipLevels": 0,
"sections": ""
}
libraries.append(new_library)
updated = True

for yaml_file in yaml_files:
name_without_ext = os.path.splitext(yaml_file)[0]
if name_without_ext not in existing_names:
new_library = {
"name": name_without_ext,
"filetype": "yaml",
"type": name_without_ext,
"skipLevels": 0,
"sections": ""
}
libraries.append(new_library)
updated = True

# Optionally, keep the original hardcoded entry if needed
# new_library = {
# "name": "PonyXl-f-body.yaml",
# "filetype": "yaml",
# "type": "Female Body",
# "skipLevels": 0,
# "sections": ""
# }
# if new_library["name"] not in existing_names:
# libraries.append(new_library)
# updated = True

if updated:
lib["libraries"] = libraries
with open(lib_path, 'w') as f:
json.dump(lib, f, indent=2)


def convert_txts_to_csv():
for filename in os.listdir(os.path.join(datapath,'txt')):
# print(filename)

lines = []
source = os.path.join(datapath,'txt', f"{filename}")
dest = os.path.join(datapath,'csv', f"{filename}")
dest = dest[:-4] + ".csv"

if not os.path.exists(dest):
with open(source,'r') as f:
lines = f.readlines()
# for l in f.readlines():
# lines.append(l)
# lines.append(f.readline())

shutil.copy(source,dest)

with open(dest,'w') as f:
for line in lines:
split = line.split(',')
if len(split) > 1:
f.write(f"{split[0]}|{line}")
else:
f.write(f"{line}|{line}")


@PromptServer.instance.routes.get("/prompt_gallery/image")
async def view_image(request):
if "filename" in request.rel_url.query:
Expand Down Expand Up @@ -46,6 +138,11 @@ async def view_image(request):
return web.Response(status=400)


@PromptServer.instance.routes.get("/prompt_gallery/update-json")
async def get_promptgallery_file(request):
convert_txts_to_csv()
update_libraries_file()

@PromptServer.instance.routes.post("/prompt_gallery/upload")
async def upload_image(request):
try:
Expand Down Expand Up @@ -81,22 +178,31 @@ async def upload_image(request):
print(f"Error in upload_image: {str(e)}")
return web.json_response({"error": str(e)}, status=500)

@PromptServer.instance.routes.get("/prompt_gallery/yaml")
async def view_yaml(request):
if "filename" in request.rel_url.query:
filename = request.rel_url.query["filename"]

if 'subfolder' in request.rel_url.query:
subfolder = request.rel_url.query["subfolder"]
@PromptServer.instance.routes.get("/prompt_gallery/get-file")
async def get_promptgallery_file(request):
url_vars = request.rel_url.query
datapath_get_file = datapath
if "filename" in url_vars:
filename = url_vars["filename"]

if "filetype" in url_vars:
datapath_get_file = os.path.join(datapath, url_vars["filetype"])

if 'subfolder' in url_vars:
subfolder = url_vars["subfolder"]
filename = os.path.join(filename, subfolder)
else:
subfolder = ""

# validation for security: prevent accessing arbitrary path
if '..' in filename or '..' in subfolder:
return web.Response(status=400)
for key, value in url_vars.items():
if '..' in value:
print(f"{key}: {value}")
return web.Response(status=400)
# if '..' in filename or '..' in subfolder:
# return web.Response(status=400)

fullpath = os.path.join(datapath, filename)
fullpath = os.path.join(datapath_get_file, filename)

try:
with open(fullpath) as yaml:
Expand All @@ -106,25 +212,26 @@ async def view_yaml(request):
# print(f"YAML file not found: {fullpath}") cut down on needless noise
return web.Response(text="", status=404)
except Exception as e:
print(f"Error reading YAML file {fullpath}: {str(e)}")
print(f"Error reading file {fullpath}: {str(e)}")
return web.Response(text="", status=500)

return web.Response(status=400)

@PromptServer.instance.routes.post("/prompt_gallery/update_libraries")
async def update_libraries(request):
try:
data = await request.json()
filename = "promptGallery_libraries.json"
fullpath = os.path.join(datapath, filename)
# @PromptServer.instance.routes.post("/prompt_gallery/update_libraries")
# async def update_libraries(request):
# try:
# data = await request.json()
# filename = "promptGallery_libraries.json"
# fullpath = os.path.join(datapath, filename)

with open(fullpath, 'w') as f:
json.dump(data, f, indent=2)
# with open(fullpath, 'w') as f:
# json.dump(data, f, indent=2)

# return web.Response(status=200)
# except Exception as e:
# print(f"Error updating libraries file: {str(e)}")
# return web.Response(status=500, text=str(e))

return web.Response(status=200)
except Exception as e:
print(f"Error updating libraries file: {str(e)}")
return web.Response(status=500, text=str(e))


NODE_CLASS_MAPPINGS = {
Expand Down
72 changes: 20 additions & 52 deletions promptImages/promptGallery_libraries.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,39 @@
"version": "1.0",
"libraries": [
{
"name": "PonyXl-artstyles.yaml",
"type": "Art Styles",
"name": "pokegirl_full_clothing",
"filetype": "csv",
"type": "pokegirl_full_clothing",
"skipLevels": 0,
"sections": null,
"order": 1
"sections": ""
},
{
"name": "PonyXl-game_persona.yaml",
"type": "Game Characters",
"name": "pokegirl_minimal",
"filetype": "csv",
"type": "pokegirl_minimal",
"skipLevels": 0,
"sections": null,
"order": 2
"sections": ""
},
{
"name": "PonyXl-show_persona.yaml",
"type": "Show Characters",
"name": "Danbooru-styles",
"filetype": "yaml",
"type": "Danbooru-styles",
"skipLevels": 0,
"sections": null,
"order": 3
"sections": ""
},
{
"name": "PonyXl-f-body.yaml",
"type": "Female Body",
"skipLevels": 1,
"sections": {
"body_race": "Race",
"body_form": "Build"
},
"order": 4
},
{
"name": "PonyXl-poses.yaml",
"type": "Poses",
"skipLevels": 0,
"sections": null,
"order": 7
},
{
"name": "PonyXl-expressions.yaml",
"type": "Expressions",
"skipLevels": 0,
"sections": null,
"order": 8,
"ignoreKey": "chara_expression"
},
{
"name": "PonyXl-scenes.yaml",
"type": "Scenes",
"skipLevels": 0,
"sections": null,
"order": 9
},
{
"name": "PonyXl-jobs.yaml",
"type": "Jobs",
"name": "PonyXl-expressions",
"filetype": "yaml",
"type": "PonyXl-expressions",
"skipLevels": 0,
"sections": null,
"order": 6
"sections": ""
},
{
"name": "PonyXl-other_persona.yaml",
"type": "Stereotypes",
"name": "PonyXl-f-body",
"filetype": "yaml",
"type": "PonyXl-f-body",
"skipLevels": 0,
"sections": null,
"order": 10
"sections": ""
}
]
}
Loading