Get transcript summaries of Web videos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB

  1. import {
  2. createSummarizer,
  3. CreateSummarizerParams,
  4. VideoType,
  5. } from '@modal-sh/webvideo-transcript-summary-core';
  6. export interface SummaryResult {
  7. summary: string;
  8. normalizedTranscript: string;
  9. rawTranscript: string;
  10. }
  11. export interface SummaryService {
  12. summarizeVideoTranscript(params: CreateSummarizerParams): Promise<Partial<SummaryResult>>
  13. }
  14. export class SummaryServiceImpl implements SummaryService {
  15. constructor(
  16. private readonly openAiApiKey: string,
  17. private readonly openAiOrganizationId?: string,
  18. ) {
  19. // noop
  20. }
  21. summarizeVideoTranscript(params: CreateSummarizerParams) {
  22. return new Promise<Partial<SummaryResult>>((resolve, reject) => {
  23. let successEvent = {} as Partial<SummaryResult>;
  24. let error: Error;
  25. const summarizer = createSummarizer({
  26. type: VideoType.YOUTUBE,
  27. url: params.url,
  28. openaiApiKey: this.openAiApiKey,
  29. openaiOrganizationId: this.openAiOrganizationId,
  30. });
  31. summarizer.on('process', (data) => {
  32. if (data.phase === 'success') {
  33. switch (data.processType) {
  34. case 'fetch-transcript':
  35. successEvent.rawTranscript = (
  36. JSON.parse(data.content) as { text: string }[]
  37. )
  38. .map((item) => item.text).join(' ');
  39. break;
  40. case 'normalize-transcript':
  41. successEvent.normalizedTranscript = data.content as string;
  42. break;
  43. case 'summarize-transcript':
  44. successEvent.summary = data.content as string;
  45. break;
  46. default:
  47. break;
  48. }
  49. }
  50. });
  51. summarizer.on('error', (err) => {
  52. error = err;
  53. });
  54. summarizer.on('end', () => {
  55. if (error) {
  56. reject(error);
  57. return;
  58. }
  59. resolve(successEvent);
  60. });
  61. summarizer.process();
  62. });
  63. }
  64. }