Skip to content

AI media harness for planning, generating, and editing locally

TypeScript
4.5 KB
Raw Blame
import { invoke } from "@tauri-apps/api/core";
import { isTauri } from "./persistence";
import { selectedWorker } from "./workers";

export type Upscaler = "none" | "realesrgan" | "seedvr2";
export const UPSCALERS = [
  { value: "none", label: "None" },
  { value: "realesrgan", label: "Real-ESRGAN" },
  { value: "seedvr2", label: "SeedVR2" },
] as const;
export const LATENT_UPSCALER_URL = "https://huggingface.co/LBH-123-AI/Minimax_h3_latent_Upscaler/resolve/main/minimax_h3_latent_upscaler_3d_conv_v1/minimax_h3_latent_upscaler_3d_conv_v1_fp16.safetensors";
export const TAEH3_URL = "https://raw.githubusercontent.com/madebyollin/taehv/62f7591f59dfbb4c3c02b7a621d180a9eeaba26c/safetensors/taeh3.safetensors";
export const OTHER_WEIGHT_TEMPLATES = [
  { id: "realesrgan", name: "Real-ESRGAN", files: [
    { id: "model", name: "RealESRGAN x4plus", url: "https://huggingface.co/Comfy-Org/Real-ESRGAN_repackaged/resolve/main/RealESRGAN_x4plus.safetensors" },
  ] },
  { id: "seedvr2", name: "SeedVR2", files: [
    { id: "model", name: "SeedVR2 3B FP16", url: "https://huggingface.co/Comfy-Org/SeedVR2/resolve/main/diffusion_models/seedvr2_3b_fp16.safetensors" },
    { id: "vae", name: "SeedVR2 VAE", url: "https://huggingface.co/Comfy-Org/SeedVR2/resolve/main/vae/seedvr2_ema_vae_fp16.safetensors" },
  ] },
  { id: "latent-upscale", name: "Latent upscale", files: [
    { id: "model", name: "MiniMax H3 latent upscaler", url: LATENT_UPSCALER_URL },
  ] },
  { id: "taeh3", name: "TAEH3 generation previews", files: [
    { id: "model", name: "TAEH3 preview VAE", url: TAEH3_URL },
  ] },
] as const;
const KEY = "slopus.other-weights.v1";
const LOCAL_KEY = "slopus.other-weights-local.v1";
const EVENT = "slopus:other-weights";
function readPaths(key: string): Record<string, string> {
  try {
    const value = JSON.parse(localStorage.getItem(key) ?? "{}");
    return Object.fromEntries(Object.entries(value).filter(([, path]) => typeof path === "string" && path && !/^https?:/i.test(path))) as Record<string, string>;
  } catch { return {}; }
}
export const localOtherWeightPaths = () => readPaths(LOCAL_KEY);
export const otherWeightPaths = () => ({ ...readPaths(KEY), ...localOtherWeightPaths() });
export function saveLocalOtherWeightPaths(paths: Record<string, string>) {
  const saved = localOtherWeightPaths();
  for (const [url, path] of Object.entries(paths)) {
    if (path.trim()) saved[url] = path.trim();
    else delete saved[url];
  }
  localStorage.setItem(LOCAL_KEY, JSON.stringify(saved));
  window.dispatchEvent(new Event(EVENT));
}
const savePaths = (paths: Record<string, string>) => {
  localStorage.setItem(KEY, JSON.stringify(paths));
  window.dispatchEvent(new Event(EVENT));
};
export const saveOtherWeightPath = (url: string, path: string) => savePaths({ ...readPaths(KEY), [url]: path });
export function subscribeOtherWeights(listener: () => void) {
  window.addEventListener(EVENT, listener);
  window.addEventListener("storage", listener);
  return () => { window.removeEventListener(EVENT, listener); window.removeEventListener("storage", listener); };
}
export interface UpscaleConfig { method: Exclude<Upscaler, "none">; modelPath: string; vaePath?: string }
export function upscaleConfig(method: Upscaler): UpscaleConfig | undefined {
  if (method === "none") return undefined;
  if (method === "seedvr2" && selectedWorker()) {
    const local = localOtherWeightPaths();
    return { method, modelPath: local[OTHER_WEIGHT_TEMPLATES[1].files[0].url] ?? "",
      vaePath: local[OTHER_WEIGHT_TEMPLATES[1].files[1].url] };
  }
  const template = OTHER_WEIGHT_TEMPLATES.find((item) => item.id === method)!;
  const paths = otherWeightPaths();
  if (template.files.some((file) => !paths[file.url])) throw new Error(`Download ${template.name} or choose a local weight file in Settings → Generator → Other weights first.`);
  return { method, modelPath: paths[template.files[0].url],
    ...(template.id === "seedvr2" ? { vaePath: paths[template.files[1].url] } : {}) };
}
/** Resolve on this machine, or let a worker download the standard checkpoint. */
export function latentUpscalerPath(): string | undefined {
  return selectedWorker() ? localOtherWeightPaths()[LATENT_UPSCALER_URL] ?? LATENT_UPSCALER_URL
    : otherWeightPaths()[LATENT_UPSCALER_URL];
}
export async function refreshOtherWeights() {
  if (!isTauri()) return;
  const urls = OTHER_WEIGHT_TEMPLATES.flatMap((item) => item.files.map((file) => file.url));
  const found = await invoke<Record<string, string>>("find_downloaded_weights", { urls });
  if (found) savePaths(found);
}