mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
refactor: rename Tool.alpha to Tool.experimental
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||
import { writeFile, readFile, unlink } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { readFile, unlink, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
|
||||
|
||||
export interface RemoveBackgroundOptions {
|
||||
model?: string;
|
||||
@@ -23,11 +23,11 @@ export async function removeBackground(
|
||||
try {
|
||||
// BiRefNet models need longer timeout (up to 10 min for first load)
|
||||
const timeout = options.model?.startsWith("birefnet") ? 600000 : 300000;
|
||||
const { stdout } = await runPythonWithProgress("remove_bg.py", [
|
||||
inputPath,
|
||||
outputPath,
|
||||
JSON.stringify(options),
|
||||
], { onProgress, timeout });
|
||||
const { stdout } = await runPythonWithProgress(
|
||||
"remove_bg.py",
|
||||
[inputPath, outputPath, JSON.stringify(options)],
|
||||
{ onProgress, timeout },
|
||||
);
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
if (!result.success) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { resolve, dirname } from "node:path";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
@@ -7,8 +7,7 @@ const PYTHON_DIR = resolve(__dirname, "../python");
|
||||
|
||||
/** Try venv first, then system python. */
|
||||
function getPythonPath(): string {
|
||||
const venvPath =
|
||||
process.env.PYTHON_VENV_PATH || resolve(__dirname, "../../../.venv");
|
||||
const venvPath = process.env.PYTHON_VENV_PATH || resolve(__dirname, "../../../.venv");
|
||||
return `${venvPath}/bin/python3`;
|
||||
}
|
||||
|
||||
@@ -40,9 +39,7 @@ function extractPythonError(error: unknown): string {
|
||||
return String(error);
|
||||
}
|
||||
|
||||
export interface ProgressCallback {
|
||||
(percent: number, stage: string): void;
|
||||
}
|
||||
export type ProgressCallback = (percent: number, stage: string) => void;
|
||||
|
||||
/**
|
||||
* Run a Python script with real-time progress streaming via stderr.
|
||||
@@ -95,10 +92,7 @@ export function runPythonWithProgress(
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed);
|
||||
if (
|
||||
typeof parsed.progress === "number" &&
|
||||
typeof parsed.stage === "string"
|
||||
) {
|
||||
if (typeof parsed.progress === "number" && typeof parsed.stage === "string") {
|
||||
options.onProgress?.(parsed.progress, parsed.stage);
|
||||
continue;
|
||||
}
|
||||
@@ -147,4 +141,3 @@ export function runPythonWithProgress(
|
||||
trySpawn(getPythonPath(), false);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||
import { writeFile, readFile } from "node:fs/promises";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
|
||||
|
||||
export interface BlurFacesOptions {
|
||||
blurRadius?: number;
|
||||
@@ -30,11 +30,11 @@ export async function blurFaces(
|
||||
const outputPath = join(outputDir, "output_faces.png");
|
||||
|
||||
await writeFile(inputPath, inputBuffer);
|
||||
const { stdout } = await runPythonWithProgress("detect_faces.py", [
|
||||
inputPath,
|
||||
outputPath,
|
||||
JSON.stringify(options),
|
||||
], { onProgress });
|
||||
const { stdout } = await runPythonWithProgress(
|
||||
"detect_faces.py",
|
||||
[inputPath, outputPath, JSON.stringify(options)],
|
||||
{ onProgress },
|
||||
);
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
if (!result.success) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export { removeBackground } from "./background-removal.js";
|
||||
export { upscale } from "./upscaling.js";
|
||||
export { extractText } from "./ocr.js";
|
||||
export { blurFaces } from "./face-detection.js";
|
||||
export { inpaint } from "./inpainting.js";
|
||||
export { extractText } from "./ocr.js";
|
||||
export { upscale } from "./upscaling.js";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||
import { writeFile, readFile } from "node:fs/promises";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
|
||||
|
||||
export async function inpaint(
|
||||
inputBuffer: Buffer,
|
||||
@@ -15,11 +15,9 @@ export async function inpaint(
|
||||
await writeFile(inputPath, inputBuffer);
|
||||
await writeFile(maskPath, maskBuffer);
|
||||
|
||||
const { stdout } = await runPythonWithProgress("inpaint.py", [
|
||||
inputPath,
|
||||
maskPath,
|
||||
outputPath,
|
||||
], { onProgress });
|
||||
const { stdout } = await runPythonWithProgress("inpaint.py", [inputPath, maskPath, outputPath], {
|
||||
onProgress,
|
||||
});
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
if (!result.success) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
|
||||
|
||||
export interface OcrOptions {
|
||||
engine?: "tesseract" | "paddleocr";
|
||||
@@ -21,10 +21,9 @@ export async function extractText(
|
||||
const inputPath = join(outputDir, "input_ocr.png");
|
||||
|
||||
await writeFile(inputPath, inputBuffer);
|
||||
const { stdout } = await runPythonWithProgress("ocr.py", [
|
||||
inputPath,
|
||||
JSON.stringify(options),
|
||||
], { onProgress });
|
||||
const { stdout } = await runPythonWithProgress("ocr.py", [inputPath, JSON.stringify(options)], {
|
||||
onProgress,
|
||||
});
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
if (!result.success) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||
import { writeFile, readFile } from "node:fs/promises";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
|
||||
|
||||
export interface UpscaleOptions {
|
||||
scale?: number;
|
||||
@@ -23,11 +23,11 @@ export async function upscale(
|
||||
const outputPath = join(outputDir, "output_upscale.png");
|
||||
|
||||
await writeFile(inputPath, inputBuffer);
|
||||
const { stdout } = await runPythonWithProgress("upscale.py", [
|
||||
inputPath,
|
||||
outputPath,
|
||||
JSON.stringify(options),
|
||||
], { onProgress });
|
||||
const { stdout } = await runPythonWithProgress(
|
||||
"upscale.py",
|
||||
[inputPath, outputPath, JSON.stringify(options)],
|
||||
{ onProgress },
|
||||
);
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
if (!result.success) {
|
||||
|
||||
Reference in New Issue
Block a user