Upload 5 files
Browse files- batch_preview_generator/flux/0_batch_template_flux_v000.json +0 -0
- batch_preview_generator/flux/1_batch_regroup.py +63 -0
- batch_preview_generator/flux/2_batch_process_meta.py +244 -0
- batch_preview_generator/flux/3_batch_upload_showcase.py +82 -0
- batch_preview_generator/flux/4_batch_modify_json.py +101 -0
batch_preview_generator/flux/0_batch_template_flux_v000.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
batch_preview_generator/flux/1_batch_regroup.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
def group_and_move_files(target_dir="."):
|
| 6 |
+
# 1. 匹配时间戳前的部分
|
| 7 |
+
pattern = re.compile(r'^(.*?)_\d{8}_')
|
| 8 |
+
|
| 9 |
+
if not os.path.exists(target_dir):
|
| 10 |
+
print(f"找不到目录: {target_dir}")
|
| 11 |
+
return
|
| 12 |
+
|
| 13 |
+
files = [f for f in os.listdir(target_dir) if os.path.isfile(os.path.join(target_dir, f))]
|
| 14 |
+
|
| 15 |
+
moved_count = 0
|
| 16 |
+
folder_count = 0
|
| 17 |
+
|
| 18 |
+
print(f"开始处理目录: {os.path.abspath(target_dir)}")
|
| 19 |
+
print("-" * 50)
|
| 20 |
+
|
| 21 |
+
for filename in files:
|
| 22 |
+
if filename == os.path.basename(__file__):
|
| 23 |
+
continue
|
| 24 |
+
|
| 25 |
+
match = pattern.match(filename)
|
| 26 |
+
if match:
|
| 27 |
+
raw_prefix = match.group(1) # 例如: "AITM_Flux-000010"
|
| 28 |
+
|
| 29 |
+
# ==========================================
|
| 30 |
+
# 2. 核心清理逻辑:去掉最后的 "-数字" 后缀
|
| 31 |
+
# re.sub 会把结尾的 "-000010" 或 "-10" 替换为空
|
| 32 |
+
# ==========================================
|
| 33 |
+
clean_prefix = re.sub(r'-\d+$', '', raw_prefix)
|
| 34 |
+
|
| 35 |
+
# 如果清理后变成了空字符串(防范极端情况),则保留原样
|
| 36 |
+
if not clean_prefix:
|
| 37 |
+
clean_prefix = raw_prefix
|
| 38 |
+
|
| 39 |
+
# 3. 构建文件夹路径
|
| 40 |
+
folder_path = os.path.join(target_dir, clean_prefix)
|
| 41 |
+
|
| 42 |
+
if not os.path.exists(folder_path):
|
| 43 |
+
os.makedirs(folder_path)
|
| 44 |
+
folder_count += 1
|
| 45 |
+
print(f"📁 创建文件夹: {clean_prefix}")
|
| 46 |
+
|
| 47 |
+
# 4. 移动文件
|
| 48 |
+
src_path = os.path.join(target_dir, filename)
|
| 49 |
+
dst_path = os.path.join(folder_path, filename)
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
shutil.move(src_path, dst_path)
|
| 53 |
+
print(f" ✅ 已移动: {filename} -> {clean_prefix}/")
|
| 54 |
+
moved_count += 1
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f" ❌ 移动失败: {filename},错误: {e}")
|
| 57 |
+
|
| 58 |
+
print("-" * 50)
|
| 59 |
+
print(f"处理完毕!共创建了 {folder_count} 个文件夹,成功移动了 {moved_count} 个文件。")
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
group_and_move_files(".")
|
batch_preview_generator/flux/2_batch_process_meta.py
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
import shutil
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# ==========================================================
|
| 9 |
+
# 核心解析逻辑 (第一版纯净版,兼容现有结构)
|
| 10 |
+
# ==========================================================
|
| 11 |
+
def clean_showcase_name(name):
|
| 12 |
+
if not name: return ""
|
| 13 |
+
base = str(name).split('\\')[-1].split('/')[-1]
|
| 14 |
+
base = re.sub(r'\.(safetensors|pt|ckpt|sft|pth|bin)$', '', base, flags=re.IGNORECASE)
|
| 15 |
+
return base
|
| 16 |
+
|
| 17 |
+
def parse_comfy_data(prompt_data):
|
| 18 |
+
res = {
|
| 19 |
+
"model": "", "vae": "", "clip": "", "loras": [], "size": "",
|
| 20 |
+
# 🌟 核心修改:统一硬编码默认值
|
| 21 |
+
"seed": "-", "steps": "8", "cfg": 1.0, "denoise": 1.0,
|
| 22 |
+
"sampler": "euler_ancestral", "scheduler": "beta", "pos": ""
|
| 23 |
+
}
|
| 24 |
+
if not isinstance(prompt_data, dict): return res
|
| 25 |
+
|
| 26 |
+
# 1. 提取模型、VAE、LoRA 等基础资源 (基于你之前的 API 格式)
|
| 27 |
+
for nid, node in prompt_data.items():
|
| 28 |
+
if not isinstance(node, dict) or node.get("mode") == 4: continue
|
| 29 |
+
ctype = node.get("class_type", "")
|
| 30 |
+
inputs = node.get("inputs", {})
|
| 31 |
+
|
| 32 |
+
if "UNETLoader" in ctype or "CheckpointLoader" in ctype:
|
| 33 |
+
res["model"] = clean_showcase_name(inputs.get("unet_name") or inputs.get("ckpt_name") or "")
|
| 34 |
+
if "VAELoader" in ctype:
|
| 35 |
+
res["vae"] = clean_showcase_name(inputs.get("vae_name", ""))
|
| 36 |
+
if "CLIPLoader" in ctype:
|
| 37 |
+
res["clip"] = clean_showcase_name(inputs.get("clip_name", ""))
|
| 38 |
+
if "LoraLoader" in ctype or "lora" in ctype.lower():
|
| 39 |
+
l_name = clean_showcase_name(inputs.get("lora_name", ""))
|
| 40 |
+
if l_name and l_name not in res["loras"]:
|
| 41 |
+
res["loras"].append(l_name)
|
| 42 |
+
|
| 43 |
+
# 2. 🌟 暴力提取 Seed
|
| 44 |
+
# 方法 A:顺着节点 92 去抓(应对 API 连线格式)
|
| 45 |
+
if "92" in prompt_data:
|
| 46 |
+
node_92 = prompt_data["92"]
|
| 47 |
+
if isinstance(node_92, dict) and "inputs" in node_92:
|
| 48 |
+
seed_val = node_92["inputs"].get("seed")
|
| 49 |
+
if isinstance(seed_val, (int, float, str)) and str(seed_val).isdigit():
|
| 50 |
+
res["seed"] = str(seed_val)
|
| 51 |
+
|
| 52 |
+
# 方法 B:如果节点 92 没拿到,直接把整个 JSON 变成字符串,用正则把那个大数字抠出来!
|
| 53 |
+
if res["seed"] == "-" or not res["seed"]:
|
| 54 |
+
json_str = json.dumps(prompt_data, ensure_ascii=False)
|
| 55 |
+
# 匹配 "seed": 893750591869527 或者 "seed": "893750591869527"
|
| 56 |
+
seed_match = re.search(r'"seed":\s*"?(\d{10,})"?', json_str)
|
| 57 |
+
if seed_match:
|
| 58 |
+
res["seed"] = seed_match.group(1)
|
| 59 |
+
else:
|
| 60 |
+
# 如果整图全是乱码,实在找不到,就把你截图里固定的那个填进去兜底
|
| 61 |
+
res["seed"] = "1234"
|
| 62 |
+
|
| 63 |
+
# 3. 纯文本提取正向提示词 (沿用你的第一版逻辑)
|
| 64 |
+
for nid, node in prompt_data.items():
|
| 65 |
+
if not isinstance(node, dict): continue
|
| 66 |
+
if "CLIPTextEncode" in node.get("class_type", ""):
|
| 67 |
+
text = node.get("inputs", {}).get("text", "")
|
| 68 |
+
if isinstance(text, str) and len(text) > len(res["pos"]):
|
| 69 |
+
res["pos"] = text
|
| 70 |
+
|
| 71 |
+
# 4. 尺寸兜底
|
| 72 |
+
json_str = json.dumps(prompt_data, ensure_ascii=False)
|
| 73 |
+
if not res["size"]:
|
| 74 |
+
size_match = re.search(r'"width":\s*(\d+).*?"height":\s*(\d+)', json_str)
|
| 75 |
+
if size_match:
|
| 76 |
+
res["size"] = f"{size_match.group(1)}x{size_match.group(2)}"
|
| 77 |
+
|
| 78 |
+
return res
|
| 79 |
+
def extract_single_png(png_path):
|
| 80 |
+
try:
|
| 81 |
+
with Image.open(png_path) as img:
|
| 82 |
+
real_w, real_h = img.size
|
| 83 |
+
real_size = f"{real_w}x{real_h}"
|
| 84 |
+
|
| 85 |
+
raw_prompt = img.info.get("prompt", "")
|
| 86 |
+
dasiwa_text = img.info.get('parameters', '') or img.info.get('Description', '')
|
| 87 |
+
|
| 88 |
+
if not raw_prompt:
|
| 89 |
+
exif = img.getexif()
|
| 90 |
+
raw_prompt = exif.get(271, "")
|
| 91 |
+
if isinstance(raw_prompt, bytes): raw_prompt = raw_prompt.decode('utf-8', errors='ignore')
|
| 92 |
+
raw_prompt = str(raw_prompt).strip().replace('\x00', '')
|
| 93 |
+
if raw_prompt.startswith("Prompt: "): raw_prompt = raw_prompt[8:]
|
| 94 |
+
|
| 95 |
+
report = {}
|
| 96 |
+
if raw_prompt:
|
| 97 |
+
json_match = re.search(r'\{.*\}', raw_prompt, re.DOTALL)
|
| 98 |
+
if json_match:
|
| 99 |
+
report = parse_comfy_data(json.loads(json_match.group(0)))
|
| 100 |
+
|
| 101 |
+
if not report:
|
| 102 |
+
report = {"size": "-", "loras": [], "model": "-", "vae": "-", "clip": "-","sampler": "-", "scheduler": "-", "seed": "-", "steps": "-", "cfg": "-", "denoise": "-", "pos": ""}
|
| 103 |
+
|
| 104 |
+
report["size"] = real_size
|
| 105 |
+
|
| 106 |
+
if (not report["pos"] or len(str(report["pos"])) < 5) and dasiwa_text:
|
| 107 |
+
pos_match = re.search(r"^(.*?)(?:Negative prompt:|$)", dasiwa_text, re.DOTALL | re.IGNORECASE)
|
| 108 |
+
if pos_match:
|
| 109 |
+
extracted = pos_match.group(1).strip()
|
| 110 |
+
report["pos"] = extracted
|
| 111 |
+
|
| 112 |
+
if not isinstance(report["pos"], str):
|
| 113 |
+
report["pos"] = ""
|
| 114 |
+
|
| 115 |
+
return report
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f" ⚠️ 提取失败 {os.path.basename(png_path)}: {e}")
|
| 118 |
+
return None
|
| 119 |
+
|
| 120 |
+
def convert_png_to_webp(src_path, dst_path, quality=90):
|
| 121 |
+
try:
|
| 122 |
+
with Image.open(src_path) as img:
|
| 123 |
+
raw_prompt = img.info.get('prompt', '')
|
| 124 |
+
raw_workflow = img.info.get('workflow', '')
|
| 125 |
+
|
| 126 |
+
exif = img.getexif()
|
| 127 |
+
if raw_prompt: exif[0x010f] = "Prompt: " + raw_prompt
|
| 128 |
+
if raw_workflow: exif[0x010e] = "Workflow: " + raw_workflow
|
| 129 |
+
|
| 130 |
+
if img.mode in ("RGBA", "P"):
|
| 131 |
+
img = img.convert("RGB")
|
| 132 |
+
|
| 133 |
+
save_kwargs = {"quality": int(quality), "method": 6}
|
| 134 |
+
if raw_prompt or raw_workflow:
|
| 135 |
+
save_kwargs["exif"] = exif.tobytes()
|
| 136 |
+
|
| 137 |
+
img.save(dst_path, "WEBP", **save_kwargs)
|
| 138 |
+
return True
|
| 139 |
+
except Exception as e:
|
| 140 |
+
print(f" ❌ 转换失败: {e}")
|
| 141 |
+
return False
|
| 142 |
+
|
| 143 |
+
# ==========================================================
|
| 144 |
+
# 批处理主逻辑
|
| 145 |
+
# ==========================================================
|
| 146 |
+
def process_folder(folder_path):
|
| 147 |
+
folder = Path(folder_path)
|
| 148 |
+
png_input_dir = folder / "png_input"
|
| 149 |
+
|
| 150 |
+
png_files = []
|
| 151 |
+
archive_needed = False # 标记是否需要把根目录的图移进 png_input
|
| 152 |
+
|
| 153 |
+
# 1. 优先检查 png_input 里有没有 PNG
|
| 154 |
+
if png_input_dir.exists():
|
| 155 |
+
png_files.extend(list(png_input_dir.glob("*.png")))
|
| 156 |
+
|
| 157 |
+
# 2. 如果 png_input 里没有,且根目录有,则用根目录的
|
| 158 |
+
if not png_files:
|
| 159 |
+
root_pngs = list(folder.glob("*.png"))
|
| 160 |
+
if root_pngs:
|
| 161 |
+
png_files.extend(root_pngs)
|
| 162 |
+
archive_needed = True # 根目录处理完后需要归档
|
| 163 |
+
|
| 164 |
+
if not png_files:
|
| 165 |
+
return False # 两个地方都没有,跳过
|
| 166 |
+
|
| 167 |
+
source_type = "png_input" if not archive_needed else "根目录"
|
| 168 |
+
print(f"\n📂 正在处理文件夹: {folder.name} (从 [{source_type}] 发现 {len(png_files)} 张源 PNG)")
|
| 169 |
+
|
| 170 |
+
# 如果打算归档,先建好文件夹
|
| 171 |
+
if archive_needed:
|
| 172 |
+
png_input_dir.mkdir(exist_ok=True)
|
| 173 |
+
|
| 174 |
+
# 读取上一级的旧 JSON(子文件夹根目录)
|
| 175 |
+
json_path = folder / "gallery.json"
|
| 176 |
+
existing_data = []
|
| 177 |
+
if json_path.exists():
|
| 178 |
+
try:
|
| 179 |
+
with open(json_path, "r", encoding="utf-8") as f:
|
| 180 |
+
existing_data = json.load(f)
|
| 181 |
+
except Exception:
|
| 182 |
+
pass
|
| 183 |
+
|
| 184 |
+
new_gallery_data = []
|
| 185 |
+
|
| 186 |
+
for png in png_files:
|
| 187 |
+
data = extract_single_png(png)
|
| 188 |
+
if data:
|
| 189 |
+
webp_name = png.stem + ".webp"
|
| 190 |
+
data["file_name"] = webp_name
|
| 191 |
+
|
| 192 |
+
# 🌟 核心逻辑:WebP 吐到 png_input 的上一级(即子文件夹根目录)
|
| 193 |
+
webp_path = folder / webp_name
|
| 194 |
+
|
| 195 |
+
if convert_png_to_webp(png, webp_path):
|
| 196 |
+
new_gallery_data.append(data)
|
| 197 |
+
print(f" ✅ 转换成功: {png.name} -> {webp_name}")
|
| 198 |
+
|
| 199 |
+
# 🌟 如果是根目录的图,转换后将其移入 png_input
|
| 200 |
+
if archive_needed:
|
| 201 |
+
try:
|
| 202 |
+
shutil.move(str(png), str(png_input_dir / png.name))
|
| 203 |
+
except Exception as e:
|
| 204 |
+
print(f" ⚠️ 原图归档失败: {e}")
|
| 205 |
+
else:
|
| 206 |
+
print(f" ⚠️ 跳过: {png.name} 转换失败")
|
| 207 |
+
|
| 208 |
+
# 增量合并 JSON 并生成到上一级
|
| 209 |
+
if new_gallery_data:
|
| 210 |
+
merged_dict = {item["file_name"]: item for item in existing_data if "file_name" in item}
|
| 211 |
+
for item in new_gallery_data:
|
| 212 |
+
merged_dict[item["file_name"]] = item
|
| 213 |
+
|
| 214 |
+
final_data = list(merged_dict.values())
|
| 215 |
+
|
| 216 |
+
with open(json_path, "w", encoding="utf-8") as f:
|
| 217 |
+
json.dump(final_data, f, indent=2, ensure_ascii=False)
|
| 218 |
+
print(f" 📄 已生成 {json_path.name},共 {len(final_data)} 条数据。")
|
| 219 |
+
return True
|
| 220 |
+
|
| 221 |
+
return False
|
| 222 |
+
|
| 223 |
+
if __name__ == "__main__":
|
| 224 |
+
root_dir = Path(os.path.dirname(os.path.abspath(__file__)))
|
| 225 |
+
|
| 226 |
+
print(f"🔍 开始扫描母目录: {root_dir}")
|
| 227 |
+
print("=" * 60)
|
| 228 |
+
|
| 229 |
+
processed_count = 0
|
| 230 |
+
|
| 231 |
+
# 遍历母目录下的所有子文件夹
|
| 232 |
+
for item in root_dir.iterdir():
|
| 233 |
+
if item.is_dir() and not item.name.startswith('.'):
|
| 234 |
+
if item.name in ["png_input", "output"]:
|
| 235 |
+
continue
|
| 236 |
+
|
| 237 |
+
success = process_folder(item)
|
| 238 |
+
if success:
|
| 239 |
+
processed_count += 1
|
| 240 |
+
|
| 241 |
+
print("\n" + "=" * 60)
|
| 242 |
+
print(f"🎉 批处理完成!共成���处理了 {processed_count} 个子文件夹。")
|
| 243 |
+
print("现在你可以打开这些文件夹检查 gallery.json,并将它们批量上传到 HF。")
|
| 244 |
+
input("\n按回车键退出...")
|
batch_preview_generator/flux/3_batch_upload_showcase.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from huggingface_hub import HfApi
|
| 5 |
+
|
| 6 |
+
# ================= 配置区 =================
|
| 7 |
+
MODEL_REPO_ID = "waveshaker/loras"
|
| 8 |
+
# 目标远端根目录
|
| 9 |
+
REMOTE_BASE_DIR = "comfyui_showcase/flux1_dev"
|
| 10 |
+
|
| 11 |
+
# 自动获取当前脚本(batch_upload_showcase.py)所在的目录
|
| 12 |
+
LOCAL_ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 13 |
+
|
| 14 |
+
TOKEN_FILE = "hf_token.txt"
|
| 15 |
+
# ==========================================
|
| 16 |
+
|
| 17 |
+
def get_token():
|
| 18 |
+
if os.path.exists(TOKEN_FILE):
|
| 19 |
+
with open(TOKEN_FILE, "r", encoding="utf-8") as f:
|
| 20 |
+
return f.read().strip()
|
| 21 |
+
return ""
|
| 22 |
+
|
| 23 |
+
def main():
|
| 24 |
+
token = get_token()
|
| 25 |
+
if not token:
|
| 26 |
+
print("❌ 找不到 Token,请检查 hf_token.txt。")
|
| 27 |
+
return
|
| 28 |
+
|
| 29 |
+
api = HfApi(token=token)
|
| 30 |
+
root_path = Path(LOCAL_ROOT_DIR)
|
| 31 |
+
|
| 32 |
+
print(f"🔍 正在扫描目录: {root_path}")
|
| 33 |
+
|
| 34 |
+
# 🌟 需求 1:只扫描文件夹(自动忽略同级目录下的 .py, .txt 等所有文件)
|
| 35 |
+
subfolders = [f for f in root_path.iterdir() if f.is_dir() and not f.name.startswith('.')]
|
| 36 |
+
|
| 37 |
+
if not subfolders:
|
| 38 |
+
print("❌ 没有找到需要上传的文件夹。")
|
| 39 |
+
return
|
| 40 |
+
|
| 41 |
+
print(f"📦 发现 {len(subfolders)} 个文件夹,准备开始上传...")
|
| 42 |
+
print(f"🎯 目标远端: {MODEL_REPO_ID}/{REMOTE_BASE_DIR}\n")
|
| 43 |
+
print("=" * 60)
|
| 44 |
+
|
| 45 |
+
success_count = 0
|
| 46 |
+
for folder in subfolders:
|
| 47 |
+
folder_name = folder.name
|
| 48 |
+
# 拼接远端路径:comfyui_showcase/flux1_dev/子文件夹名
|
| 49 |
+
repo_path = f"{REMOTE_BASE_DIR}/{folder_name}"
|
| 50 |
+
|
| 51 |
+
# 检查里面是否有 webp 或 json
|
| 52 |
+
files_to_upload = [f for f in folder.iterdir() if f.is_file() and f.suffix.lower() in ['.webp', '.json']]
|
| 53 |
+
if not files_to_upload:
|
| 54 |
+
print(f"⏭️ 跳过 [{folder_name}]:没有找到 .webp 或 .json 文件。")
|
| 55 |
+
continue
|
| 56 |
+
|
| 57 |
+
print(f"🚀 正在上传: {folder_name}")
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
# 🌟 需求 2:上传时,只允许传 webp 和 json,彻底剔除 png_input 等无关文件
|
| 61 |
+
api.upload_folder(
|
| 62 |
+
folder_path=str(folder),
|
| 63 |
+
repo_id=MODEL_REPO_ID,
|
| 64 |
+
repo_type="model",
|
| 65 |
+
path_in_repo=repo_path,
|
| 66 |
+
commit_message=f"Upload showcase for {folder_name}",
|
| 67 |
+
allow_patterns=["*.webp", "*.json"], # 过滤规则
|
| 68 |
+
ignore_patterns=["png_input/*", "*.png"] # 双重保险,彻底屏蔽 png_input
|
| 69 |
+
)
|
| 70 |
+
print(f" ✅ 上传成功!")
|
| 71 |
+
success_count += 1
|
| 72 |
+
time.sleep(0.5) # 稍微停顿,防限流
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(f" ❌ 上传失败: {e}")
|
| 75 |
+
|
| 76 |
+
print("-" * 60)
|
| 77 |
+
|
| 78 |
+
print(f"\n🎉 批处理完毕!共成功上传 {success_count} 个文件夹。")
|
| 79 |
+
input("\n按回车键退出...")
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
main()
|
batch_preview_generator/flux/4_batch_modify_json.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
# ================= 配置区 =================
|
| 7 |
+
JSON_FILE = "models.json"
|
| 8 |
+
REMOTE_SHOWCASE_BASE = "https://huggingface.co/waveshaker/loras/tree/main/comfyui_showcase/flux1_dev"
|
| 9 |
+
# ==========================================
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
current_dir = Path(os.path.dirname(os.path.abspath(__file__)))
|
| 13 |
+
json_path = current_dir / JSON_FILE
|
| 14 |
+
|
| 15 |
+
if not json_path.exists():
|
| 16 |
+
print(f"❌ 找不到 {JSON_FILE},请确认脚本与它同级。")
|
| 17 |
+
return
|
| 18 |
+
|
| 19 |
+
print(f"📖 正在加载 {JSON_FILE}...\n")
|
| 20 |
+
with open(json_path, "r", encoding="utf-8") as f:
|
| 21 |
+
data = json.load(f)
|
| 22 |
+
|
| 23 |
+
items = data.get("items", {})
|
| 24 |
+
if not items:
|
| 25 |
+
print("❌ JSON 中未找到 'items' 字段。")
|
| 26 |
+
return
|
| 27 |
+
|
| 28 |
+
folders = [f for f in current_dir.iterdir() if f.is_dir() and not f.name.startswith('.') and f.name not in ["png_input", "old"]]
|
| 29 |
+
|
| 30 |
+
if not folders:
|
| 31 |
+
print("❌ 当前目录下没有找到包含 webp 的子文件夹。")
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
+
updated_count = 0
|
| 35 |
+
unmatched_list = [] # 🌟 新增:用来收集未匹配的文件夹
|
| 36 |
+
|
| 37 |
+
print("=" * 60)
|
| 38 |
+
print("🔍 开始精准提取并匹配...")
|
| 39 |
+
print("=" * 60)
|
| 40 |
+
|
| 41 |
+
for folder in folders:
|
| 42 |
+
folder_name = folder.name
|
| 43 |
+
webp_files = list(folder.glob("*.webp"))
|
| 44 |
+
|
| 45 |
+
if not webp_files:
|
| 46 |
+
continue
|
| 47 |
+
|
| 48 |
+
# 提取时间戳前的精确名字
|
| 49 |
+
raw_name = webp_files[0].stem
|
| 50 |
+
match = re.search(r'[_-]20\d{2}', raw_name)
|
| 51 |
+
|
| 52 |
+
if match:
|
| 53 |
+
exact_lora_name = raw_name[:match.start()].strip()
|
| 54 |
+
else:
|
| 55 |
+
exact_lora_name = raw_name.strip()
|
| 56 |
+
|
| 57 |
+
matched = False
|
| 58 |
+
for key, meta in items.items():
|
| 59 |
+
key_filename = os.path.basename(key).replace(".safetensors", "")
|
| 60 |
+
|
| 61 |
+
if "flux1_dev" in key.lower() and key_filename.lower() == exact_lora_name.lower():
|
| 62 |
+
new_url = f"{REMOTE_SHOWCASE_BASE}/{folder_name}"
|
| 63 |
+
|
| 64 |
+
if meta.get("showcase") != new_url:
|
| 65 |
+
meta["showcase"] = new_url
|
| 66 |
+
updated_count += 1
|
| 67 |
+
matched = True
|
| 68 |
+
|
| 69 |
+
# 你之前说不想看成功的长数据了,这里就保持安静,只打印结果
|
| 70 |
+
# print(f" ✅ 精准命中: {key}")
|
| 71 |
+
|
| 72 |
+
if not matched:
|
| 73 |
+
# 🌟 如果没匹配上,记录下来
|
| 74 |
+
unmatched_list.append({
|
| 75 |
+
"folder": folder_name,
|
| 76 |
+
"extracted_name": exact_lora_name
|
| 77 |
+
})
|
| 78 |
+
|
| 79 |
+
print("\n" + "=" * 60)
|
| 80 |
+
if updated_count > 0:
|
| 81 |
+
with open(json_path, "w", encoding="utf-8") as f:
|
| 82 |
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
| 83 |
+
print(f"🎉 成功更新了 {updated_count} 条数据的 showcase 链接!")
|
| 84 |
+
print(f"文件已保存: {json_path}")
|
| 85 |
+
else:
|
| 86 |
+
print("\n🤷♂️ 没有找到任何需要更新的模型,或者全部已经匹配完成。")
|
| 87 |
+
|
| 88 |
+
# 🌟 核心新增:统一打印未匹配的清单
|
| 89 |
+
if unmatched_list:
|
| 90 |
+
print("\n" + "🚨" * 20)
|
| 91 |
+
print(f"⚠️ 发现 {len(unmatched_list)} 个文件夹未能匹配到对应的 flux1_dev 模型:")
|
| 92 |
+
for item in unmatched_list:
|
| 93 |
+
print(f" ❌ 文件夹: {item['folder']}")
|
| 94 |
+
print(f" (提取出的名字: {item['extracted_name']})")
|
| 95 |
+
print("🚨" * 20)
|
| 96 |
+
print("💡 建议:请检查上述提取出的名字,是否和你的 models.json 里的 .safetensors 名字完全一致。")
|
| 97 |
+
else:
|
| 98 |
+
print("\n✨ 完美!所有文件夹都精准匹配到了对应的模型。")
|
| 99 |
+
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
main()
|