diff --git a/src/modules/summary/SummaryController.ts b/src/modules/summary/SummaryController.ts index 796f717..3de38de 100644 --- a/src/modules/summary/SummaryController.ts +++ b/src/modules/summary/SummaryController.ts @@ -1,5 +1,5 @@ import { RouteHandlerMethod } from 'fastify'; -import {CreateSummarizerParams} from '@modal-sh/webvideo-transcript-summary-core'; +import {CreateSummarizerParams, SummarizerProcessParams} from '@modal-sh/webvideo-transcript-summary-core'; import * as config from '../../config'; import { SummaryService, SummaryServiceImpl } from './SummaryService'; @@ -15,10 +15,9 @@ export class SummaryControllerImpl implements SummaryController { } readonly summarizeVideoTranscript: RouteHandlerMethod = async (request, reply) => { - const params = request.body as CreateSummarizerParams; + const params = request.body as CreateSummarizerParams & SummarizerProcessParams; try { - const summaryResult = await this.summaryService.summarizeVideoTranscript({ - url: params.url, + this.summaryService.initializeSummarizer({ type: params.type, openAiParams: { apiKey: config.openAi.apiKey, @@ -26,9 +25,15 @@ export class SummaryControllerImpl implements SummaryController { temperature: params.openAiParams?.temperature ?? 0.6, model: params.openAiParams?.model ?? 'gpt-3.5-turbo', }, - language: params.language ?? 'en', - country: params.country ?? 'US', }); + + const summaryResult = await this.summaryService.summarizeVideoTranscript( + { + url: params.url, + language: params.language ?? 'en', + country: params.country ?? 'US', + }, + ); reply.send(summaryResult); } catch (errRaw) { const err = errRaw as Error; diff --git a/src/modules/summary/SummaryService.ts b/src/modules/summary/SummaryService.ts index 489e2b0..a7f0f7d 100644 --- a/src/modules/summary/SummaryService.ts +++ b/src/modules/summary/SummaryService.ts @@ -1,7 +1,7 @@ import { createSummarizer, - CreateSummarizerParams, - VideoType, + CreateSummarizerParams, SummarizerEventEmitter, + SummarizerProcessParams, } from '@modal-sh/webvideo-transcript-summary-core'; export interface SummaryResult { @@ -11,21 +11,32 @@ export interface SummaryResult { } export interface SummaryService { - summarizeVideoTranscript(params: CreateSummarizerParams): Promise> + initializeSummarizer(params: CreateSummarizerParams): void; + summarizeVideoTranscript(processParams: SummarizerProcessParams): Promise>; } export class SummaryServiceImpl implements SummaryService { + private summarizer?: SummarizerEventEmitter; + constructor(private readonly logger = console) { // noop } - summarizeVideoTranscript(params: CreateSummarizerParams) { + initializeSummarizer(params: CreateSummarizerParams): void { + this.summarizer = createSummarizer(params); + } + + summarizeVideoTranscript(processParams: SummarizerProcessParams) { return new Promise>((resolve, reject) => { + if (!this.summarizer) { + reject(new Error('Summarizer is not initialized')); + return; + } + const successEvent = {} as Partial; let error: Error; - const summarizer = createSummarizer(params); - summarizer.on('process', (data) => { + this.summarizer.on('process', (data) => { this.logger.log('process', data); if (data.phase === 'success') { switch (data.processType) { @@ -47,12 +58,12 @@ export class SummaryServiceImpl implements SummaryService { } }); - summarizer.on('error', (err) => { + this.summarizer.on('error', (err) => { this.logger.log('error', err); error = err; }); - summarizer.on('end', () => { + this.summarizer.on('end', () => { this.logger.log('end'); if (error) { reject(error); @@ -61,7 +72,7 @@ export class SummaryServiceImpl implements SummaryService { resolve(successEvent); }); - summarizer.process(); + this.summarizer.process(processParams); }); } }