Fix stt hang up (#791)

* handle transcribe error

* refactor
This commit is contained in:
an-lee
2024-07-10 12:59:50 +08:00
committed by GitHub
parent b406db289d
commit d9523269a3
3 changed files with 65 additions and 65 deletions

View File

@@ -58,12 +58,7 @@ export class Client {
return Promise.reject(new Error(err.response.data));
}
if (err.request) {
this.logger.error(err.request);
} else {
this.logger.error(err.message);
}
this.logger.error(err.message);
return Promise.reject(err);
}
);

View File

@@ -142,6 +142,7 @@ export const useTranscribe = () => {
apiKey: openai.key,
baseURL: openai.baseUrl,
dangerouslyAllowBrowser: true,
maxRetries: 0,
});
const res: { text: string } = (await client.audio.transcriptions.create({

View File

@@ -73,70 +73,74 @@ export const useTranscriptions = (media: AudioType | VideoType) => {
isolate = false,
} = params || {};
setService(service);
if (originalText === undefined) {
if (transcription?.targetId === media.id) {
originalText = transcription.result?.originalText;
} else {
const r = await findOrCreateTranscription();
if (r) {
originalText = r.result?.originalText;
}
}
}
setTranscribing(true);
setTranscribingProgress(0);
const { engine, model, alignmentResult, tokenId } = await transcribe(
media.src,
{
targetId: media.id,
targetType: media.mediaType,
originalText,
try {
if (originalText === undefined) {
if (transcription?.targetId === media.id) {
originalText = transcription.result?.originalText;
} else {
const r = await findOrCreateTranscription();
if (r) {
originalText = r.result?.originalText;
}
}
}
const { engine, model, alignmentResult, tokenId } = await transcribe(
media.src,
{
targetId: media.id,
targetType: media.mediaType,
originalText,
language,
service,
isolate,
}
);
let timeline: TimelineEntry[] = [];
alignmentResult.timeline.forEach((t) => {
if (t.type === "sentence") {
timeline.push(t);
} else {
t.timeline.forEach((st) => {
timeline.push(st);
});
}
});
timeline = preProcessTranscription(timeline);
if (media.language !== language) {
if (media.mediaType === "Video") {
await EnjoyApp.videos.update(media.id, {
language,
});
} else {
await EnjoyApp.audios.update(media.id, {
language,
});
}
}
await EnjoyApp.transcriptions.update(transcription.id, {
state: "finished",
result: {
timeline: timeline,
transcript: alignmentResult.transcript,
originalText,
tokenId,
},
engine,
model,
language,
service,
isolate,
}
);
});
let timeline: TimelineEntry[] = [];
alignmentResult.timeline.forEach((t) => {
if (t.type === "sentence") {
timeline.push(t);
} else {
t.timeline.forEach((st) => {
timeline.push(st);
});
}
});
timeline = preProcessTranscription(timeline);
if (media.language !== language) {
if (media.mediaType === "Video") {
await EnjoyApp.videos.update(media.id, {
language,
});
} else {
await EnjoyApp.audios.update(media.id, {
language,
});
}
setTranscribing(false);
} catch (err) {
setTranscribing(false);
toast.error(err.message);
}
await EnjoyApp.transcriptions.update(transcription.id, {
state: "finished",
result: {
timeline: timeline,
transcript: alignmentResult.transcript,
originalText,
tokenId,
},
engine,
model,
language,
});
setTranscribing(false);
};
const preProcessTranscription = (timeline: TimelineEntry[]) => {