Skip to content

Commit

Permalink
feat: add Translate Model to setting page
Browse files Browse the repository at this point in the history
  • Loading branch information
hqwuzhaoyi committed Nov 12, 2023
1 parent 5856b03 commit ef956f9
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 29 deletions.
5 changes: 3 additions & 2 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ STATIC_PATH=/static # Static file path
LANGUAGE=zh-CN # Output SRT file and then translate the language
TRANSLATE_DELAY=1500 # Delay between calling translation interface
TRANSLATE_GROUP=4 # Translate sentences for grouping translation, how many sentences can be translated at most at a time
TranslateModel=google # google or gpt3


REDIS_PORT=6379 # Redis port
REDIS_HOST=subtitle_redis # Redis address
Expand All @@ -30,4 +30,5 @@ AUTH_SECRET = YOUR_KEY_HERE # JWT secret you can run `openssl rand -base64 32` t

# System Setting
# You can edit in Setting
OUTPUT_SRT_THEN_TRANSLATE=true # Whether to output the SRT file first and then translate it
OUTPUT_SRT_THEN_TRANSLATE=true # Whether to output the SRT file first and then translate it
TranslateModel=google # google or gpt3
13 changes: 11 additions & 2 deletions apps/server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,18 @@ export class AuthService {
};
}

async updateProfile(user, { username, password, OUTPUT_SRT_THEN_TRANSLATE }) {
async updateProfile(
user,
{ username, password, OUTPUT_SRT_THEN_TRANSLATE, TranslateModel }
) {
if (typeof OUTPUT_SRT_THEN_TRANSLATE === "boolean") {
this.configService.set("OUTPUT_SRT_THEN_TRANSLATE", OUTPUT_SRT_THEN_TRANSLATE ? '1' : '0');
this.configService.set(
"OUTPUT_SRT_THEN_TRANSLATE",
OUTPUT_SRT_THEN_TRANSLATE ? "1" : "0"
);
}
if (TranslateModel) {
this.configService.set("TranslateModel", TranslateModel);
}

return this.usersService.updateProfile(user.id, {
Expand Down
45 changes: 27 additions & 18 deletions apps/server/src/translate/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { TranslateModel, TranslateType } from "translator";
import { staticPath } from "utils";
import { TranslateResult } from "shared-types";
import { FilesService } from "@/files/files.service";
import { CustomConfigService } from "@/config/custom-config.service";

@Injectable()
export class TranslateService {
constructor(
@Inject("STATIC_DIR") private staticDir: string,
private readonly filesService: FilesService
private readonly filesService: FilesService,
private customConfigService: CustomConfigService
) {}

async create(createTranslateDto: CreateTranslateDto) {}
Expand Down Expand Up @@ -47,6 +49,15 @@ export class TranslateService {
return translateName;
}

private async getTranslateModel() {
const translateModel =
((await this.customConfigService.get(
"TranslateModel"
)) as TranslateType) ?? TranslateType.GPT3;

return translateModel;
}

translateFile(
filename,
dir = ""
Expand All @@ -55,7 +66,7 @@ export class TranslateService {
filename: string;
path: string;
}> {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
const translateName = this.translateFileName(filename);

// const existUrl = this.existFile(translateName, dir);
Expand All @@ -68,14 +79,13 @@ export class TranslateService {
// return;
// }

const model = new TranslateModel(
(process.env.TranslateModel as TranslateType) ?? TranslateType.GPT3,
{
baseUrl: process.env.BASE_URL,
gpt3Key: process.env.OPENAI_API_KEY,
googleKey: process.env.GOOGLE_TRANSLATE_API_KEY,
}
)
const translateModel = await this.getTranslateModel();

const model = new TranslateModel(translateModel, {
baseUrl: process.env.BASE_URL,
gpt3Key: process.env.OPENAI_API_KEY,
googleKey: process.env.GOOGLE_TRANSLATE_API_KEY,
})
.translateSrtStreamGroup(
path.join(this.staticDir, dir, filename),
path.join(this.staticDir, dir, translateName),
Expand Down Expand Up @@ -133,14 +143,13 @@ export class TranslateService {
return;
}

const model = new TranslateModel(
(process.env.TranslateModel as TranslateType) ?? TranslateType.GPT3,
{
baseUrl: process.env.BASE_URL,
gpt3Key: process.env.OPENAI_API_KEY,
googleKey: process.env.GOOGLE_TRANSLATE_API_KEY,
}
)
const translateModel = await this.getTranslateModel();

const model = new TranslateModel(translateModel, {
baseUrl: process.env.BASE_URL,
gpt3Key: process.env.OPENAI_API_KEY,
googleKey: process.env.GOOGLE_TRANSLATE_API_KEY,
})
.translateSrtStreamGroup(
subtitle.filePath,
translatePath,
Expand Down
1 change: 1 addition & 0 deletions apps/web/app/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default async function SettingsProfilePage() {
username: profile.username,
OUTPUT_SRT_THEN_TRANSLATE:
config.OUTPUT_SRT_THEN_TRANSLATE === "1" ? true : false,
TranslateModel: config.TranslateModel,
}}
/>
</div>
Expand Down
36 changes: 34 additions & 2 deletions apps/web/app/settings/profile-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import {
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
import { toast } from "@/components/ui/use-toast";
import { Switch } from "@/components/ui/switch";
import { useSession } from "next-auth/react";
import { redirect, useRouter } from "next/navigation";
import { useRouter } from "next/navigation";
import { TranslateType } from "shared-types";

const profileFormSchema = z.object({
username: z
Expand All @@ -46,6 +46,7 @@ const profileFormSchema = z.object({
})
.optional(),
OUTPUT_SRT_THEN_TRANSLATE: z.boolean().optional(),
TranslateModel: z.nativeEnum(TranslateType).optional(),
});

type ProfileFormValues = z.infer<typeof profileFormSchema>;
Expand Down Expand Up @@ -148,6 +149,37 @@ export function ProfileForm({
</FormItem>
)}
/>
<FormField
control={form.control}
name="TranslateModel"
render={({ field }) => (
<FormItem className="flex flex-col justify-between rounded-lg border p-4">
<div className="space-y-0.5">
<FormLabel className="text-base">Translate Model</FormLabel>
<FormDescription>
Choose the translation model you need
</FormDescription>
</div>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a model" />
</SelectTrigger>
</FormControl>
<SelectContent>
{Object.values(TranslateType).map((item) => (
<SelectItem key={item} value={item}>
{item}
</SelectItem>
))}
</SelectContent>
</Select>
</FormItem>
)}
/>
</div>
</div>
<Button type="submit">Update profile</Button>
Expand Down
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"tailwind-merge": "^1.14.0",
"tailwindcss": "3.3.3",
"tailwindcss-animate": "^1.0.7",
"translator": "workspace:^",
"typescript": "^5.2.2",
"utils": "workspace:*",
"zod": "^3.22.4"
Expand Down
5 changes: 5 additions & 0 deletions packages/shared-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ export enum LanguageEnum {
Chinese = "cn",
Auto = "auto",
}

export enum TranslateType {
GOOGLE = "google",
GPT3 = "gpt3",
}
3 changes: 2 additions & 1 deletion packages/translater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
"typescript": "^5.2.2"
},
"dependencies": {
"p-queue": "npm:@esm2cjs/p-queue@^7.3.0",
"@google-cloud/translate": "^8.0.2",
"openai": "^4.13.0",
"p-queue": "npm:@esm2cjs/p-queue@^7.3.0",
"shared-types": "workspace:^",
"subtitle": "^4.2.1"
}
}
5 changes: 1 addition & 4 deletions packages/translater/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { GPTTranslator } from "./gpt3";
import { GoogleTranslator } from "./google";
import { Translator } from "./types";

export enum TranslateType {
GOOGLE = "google",
GPT3 = "gpt3",
}
export { TranslateType } from "shared-types";

export type TranslateOptions = {
gpt3Key?: string;
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit ef956f9

Please sign in to comment.