# -*- coding: utf-8 -*-
"""配对质检：线条图 + 配色图放进同一条 message，问构图一致性与上色质量
（线稿/配色/配对是三套不同问卷，不可复用）"""
import base64, json, os, requests
from concurrent.futures import ThreadPoolExecutor

CFG = json.load(open('/data/www/files/LhyjdesignPro/config.json'))
KEY, BASE, MODEL = CFG['api_key'], CFG['base_url'], CFG['models']['text_vision']
BASE_DIR = '/data/www/files/waterHealthArt'

Q = ("图片1是一张黑白线条稿，图片2是同一构图的水彩配色稿。请严格简短回答（每项一行，不展开）：\n"
     "1. 图片2与图片1的形状、位置、数量是否一致？多了或少了什么？\n"
     "2. 图片2里有没有出现任何文字/字母/数字/印章/水印？有则原样列出，无则写「无」。\n"
     "3. 图片2是否为干净的水彩平涂（黑色轮廓线仍然清晰可见，无厚重黑块、无脏灰调子）？（是/否+一句）\n"
     "4. 颜色是否明亮协调、适合儿童水彩画？（是/否+一句）\n"
     "5. 画面中「水」的部分（水杯/河流/水花）是否是清亮的蓝色调？是/否+一句。\n"
     "6. 图片2整体是否体现「科学饮水·健康饮水」的积极主题？（是/否+一句）\n")


def pair(name):
    l = os.path.join(BASE_DIR, 'lineart', name)
    c = os.path.join(BASE_DIR, 'color', name)
    b1 = base64.b64encode(open(l, 'rb').read()).decode()
    b2 = base64.b64encode(open(c, 'rb').read()).decode()
    msgs = [{"role": "user", "content": [
        {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b1}"}},
        {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b2}"}},
        {"type": "text", "text": Q}]}]
    try:
        r = requests.post(f"{BASE}/chat/completions",
                          headers={"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"},
                          json={"model": MODEL, "messages": msgs, "max_tokens": 600}, timeout=200).json()
        txt = (r.get('choices') or [{}])[0].get('message', {}).get('content') or 'ERR'
    except Exception as e:
        txt = f'EXC {e}'
    return f"===== {name}\n{txt.strip()}\n"


if __name__ == '__main__':
    names = sorted(f for f in os.listdir(os.path.join(BASE_DIR, 'color')) if f.endswith('.jpg'))
    with ThreadPoolExecutor(max_workers=5) as ex:
        for o in ex.map(pair, names):
            print(o, flush=True)
