Upload 7 files
Browse files- app/api/image-to-text/route.ts +11 -7
- app/api/speech-to-audio/route.ts +12 -8
- app/api/text-to-speech-lang/route.ts +10 -6
- app/api/text-to-speech/route.ts +14 -9
- app/api/translator/route.ts +11 -7
app/api/image-to-text/route.ts
CHANGED
|
@@ -3,11 +3,15 @@ import { NextRequest, NextResponse } from "next/server";
|
|
| 3 |
env.cacheDir = "./.cache";
|
| 4 |
|
| 5 |
export const POST = async (request: NextRequest) => {
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
};
|
|
|
|
| 3 |
env.cacheDir = "./.cache";
|
| 4 |
|
| 5 |
export const POST = async (request: NextRequest) => {
|
| 6 |
+
try {
|
| 7 |
+
const { url } = await request.json();
|
| 8 |
+
const captioner = await pipeline(
|
| 9 |
+
"image-to-text",
|
| 10 |
+
"Xenova/vit-gpt2-image-captioning",
|
| 11 |
+
);
|
| 12 |
+
const output = await captioner(url);
|
| 13 |
+
return NextResponse.json(output);
|
| 14 |
+
}catch(error){
|
| 15 |
+
console.error(error)
|
| 16 |
+
}
|
| 17 |
};
|
app/api/speech-to-audio/route.ts
CHANGED
|
@@ -5,12 +5,16 @@ import path from "path";
|
|
| 5 |
|
| 6 |
const wav = new WaveFile();
|
| 7 |
export const POST = async (request: NextRequest) => {
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
};
|
|
|
|
| 5 |
|
| 6 |
const wav = new WaveFile();
|
| 7 |
export const POST = async (request: NextRequest) => {
|
| 8 |
+
try{
|
| 9 |
+
const { audio, sampling_rate, name } = await request.json();
|
| 10 |
+
console.log(Object.values(audio));
|
| 11 |
+
wav.fromScratch(1, sampling_rate, "32f", Object.values(audio));
|
| 12 |
+
fs.writeFileSync(
|
| 13 |
+
path.join(process.cwd() + `/public/${name}.wav`),
|
| 14 |
+
wav.toBuffer(),
|
| 15 |
+
);
|
| 16 |
+
return NextResponse.json({ audio: `/${name}.wav` });
|
| 17 |
+
}catch (e) {
|
| 18 |
+
console.error(e)
|
| 19 |
+
}
|
| 20 |
};
|
app/api/text-to-speech-lang/route.ts
CHANGED
|
@@ -4,10 +4,14 @@ import { NextRequest, NextResponse } from "next/server";
|
|
| 4 |
env.cacheDir = "./.cache";
|
| 5 |
|
| 6 |
export const POST = async (request: NextRequest) => {
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
};
|
|
|
|
| 4 |
env.cacheDir = "./.cache";
|
| 5 |
|
| 6 |
export const POST = async (request: NextRequest) => {
|
| 7 |
+
try{
|
| 8 |
+
const { text, modal } = await request.json();
|
| 9 |
+
const synthesizer = await pipeline("text-to-speech", modal);
|
| 10 |
+
const speaker_embeddings =
|
| 11 |
+
"https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/v3.0.0/speaker_embeddings.bin";
|
| 12 |
+
const out = await synthesizer(text, { speaker_embeddings });
|
| 13 |
+
return NextResponse.json(out);
|
| 14 |
+
}catch (e) {
|
| 15 |
+
console.error(e)
|
| 16 |
+
}
|
| 17 |
};
|
app/api/text-to-speech/route.ts
CHANGED
|
@@ -4,13 +4,18 @@ import { NextRequest, NextResponse } from "next/server";
|
|
| 4 |
env.cacheDir = "./.cache";
|
| 5 |
|
| 6 |
export const POST = async (request: NextRequest) => {
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
};
|
|
|
|
| 4 |
env.cacheDir = "./.cache";
|
| 5 |
|
| 6 |
export const POST = async (request: NextRequest) => {
|
| 7 |
+
try{
|
| 8 |
+
const { text } = await request.json();
|
| 9 |
+
console.log("the text", text);
|
| 10 |
+
const synthesizer = await pipeline("text-to-speech", "Xenova/speecht5_tts");
|
| 11 |
+
const speaker_embeddings =
|
| 12 |
+
"https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin";
|
| 13 |
+
const out = await synthesizer(text, {
|
| 14 |
+
speaker_embeddings,
|
| 15 |
+
});
|
| 16 |
+
return NextResponse.json(out);
|
| 17 |
+
}catch (e) {
|
| 18 |
+
console.error(e)
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
};
|
app/api/translator/route.ts
CHANGED
|
@@ -6,11 +6,15 @@ env.allowLocalModels=false;
|
|
| 6 |
env.cacheDir = "./.cache";
|
| 7 |
|
| 8 |
export const POST = async (request: NextRequest) => {
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
};
|
|
|
|
| 6 |
env.cacheDir = "./.cache";
|
| 7 |
|
| 8 |
export const POST = async (request: NextRequest) => {
|
| 9 |
+
try{
|
| 10 |
+
const { text, code } = await request.json();
|
| 11 |
+
const translator = await pipeline("translation", "Xenova/m2m100_418M");
|
| 12 |
+
const out = await translator(text, {
|
| 13 |
+
src_lang: "en",
|
| 14 |
+
tgt_lang: code,
|
| 15 |
+
} as never);
|
| 16 |
+
return NextResponse.json(out);
|
| 17 |
+
}catch(e){
|
| 18 |
+
console.error(e)
|
| 19 |
+
}
|
| 20 |
};
|