- 新增阿里云OSS SDK核心类库文件 - 新增OSS插件配置文件及钩子监听 - 新增分类图标SVG资源并优化为轻量化格式 - 替换定位接口为天地图逆地理编码接口 - 新增OSS图片缩略处理工具方法
94 行
3.8 KiB
Python
94 行
3.8 KiB
Python
import json
|
||
import os
|
||
import shutil
|
||
import subprocess
|
||
import asyncio
|
||
|
||
schema = {
|
||
"type": "object",
|
||
"properties": {
|
||
"file_path": {"type": "string"},
|
||
"success": {"type": "boolean"}
|
||
},
|
||
"required": ["file_path", "success"]
|
||
}
|
||
|
||
php_bin = "D:/Softwares/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe"
|
||
out_dir = "D:/Softwares/phpstudy_pro/WWW/fenleixinxi/tmp_img/downloaded"
|
||
os.makedirs(out_dir, exist_ok=True)
|
||
|
||
# 1. 读取 prompt 映射字典
|
||
prompt_map_file = "D:/Softwares/phpstudy_pro/WWW/fenleixinxi/tmp_img/prompt_map.json"
|
||
with open(prompt_map_file, "r", encoding="utf-8") as f:
|
||
prompts_list = json.load(f)
|
||
prompt_dict = {str(item["id"]): item["prompt"] for item in prompts_list}
|
||
|
||
# 2. 从 PHP 脚本获取当前在数据库中真正尚未完成 OSS 入库的分类列表
|
||
proc = subprocess.run([php_bin, "D:/Softwares/phpstudy_pro/WWW/fenleixinxi/tmp_img/get_pending.php"], capture_output=True, text=True)
|
||
pending_categories = json.loads(proc.stdout) if proc.stdout else []
|
||
|
||
log(f"Current pending in DB: {len(pending_categories)}")
|
||
|
||
# 每次执行的总项数(默认 20 项,分为多小批平稳生成)
|
||
total_to_process = args.get("count", 20) if "args" in globals() and isinstance(args, dict) else 20
|
||
items_to_run = pending_categories[:total_to_process]
|
||
mini_batch_size = 3 # 每小批 3 个并发,稳健不触发 QPS 限流
|
||
|
||
log(f"Will process total {len(items_to_run)} items in chunks of {mini_batch_size}")
|
||
|
||
async def generate_single(cat):
|
||
cat_id = str(cat["id"])
|
||
name = cat["name"]
|
||
prompt = prompt_dict.get(cat_id)
|
||
if not prompt:
|
||
log(f"[WARN] No prompt for #{cat_id} {name}")
|
||
return False
|
||
|
||
dest_path = os.path.join(out_dir, f"{cat_id}.jpg")
|
||
|
||
agent_prompt = (
|
||
f"请生成分类『{name}』的配图,提示词:{prompt}\n"
|
||
f"要求:1:1方形构图,写实摄影质感,绝对不要包含任何文字、水印、LOGO、AI标识或角标。\n"
|
||
f"生成后通过 finish 工具返回保存的文件本地路径 file_path 和 success: true。"
|
||
)
|
||
|
||
try:
|
||
res = await agent(
|
||
prompt=agent_prompt,
|
||
schema=schema,
|
||
type_name="image-generator",
|
||
role=f"Gen-{name}",
|
||
model="flash"
|
||
)
|
||
if res and res.get("success") and res.get("file_path"):
|
||
src_file = res["file_path"]
|
||
if os.path.exists(src_file) and os.path.getsize(src_file) > 50000:
|
||
shutil.copyfile(src_file, dest_path)
|
||
log(f"[OK] #{cat_id} {name} -> {dest_path} (size: {os.path.getsize(dest_path)})")
|
||
return True
|
||
log(f"[FAIL] #{cat_id} {name} -> Invalid result: {res}")
|
||
return False
|
||
except Exception as e:
|
||
log(f"[ERROR] #{cat_id} {name} -> {str(e)}")
|
||
return False
|
||
|
||
# 分小批逐步执行
|
||
total_succeeded = 0
|
||
for i in range(0, len(items_to_run), mini_batch_size):
|
||
chunk = items_to_run[i:i + mini_batch_size]
|
||
log(f"--- Running chunk {i // mini_batch_size + 1}: {[c['name'] for c in chunk]} ---")
|
||
chunk_results = await parallel(chunk, generate_single)
|
||
chunk_ok = sum(1 for r in chunk_results if r)
|
||
total_succeeded += chunk_ok
|
||
log(f"Chunk finished: {chunk_ok}/{len(chunk)} succeeded. (Cumulative: {total_succeeded}/{len(items_to_run)})")
|
||
|
||
# 每一小批完成立即自动推送到 OSS 入库,确保成果实时落盘
|
||
if chunk_ok > 0:
|
||
ingest_cmd = [php_bin, "think", "fc:category-images"]
|
||
ingest_res = subprocess.run(ingest_cmd, cwd="D:/Softwares/phpstudy_pro/WWW/fenleixinxi", capture_output=True, text=True)
|
||
log(f"Incremental Ingest: {ingest_res.stdout.strip().splitlines()[-2:] if ingest_res.stdout else 'None'}")
|
||
|
||
await asyncio.sleep(2) # 平稳休眠 2 秒,防触发突发限速
|
||
|
||
log(f"All chunks in this run completed! Total success in this run: {total_succeeded}")
|