46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
import sys, re, shutil, time
|
|
|
|
if len(sys.argv) < 5:
|
|
print("Nutzung: auto_adopt.py <config_path> <rolle> <new_hf_id> <new_gguf_path> [new_mmproj_path]")
|
|
sys.exit(1)
|
|
|
|
config_path = sys.argv[1]
|
|
rolle = sys.argv[2]
|
|
new_hf_id = sys.argv[3]
|
|
new_gguf_path = sys.argv[4]
|
|
new_mmproj_path = sys.argv[5] if len(sys.argv) > 5 else ""
|
|
|
|
shutil.copy(config_path, config_path + f".bak.{time.strftime('%Y%m%d%H%M%S')}")
|
|
content = open(config_path, "r", encoding="utf-8").read()
|
|
|
|
blocks = re.split(r'\n ([a-zA-Z0-9_.-]+):\n', "\n" + content)
|
|
new_content = blocks[0].lstrip("\n")
|
|
adopted = False
|
|
|
|
for i in range(1, len(blocks), 2):
|
|
m_name = blocks[i]
|
|
m_content = blocks[i+1]
|
|
|
|
if re.search(r'aliases:\s*\n(?:\s+- [^\n]+\n)*\s+- ' + re.escape(rolle) + r'\b', m_content):
|
|
m_content = re.sub(r'(-m\s+)[^\s]+', r'\g<1>' + new_gguf_path, m_content)
|
|
if new_mmproj_path:
|
|
if "--mmproj" in m_content:
|
|
m_content = re.sub(r'(--mmproj\s+)[^\s]+', r'\g<1>' + new_mmproj_path, m_content)
|
|
else:
|
|
m_content = re.sub(r'(-m\s+[^\s]+)', r'\g<1> --mmproj ' + new_mmproj_path, m_content)
|
|
else:
|
|
m_content = re.sub(r'\s*--mmproj\s+[^\s]+', '', m_content)
|
|
|
|
adopted = True
|
|
print(f"Auto-Adoption: {rolle} -> {new_hf_id} (ersetzt in {m_name})")
|
|
|
|
new_content += f" {m_name}:\n{m_content}"
|
|
|
|
with open(config_path, "w", encoding="utf-8") as f:
|
|
f.write(new_content)
|
|
|
|
if not adopted:
|
|
print(f"Warnung: Rolle {rolle} in der config nicht gefunden. Auto-Adoption übersprungen.")
|
|
sys.exit(1)
|