Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
Merge branch 'main' into online
Browse files Browse the repository at this point in the history
  • Loading branch information
SIPC committed Feb 9, 2024
2 parents 334fc30 + cb3538e commit 427c9b6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
24 changes: 18 additions & 6 deletions src/pages/api/lib/autodetect.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
// code from sipc

import axios from "axios";
const { detectOne } = require("langdetect");

const detectionMap: Record<string, string> = {
"zh-cn": "zh",
"zh-tw": "zh",
};

export function autodetect(content: string): string {
export async function autodetect(content: string): Promise<string> {
try {
const lang = detectOne(content);
return detectionMap[lang] || lang;
} catch {
return "auto";
let data = JSON.stringify({"text": content})
const langResponse = await axios.post(`https://api.translatedlabs.com/language-identifier/identify`,data);
const detectedLang = langResponse.data.code.substring(0, 2).toLowerCase();
return detectedLang;
} catch (axiosError) {
console.error("Axios Error:", axiosError);
try {
return localDetect(content);
} catch (langdetectError) {
console.error("Langdetect Error:", langdetectError);
return "auto";
}
}
}
function localDetect(content: string): string {
const detectedLang = detectOne(content);
return detectionMap[detectedLang] || detectedLang || "auto";
}
17 changes: 15 additions & 2 deletions src/pages/api/lib/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@ export class Gemini {
},
],
});
const response = await axios.post(`${this.apiUrl}/v1beta/models/gemini-pro:generateContent?key=${this.key}`, data, { headers },);
return response.data.candidates[0].content.parts[0].text;
const response = await axios.post(`${this.apiUrl}/v1beta/models/gemini-pro:generateContent?key=${this.key}`, data, { headers });
if (response.data.candidates && response.data.candidates[0].content) {
return response.data.candidates[0].content.parts[0].text;
} else if (response.data.promptFeedback && response.data.promptFeedback.blockReason) {
if ( response.data.promptFeedback.blockReason == 'SAFETY') {
return 'Request intercepted.'
}
} else if (response.data.candidates && response.data.candidates[0].finishReason) {
if ( response.data.candidates[0].finishReason == 'SAFETY') {
return 'Request intercepted.'
}
} else {
throw new Error("No translation result, no block reason, and no finish reason available");
}
} catch (error) {
console.log(JSON.stringify(error));
throw new Error(`Error while translating: ${getErrorMessage(error)}`);
}
}

}

export const GeminiInstance = new Gemini(
Expand Down
16 changes: 8 additions & 8 deletions src/pages/api/translate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
import { ChatGPT, ChatGPTInstance } from "./lib/chatgpt";
import { DeepLX, DeeplXInstance } from "./lib/deeplx";
import { Microsoft, MicrosoftInstance } from "./lib/microsoft";
import { Google, GoogleInstance } from "./lib/google";
import { Niutrans, NiutransInstance } from "./lib/niutrans";
import { M2m100 , M2m100Instance } from "./lib/m2m100";
import { Gemini ,GeminiInstance } from "./lib/gemini";
import { ChatGPTInstance } from "./lib/chatgpt";
import { DeeplXInstance } from "./lib/deeplx";
import { MicrosoftInstance } from "./lib/microsoft";
import { GoogleInstance } from "./lib/google";
import { NiutransInstance } from "./lib/niutrans";
import { M2m100Instance } from "./lib/m2m100";
import { GeminiInstance } from "./lib/gemini";
import { autodetect } from "./lib/autodetect";


Expand Down Expand Up @@ -48,7 +48,7 @@ export default async function handler(
try {
if (sourceLanguage.length === 0 || sourceLanguage === "auto")
sourceLanguage = autodetect(text);

// code from sipc
const [chatgpt, gemini, deeplx, microsoft, google, niutrans, m2m100] =
await Promise.all([
Expand Down

0 comments on commit 427c9b6

Please sign in to comment.