Skip to content

Commit

Permalink
add Qwen model
Browse files Browse the repository at this point in the history
  • Loading branch information
SIPC committed Feb 25, 2024
1 parent 0dab206 commit 6460492
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 22 deletions.
19 changes: 12 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
OpenAI_API_KEY='sk-...'
OpenAI_API_ENDPOINT='https://api.openai.com/v1/chat/completions'
OpenAI_API_ENDPOINT='https://api.chatnio.net/v1/chat/completions'
OpenAI_API_KEY='sk-'
OpenAI_MODEL='gpt-3.5-turbo'

Gemini_API_KEY=''
Gemini_API_ENDPOINT='https://generativelanguage.googleapis.com'
Gemini_API_KEY=''

NIUTRANS_KEY=''

DEEPL_X_API_URL=''

NIUTRANS_KEY='...'
DEEPL_X_API_URL='...'
BAIDU_APP_ID=''
BAIDU_KEY=''

BAIDU_APP_ID='...'
BAIDU_KEY='...'
QWEN_API_ENDPOINT='https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation'
QWEN_API_KEY='sk-'
QWEN_MODEL='qwen-turbo'
36 changes: 28 additions & 8 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ if docker ps -a -q --filter "name=lyrify" | grep -q .; then
read -p "Gemini API Endpoint (Press Enter to use default): " gemini_api_endpoint
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
read -p "DEEPL X API URL: " deepl_x_api_url
read -p "BAIDU APP ID: " baidu_app_id
read -p "BAIDU KEY: " baidu_key
read -p "Qwen API Endpoint (Press Enter to use default): " qwen_api_endpoint
read -p "Qwen API Key: " qwen_api_key
read -p "Qwen Model: " qwen_model


cat <<EOL >/tmp/.Lyrify/config.txt
openai_api_endpoint=${openai_api_endpoint:-https://api.openai.com/v1/chat/completions}
Expand All @@ -91,6 +95,9 @@ niutrans_key=$niutrans_key
deepl_x_api_url=$deepl_x_api_url
baidu_app_id=$baidu_app_id
baidu_key=$baidu_key
qwen_api_endpoint=${qwen_api_endpoint-https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation}
qwen_api_key=$qwen_api_key
qwen_model=$qwen_model
EOL
fi

Expand All @@ -109,7 +116,10 @@ EOL
-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"
-e BAIDU_KEY="$baidu_key" \
-e QWEN_API_ENDPOINT="$qwen_api_endpoint" \
-e QWEN_API_KEY="$qwen_api_key" \
-e QWEN_MODEL="$qwen_model" \
sipcink/lyrify:latest

if [ $? -ne 0 ]; then
Expand Down Expand Up @@ -141,9 +151,13 @@ else
read -p "Gemini API Endpoint (Press Enter to use default): " gemini_api_endpoint
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
read -p "DEEPL X API URL: " deepl_x_api_url
read -p "BAIDU APP ID: " baidu_app_id
read -p "BAIDU KEY: " baidu_key
read -p "Qwen API Endpoint (Press Enter to use default): " qwen_api_endpoint
read -p "Qwen API Key: " qwen_api_key
read -p "Qwen Model: " qwen_model


cat <<EOL >config.txt
openai_api_endpoint=${openai_api_endpoint:-https://api.openai.com/v1/chat/completions}
Expand All @@ -155,6 +169,9 @@ niutrans_key=$niutrans_key
deepl_x_api_url=$deepl_x_api_url
baidu_app_id=$baidu_app_id
baidu_key=$baidu_key
qwen_api_endpoint=${qwen_api_endpoint-https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation}
qwen_api_key=$qwen_api_key
qwen_model=$qwen_model
EOL

docker run -d --name lyrify \
Expand All @@ -167,7 +184,10 @@ EOL
-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"
-e BAIDU_KEY="$baidu_key" \
-e QWEN_API_ENDPOINT="$qwen_api_endpoint" \
-e QWEN_API_KEY="$qwen_api_key" \
-e QWEN_MODEL="$qwen_model" \
sipcink/lyrify:latest

if [ $? -ne "0" ]; then
Expand Down
3 changes: 2 additions & 1 deletion src/components/ResultBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ 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="Qwen" loading={loading.qwen} name="Qwen" content={result.qwen} />
<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} />
Expand All @@ -115,4 +116,4 @@ export function ResultContainer({ loading, result, isExpanded}: ResultContainerP
</div>
);
}
export function getResult() {return ['chatgpt', 'gemini', 'deeplx', 'microsoft', 'google', 'transmart', 'niutrans', 'baidu']}
export function getResult() {return ['chatgpt', 'gemini', 'qwen', '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 @@ -3,6 +3,7 @@ import axios from "axios";
export type TranslateResult = {
chatgpt: string;
gemini: string;
qwen: string;
deeplx: string;
microsoft: string;
google: string;
Expand All @@ -15,6 +16,7 @@ export type Translateloader = {
translate: boolean,
chatgpt: boolean;
gemini: boolean;
qwen: boolean;
deeplx: boolean;
microsoft: boolean;
google: boolean;
Expand All @@ -33,6 +35,7 @@ export type TranslateResponse = {
export const initializeTranslateState: TranslateResult = {
chatgpt: "",
gemini: "",
qwen: "",
deeplx: "",
microsoft: "",
google: "",
Expand All @@ -45,6 +48,7 @@ export const initializeTranslateloader: Translateloader = {
translate: false,
chatgpt: false,
gemini: false,
qwen: false,
deeplx: false,
microsoft: false,
google: false,
Expand Down
70 changes: 70 additions & 0 deletions src/pages/api/lib/qwen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// code from sipc

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

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

constructor(
apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation",
key: string,
model = "qwen-turbo",
) {
this.apiUrl = apiUrl;
this.key = key;
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 headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${this.key}`,
};
const data = JSON.stringify({
model: this.model,
input: {
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, without explaining my original text.`,
},
{
role: "user",
content: text,
},
],
},
});
const response = await axios.post(this.apiUrl, data, { headers });
return response.data.output.text;
} catch (error:any) {
throw new Error(`Error while translating: ${getErrorMessage(error.response.data)}`);
}
}
}

export const QwenInstance = new Qwen(
process.env.QWEN_API_ENDPOINT!,
process.env.QWEN_API_KEY!,
process.env.QWEN_MODEL!,
);
7 changes: 7 additions & 0 deletions src/pages/api/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GeminiInstance } from "./lib/gemini";
import { TransmartInstance } from "./lib/transmart";
import { NiutransInstance } from "./lib/niutrans";
import { BaiduInstance } from "./lib/baidu";
import { QwenInstance } from "./lib/qwen";
import { autodetect } from "./lib/autodetect";

type TranslateResponse = {
Expand Down Expand Up @@ -54,6 +55,12 @@ export default async function handler(
targetLanguage,
sourceLanguage,
).catch((e) => e.message);
case "qwen":
return await QwenInstance.translate(
text,
targetLanguage,
sourceLanguage,
).catch((e) => e.message);
case "baidu":
return await BaiduInstance.translate(
text,
Expand Down
7 changes: 1 addition & 6 deletions src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,4 @@ main {

.result-container p {
text-align: justify;
}

@media (max-width: 680px) {
.result-container {
}
}
}

0 comments on commit 6460492

Please sign in to comment.