diff --git a/.env.example b/.env.example
index 489477b..0280705 100644
--- a/.env.example
+++ b/.env.example
@@ -1,5 +1,8 @@
OpenAI_API_KEY='sk-...'
OpenAI_API_ENDPOINT='https://api.openai.com/v1/chat/completions'
-OpenAI_MODEL='gpt-3.5-turbo=0613'
+Gemini_API_KEY=''
+Gemini_API_ENDPOINT='https://generativelanguage.googleapis.com'
+OpenAI_MODEL='gpt-3.5-turbo'
NIUTRANS_KEY='...'
DEEPL_X_API_URL='...'
+
diff --git a/lib/translate/gemini.js b/lib/translate/gemini.js
index f027c72..7e4a979 100644
--- a/lib/translate/gemini.js
+++ b/lib/translate/gemini.js
@@ -22,12 +22,7 @@ class Gemini {
},
],
});
- const response = await axios.post(
- this.apiUrl +
- `/v1beta/models/gemini-pro:generateContent?key=${this.key}`,
- data,
- { headers },
- );
+ 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;
} catch (error) {
console.log(JSON.stringify(error));
diff --git a/src/components/ResultBox.tsx b/src/components/ResultBox.tsx
index 23da01d..494fbcc 100644
--- a/src/components/ResultBox.tsx
+++ b/src/components/ResultBox.tsx
@@ -81,11 +81,11 @@ export function ResultContainer({ loading, result }: ResultContainerProps) {
return (
-
+
+
-
);
}
diff --git a/src/lib/api.ts b/src/lib/api.ts
index ca1d572..f4fb270 100644
--- a/src/lib/api.ts
+++ b/src/lib/api.ts
@@ -2,10 +2,10 @@ import axios from "axios";
export type TranslateResult = {
chatgpt: string;
+ gemini: string;
deeplx: string;
microsoft: string;
google: string;
- m2m100: string;
niutrans: string;
};
diff --git a/src/pages/api/lib/autodetect.ts b/src/pages/api/lib/autodetect.ts
index 1359ab8..bfb7853 100644
--- a/src/pages/api/lib/autodetect.ts
+++ b/src/pages/api/lib/autodetect.ts
@@ -12,6 +12,6 @@ export function autodetect(content: string): string {
const lang = detectOne(content);
return detectionMap[lang] || lang;
} catch {
- return "";
+ return "auto";
}
}
diff --git a/src/pages/api/lib/gemini.ts b/src/pages/api/lib/gemini.ts
new file mode 100644
index 0000000..fc3883c
--- /dev/null
+++ b/src/pages/api/lib/gemini.ts
@@ -0,0 +1,41 @@
+const axios = require("axios");
+import { getErrorMessage } from "@/pages/api/lib/utils";
+
+export class Gemini {
+ public key: string;
+ public apiUrl: string;
+ constructor(key: string, apiUrl = "https://generativelanguage.googleapis.com") {
+ this.key = key;
+ this.apiUrl = apiUrl;
+ }
+
+ async translate(text: string, targetLanguage: string, sourceLanguage: string = "auto") {
+ try {
+ const headers = {
+ "Content-Type": "application/json",
+ };
+ const data = JSON.stringify({
+ contents: [
+ {
+ parts: [
+ {
+ text: `将"${text}"从${sourceLanguage}翻译为${targetLanguage}(!!!直接返回翻译内容不要打引号)`,
+ },
+ ],
+ },
+ ],
+ });
+ 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;
+ } catch (error) {
+ console.log(JSON.stringify(error));
+ throw new Error(`Error while translating: ${getErrorMessage(error)}`);
+ }
+ }
+}
+
+export const GeminiInstance = new Gemini(
+ process.env.Gemini_API_KEY as string,
+ process.env.Gemini_API_ENDPOINT,
+);
+
diff --git a/src/pages/api/lib/microsoft.ts b/src/pages/api/lib/microsoft.ts
index b9ba856..1901e78 100644
--- a/src/pages/api/lib/microsoft.ts
+++ b/src/pages/api/lib/microsoft.ts
@@ -12,12 +12,14 @@ export class Microsoft {
constructor() {
this.API_AUTH = "https://edge.microsoft.com/translate/auth";
- this.API_TRANSLATE =
- "https://api.cognitive.microsofttranslator.com/translate";
+ this.API_TRANSLATE ="https://api.cognitive.microsofttranslator.com/translate";
this.authToken = "";
}
async translate(text: string, targetLanguage: string, sourceLanguage = "en") {
+ if (sourceLanguage === "auto") {
+ sourceLanguage = "";
+ }
try {
// 检测 JWT 是否存在/过期
if (
diff --git a/src/pages/api/translate.ts b/src/pages/api/translate.ts
index 915ac81..4fec49e 100644
--- a/src/pages/api/translate.ts
+++ b/src/pages/api/translate.ts
@@ -4,16 +4,16 @@ import { ChatGPT, ChatGPTInstance } from "./lib/chatgpt";
import { DeepLX, DeeplXInstance } from "@/pages/api/lib/deeplx";
import { Microsoft, MicrosoftInstance } from "@/pages/api/lib/microsoft";
import { Google, GoogleInstance } from "@/pages/api/lib/google";
-import { M2m100, M2m100Instance } from "@/pages/api/lib/m2m100";
import { Niutrans, NiutransInstance } from "@/pages/api/lib/niutrans";
import { autodetect } from "@/pages/api/lib/autodetect";
+import { GeminiInstance } from "./lib/gemini";
type TranslateResult = {
chatgpt: string;
deeplx: string;
microsoft: string;
google: string;
- m2m100: string;
+ gemini: string;
niutrans: string;
};
@@ -47,11 +47,14 @@ export default async function handler(
sourceLanguage = autodetect(text);
// code from sipc
- const [chatgpt, deeplx, microsoft, google, m2m100, niutrans] =
+ const [chatgpt, gemini, deeplx, microsoft, google, niutrans] =
await Promise.all([
ChatGPTInstance.translate(text, targetLanguage, sourceLanguage).catch(
(e) => e.message,
),
+ GeminiInstance.translate(text, targetLanguage, sourceLanguage).catch(
+ (e) => e.message,
+ ),
DeeplXInstance.translate(text, targetLanguage, sourceLanguage).catch(
(e) => e.message,
),
@@ -61,9 +64,6 @@ export default async function handler(
GoogleInstance.translate(text, targetLanguage, sourceLanguage).catch(
(e) => e.message,
),
- M2m100Instance.translate(text, targetLanguage, sourceLanguage).catch(
- (e) => e.message,
- ),
NiutransInstance.translate(text, targetLanguage, sourceLanguage).catch(
(e) => e.message,
),
@@ -74,11 +74,11 @@ export default async function handler(
source: sourceLanguage,
data: {
chatgpt,
+ gemini,
deeplx,
microsoft,
google,
- m2m100,
- niutrans,
+ niutrans
},
});
} catch (e) {