# SPDX-License-Identifier: Apache-2.0 # Copyright 2025-2026 Raykosan (RaykoStudio) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import folder_paths import json import os import struct import hashlib import re import asyncio import urllib.request import urllib.error from server import PromptServer from datetime import datetime import aiohttp CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) PRESETS_DIR = os.path.join(CURRENT_DIR, "preset_models") LORA_PRESETS_DIR = os.path.join(PRESETS_DIR, "loras") RAYKO_LORA_DATA_DIR = os.path.join(CURRENT_DIR, "rayko_lora_data") os.makedirs(PRESETS_DIR, exist_ok=True) os.makedirs(LORA_PRESETS_DIR, exist_ok=True) os.makedirs(RAYKO_LORA_DATA_DIR, exist_ok=True) def clean_html(text): if not text: return "" clean = re.sub(r'<[^>]+>', ' ', str(text)) clean = re.sub(r'\s+', ' ', clean).strip() return clean def compute_sha256(file_path): sha256_hash = hashlib.sha256() with open(file_path, "rb") as f: for chunk in iter(lambda: f.read(8192), b""): sha256_hash.update(chunk) return sha256_hash.hexdigest() def _http_get_json(url, headers=None, timeout=30): req = urllib.request.Request(url, headers=headers or {}) opener = urllib.request.build_opener() try: with opener.open(req, timeout=timeout) as resp: raw = resp.read().decode("utf-8", errors="replace") try: return resp.status, json.loads(raw) except json.JSONDecodeError: return resp.status, None except urllib.error.HTTPError as e: return e.code, None def save_to_rayko_db(file_hash, data, source, overwrite=False): db_path = os.path.join(RAYKO_LORA_DATA_DIR, f"{file_hash}.json") if not overwrite and os.path.exists(db_path): return False db_data = { "hash": file_hash, "name": data.get("full_name", ""), "trained_words": data.get("trained_words", []), "description": data.get("description", ""), "source": source, "imported_at": datetime.now().isoformat() } with open(db_path, 'w', encoding='utf-8') as f: json.dump(db_data, f, indent=2, ensure_ascii=False) return True def extract_metadata_from_safetensors(lora_full_path): try: with open(lora_full_path, "rb") as f: header_size = struct.unpack("