for first push
This commit is contained in:
196
services/voiceprint/src/embedding/EmbeddingService.ts
Normal file
196
services/voiceprint/src/embedding/EmbeddingService.ts
Normal file
@@ -0,0 +1,196 @@
|
||||
import { spawn } from "child_process";
|
||||
|
||||
const EMBEDDING_DIM = 192;
|
||||
const MODEL_VERSION = "ecapa-tdnn-0.1.0-mock";
|
||||
|
||||
export class EmbeddingService {
|
||||
private mlServiceUrl: string;
|
||||
|
||||
constructor() {
|
||||
this.mlServiceUrl = process.env.VOICEPRINT_ML_URL || "http://localhost:8001";
|
||||
}
|
||||
|
||||
async extract(audioBuffer: Buffer): Promise<EmbeddingOutput> {
|
||||
const mlAvailable = await this.checkMLService();
|
||||
|
||||
if (mlAvailable) {
|
||||
return this.extractViaML(audioBuffer);
|
||||
}
|
||||
|
||||
return this.extractMock(audioBuffer);
|
||||
}
|
||||
|
||||
async classify(embedding: number[]): Promise<number> {
|
||||
const mlAvailable = await this.checkMLService();
|
||||
|
||||
if (mlAvailable) {
|
||||
return this.classifyViaML(embedding);
|
||||
}
|
||||
|
||||
return this.classifyMock(embedding);
|
||||
}
|
||||
|
||||
getModelVersion(): string {
|
||||
return MODEL_VERSION;
|
||||
}
|
||||
|
||||
private async extractViaML(audioBuffer: Buffer): Promise<EmbeddingOutput> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const jsonInput = audioBuffer.toString("base64");
|
||||
const proc = spawn("python3", [
|
||||
"-c",
|
||||
`
|
||||
import urllib.request, json, sys
|
||||
req = urllib.request.Request(
|
||||
"${this.mlServiceUrl}/embedding",
|
||||
data=json.dumps({"audio": "${jsonInput.substring(0, 5000)}"}).encode(),
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=60) as resp:
|
||||
data = json.loads(resp.read())
|
||||
sys.stdout.write(json.dumps({"ok": True, "vector": data.get("embedding", []), "dim": data.get("dimension", ${EMBEDDING_DIM})}))
|
||||
except Exception as e:
|
||||
sys.stdout.write(json.dumps({"ok": False, "error": str(e)}))
|
||||
`,
|
||||
]);
|
||||
|
||||
let output = "";
|
||||
proc.stdout.on("data", (chunk) => { output += chunk.toString(); });
|
||||
proc.on("close", (code) => {
|
||||
try {
|
||||
const result = JSON.parse(output);
|
||||
if (result.ok && result.vector.length === EMBEDDING_DIM) {
|
||||
resolve({ vector: result.vector, dimension: EMBEDDING_DIM });
|
||||
} else {
|
||||
resolve(this.generateMockFromBuffer(audioBuffer));
|
||||
}
|
||||
} catch {
|
||||
resolve(this.generateMockFromBuffer(audioBuffer));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private async classifyViaML(embedding: number[]): Promise<number> {
|
||||
return new Promise((resolve) => {
|
||||
const proc = spawn("python3", [
|
||||
"-c",
|
||||
`
|
||||
import urllib.request, json, sys
|
||||
req = urllib.request.Request(
|
||||
"${this.mlServiceUrl}/classify",
|
||||
data=json.dumps({"embedding": ${JSON.stringify(embedding)}}).encode(),
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as resp:
|
||||
data = json.loads(resp.read())
|
||||
sys.stdout.write(json.dumps({"score": data.get("synthetic_score", 0.5)}))
|
||||
except:
|
||||
sys.stdout.write(json.dumps({"score": 0.5}))
|
||||
`,
|
||||
]);
|
||||
|
||||
let output = "";
|
||||
proc.stdout.on("data", (chunk) => { output += chunk.toString(); });
|
||||
proc.on("close", () => {
|
||||
try {
|
||||
const result = JSON.parse(output);
|
||||
resolve(result.score || 0.5);
|
||||
} catch {
|
||||
resolve(0.5);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private async extractMock(audioBuffer: Buffer): Promise<EmbeddingOutput> {
|
||||
return this.generateMockFromBuffer(audioBuffer);
|
||||
}
|
||||
|
||||
private async classifyMock(embedding: number[]): Promise<number> {
|
||||
const mean = embedding.reduce((s, v) => s + v, 0) / embedding.length;
|
||||
const variance = embedding.reduce((s, v) => s + (v - mean) ** 2, 0) / embedding.length;
|
||||
const stdDev = Math.sqrt(variance);
|
||||
|
||||
const syntheticIndicators = [
|
||||
stdDev < 0.1 ? 0.8 : 0.2,
|
||||
Math.abs(mean) > 0.5 ? 0.7 : 0.3,
|
||||
this.hasArtifacts(embedding) ? 0.9 : 0.1,
|
||||
];
|
||||
|
||||
return syntheticIndicators.reduce((s, v) => s + v, 0) / syntheticIndicators.length;
|
||||
}
|
||||
|
||||
private generateMockFromBuffer(audioBuffer: Buffer): EmbeddingOutput {
|
||||
const seed = this.computeSeed(audioBuffer);
|
||||
const rng = this.createRNG(seed);
|
||||
const vector: number[] = [];
|
||||
|
||||
for (let i = 0; i < EMBEDDING_DIM; i++) {
|
||||
const u1 = rng();
|
||||
const u2 = rng();
|
||||
const gauss = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
|
||||
vector.push(parseFloat(gauss.toFixed(6)));
|
||||
}
|
||||
|
||||
const norm = Math.sqrt(vector.reduce((s, v) => s + v * v, 0));
|
||||
const normalized = vector.map((v) => parseFloat((v / norm).toFixed(6)));
|
||||
|
||||
return { vector: normalized, dimension: EMBEDDING_DIM };
|
||||
}
|
||||
|
||||
private hasArtifacts(embedding: number[]): boolean {
|
||||
const window = 16;
|
||||
let artifactCount = 0;
|
||||
|
||||
for (let i = 0; i < embedding.length - window; i += window) {
|
||||
const slice = embedding.slice(i, i + window);
|
||||
const localMean = slice.reduce((s, v) => s + v, 0) / slice.length;
|
||||
const localVar = slice.reduce((s, v) => s + (v - localMean) ** 2, 0) / slice.length;
|
||||
|
||||
if (localVar < 0.001) artifactCount++;
|
||||
}
|
||||
|
||||
return artifactCount > embedding.length / window / 3;
|
||||
}
|
||||
|
||||
private async checkMLService(): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
const proc = spawn("python3", [
|
||||
"-c",
|
||||
`
|
||||
import urllib.request, sys
|
||||
try:
|
||||
urllib.request.urlopen("${this.mlServiceUrl}/health", timeout=2)
|
||||
sys.exit(0)
|
||||
except:
|
||||
sys.exit(1)
|
||||
`,
|
||||
]);
|
||||
proc.on("close", (code) => resolve(code === 0));
|
||||
});
|
||||
}
|
||||
|
||||
private computeSeed(buffer: Buffer): number {
|
||||
let hash = 0;
|
||||
const sampleSize = Math.min(buffer.length, 1024);
|
||||
for (let i = 0; i < sampleSize; i += 4) {
|
||||
hash = ((hash << 5) - hash + buffer.readInt32LE(i)) | 0;
|
||||
}
|
||||
return Math.abs(hash);
|
||||
}
|
||||
|
||||
private createRNG(seed: number): () => number {
|
||||
return () => {
|
||||
seed = (seed * 1664525 + 1013904223) & 0xffffffff;
|
||||
return (seed >>> 0) / 0xffffffff;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export interface EmbeddingOutput {
|
||||
vector: number[];
|
||||
dimension: number;
|
||||
}
|
||||
Reference in New Issue
Block a user