mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat(ai): add onProgress callback to all AI wrapper functions
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { runPythonScript } from "./bridge.js";
|
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||||
import { writeFile, readFile, unlink } from "node:fs/promises";
|
import { writeFile, readFile, unlink } from "node:fs/promises";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
@@ -13,6 +13,7 @@ export async function removeBackground(
|
|||||||
inputBuffer: Buffer,
|
inputBuffer: Buffer,
|
||||||
outputDir: string,
|
outputDir: string,
|
||||||
options: RemoveBackgroundOptions = {},
|
options: RemoveBackgroundOptions = {},
|
||||||
|
onProgress?: ProgressCallback,
|
||||||
): Promise<Buffer> {
|
): Promise<Buffer> {
|
||||||
const id = randomUUID();
|
const id = randomUUID();
|
||||||
const inputPath = join(tmpdir(), `rembg_in_${id}.png`);
|
const inputPath = join(tmpdir(), `rembg_in_${id}.png`);
|
||||||
@@ -22,11 +23,11 @@ export async function removeBackground(
|
|||||||
try {
|
try {
|
||||||
// BiRefNet models need longer timeout (up to 10 min for first load)
|
// BiRefNet models need longer timeout (up to 10 min for first load)
|
||||||
const timeout = options.model?.startsWith("birefnet") ? 600000 : 300000;
|
const timeout = options.model?.startsWith("birefnet") ? 600000 : 300000;
|
||||||
const { stdout } = await runPythonScript("remove_bg.py", [
|
const { stdout } = await runPythonWithProgress("remove_bg.py", [
|
||||||
inputPath,
|
inputPath,
|
||||||
outputPath,
|
outputPath,
|
||||||
JSON.stringify(options),
|
JSON.stringify(options),
|
||||||
], timeout);
|
], { onProgress, timeout });
|
||||||
|
|
||||||
const result = JSON.parse(stdout);
|
const result = JSON.parse(stdout);
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { runPythonScript } from "./bridge.js";
|
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||||
import { writeFile, readFile } from "node:fs/promises";
|
import { writeFile, readFile } from "node:fs/promises";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
@@ -24,16 +24,17 @@ export async function blurFaces(
|
|||||||
inputBuffer: Buffer,
|
inputBuffer: Buffer,
|
||||||
outputDir: string,
|
outputDir: string,
|
||||||
options: BlurFacesOptions = {},
|
options: BlurFacesOptions = {},
|
||||||
|
onProgress?: ProgressCallback,
|
||||||
): Promise<BlurFacesResult> {
|
): Promise<BlurFacesResult> {
|
||||||
const inputPath = join(outputDir, "input_faces.png");
|
const inputPath = join(outputDir, "input_faces.png");
|
||||||
const outputPath = join(outputDir, "output_faces.png");
|
const outputPath = join(outputDir, "output_faces.png");
|
||||||
|
|
||||||
await writeFile(inputPath, inputBuffer);
|
await writeFile(inputPath, inputBuffer);
|
||||||
const { stdout } = await runPythonScript("detect_faces.py", [
|
const { stdout } = await runPythonWithProgress("detect_faces.py", [
|
||||||
inputPath,
|
inputPath,
|
||||||
outputPath,
|
outputPath,
|
||||||
JSON.stringify(options),
|
JSON.stringify(options),
|
||||||
]);
|
], { onProgress });
|
||||||
|
|
||||||
const result = JSON.parse(stdout);
|
const result = JSON.parse(stdout);
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
export { runPythonScript } from "./bridge.js";
|
export { runPythonScript } from "./bridge.js";
|
||||||
|
export { runPythonWithProgress } from "./bridge.js";
|
||||||
|
export type { ProgressCallback } from "./bridge.js";
|
||||||
export { removeBackground } from "./background-removal.js";
|
export { removeBackground } from "./background-removal.js";
|
||||||
export type { RemoveBackgroundOptions } from "./background-removal.js";
|
export type { RemoveBackgroundOptions } from "./background-removal.js";
|
||||||
export { upscale } from "./upscaling.js";
|
export { upscale } from "./upscaling.js";
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { runPythonScript } from "./bridge.js";
|
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||||
import { writeFile, readFile } from "node:fs/promises";
|
import { writeFile, readFile } from "node:fs/promises";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
@@ -6,6 +6,7 @@ export async function inpaint(
|
|||||||
inputBuffer: Buffer,
|
inputBuffer: Buffer,
|
||||||
maskBuffer: Buffer,
|
maskBuffer: Buffer,
|
||||||
outputDir: string,
|
outputDir: string,
|
||||||
|
onProgress?: ProgressCallback,
|
||||||
): Promise<Buffer> {
|
): Promise<Buffer> {
|
||||||
const inputPath = join(outputDir, "input_inpaint.png");
|
const inputPath = join(outputDir, "input_inpaint.png");
|
||||||
const maskPath = join(outputDir, "mask_inpaint.png");
|
const maskPath = join(outputDir, "mask_inpaint.png");
|
||||||
@@ -14,11 +15,11 @@ export async function inpaint(
|
|||||||
await writeFile(inputPath, inputBuffer);
|
await writeFile(inputPath, inputBuffer);
|
||||||
await writeFile(maskPath, maskBuffer);
|
await writeFile(maskPath, maskBuffer);
|
||||||
|
|
||||||
const { stdout } = await runPythonScript("inpaint.py", [
|
const { stdout } = await runPythonWithProgress("inpaint.py", [
|
||||||
inputPath,
|
inputPath,
|
||||||
maskPath,
|
maskPath,
|
||||||
outputPath,
|
outputPath,
|
||||||
]);
|
], { onProgress });
|
||||||
|
|
||||||
const result = JSON.parse(stdout);
|
const result = JSON.parse(stdout);
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { runPythonScript } from "./bridge.js";
|
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||||
import { writeFile } from "node:fs/promises";
|
import { writeFile } from "node:fs/promises";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
@@ -16,14 +16,15 @@ export async function extractText(
|
|||||||
inputBuffer: Buffer,
|
inputBuffer: Buffer,
|
||||||
outputDir: string,
|
outputDir: string,
|
||||||
options: OcrOptions = {},
|
options: OcrOptions = {},
|
||||||
|
onProgress?: ProgressCallback,
|
||||||
): Promise<OcrResult> {
|
): Promise<OcrResult> {
|
||||||
const inputPath = join(outputDir, "input_ocr.png");
|
const inputPath = join(outputDir, "input_ocr.png");
|
||||||
|
|
||||||
await writeFile(inputPath, inputBuffer);
|
await writeFile(inputPath, inputBuffer);
|
||||||
const { stdout } = await runPythonScript("ocr.py", [
|
const { stdout } = await runPythonWithProgress("ocr.py", [
|
||||||
inputPath,
|
inputPath,
|
||||||
JSON.stringify(options),
|
JSON.stringify(options),
|
||||||
]);
|
], { onProgress });
|
||||||
|
|
||||||
const result = JSON.parse(stdout);
|
const result = JSON.parse(stdout);
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { runPythonScript } from "./bridge.js";
|
import { runPythonWithProgress, type ProgressCallback } from "./bridge.js";
|
||||||
import { writeFile, readFile } from "node:fs/promises";
|
import { writeFile, readFile } from "node:fs/promises";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
@@ -17,16 +17,17 @@ export async function upscale(
|
|||||||
inputBuffer: Buffer,
|
inputBuffer: Buffer,
|
||||||
outputDir: string,
|
outputDir: string,
|
||||||
options: UpscaleOptions = {},
|
options: UpscaleOptions = {},
|
||||||
|
onProgress?: ProgressCallback,
|
||||||
): Promise<UpscaleResult> {
|
): Promise<UpscaleResult> {
|
||||||
const inputPath = join(outputDir, "input_upscale.png");
|
const inputPath = join(outputDir, "input_upscale.png");
|
||||||
const outputPath = join(outputDir, "output_upscale.png");
|
const outputPath = join(outputDir, "output_upscale.png");
|
||||||
|
|
||||||
await writeFile(inputPath, inputBuffer);
|
await writeFile(inputPath, inputBuffer);
|
||||||
const { stdout } = await runPythonScript("upscale.py", [
|
const { stdout } = await runPythonWithProgress("upscale.py", [
|
||||||
inputPath,
|
inputPath,
|
||||||
outputPath,
|
outputPath,
|
||||||
JSON.stringify(options),
|
JSON.stringify(options),
|
||||||
]);
|
], { onProgress });
|
||||||
|
|
||||||
const result = JSON.parse(stdout);
|
const result = JSON.parse(stdout);
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
|||||||
Reference in New Issue
Block a user