Get transcript summaries of Web videos.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

97 satır
2.5 KiB

  1. import { EventEmitter } from 'events';
  2. import { CreateBaseSummarizerParams, SummarizerEventEmitter } from '../../common';
  3. import {
  4. retrieveVideoId,
  5. getVideoPage,
  6. extractDataFromPage,
  7. fetchTranscriptItems, TranscriptResponse,
  8. } from './transcript';
  9. import { normalizeTranscriptText, summarizeTranscript } from '../../summarizer';
  10. export interface CreateYouTubeSummarizerParams extends CreateBaseSummarizerParams {}
  11. export class YouTubeSummarizerEventEmitter extends EventEmitter implements SummarizerEventEmitter {
  12. constructor(private readonly params: CreateYouTubeSummarizerParams) {
  13. super();
  14. }
  15. process() {
  16. const {
  17. url,
  18. openaiApiKey,
  19. openaiOrganizationId,
  20. ...config
  21. } = this.params;
  22. const identifier = retrieveVideoId(url);
  23. let transcripts: TranscriptResponse[] = [];
  24. this.emit('process', {
  25. type: 'extract-data',
  26. phase: 'download-page',
  27. });
  28. getVideoPage(identifier)
  29. .then((videoPageBody) => {
  30. const pageData = extractDataFromPage(videoPageBody);
  31. this.emit('process', {
  32. type: 'extract-data',
  33. phase: 'success',
  34. });
  35. this.emit('process', {
  36. type: 'fetch-transcript',
  37. phase: 'start',
  38. });
  39. return fetchTranscriptItems(pageData, config);
  40. })
  41. .then((transcript) => {
  42. this.emit('process', {
  43. type: 'fetch-transcript',
  44. phase: 'success',
  45. });
  46. transcripts = transcript;
  47. this.emit('process', {
  48. type: 'normalize-caption',
  49. phase: 'start',
  50. });
  51. return normalizeTranscriptText(
  52. transcript.map((item) => item.text).join(' '),
  53. openaiApiKey,
  54. openaiOrganizationId,
  55. );
  56. })
  57. .then((normalizedCaption) => {
  58. this.emit('process', {
  59. type: 'normalize-caption',
  60. phase: 'success',
  61. });
  62. this.emit('process', {
  63. type: 'summarize-caption',
  64. phase: 'start',
  65. });
  66. return summarizeTranscript(normalizedCaption, openaiApiKey, openaiOrganizationId);
  67. })
  68. .then((summary) => {
  69. this.emit('process', {
  70. type: 'summarize-caption',
  71. phase: 'success',
  72. });
  73. this.emit('success', {
  74. contentType: 'application/json',
  75. content: JSON.stringify({ transcripts, summary }),
  76. });
  77. this.emit('end');
  78. })
  79. .catch((error) => {
  80. this.emit('error', error);
  81. this.emit('end');
  82. });
  83. }
  84. }