|
- import {
- createSummarizer,
- CreateSummarizerParams,
- VideoType,
- } from '@modal-sh/webvideo-transcript-summary-core';
-
- export interface SummaryResult {
- summary: string;
- normalizedTranscript: string;
- rawTranscript: string;
- }
-
- export interface SummaryService {
- summarizeVideoTranscript(params: CreateSummarizerParams): Promise<Partial<SummaryResult>>
- }
-
- export class SummaryServiceImpl implements SummaryService {
- constructor(
- private readonly openAiApiKey: string,
- private readonly openAiOrganizationId?: string,
- ) {
- // noop
- }
-
- summarizeVideoTranscript(params: CreateSummarizerParams) {
- return new Promise<Partial<SummaryResult>>((resolve, reject) => {
- let successEvent = {} as Partial<SummaryResult>;
- let error: Error;
- const summarizer = createSummarizer({
- type: VideoType.YOUTUBE,
- url: params.url,
- openaiApiKey: this.openAiApiKey,
- openaiOrganizationId: this.openAiOrganizationId,
- });
-
- summarizer.on('process', (data) => {
- if (data.phase === 'success') {
- switch (data.processType) {
- case 'fetch-transcript':
- successEvent.rawTranscript = (
- JSON.parse(data.content) as { text: string }[]
- )
- .map((item) => item.text).join(' ');
- break;
- case 'normalize-transcript':
- successEvent.normalizedTranscript = data.content as string;
- break;
- case 'summarize-transcript':
- successEvent.summary = data.content as string;
- break;
- default:
- break;
- }
- }
- });
-
- summarizer.on('error', (err) => {
- error = err;
- });
-
- summarizer.on('end', () => {
- if (error) {
- reject(error);
- return;
- }
- resolve(successEvent);
- });
-
- summarizer.process();
- });
- }
- }
|