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

Commit

Permalink
hide m2m100 add Gemini
Browse files Browse the repository at this point in the history
Temporarily hide m2m100
  • Loading branch information
SIPC committed Feb 8, 2024
1 parent 8b369b3 commit a19ef38
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 21 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -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='...'

7 changes: 1 addition & 6 deletions lib/translate/gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/components/ResultBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ export function ResultContainer({ loading, result }: ResultContainerProps) {
return (
<div className={`result-container grid-cols-3 grid gap-4`}>
<ResultBox loading={loading} name="ChatGPT" content={result.chatgpt} />
<ResultBox loading={loading} name="DeepL X" content={result.deeplx} />
<ResultBox loading={loading} name="Gemini" content={result.gemini} />
<ResultBox loading={loading} name="DeepLX" content={result.deeplx} />
<ResultBox loading={loading} name="Microsoft" content={result.microsoft} />
<ResultBox loading={loading} name="Google" content={result.google} />
<ResultBox loading={loading} name="Niutrans" content={result.niutrans} />
<ResultBox loading={loading} name="M2m100" content={result.m2m100} />
</div>
);
}
2 changes: 1 addition & 1 deletion src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/lib/autodetect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export function autodetect(content: string): string {
const lang = detectOne(content);
return detectionMap[lang] || lang;
} catch {
return "";
return "auto";
}
}
41 changes: 41 additions & 0 deletions src/pages/api/lib/gemini.ts
Original file line number Diff line number Diff line change
@@ -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,
);

6 changes: 4 additions & 2 deletions src/pages/api/lib/microsoft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
16 changes: 8 additions & 8 deletions src/pages/api/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -74,11 +74,11 @@ export default async function handler(
source: sourceLanguage,
data: {
chatgpt,
gemini,
deeplx,
microsoft,
google,
m2m100,
niutrans,
niutrans
},
});
} catch (e) {
Expand Down

0 comments on commit a19ef38

Please sign in to comment.