diff --git a/.env.example b/.env.example index d725ae3..dd705bb 100644 --- a/.env.example +++ b/.env.example @@ -7,3 +7,6 @@ Gemini_API_ENDPOINT='https://generativelanguage.googleapis.com' NIUTRANS_KEY='...' DEEPL_X_API_URL='...' + +BAIDU_APP_ID='...' +BAIDU_KEY='...' \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 4850ecf..27da465 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,6 +34,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", @@ -947,6 +948,12 @@ "integrity": "sha512-wBYdB/PlRN7K30x37Pi/A7+KIFDNaqliz8IpUY0joG0Coyfszenspx0ID2s0KBAdNHXbd2WoMmpV5orFwGojUQ==", "dev": true }, + "node_modules/@types/md5": { + "version": "2.3.5", + "resolved": "https://registry.npmjs.org/@types/md5/-/md5-2.3.5.tgz", + "integrity": "sha512-/i42wjYNgE6wf0j2bcTX6kuowmdL/6PE4IVitMpm2eYKBUuYCprdcWVK+xEF0gcV6ufMCRhtxmReGfc6hIK7Jw==", + "dev": true + }, "node_modules/@types/node": { "version": "20.11.16", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.16.tgz", diff --git a/package.json b/package.json index e8553ce..6c87c67 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/setup.sh b/setup.sh index 14b114e..f921e96 100644 --- a/setup.sh +++ b/setup.sh @@ -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 </tmp/.Lyrify/config.txt openai_api_endpoint=${openai_api_endpoint:-https://api.openai.com/v1/chat/completions} @@ -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 @@ -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 @@ -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 <config.txt openai_api_endpoint=${openai_api_endpoint:-https://api.openai.com/v1/chat/completions} @@ -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 \ @@ -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 diff --git a/src/components/ResultBox.tsx b/src/components/ResultBox.tsx index 8d6c135..6411b91 100644 --- a/src/components/ResultBox.tsx +++ b/src/components/ResultBox.tsx @@ -106,12 +106,13 @@ export function ResultContainer({ loading, result, isExpanded}: ResultContainerP
- + +
); } -export function getResult() {return ['chatgpt', 'gemini', 'deeplx', 'microsoft', 'google', 'transmart', 'niutrans']} \ No newline at end of file +export function getResult() {return ['chatgpt', 'gemini', 'deeplx', 'microsoft', 'google', 'transmart', 'niutrans', 'baidu']} \ No newline at end of file diff --git a/src/lib/api.ts b/src/lib/api.ts index e097da7..86e2022 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -8,6 +8,7 @@ export type TranslateResult = { google: string; transmart: string; niutrans: string; + baidu: string; }; export type Translateloader = { @@ -19,6 +20,7 @@ export type Translateloader = { google: boolean; transmart: boolean; niutrans: boolean; + baidu: boolean; }; export type TranslateResponse = { @@ -36,6 +38,7 @@ export const initializeTranslateState: TranslateResult = { google: "", transmart: "", niutrans: "", + baidu: "", }; export const initializeTranslateloader: Translateloader = { @@ -47,6 +50,7 @@ export const initializeTranslateloader: Translateloader = { google: false, transmart: false, niutrans: false, + baidu: false, }; export async function translateContent( diff --git a/src/lib/constant.ts b/src/lib/constant.ts index 889174d..05f59d3 100644 --- a/src/lib/constant.ts +++ b/src/lib/constant.ts @@ -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" }, ]; diff --git a/src/pages/api/lib/baidu.ts b/src/pages/api/lib/baidu.ts new file mode 100644 index 0000000..43aaa46 --- /dev/null +++ b/src/pages/api/lib/baidu.ts @@ -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!, +); diff --git a/src/pages/api/lib/chatgpt.ts b/src/pages/api/lib/chatgpt.ts index 4b5da59..11dad6c 100644 --- a/src/pages/api/lib/chatgpt.ts +++ b/src/pages/api/lib/chatgpt.ts @@ -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", diff --git a/src/pages/api/lib/gemini.ts b/src/pages/api/lib/gemini.ts index 9ce4935..369b287 100644 --- a/src/pages/api/lib/gemini.ts +++ b/src/pages/api/lib/gemini.ts @@ -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", diff --git a/src/pages/api/lib/transmart.ts b/src/pages/api/lib/transmart.ts index e705460..71aea4e 100644 --- a/src/pages/api/lib/transmart.ts +++ b/src/pages/api/lib/transmart.ts @@ -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}, }; } } diff --git a/src/pages/api/translate.ts b/src/pages/api/translate.ts index 6f48a72..0dca10a 100644 --- a/src/pages/api/translate.ts +++ b/src/pages/api/translate.ts @@ -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 = { @@ -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": @@ -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,