Files
dj/model-bridge/label_gpu_ports.py

28 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Label the verified ComfyUI worker ports; change no routing settings."""
import datetime,json,re,sqlite3,urllib.request
from pathlib import Path
from urllib.parse import urlsplit
root=Path(__file__).resolve().parent.parent
base=json.loads((root/'huobao-data/last-launch.json').read_text())['api']
def api(path,body=None):
req=urllib.request.Request(base+path,data=json.dumps(body).encode() if body is not None else None,
headers={'Content-Type':'application/json'},method='PUT' if body is not None else 'GET')
with urllib.request.urlopen(req,timeout=15) as r:return json.load(r).get('data')
rows=api('/ai-configs')
targets=[r for r in rows if r['service_type'] in ('image','video') and urlsplit(r['base_url']).hostname=='192.168.110.111']
assert len(targets)==24
backup=root/'huobao-data/backups'/('before-comfy-port-labels-'+datetime.datetime.now().strftime('%Y%m%d-%H%M%S')+'.sqlite3')
with sqlite3.connect(root/'huobao-data/huobao.sqlite3') as src,sqlite3.connect(backup) as dst:src.backup(dst)
for row in targets:
port=urlsplit(row['base_url']).port
assert port in (18792,18794)
gpu=row.get('gpu','auto')
label='ComfyUI '+({'gpu1':'8189','gpu2':'8188','gpu3':'8191'}.get(gpu,'8188/8189/8191'))
name=re.sub(r' · (?:端口 \d+|ComfyUI [\d/]+(?:([^)]*))?)$','',row['name'])+' · '+label
if name!=row['name']:api('/ai-configs/'+str(row['id']),{'name':name})
after={r['id']:r for r in api('/ai-configs')}
for row in rows:
assert all(after[row['id']][k]==v for k,v in row.items() if k not in ('name','updated_at'))
if row not in targets:assert after[row['id']]['name']==row['name']
print(json.dumps({'labelled':len(targets),'comfyui_ports':{'gpu1':8189,'gpu2':8188,'gpu3':8191},'other_fields_unchanged':True,'backup':str(backup)}))