perf(backend): 必要になるまでnsfwjsを読み込まないように
This commit is contained in:
parent
18caa20969
commit
1590a73d3d
|
|
@ -7,11 +7,11 @@ import * as fs from 'node:fs';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import * as nsfw from 'nsfwjs';
|
|
||||||
import si from 'systeminformation';
|
import si from 'systeminformation';
|
||||||
import { Mutex } from 'async-mutex';
|
import { Mutex } from 'async-mutex';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
|
import type { NSFWJS, PredictionType } from 'nsfwjs';
|
||||||
|
|
||||||
const _filename = fileURLToPath(import.meta.url);
|
const _filename = fileURLToPath(import.meta.url);
|
||||||
const _dirname = dirname(_filename);
|
const _dirname = dirname(_filename);
|
||||||
|
|
@ -21,7 +21,7 @@ let isSupportedCpu: undefined | boolean = undefined;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AiService {
|
export class AiService {
|
||||||
private model: nsfw.NSFWJS;
|
private model: NSFWJS;
|
||||||
private modelLoadMutex: Mutex = new Mutex();
|
private modelLoadMutex: Mutex = new Mutex();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
|
|
@ -29,7 +29,7 @@ export class AiService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async detectSensitive(source: string | Buffer): Promise<nsfw.PredictionType[] | null> {
|
public async detectSensitive(source: string | Buffer): Promise<PredictionType[] | null> {
|
||||||
try {
|
try {
|
||||||
if (isSupportedCpu === undefined) {
|
if (isSupportedCpu === undefined) {
|
||||||
isSupportedCpu = await this.computeIsSupportedCpu();
|
isSupportedCpu = await this.computeIsSupportedCpu();
|
||||||
|
|
@ -44,6 +44,7 @@ export class AiService {
|
||||||
tf.env().global.fetch = fetch;
|
tf.env().global.fetch = fetch;
|
||||||
|
|
||||||
if (this.model == null) {
|
if (this.model == null) {
|
||||||
|
const nsfw = await import('nsfwjs');
|
||||||
await this.modelLoadMutex.runExclusive(async () => {
|
await this.modelLoadMutex.runExclusive(async () => {
|
||||||
if (this.model == null) {
|
if (this.model == null) {
|
||||||
this.model = await nsfw.load(`file://${_dirname}/../../nsfw-model/`, { size: 299 });
|
this.model = await nsfw.load(`file://${_dirname}/../../nsfw-model/`, { size: 299 });
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue