Fix punctuation (#477)

* add punctuate command

* check punctuation before alignment
This commit is contained in:
an-lee
2024-04-02 14:43:15 +08:00
committed by GitHub
parent f0f4319044
commit 071d80060d
4 changed files with 62 additions and 1 deletions

View File

@@ -3,3 +3,4 @@ export * from "./lookup.command";
export * from "./translate.command";
export * from "./ipa.command";
export * from "./analyze.command";
export * from "./punctuate.command";

View File

@@ -0,0 +1,38 @@
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
export const punctuateCommand = async (
text: string,
options: {
key: string;
modelName?: string;
temperature?: number;
baseUrl?: string;
}
): Promise<string> => {
const { key, temperature = 0, baseUrl } = options;
let { modelName = "gpt-4-turbo-preview" } = options;
const chatModel = new ChatOpenAI({
openAIApiKey: key,
modelName,
temperature,
configuration: {
baseURL: baseUrl,
},
cache: false,
verbose: true,
maxRetries: 2,
});
const prompt = ChatPromptTemplate.fromMessages([
["system", SYSTEM_PROMPT],
["human", text],
]);
const response = await prompt.pipe(chatModel).invoke({});
return response.text;
};
const SYSTEM_PROMPT = `Please add proper punctuation to the text I provide you. Return the corrected text only.`;

View File

@@ -8,6 +8,7 @@ import {
extractStoryCommand,
translateCommand,
analyzeCommand,
punctuateCommand,
} from "@commands";
export const useAiCommand = () => {
@@ -102,10 +103,19 @@ export const useAiCommand = () => {
});
};
const punctuateText = async (text: string) => {
return punctuateCommand(text, {
key: currentEngine.key,
modelName: currentEngine.model,
baseUrl: currentEngine.baseUrl,
})
}
return {
lookupWord,
extractStory,
translate,
analyzeText,
punctuateText
};
};

View File

@@ -9,10 +9,12 @@ import { AI_WORKER_ENDPOINT } from "@/constants";
import * as sdk from "microsoft-cognitiveservices-speech-sdk";
import axios from "axios";
import { AlignmentResult } from "echogarden/dist/api/API.d.js";
import { useAiCommand } from "./use-ai-command";
export const useTranscribe = () => {
const { EnjoyApp, user, webApi } = useContext(AppSettingsProviderContext);
const { whisperConfig, openai } = useContext(AISettingsProviderContext);
const { punctuateText } = useAiCommand();
const transcode = async (src: string | Blob): Promise<string> => {
if (src instanceof Blob) {
@@ -61,9 +63,19 @@ export const useTranscribe = () => {
throw new Error(t("whisperServiceNotSupported"));
}
let transcript = originalText || result.text;
// if the transcript does not contain any punctuation, use AI command to add punctuation
if (!transcript.match(/\w[.,!?](\s|$)/)) {
try {
transcript = await punctuateText(transcript);
} catch (err) {
console.warn(err.message);
}
}
const alignmentResult = await EnjoyApp.echogarden.align(
new Uint8Array(await blob.arrayBuffer()),
originalText || result.text
transcript
);
return {