Skip to content

Commit

Permalink
add baidu translate
Browse files Browse the repository at this point in the history
  • Loading branch information
SIPC committed Feb 24, 2024
1 parent caaf7e6 commit 62a005e
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ Gemini_API_ENDPOINT='https://generativelanguage.googleapis.com'

NIUTRANS_KEY='...'
DEEPL_X_API_URL='...'

BAIDU_APP_ID='...'
BAIDU_KEY='...'
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@types/autosize": "^4.0.3",
"@types/jsonwebtoken": "^9.0.5",
"@types/langdetect": "^0.2.2",
"@types/md5": "^2.3.5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-beautiful-dnd": "^13.1.8",
Expand Down
12 changes: 12 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ if docker ps -a -q --filter "name=lyrify" | grep -q .; then
read -p "Gemini API Key: " gemini_api_key
read -p "NIUTRANS Key: " niutrans_key
read -p "DEEPL_X API URL: " deepl_x_api_url
read -p "BAIDU_APP_ID: " baidu_app_id
read -p "BAIDU_KEY: " baidu_key

cat <<EOL >/tmp/.Lyrify/config.txt
openai_api_endpoint=${openai_api_endpoint:-https://api.openai.com/v1/chat/completions}
Expand All @@ -87,6 +89,8 @@ gemini_api_endpoint=${gemini_api_endpoint:-https://generativelanguage.googleapis
gemini_api_key=$gemini_api_key
niutrans_key=$niutrans_key
deepl_x_api_url=$deepl_x_api_url
baidu_app_id=$baidu_app_id
baidu_key=$baidu_key
EOL
fi

Expand All @@ -104,6 +108,8 @@ EOL
-e Gemini_API_KEY="$gemini_api_key" \
-e NIUTRANS_KEY="$niutrans_key" \
-e DEEPL_X_API_URL="$deepl_x_api_url" \
-e BAIDU_APP_ID="$baidu_app_id" \
-e BAIDU_KEY="$baidu_key"
sipcink/lyrify:latest

if [ $? -ne 0 ]; then
Expand Down Expand Up @@ -136,6 +142,8 @@ else
read -p "Gemini API Key: " gemini_api_key
read -p "NIUTRANS Key: " niutrans_key
read -p "DEEPL_X API URL: " deepl_x_api_url
read -p "BAIDU_APP_ID: " baidu_app_id
read -p "BAIDU_KEY: " baidu_key

cat <<EOL >config.txt
openai_api_endpoint=${openai_api_endpoint:-https://api.openai.com/v1/chat/completions}
Expand All @@ -145,6 +153,8 @@ gemini_api_endpoint=${gemini_api_endpoint:-https://generativelanguage.googleapis
gemini_api_key=$gemini_api_key
niutrans_key=$niutrans_key
deepl_x_api_url=$deepl_x_api_url
baidu_app_id=$baidu_app_id
baidu_key=$baidu_key
EOL

docker run -d --name lyrify \
Expand All @@ -156,6 +166,8 @@ EOL
-e Gemini_API_KEY="$gemini_api_key" \
-e NIUTRANS_KEY="$niutrans_key" \
-e DEEPL_X_API_URL="$deepl_x_api_url" \
-e BAIDU_APP_ID="$baidu_app_id" \
-e BAIDU_KEY="$baidu_key"
sipcink/lyrify:latest

if [ $? -ne "0" ]; then
Expand Down
5 changes: 3 additions & 2 deletions src/components/ResultBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ export function ResultContainer({ loading, result, isExpanded}: ResultContainerP
<div className={`result-container ${isExpanded ? 'grid-cols-4' : 'grid-cols-3'} grid gap-4`}>
<ResultBox data-name="ChatGPT" loading={loading.chatgpt} name="ChatGPT" content={result.chatgpt} />
<ResultBox data-name="Gemini" loading={loading.gemini} name="Gemini" content={result.gemini} />
<ResultBox data-name="DeepLX" loading={loading.deeplx} name="DeepLX" content={result.deeplx} />
<ResultBox data-name="DeepLX" loading={loading.deeplx} name="DeepL X" content={result.deeplx} />
<ResultBox data-name="Microsoft" loading={loading.microsoft} name="Microsoft" content={result.microsoft} />
<ResultBox data-name="Google" loading={loading.google} name="Google" content={result.google} />
<ResultBox data-name="Transmart" loading={loading.transmart} name="Transmart" content={result.transmart} />
<ResultBox data-name="Niutrans" loading={loading.niutrans} name="Niutrans" content={result.niutrans} />
<ResultBox data-name="Baidu" loading={loading.baidu} name="Baidu" content={result.baidu} />
</div>
);
}
export function getResult() {return ['chatgpt', 'gemini', 'deeplx', 'microsoft', 'google', 'transmart', 'niutrans']}
export function getResult() {return ['chatgpt', 'gemini', 'deeplx', 'microsoft', 'google', 'transmart', 'niutrans', 'baidu']}
4 changes: 4 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type TranslateResult = {
google: string;
transmart: string;
niutrans: string;
baidu: string;
};

export type Translateloader = {
Expand All @@ -19,6 +20,7 @@ export type Translateloader = {
google: boolean;
transmart: boolean;
niutrans: boolean;
baidu: boolean;
};

export type TranslateResponse = {
Expand All @@ -36,6 +38,7 @@ export const initializeTranslateState: TranslateResult = {
google: "",
transmart: "",
niutrans: "",
baidu: "",
};

export const initializeTranslateloader: Translateloader = {
Expand All @@ -47,6 +50,7 @@ export const initializeTranslateloader: Translateloader = {
google: false,
transmart: false,
niutrans: false,
baidu: false,
};

export async function translateContent(
Expand Down
18 changes: 0 additions & 18 deletions src/lib/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,13 @@ export type Language = {
export const languages: Language[] = [
{ key: "zh", value: "Chinese" },
{ key: "en", value: "English" },
{ key: "bg", value: "Bulgarian" },
{ key: "cs", value: "Czech" },
{ key: "da", value: "Danish" },
{ key: "nl", value: "Dutch" },
{ key: "et", value: "Estonian" },
{ key: "fi", value: "Finnish" },
{ key: "fr", value: "French" },
{ key: "de", value: "Deutsch" },
{ key: "el", value: "Greek" },
{ key: "hu", value: "Hungarian" },
{ key: "id", value: "Indonesian" },
{ key: "it", value: "Italian" },
{ key: "ja", value: "Japanese" },
{ key: "ko", value: "Korean" },
{ key: "lv", value: "Latvian" },
{ key: "lt", value: "Lithuanian" },
{ key: "no", value: "Norwegian" },
{ key: "pl", value: "Polish" },
{ key: "pt", value: "Portuguese" },
{ key: "ro", value: "Romanian" },
{ key: "ru", value: "Russian" },
{ key: "sk", value: "Slovak" },
{ key: "sl", value: "Slovenian" },
{ key: "es", value: "Spanish" },
{ key: "sv", value: "Swedish" },
{ key: "tr", value: "Turkish" },
{ key: "uk", value: "Ukrainian" },
];
35 changes: 35 additions & 0 deletions src/pages/api/lib/baidu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// code from sipc

import MD5 from "md5";
import axios from "axios";
import { getErrorMessage } from "@/pages/api/lib/utils";

export class Baidu {
public APPID: string;
public APPKEY: string;

constructor(APPID: string, APPKEY: string) {
this.APPID = APPID;
this.APPKEY = APPKEY;
}
async translate(text: string, target: string, source: string = "auto") {
const salt = String(Math.random()).slice(2)
const sign = MD5(this.APPID+text+salt+this.APPKEY)
try {
const apiUrl =`http://api.fanyi.baidu.com/api/trans/vip/translate?q=${text}&from=${source}&to=${target}&appid=${this.APPID}&salt=${salt}&sign=${sign}`;
const response = await axios.get(apiUrl);
if (response.data) {
return response.data.trans_result[0].dst;
} else {
throw new Error("Invalid response from Baidu");
}
} catch (error) {
throw new Error(`Error while translating: ${getErrorMessage(error)}`);
}
}
}

export const BaiduInstance = new Baidu(
process.env.BAIDU_APP_ID!,
process.env.BAIDU_KEY!,
);
11 changes: 11 additions & 0 deletions src/pages/api/lib/chatgpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ export class ChatGPT {
}

async translate(text: string, target: string, source: string = "auto") {
if (target === 'classical-chinese') {
target = '文言文'
if (source === 'zh') {
source = '白话文'
}
} if (source === 'classical-chinese') {
source = '文言文'
if (target === 'zh') {
target = '白话文'
}
}
try {
const headers = {
"Content-Type": "application/json",
Expand Down
11 changes: 11 additions & 0 deletions src/pages/api/lib/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ export class Gemini {
target: string,
source: string = "auto",
) {
if (target === 'classical-chinese') {
target = '文言文'
if (source === 'zh') {
source = '白话文'
}
} if (source === 'classical-chinese') {
source = '文言文'
if (target === 'zh') {
target = '白话文'
}
}
try {
const headers = {
"Content-Type": "application/json",
Expand Down
12 changes: 3 additions & 9 deletions src/pages/api/lib/transmart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,12 @@ export class Transmart {
return {
header: {
fn: "auto_translation",
client_key:
"browser-chrome-110.0.0-Mac OS-df4bd4c5-a65d-44b2-a40f-42f34f3535f2-1677486696487",
client_key:"browser-edge-chromium-123.0.0-Windows_10-793961b6-556c-46fd-8092-8f975f2335d3-1708529948457",
},
type: "plain",
model_category: "normal",
source: {
lang: source_lang,
text_list: [translate_text],
},
target: {
lang: target_lang,
},
source: {lang: source_lang,text_list: [translate_text]},
target: {lang: target_lang},
};
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/pages/api/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { GoogleInstance } from "./lib/google";
import { GeminiInstance } from "./lib/gemini";
import { TransmartInstance } from "./lib/transmart";
import { NiutransInstance } from "./lib/niutrans";
import { BaiduInstance } from "./lib/baidu";
import { autodetect } from "./lib/autodetect";

type TranslateResponse = {
Expand Down Expand Up @@ -39,7 +40,6 @@ export default async function handler(
if (sourceLanguage.length === 0 || sourceLanguage === "auto")
sourceLanguage = await autodetect(text);

// code from sipc
const response = async (model: string) => {
switch (model) {
case "chatgpt":
Expand All @@ -54,6 +54,12 @@ export default async function handler(
targetLanguage,
sourceLanguage,
).catch((e) => e.message);
case "baidu":
return await BaiduInstance.translate(
text,
targetLanguage,
sourceLanguage,
).catch((e) => e.message);
case "deeplx":
return await DeeplXInstance.translate(
text,
Expand Down

0 comments on commit 62a005e

Please sign in to comment.