Skip to content

Commit

Permalink
Merge branch 'main' into online
Browse files Browse the repository at this point in the history
  • Loading branch information
SIPC committed Feb 29, 2024
2 parents 7e39551 + b9e7afc commit efd38bd
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/components/ResultBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ export function ResultContainer({
isExpanded,
}: ResultContainerProps) {
useEffect(() => {
const version = "1.0"
const version = "1.1";
const localVersion = localStorage.getItem("version");
if (version !== localVersion) {
const keys = Object.keys(localStorage);
const filteredKeys = keys.filter(key => key !== 'theme');
filteredKeys.forEach(key => localStorage.removeItem(key));
const filteredKeys = keys.filter((key) => key !== "theme");
filteredKeys.forEach((key) => localStorage.removeItem(key));
}
localStorage.setItem("version", version);

Expand Down Expand Up @@ -127,57 +127,53 @@ export function ResultContainer({
className={`result-container ${isExpanded ? "grid-cols-4" : "grid-cols-3"} grid gap-4`}
>
<ResultBox
data-name="ChatGPT"
loading={loading.chatgpt}
name="ChatGPT"
loading={loading.chatgpt}
content={result.chatgpt}
/>
<ResultBox
data-name="Gemini"
loading={loading.gemini}
name="Gemini"
loading={loading.gemini}
content={result.gemini}
/>
<ResultBox
data-name="Qwen"
loading={loading.qwen}
name="Qwen"
loading={loading.qwen}
content={result.qwen}
/>
<ResultBox
data-name="DeepLX"
loading={loading.deeplx}
name="GLM"
loading={loading.qwen}
content={result.glm}
/>
<ResultBox
name="DeepL X"
loading={loading.deeplx}
content={result.deeplx}
/>
<ResultBox
data-name="Microsoft"
loading={loading.microsoft}
name="Microsoft"
loading={loading.microsoft}
content={result.microsoft}
/>
<ResultBox
data-name="Google"
loading={loading.google}
name="Google"
loading={loading.google}
content={result.google}
/>
<ResultBox
data-name="Transmart"
loading={loading.transmart}
name="Transmart"
loading={loading.transmart}
content={result.transmart}
/>
<ResultBox
data-name="Niutrans"
loading={loading.niutrans}
name="Niutrans"
loading={loading.niutrans}
content={result.niutrans}
/>
<ResultBox
data-name="Baidu"
loading={loading.baidu}
name="Baidu"
loading={loading.baidu}
content={result.baidu}
/>
</div>
Expand All @@ -188,6 +184,7 @@ export function getResult() {
"chatgpt",
"gemini",
"qwen",
"glm",
"deeplx",
"microsoft",
"google",
Expand Down
4 changes: 4 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type TranslateResult = {
chatgpt: string;
gemini: string;
qwen: string;
glm: string;
deeplx: string;
microsoft: string;
google: string;
Expand All @@ -17,6 +18,7 @@ export type Translateloader = {
chatgpt: boolean;
gemini: boolean;
qwen: boolean;
glm: boolean;
deeplx: boolean;
microsoft: boolean;
google: boolean;
Expand All @@ -36,6 +38,7 @@ export const initializeTranslateState: TranslateResult = {
chatgpt: "",
gemini: "",
qwen: "",
glm: "",
deeplx: "",
microsoft: "",
google: "",
Expand All @@ -49,6 +52,7 @@ export const initializeTranslateloader: Translateloader = {
chatgpt: false,
gemini: false,
qwen: false,
glm: false,
deeplx: false,
microsoft: false,
google: false,
Expand Down
82 changes: 82 additions & 0 deletions src/pages/api/lib/glm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// code from sipc

import axios from "axios";
import jwt from "jsonwebtoken";
import { getErrorMessage } from "@/pages/api/lib/utils";

function generate_token(apikey: string) {
const [apiKeyId, secret] = apikey.split(".");
const headers = { alg: "HS256", sign_type: "SIGN" };
const payload = {
api_key: apiKeyId,
exp: Date.now() + 30 * 1000,
timestamp: Date.now(),
};
return jwt.sign(payload, secret, { header:headers });
}

export class GLM {
public key: string;
public apiUrl: string;
public model: string;

constructor(
key: string,
apiUrl = "https://open.bigmodel.cn/api/paas/v4/chat/completions",
model = "glm-3-turbo",
) {
this.key = key;
this.apiUrl = apiUrl;
this.model = model;
}

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 token = generate_token(this.key);
const headers = {
"Content-Type": "application/json",
Authorization: token,
};
const data = JSON.stringify({
model: this.model,
messages: [
{
role: "system",
content: `You are a professional, authentic translation engine, only returns translations.`,
},
{
role: "user",
content: `Please translate the text from ${source} to ${target} language,Translation will be enclosed within <start></end> tags, and they should not be included in the output.`,
},
{
role: "user",
content: `<start>${text}</end>`,
},
],
temperature: 0.7,
});
const response = await axios.post(this.apiUrl, data, { headers });
return response.data.choices[0].message.content;
} catch (error) {
throw new Error(`Error while translating: ${getErrorMessage(error)}`);
}
}
}

export const GLMInstance = new GLM(
process.env.GLM_API_KEY!,
process.env.GLM_API_ENDPOINT!,
process.env.GLM_MODEL!,
);
7 changes: 7 additions & 0 deletions src/pages/api/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TransmartInstance } from "./lib/transmart";
import { NiutransInstance } from "./lib/niutrans";
import { BaiduInstance } from "./lib/baidu";
import { QwenInstance } from "./lib/qwen";
import { GLMInstance } from "./lib/glm";
import { autodetect } from "./lib/autodetect";

type TranslateResponse = {
Expand Down Expand Up @@ -61,6 +62,12 @@ export default async function handler(
targetLanguage,
sourceLanguage,
).catch((e) => e.message);
case "glm":
return await GLMInstance.translate(
text,
targetLanguage,
sourceLanguage,
).catch((e) => e.message);
case "baidu":
return await BaiduInstance.translate(
text,
Expand Down

0 comments on commit efd38bd

Please sign in to comment.